mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
FEATURE: Consolidate likes notifications. (#6879)
This commit is contained in:
@@ -76,16 +76,87 @@ class PostActionNotifier
|
||||
post = post_action.post
|
||||
return if post_action.user.blank?
|
||||
|
||||
alerter.create_notification(
|
||||
post.user,
|
||||
Notification.types[:liked],
|
||||
post,
|
||||
display_username: post_action.user.username,
|
||||
post_action_id: post_action.id,
|
||||
user_id: post_action.user_id
|
||||
user_notifications = post.user.notifications
|
||||
|
||||
consolidation_window =
|
||||
SiteSetting.likes_notification_consolidation_window_mins.minutes.ago
|
||||
|
||||
liked_by_user_notifications =
|
||||
user_notifications
|
||||
.get_liked_by(post_action.user)
|
||||
.where("created_at > ?", consolidation_window)
|
||||
|
||||
user_liked_consolidated_notification =
|
||||
user_notifications
|
||||
.where(
|
||||
"created_at > ? AND notification_type = ?",
|
||||
consolidation_window,
|
||||
Notification.types[:liked_consolidated]
|
||||
)
|
||||
.first
|
||||
|
||||
if user_liked_consolidated_notification
|
||||
update_consolidated_liked_notification_count!(
|
||||
user_liked_consolidated_notification
|
||||
)
|
||||
elsif (
|
||||
liked_by_user_notifications.count >=
|
||||
SiteSetting.likes_notification_consolidation_threshold
|
||||
)
|
||||
create_consolidated_liked_notification!(
|
||||
liked_by_user_notifications,
|
||||
post,
|
||||
post_action
|
||||
)
|
||||
else
|
||||
alerter.create_notification(
|
||||
post.user,
|
||||
Notification.types[:liked],
|
||||
post,
|
||||
display_username: post_action.user.username,
|
||||
post_action_id: post_action.id,
|
||||
user_id: post_action.user_id
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
def self.update_consolidated_liked_notification_count!(notification)
|
||||
Notification.transaction do
|
||||
data = JSON.parse(notification.data)
|
||||
data["count"] += 1
|
||||
|
||||
notification.update!(
|
||||
data: data.to_json,
|
||||
read: false
|
||||
)
|
||||
end
|
||||
end
|
||||
private_class_method :update_consolidated_liked_notification_count!
|
||||
|
||||
def self.create_consolidated_liked_notification!(notifications,
|
||||
post,
|
||||
post_action)
|
||||
|
||||
Notification.transaction do
|
||||
timestamp = notifications.last.created_at
|
||||
|
||||
Notification.create!(
|
||||
notification_type: Notification.types[:liked_consolidated],
|
||||
user_id: post.user_id,
|
||||
data: {
|
||||
username: post_action.user.username,
|
||||
display_username: post_action.user.username,
|
||||
count: notifications.count + 1
|
||||
}.to_json,
|
||||
updated_at: timestamp,
|
||||
created_at: timestamp
|
||||
)
|
||||
|
||||
notifications.delete_all
|
||||
end
|
||||
end
|
||||
private_class_method :create_consolidated_liked_notification!
|
||||
|
||||
def self.after_create_post_revision(post_revision)
|
||||
return if @disabled
|
||||
|
||||
|
||||
Reference in New Issue
Block a user