mirror of
https://github.com/discourse/discourse.git
synced 2025-02-27 21:39:56 +08:00

* REFACTOR: Improve support for consolidating notifications. Before this commit, we didn't have a single way of consolidating notifications. For notifications like group summaries, we manually removed old ones before creating a new one. On the other hand, we used an after_create callback for likes and group membership requests, which caused unnecessary work, as we need to delete the record we created to replace it with a consolidated one. We now have all the consolidation rules centralized in a single place: the consolidation planner class. Other parts of the app looking to create a consolidable notification can do so by calling Notification#consolidate_or_save!, instead of the default Notification#create! method. Finally, we added two more rules: one for re-using existing group summaries and another for deleting duplicated dashboard problems PMs notifications when the user is tracking the moderator's inbox. Setting the threshold to one forces the planner to apply this rule every time. I plan to add plugin support for adding custom rules in another PR to keep this one relatively small. * DEV: Introduces a plugin API for consolidating notifications. This commit removes the `Notification#filter_by_consolidation_data` scope since plugins could have to define their criteria. The Plan class now receives two blocks, one to query for an already consolidated notification, which we'll try to update, and another to query for existing ones to consolidate. It also receives a consolidation window, which accepts an ActiveSupport::Duration object, and filter notifications created since that value.
153 lines
5.1 KiB
Ruby
153 lines
5.1 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
# Represents a rule to consolidate a specific notification.
|
|
#
|
|
# If a consolidated notification already exists, we'll update it instead.
|
|
# If it doesn't and creating a new one would match the threshold, we delete existing ones and create a consolidated one.
|
|
# Otherwise, save the original one.
|
|
#
|
|
# Constructor arguments:
|
|
#
|
|
# - from: The notification type of the unconsolidated notification. e.g. `Notification.types[:private_message]`
|
|
# - to: The type the consolidated notification will have. You can use the same value as from to flatten notifications or bump existing ones.
|
|
# - threshold: If creating a new notification would match this number, we'll destroy existing ones and create a consolidated one. It also accepts a lambda that returns a number.
|
|
# - 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.
|
|
# - consolidated_query_blk: A block with additional queries to apply when fetching for a consolidated notification.
|
|
#
|
|
# 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 ConsolidateNotifications
|
|
def initialize(from:, to:, consolidation_window: nil, unconsolidated_query_blk: nil, consolidated_query_blk: nil, threshold:)
|
|
@from = from
|
|
@to = to
|
|
@threshold = threshold
|
|
@consolidation_window = consolidation_window
|
|
@consolidated_query_blk = consolidated_query_blk
|
|
@unconsolidated_query_blk = unconsolidated_query_blk
|
|
@precondition_blk = nil
|
|
@set_data_blk = nil
|
|
end
|
|
|
|
def set_precondition(precondition_blk: nil)
|
|
@precondition_blk = precondition_blk
|
|
|
|
self
|
|
end
|
|
|
|
def set_mutations(set_data_blk: nil)
|
|
@set_data_blk = set_data_blk
|
|
|
|
self
|
|
end
|
|
|
|
def can_consolidate_data?(notification)
|
|
return false if get_threshold.zero? || to.blank?
|
|
return false if notification.notification_type != from
|
|
|
|
@data = consolidated_data(notification)
|
|
|
|
return true if @precondition_blk.nil?
|
|
@precondition_blk.call(data)
|
|
end
|
|
|
|
def consolidate_or_save!(notification)
|
|
@data ||= consolidated_data(notification)
|
|
return unless can_consolidate_data?(notification)
|
|
|
|
update_consolidated_notification!(notification) ||
|
|
create_consolidated_notification!(notification) ||
|
|
notification.tap(&:save!)
|
|
end
|
|
|
|
private
|
|
|
|
attr_reader :notification, :from, :to, :data, :threshold, :consolidated_query_blk, :unconsolidated_query_blk, :consolidation_window
|
|
|
|
def consolidated_data(notification)
|
|
return notification.data_hash if @set_data_blk.nil?
|
|
@set_data_blk.call(notification)
|
|
end
|
|
|
|
def update_consolidated_notification!(notification)
|
|
notifications = user_notifications(notification, to)
|
|
|
|
if consolidated_query_blk.present?
|
|
notifications = consolidated_query_blk.call(notifications, data)
|
|
end
|
|
consolidated = notifications.first
|
|
return if consolidated.blank?
|
|
|
|
data_hash = consolidated.data_hash.merge(data)
|
|
data_hash[:count] += 1 if data_hash[:count].present?
|
|
|
|
# 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.update!(
|
|
data: data_hash.to_json,
|
|
read: false,
|
|
updated_at: timestamp
|
|
)
|
|
|
|
consolidated
|
|
end
|
|
|
|
def create_consolidated_notification!(notification)
|
|
notifications = user_notifications(notification, from)
|
|
if unconsolidated_query_blk.present?
|
|
notifications = unconsolidated_query_blk.call(notifications, data)
|
|
end
|
|
|
|
# Saving the new notification would pass the threshold? Consolidate instead.
|
|
count_after_saving_notification = notifications.count + 1
|
|
return if count_after_saving_notification <= get_threshold
|
|
|
|
timestamp = notifications.last.created_at
|
|
data[:count] = count_after_saving_notification
|
|
|
|
consolidated = nil
|
|
|
|
Notification.transaction do
|
|
notifications.destroy_all
|
|
|
|
consolidated = Notification.create!(
|
|
notification_type: to,
|
|
user_id: notification.user_id,
|
|
data: data.to_json,
|
|
updated_at: timestamp,
|
|
created_at: timestamp
|
|
)
|
|
end
|
|
|
|
consolidated
|
|
end
|
|
|
|
def get_threshold
|
|
threshold.is_a?(Proc) ? threshold.call : threshold
|
|
end
|
|
|
|
def user_notifications(notification, type)
|
|
notifications = notification.user.notifications
|
|
.where(notification_type: type)
|
|
|
|
if consolidation_window.present?
|
|
notifications = notifications.where('created_at > ?', consolidation_window.ago)
|
|
end
|
|
|
|
notifications
|
|
end
|
|
|
|
def timestamp
|
|
@timestamp ||= Time.zone.now
|
|
end
|
|
end
|
|
end
|