Cap the max number of unread notifications to 500
Simpler than adding 2 levels of pagination here
This commit is contained in:
@@ -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|
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -13,30 +13,8 @@
|
||||
|
||||
<div data-controller="badge navigable-list" data-badge-unread-class="unread" data-action="keydown@document->navigable-list#navigate" data-navigable-list-actionable-items-value="true"
|
||||
data-navigable-list-focus-on-selection-value="false">
|
||||
<section class="notifications-list panel panel--wide center borderless unpad flex flex-column gap-half">
|
||||
<% if @unread.any? %>
|
||||
<div class="flex align-center justify-space-between margin-block-start margin-block-end-half">
|
||||
<h2 class="txt-medium txt-uppercase txt-alert">New for you</h2>
|
||||
<%= button_to "Mark all as read", bulk_reading_path, class: "btn txt-small", form: { data: { turbo: false } }, data: { action: "badge#clear" } %>
|
||||
</div>
|
||||
<% else %>
|
||||
<div class="notifications-list__empty-message pad border-radius border translucent" style="--border-style: dashed; --border-color: var(--color-ink); --border-radius: 0.2em;">
|
||||
<strong>Nothing new for you.</strong>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<div id="notifications_list" contents>
|
||||
<%= render partial: "notifications/index/notification", collection: @unread, cached: true %>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section class="notifications-list notifications-list--read panel panel--wide center borderless unpad flex flex-column gap-half margin-block-start">
|
||||
<h2 class="txt-medium margin-block-start-double margin-block-end-half txt-uppercase translucent">Previously seen</h2>
|
||||
|
||||
<div id="notifications_list_read" contents>
|
||||
<%= render partial: "notifications/index/notification", collection: @page.records, cached: true %>
|
||||
</div>
|
||||
</section>
|
||||
<%= render "notifications/index/unread_notifications", unread: @unread if @unread %>
|
||||
<%= render "notifications/index/read_notifications", page: @page %>
|
||||
</div>
|
||||
|
||||
<%= notifications_next_page_link(@page) if @page.records.any? %>
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
<section class="notifications-list notifications-list--read panel panel--wide center borderless unpad flex flex-column gap-half margin-block-start">
|
||||
<h2 class="txt-medium margin-block-start-double margin-block-end-half txt-uppercase translucent">Previously seen</h2>
|
||||
|
||||
<div id="notifications_list_read" contents>
|
||||
<%= render partial: "notifications/index/notification", collection: page.records, cached: true %>
|
||||
</div>
|
||||
</section>
|
||||
@@ -0,0 +1,23 @@
|
||||
<section class="notifications-list panel panel--wide center borderless unpad flex flex-column gap-half">
|
||||
<% if unread.any? %>
|
||||
<div class="flex align-center justify-space-between margin-block-start margin-block-end-half">
|
||||
<h2 class="txt-medium txt-uppercase txt-alert">New for you</h2>
|
||||
<%= button_to "Mark all as read", bulk_reading_path, class: "btn txt-small", form: { data: { turbo: false } }, data: { action: "badge#clear" } %>
|
||||
</div>
|
||||
|
||||
<% total_unread_count = Current.user.notifications.unread.count %>
|
||||
<% if Current.user.notifications.unread.count > NotificationsController::MAX_UNREAD_NOTIFICATIONS %>
|
||||
<div class="events__column-footer fill-highlight txt-x-small border-radius margin-block-end">
|
||||
Showing the <%= NotificationsController::MAX_UNREAD_NOTIFICATIONS %> most recent (<%= total_unread_count - NotificationsController::MAX_UNREAD_NOTIFICATIONS %> are hidden)
|
||||
</div>
|
||||
<% end %>
|
||||
<% else %>
|
||||
<div class="notifications-list__empty-message pad border-radius border translucent" style="--border-style: dashed; --border-color: var(--color-ink); --border-radius: 0.2em;">
|
||||
<strong>Nothing new for you.</strong>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<div id="notifications_list" contents>
|
||||
<%= render partial: "notifications/index/notification", collection: unread, cached: true %>
|
||||
</div>
|
||||
</section>
|
||||
Reference in New Issue
Block a user