Skip to content

Commit f440b02

Browse files
committed
Tighten scope on card notification removals
* Iterate each association separately to favor db indexing * Pluck user IDs rather than select to avoid correlated subquery
1 parent 49139b7 commit f440b02

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

app/models/card/readable.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ def unread_by(user)
1414
end
1515

1616
def remove_inaccessible_notifications
17-
User.find_each do |user|
18-
all_notifications_for(user).destroy_all unless accessible_to?(user)
17+
accessible_user_ids = board.accesses.pluck(:user_id)
18+
notification_sources.each do |sources|
19+
inaccessible_notifications_from(sources, accessible_user_ids).in_batches.destroy_all
1920
end
2021
end
2122

@@ -44,6 +45,14 @@ def comment_creation_events
4445
Event.where(eventable: comments)
4546
end
4647

48+
def inaccessible_notifications_from(sources, accessible_user_ids)
49+
Notification.where(source: sources).where.not(user_id: accessible_user_ids)
50+
end
51+
52+
def notification_sources
53+
[ events, comment_creation_events, mentions, comment_mentions ]
54+
end
55+
4756
def mention_notification_sources
4857
mentions.or(comment_mentions)
4958
end

0 commit comments

Comments
 (0)