DEV: Prioritize unread notifications in the experimental user menu (#18216)

Right now the experimental user menu sorts notifications the same way that the old menu does: unread high-priority notifications are shown first in reverse-chronological order followed by everything else also in reverse-chronological order. However, since the experimental user menu has dedicated tabs for some notification types and each tab displays a badge with the count of unread notifications in the tab, we feel like it makes sense to change how notifications are sorted in the experimental user menu to this:

1. unread high-priority notifications
2. unread regular notifications
3. all read notifications (both high-priority and regular)
4. within each group, notifications are sorted in reverse-chronological order (i.e. newest is shown first).

This new sorting logic applies to all tabs in the experimental user menu, however it doesn't change anything in the old menu. With this change, if a tab in the experimental user menu shows an unread notification badge for a really old notification, it will be surfaced to the top and prevents confusing scenarios where a user sees an unread notification badge on a tab, but the tab doesn't show the unread notification because it's too old to make it to the list.

Internal topic: t72199.
This commit is contained in:
Osama Sayegh
2022-09-12 21:19:25 +03:00
committed by GitHub
parent 321aa4b4b4
commit 1fa21ed415
6 changed files with 256 additions and 13 deletions

View File

@@ -18,6 +18,12 @@ class Notification < ActiveRecord::Base
scope :unread_type, ->(user, type, limit = 20) do
where(user_id: user.id, read: false, notification_type: type).visible.includes(:topic).limit(limit)
end
scope :prioritized, ->(limit = nil) do
order("notifications.high_priority AND NOT notifications.read DESC")
.order("NOT notifications.read DESC")
.order("notifications.created_at DESC")
.limit(limit || 30)
end
attr_accessor :skip_send_email
@@ -214,6 +220,30 @@ class Notification < ActiveRecord::Base
Post.find_by(topic_id: topic_id, post_number: post_number)
end
def self.prioritized_list(user, count: 30, types: [])
return [] if !user&.user_option
notifications = user.notifications
.includes(:topic)
.visible
.prioritized(count)
if types.present?
notifications = notifications.where(notification_type: types)
elsif user.user_option.like_notification_frequency == UserOption.like_notification_frequency_type[:never]
[
Notification.types[:liked],
Notification.types[:liked_consolidated]
].each do |notification_type|
notifications = notifications.where(
'notification_type <> ?', notification_type
)
end
end
notifications.to_a
end
# TODO(osama): deprecate this method when redesigned_user_menu_enabled is removed
def self.recent_report(user, count = nil, types = [])
return unless user && user.user_option