From 42a39b224c895bdf6b9734c3c9e913e45d5c3c09 Mon Sep 17 00:00:00 2001 From: Jorge Manrubia Date: Fri, 21 Nov 2025 15:49:14 +0100 Subject: [PATCH] Cap the max number of unread notifications to 500 Simpler than adding 2 levels of pagination here --- app/controllers/notifications_controller.rb | 4 ++- app/models/notification.rb | 1 + app/views/notifications/index.html.erb | 26 ++----------------- .../index/_read_notifications.html.erb | 7 +++++ .../index/_unread_notifications.html.erb | 23 ++++++++++++++++ 5 files changed, 36 insertions(+), 25 deletions(-) create mode 100644 app/views/notifications/index/_read_notifications.html.erb create mode 100644 app/views/notifications/index/_unread_notifications.html.erb diff --git a/app/controllers/notifications_controller.rb b/app/controllers/notifications_controller.rb index 8adb04b3c..76bd62974 100644 --- a/app/controllers/notifications_controller.rb +++ b/app/controllers/notifications_controller.rb @@ -1,6 +1,8 @@ class NotificationsController < ApplicationController + MAX_UNREAD_NOTIFICATIONS = 500 + def index - @unread = Current.user.notifications.unread.ordered.preloaded unless current_page_param + @unread = Current.user.notifications.unread.ordered.preloaded.limit(MAX_UNREAD_NOTIFICATIONS) unless current_page_param set_page_and_extract_portion_from Current.user.notifications.read.ordered.preloaded respond_to do |format| diff --git a/app/models/notification.rb b/app/models/notification.rb index 4b69dd6e6..5cd3802a4 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -9,6 +9,7 @@ class Notification < ApplicationRecord scope :unread, -> { where(read_at: nil) } scope :read, -> { where.not(read_at: nil) } scope :ordered, -> { order(read_at: :desc, created_at: :desc) } + scope :with_unread_first, -> { order(Arel.sql("CASE WHEN read_at IS NULL THEN 0 ELSE 1 END")) } after_create_commit :broadcast_unread after_destroy_commit :broadcast_read diff --git a/app/views/notifications/index.html.erb b/app/views/notifications/index.html.erb index eae50830b..70fee45b8 100644 --- a/app/views/notifications/index.html.erb +++ b/app/views/notifications/index.html.erb @@ -13,30 +13,8 @@
-
- <% if @unread.any? %> -
-

New for you

- <%= button_to "Mark all as read", bulk_reading_path, class: "btn txt-small", form: { data: { turbo: false } }, data: { action: "badge#clear" } %> -
- <% else %> -
- Nothing new for you. -
- <% end %> - -
- <%= render partial: "notifications/index/notification", collection: @unread, cached: true %> -
-
- -
-

Previously seen

- -
- <%= render partial: "notifications/index/notification", collection: @page.records, cached: true %> -
-
+ <%= render "notifications/index/unread_notifications", unread: @unread if @unread %> + <%= render "notifications/index/read_notifications", page: @page %>
<%= notifications_next_page_link(@page) if @page.records.any? %> diff --git a/app/views/notifications/index/_read_notifications.html.erb b/app/views/notifications/index/_read_notifications.html.erb new file mode 100644 index 000000000..c5e282237 --- /dev/null +++ b/app/views/notifications/index/_read_notifications.html.erb @@ -0,0 +1,7 @@ +
+

Previously seen

+ +
+ <%= render partial: "notifications/index/notification", collection: page.records, cached: true %> +
+
diff --git a/app/views/notifications/index/_unread_notifications.html.erb b/app/views/notifications/index/_unread_notifications.html.erb new file mode 100644 index 000000000..4fbfdba7d --- /dev/null +++ b/app/views/notifications/index/_unread_notifications.html.erb @@ -0,0 +1,23 @@ +
+ <% if unread.any? %> +
+

New for you

+ <%= button_to "Mark all as read", bulk_reading_path, class: "btn txt-small", form: { data: { turbo: false } }, data: { action: "badge#clear" } %> +
+ + <% total_unread_count = Current.user.notifications.unread.count %> + <% if Current.user.notifications.unread.count > NotificationsController::MAX_UNREAD_NOTIFICATIONS %> + + <% end %> + <% else %> +
+ Nothing new for you. +
+ <% end %> + +
+ <%= render partial: "notifications/index/notification", collection: unread, cached: true %> +
+