PERF: Destroy collapsed notifications in 1 query instead of 3.

This commit is contained in:
Guo Xiang Tan 2018-05-25 11:59:29 +08:00
parent 80adc1ee80
commit 755b511b5c
2 changed files with 15 additions and 7 deletions

View File

@ -174,12 +174,15 @@ class PostAlerter
unread_posts(user, topic).count
end
def destroy_notifications(user, type, topic)
def destroy_notifications(user, types, topic)
return if user.blank?
return unless Guardian.new(user).can_see?(topic)
user.notifications.where(notification_type: type,
topic_id: topic.id).destroy_all
user.notifications.where(
notification_type: types,
topic_id: topic.id
).destroy_all
# HACK so notification counts sync up correctly
user.reload
end
@ -326,9 +329,7 @@ class PostAlerter
collapsed = false
if COLLAPSED_NOTIFICATION_TYPES.include?(type)
COLLAPSED_NOTIFICATION_TYPES.each do |t|
destroy_notifications(user, t, post.topic)
end
destroy_notifications(user, COLLAPSED_NOTIFICATION_TYPES, post.topic)
collapsed = true
end

View File

@ -267,8 +267,15 @@ describe PostAlerter do
end
it 'notifies a user by username' do
topic = Fabricate(:topic)
expect {
create_post_with_alerts(raw: '[quote="EvilTrout, post:1"]whatup[/quote]')
2.times do
create_post_with_alerts(
raw: '[quote="EvilTrout, post:1"]whatup[/quote]',
topic: topic
)
end
}.to change(evil_trout.notifications, :count).by(1)
end