Skip to content

A platform for community discussion. Free, open, simple.

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

Guo Xiang Tan755b511b5c54
diff --git a/app/services/post_alerter.rb b/app/services/post_alerter.rb
index 3f151f875cb..748d4d0b2d7 100644
--- a/app/services/post_alerter.rb
+++ b/app/services/post_alerter.rb
@@ -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
diff --git a/spec/services/post_alerter_spec.rb b/spec/services/post_alerter_spec.rb
index 20b6e8a7785..c7abefee984 100644
--- a/spec/services/post_alerter_spec.rb
+++ b/spec/services/post_alerter_spec.rb
@@ -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