DEV: Combine all header notification bubbles into one in the new user menu (#17718)

Extracted from https://github.com/discourse/discourse/pull/17379.
This commit is contained in:
Osama Sayegh
2022-08-03 08:57:59 +03:00
committed by GitHub
parent bd92df6bbe
commit ce9eec8606
19 changed files with 695 additions and 155 deletions

View File

@@ -541,6 +541,17 @@ class Reviewable < ActiveRecord::Base
result
end
def self.unseen_list_for(user, preload: true, limit: nil)
results = list_for(user, preload: preload, limit: limit)
if user.last_seen_reviewable_id
results = results.where(
"reviewables.id > ?",
user.last_seen_reviewable_id
)
end
results
end
def self.recent_list_with_pending_first(user, limit: 30)
min_score = Reviewable.min_score_for_priority

View File

@@ -487,6 +487,7 @@ class User < ActiveRecord::Base
def reload
@unread_notifications = nil
@all_unread_notifications_count = nil
@unread_total_notifications = nil
@unread_pms = nil
@unread_bookmarks = nil
@@ -587,6 +588,29 @@ class User < ActiveRecord::Base
end
end
def all_unread_notifications_count
@all_unread_notifications_count ||= begin
sql = <<~SQL
SELECT COUNT(*) FROM (
SELECT 1 FROM
notifications n
LEFT JOIN topics t ON t.id = n.topic_id
WHERE t.deleted_at IS NULL AND
n.user_id = :user_id AND
n.id > :seen_notification_id AND
NOT read
LIMIT :limit
) AS X
SQL
DB.query_single(sql,
user_id: id,
seen_notification_id: seen_notification_id,
limit: User.max_unread_notifications
)[0].to_i
end
end
def total_unread_notifications
@unread_total_notifications ||= notifications.where("read = false").count
end
@@ -595,6 +619,10 @@ class User < ActiveRecord::Base
Reviewable.list_for(self).count
end
def unseen_reviewable_count
Reviewable.unseen_list_for(self).count
end
def saw_notification_id(notification_id)
if seen_notification_id.to_i < notification_id.to_i
update_columns(seen_notification_id: notification_id.to_i)
@@ -604,6 +632,29 @@ class User < ActiveRecord::Base
end
end
def bump_last_seen_reviewable!
query = Reviewable.unseen_list_for(self, preload: false)
if last_seen_reviewable_id
query = query.where("id > ?", last_seen_reviewable_id)
end
max_reviewable_id = query.maximum(:id)
if max_reviewable_id
update!(last_seen_reviewable_id: max_reviewable_id)
publish_reviewable_counts(unseen_reviewable_count: self.unseen_reviewable_count)
MessageBus.publish(
"/reviewable_counts",
{ unseen_reviewable_count: self.unseen_reviewable_count },
user_ids: [self.id]
)
end
end
def publish_reviewable_counts(data)
MessageBus.publish("/reviewable_counts/#{self.id}", data, user_ids: [self.id])
end
TRACK_FIRST_NOTIFICATION_READ_DURATION = 1.week.to_i
def read_first_notification?
@@ -663,6 +714,10 @@ class User < ActiveRecord::Base
seen_notification_id: seen_notification_id,
}
if self.redesigned_user_menu_enabled?
payload[:all_unread_notifications_count] = all_unread_notifications_count
end
MessageBus.publish("/notification/#{id}", payload, user_ids: [id])
end