FIX: Allow liked notifications consolidation to be disabled.

This commit is contained in:
Guo Xiang Tan 2019-01-16 16:17:04 +08:00
parent f8db93df5f
commit e7b49c42c4
4 changed files with 52 additions and 25 deletions

View File

@ -76,6 +76,10 @@ class PostActionNotifier
post = post_action.post
return if post_action.user.blank?
if SiteSetting.likes_notification_consolidation_threshold.zero?
return create_liked_notification(alerter, post, post_action)
end
user_notifications = post.user.notifications
consolidation_window =
@ -114,6 +118,11 @@ class PostActionNotifier
post_action
)
else
create_liked_notification(alerter, post, post_action)
end
end
def self.create_liked_notification(alerter, post, post_action)
alerter.create_notification(
post.user,
Notification.types[:liked],
@ -123,10 +132,9 @@ class PostActionNotifier
user_id: post_action.user_id
)
end
end
private_class_method :create_liked_notification
def self.update_consolidated_liked_notification_count!(notification)
Notification.transaction do
data = JSON.parse(notification.data)
data["count"] += 1
@ -135,7 +143,6 @@ class PostActionNotifier
read: false
)
end
end
private_class_method :update_consolidated_liked_notification_count!
def self.create_consolidated_liked_notification!(notifications,

View File

@ -1789,7 +1789,7 @@ en:
disable_edit_notifications: "Disables edit notifications by the system user when 'download_remote_images_to_local' is active."
likes_notification_consolidation_threshold: "Number of liked notifications received before the notifications are consolidated into a single one. The window can be configured via `SiteSetting.likes_notification_consolidation_window_mins`."
likes_notification_consolidation_threshold: "Number of liked notifications received before the notifications are consolidated into a single one. Set to 0 to disable. The window can be configured via `SiteSetting.likes_notification_consolidation_window_mins`."
likes_notification_consolidation_window_mins: "Duration in minutes where liked notifications are consolidated into a single notification once the threshold has been reaced. The threshold can be configured via `SiteSetting.likes_notification_consolidation_threshold`."

View File

@ -1702,7 +1702,7 @@ uncategorized:
likes_notification_consolidation_threshold:
default: 5
min: 3
min: 0
likes_notification_consolidation_window_mins:
default: 120

View File

@ -300,15 +300,35 @@ describe PostAction do
let(:liker) { Fabricate(:user) }
let(:likee) { Fabricate(:user) }
before do
SiteSetting.likes_notification_consolidation_threshold = 3
it "can be disabled" do
SiteSetting.likes_notification_consolidation_threshold = 0
expect do
PostAction.act(
liker,
Fabricate(:post, user: likee),
PostActionType.types[:like]
)
end.to change { likee.reload.notifications.count }.by(1)
SiteSetting.likes_notification_consolidation_threshold = 1
expect do
PostAction.act(
liker,
Fabricate(:post, user: likee),
PostActionType.types[:like]
)
end.to_not change { likee.reload.notifications.count }
end
it 'should consolidate likes notification when the threshold is reached' do
SiteSetting.likes_notification_consolidation_threshold = 2
freeze_time
expect do
4.times do
3.times do
PostAction.act(
liker,
Fabricate(:post, user: likee),
@ -327,7 +347,7 @@ describe PostAction do
expect(data["username"]).to eq(liker.username)
expect(data["display_username"]).to eq(liker.username)
expect(data["count"]).to eq(4)
expect(data["count"]).to eq(3)
notification.update!(read: true)
@ -344,7 +364,7 @@ describe PostAction do
data = JSON.parse(notification.reload.data)
expect(notification.read).to eq(false)
expect(data["count"]).to eq(6)
expect(data["count"]).to eq(5)
# Like from a different user shouldn't be consolidated
expect do