mirror of
https://github.com/discourse/discourse.git
synced 2024-11-30 04:34:13 -06:00
b7b61d4b56
We previously used ConsolidateNotifications with a threshold of 1 to re-use an existing notification and bump it to the top instead of creating a new one. It produces some jumpiness in the user notification list, and it relies on updating the `created_at` attribute, which is a bit hacky. As a better alternative, we're introducing a new plan that deletes all the previous versions of the notification, then creates a new one.
55 lines
1.6 KiB
Ruby
55 lines
1.6 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
# Create a new notification while deleting previous versions of it.
|
|
#
|
|
# Constructor arguments:
|
|
#
|
|
# - type: The notification type. e.g. `Notification.types[:private_message]`
|
|
# - previous_query_blk: A block with the query we'll use to find previous notifications.
|
|
#
|
|
# Need to call #set_precondition to configure this:
|
|
#
|
|
# - precondition_blk: A block that receives the mutated data and returns true if we have everything we need to consolidate.
|
|
#
|
|
# Need to call #set_mutations to configure this:
|
|
#
|
|
# - set_data_blk: A block that receives the notification data hash and mutates it, adding additional data needed for consolidation.
|
|
|
|
module Notifications
|
|
class DeletePreviousNotifications < ConsolidationPlan
|
|
def initialize(type:, previous_query_blk:)
|
|
@type = type
|
|
@previous_query_blk = previous_query_blk
|
|
end
|
|
|
|
def can_consolidate_data?(notification)
|
|
return false if notification.notification_type != type
|
|
|
|
@data = consolidated_data(notification)
|
|
|
|
precondition_blk.nil? || precondition_blk.call(notification.data_hash)
|
|
end
|
|
|
|
def consolidate_or_save!(notification)
|
|
@data ||= consolidated_data(notification)
|
|
return unless can_consolidate_data?(notification)
|
|
|
|
notifications = user_notifications(notification, type)
|
|
if previous_query_blk.present?
|
|
notifications = previous_query_blk.call(notifications, data)
|
|
end
|
|
|
|
Notification.transaction do
|
|
notifications.destroy_all
|
|
notification.save!
|
|
end
|
|
|
|
notification
|
|
end
|
|
|
|
private
|
|
|
|
attr_reader :type, :data, :precondition_blk, :previous_query_blk
|
|
end
|
|
end
|