From f440b0243ea78d3435a7ee565c2d4aca46ffa369 Mon Sep 17 00:00:00 2001 From: Jeremy Daer Date: Wed, 3 Dec 2025 15:32:29 -0800 Subject: [PATCH] Tighten scope on card notification removals * Iterate each association separately to favor db indexing * Pluck user IDs rather than select to avoid correlated subquery --- app/models/card/readable.rb | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/app/models/card/readable.rb b/app/models/card/readable.rb index 903db2d71..9c272dd75 100644 --- a/app/models/card/readable.rb +++ b/app/models/card/readable.rb @@ -14,8 +14,9 @@ module Card::Readable end def remove_inaccessible_notifications - User.find_each do |user| - all_notifications_for(user).destroy_all unless accessible_to?(user) + accessible_user_ids = board.accesses.pluck(:user_id) + notification_sources.each do |sources| + inaccessible_notifications_from(sources, accessible_user_ids).in_batches.destroy_all end end @@ -44,6 +45,14 @@ module Card::Readable Event.where(eventable: comments) end + def inaccessible_notifications_from(sources, accessible_user_ids) + Notification.where(source: sources).where.not(user_id: accessible_user_ids) + end + + def notification_sources + [ events, comment_creation_events, mentions, comment_mentions ] + end + def mention_notification_sources mentions.or(comment_mentions) end