mirror of
https://github.com/discourse/discourse.git
synced 2024-11-26 07:23:40 +08:00
FIX: Updating a consolidated notification should bump it to the top. (#15199)
In the future, it would be better to have a consolidated_at timestamp instead of updating created_at.
This commit is contained in:
parent
3ebce550fe
commit
43903f8dfe
|
@ -14,6 +14,7 @@
|
||||||
# - consolidation_window: Only consolidate notifications created since this value (Pass a ActiveSupport::Duration instance, and we'll call #ago on it).
|
# - consolidation_window: Only consolidate notifications created since this value (Pass a ActiveSupport::Duration instance, and we'll call #ago on it).
|
||||||
# - unconsolidated_query_blk: A block with additional queries to apply when fetching for unconsolidated notifications.
|
# - unconsolidated_query_blk: A block with additional queries to apply when fetching for unconsolidated notifications.
|
||||||
# - consolidated_query_blk: A block with additional queries to apply when fetching for a consolidated notification.
|
# - consolidated_query_blk: A block with additional queries to apply when fetching for a consolidated notification.
|
||||||
|
# - bump_notification: Bump the consolidated notification to the top after updating it.
|
||||||
#
|
#
|
||||||
# Need to call #set_precondition to configure this:
|
# Need to call #set_precondition to configure this:
|
||||||
#
|
#
|
||||||
|
@ -25,7 +26,7 @@
|
||||||
|
|
||||||
module Notifications
|
module Notifications
|
||||||
class ConsolidateNotifications
|
class ConsolidateNotifications
|
||||||
def initialize(from:, to:, consolidation_window: nil, unconsolidated_query_blk: nil, consolidated_query_blk: nil, threshold:)
|
def initialize(from:, to:, consolidation_window: nil, unconsolidated_query_blk: nil, consolidated_query_blk: nil, threshold:, bump_notification: true)
|
||||||
@from = from
|
@from = from
|
||||||
@to = to
|
@to = to
|
||||||
@threshold = threshold
|
@threshold = threshold
|
||||||
|
@ -34,6 +35,7 @@ module Notifications
|
||||||
@unconsolidated_query_blk = unconsolidated_query_blk
|
@unconsolidated_query_blk = unconsolidated_query_blk
|
||||||
@precondition_blk = nil
|
@precondition_blk = nil
|
||||||
@set_data_blk = nil
|
@set_data_blk = nil
|
||||||
|
@bump_notification = bump_notification
|
||||||
end
|
end
|
||||||
|
|
||||||
def set_precondition(precondition_blk: nil)
|
def set_precondition(precondition_blk: nil)
|
||||||
|
@ -69,7 +71,10 @@ module Notifications
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
attr_reader :notification, :from, :to, :data, :threshold, :consolidated_query_blk, :unconsolidated_query_blk, :consolidation_window
|
attr_reader(
|
||||||
|
:notification, :from, :to, :data, :threshold, :consolidated_query_blk,
|
||||||
|
:unconsolidated_query_blk, :consolidation_window, :bump_notification
|
||||||
|
)
|
||||||
|
|
||||||
def consolidated_data(notification)
|
def consolidated_data(notification)
|
||||||
return notification.data_hash if @set_data_blk.nil?
|
return notification.data_hash if @set_data_blk.nil?
|
||||||
|
@ -91,11 +96,17 @@ module Notifications
|
||||||
# Hack: We don't want to cache the old data if we're about to update it.
|
# Hack: We don't want to cache the old data if we're about to update it.
|
||||||
consolidated.instance_variable_set(:@data_hash, nil)
|
consolidated.instance_variable_set(:@data_hash, nil)
|
||||||
|
|
||||||
consolidated.update!(
|
attrs = {
|
||||||
data: data_hash.to_json,
|
data: data_hash.to_json,
|
||||||
read: false,
|
read: false,
|
||||||
updated_at: timestamp
|
updated_at: timestamp,
|
||||||
)
|
}
|
||||||
|
|
||||||
|
# Updating created_at may seem wrong, but it's the only way of bumping the notification.
|
||||||
|
# We cannot order by updated_at because marking them as read will move them to the top.
|
||||||
|
attrs[:created_at] = timestamp if bump_notification
|
||||||
|
|
||||||
|
consolidated.update!(attrs)
|
||||||
|
|
||||||
consolidated
|
consolidated
|
||||||
end
|
end
|
||||||
|
|
|
@ -103,7 +103,8 @@ describe PostAlerter do
|
||||||
expect(notification_payload["group_name"]).to eq(group.name)
|
expect(notification_payload["group_name"]).to eq(group.name)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'consolidates group summary notifications by bumping an existing one' do
|
it 'updates the consolidated group summary inbox count and bumps the notification' do
|
||||||
|
user2.update!(last_seen_at: 5.minutes.ago)
|
||||||
TopicUser.change(user2.id, pm.id, notification_level: TopicUser.notification_levels[:tracking])
|
TopicUser.change(user2.id, pm.id, notification_level: TopicUser.notification_levels[:tracking])
|
||||||
PostAlerter.post_created(op)
|
PostAlerter.post_created(op)
|
||||||
|
|
||||||
|
@ -113,21 +114,25 @@ describe PostAlerter do
|
||||||
).last
|
).last
|
||||||
starting_count = group_summary_notification.data_hash[:inbox_count]
|
starting_count = group_summary_notification.data_hash[:inbox_count]
|
||||||
|
|
||||||
expect(starting_count).to eq(1)
|
# Create another notification to ensure summary is correctly bumped
|
||||||
|
user2_post = Fabricate(:post, topic: pm, user: user2)
|
||||||
|
PostAlerter.new.create_notification(
|
||||||
|
user2, Notification.types[:liked], user2_post, user_id: pm.user, display_username: pm.user.username
|
||||||
|
)
|
||||||
|
|
||||||
|
Notification.where(user: user2).update_all('read = true')
|
||||||
another_pm = Fabricate(:topic, archetype: 'private_message', category_id: nil, allowed_groups: [group])
|
another_pm = Fabricate(:topic, archetype: 'private_message', category_id: nil, allowed_groups: [group])
|
||||||
another_post = Fabricate(:post, user: another_pm.user, topic: another_pm)
|
another_post = Fabricate(:post, user: another_pm.user, topic: another_pm)
|
||||||
TopicUser.change(user2.id, another_pm.id, notification_level: TopicUser.notification_levels[:tracking])
|
TopicUser.change(user2.id, another_pm.id, notification_level: TopicUser.notification_levels[:tracking])
|
||||||
|
|
||||||
PostAlerter.post_created(another_post)
|
message_data = MessageBus.track_publish("/notification/#{user2.id}") do
|
||||||
consolidated_summary = Notification.where(
|
PostAlerter.post_created(another_post)
|
||||||
user_id: user2.id,
|
end.first.data
|
||||||
notification_type: Notification.types[:group_message_summary]
|
|
||||||
).last
|
|
||||||
updated_inbox_count = consolidated_summary.data_hash[:inbox_count]
|
|
||||||
|
|
||||||
expect(group_summary_notification.id).to eq(consolidated_summary.id)
|
expect(Notification.recent_report(user2, 1).first.notification_type).to eq(Notification.types[:group_message_summary])
|
||||||
expect(updated_inbox_count).to eq(starting_count + 1)
|
expect(message_data.dig(:last_notification, :notification, :id)).to eq(group_summary_notification.id)
|
||||||
|
expect(message_data.dig(:last_notification, :notification, :data, :inbox_count)).to eq(starting_count + 1)
|
||||||
|
expect(message_data[:unread_notifications]).to eq(1)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue
Block a user