A platform for community discussion. Free, open, simple.
FIX: Clean up stranded PM notifications when access is revoked (#42418)
Bannon Tannerf1c986eaf274
diff --git a/app/jobs/regular/delete_inaccessible_notifications.rb b/app/jobs/regular/delete_inaccessible_notifications.rbindex e0f0e55d4db..4ed09c09112 100644--- a/app/jobs/regular/delete_inaccessible_notifications.rb+++ b/app/jobs/regular/delete_inaccessible_notifications.rb@@ -5,13 +5,37 @@ module Jobsdef execute(args)raise Discourse::InvalidParameters.new(:topic_id) if args[:topic_id].blank?- Notification- .where(topic_id: args[:topic_id])- .find_each do |notification|- next unless notification.user && notification.topic+ topic = Topic.find_by(id: args[:topic_id])+ return if topic.blank?- notification.destroy if !Guardian.new(notification.user).can_see?(notification.topic)- end+ user_ids = args[:user_ids].presence+ user_ids ||= Notification.where(topic_id: topic.id).distinct.pluck(:user_id)+ return if user_ids.blank?++ inaccessible_ids = inaccessible_user_ids(topic, user_ids)+ return if inaccessible_ids.empty?++ Notification.where(user_id: inaccessible_ids, topic_id: topic.id).delete_all+ User.where(id: inaccessible_ids).find_each(&:publish_notifications_state)+ end++ private++ # For PMs (the common case) resolve access with two set-based queries instead+ # of one Guardian#can_see? call per user, so a job on a large group PM does+ # not fan out into N SQL round-trips.+ def inaccessible_user_ids(topic, user_ids)+ return per_user_guardian_check(topic, user_ids) if !topic.private_message? || topic.deleted_at++ accessible_ids = topic.all_allowed_users.where(id: user_ids).pluck(:id)+ if !SiteSetting.suppress_secured_categories_from_admin+ accessible_ids |= User.where(id: user_ids, admin: true).pluck(:id)+ end+ user_ids - accessible_ids+ end++ def per_user_guardian_check(topic, user_ids)+ User.where(id: user_ids).reject { |u| u.guardian.can_see?(topic) }.map(&:id)endendenddiff --git a/app/models/topic.rb b/app/models/topic.rbindex b242f093a38..7fb7d7f75ec 100644--- a/app/models/topic.rb+++ b/app/models/topic.rb@@ -1257,6 +1257,7 @@ class Topic < ActiveRecord::Basegroup_user.destroyallowed_groups.reloadadd_small_action(removed_by, "removed_group", group.name, skip_guardian: true)+ Jobs.enqueue(:delete_inaccessible_notifications, topic_id: id)return trueendend@@ -1279,6 +1280,10 @@ class Topic < ActiveRecord::Baseadd_small_action(removed_by, "removed_user", user.username, skip_guardian: true)end+ unless user.guardian.can_see?(self)+ user.publish_notifications_state if Notification.remove_for(user.id, id).positive?+ end+MessageBus.publish("/topic/#{id}", { type: "remove_allowed_user" }, user_ids: [user.id])return trueenddiff --git a/db/post_migrate/20260807182856_cleanup_stranded_pm_notifications.rb b/db/post_migrate/20260807182856_cleanup_stranded_pm_notifications.rbnew file mode 100644index 00000000000..1c0f096a318--- /dev/null+++ b/db/post_migrate/20260807182856_cleanup_stranded_pm_notifications.rb@@ -0,0 +1,37 @@+# frozen_string_literal: true++class CleanupStrandedPmNotifications < ActiveRecord::Migration[8.0]+ disable_ddl_transaction!++ BATCH_SIZE = 10_000++ def up+ loop do+ count = DB.exec(<<~SQL, batch_size: BATCH_SIZE)+ DELETE FROM notifications+ WHERE id IN (+ SELECT n.id+ FROM notifications n+ INNER JOIN topics t ON t.id = n.topic_id+ WHERE t.archetype = 'private_message'+ AND NOT EXISTS (+ SELECT 1 FROM topic_allowed_users tau+ WHERE tau.topic_id = n.topic_id AND tau.user_id = n.user_id+ )+ AND NOT EXISTS (+ SELECT 1 FROM topic_allowed_groups tag+ INNER JOIN group_users gu ON gu.group_id = tag.group_id+ WHERE tag.topic_id = n.topic_id AND gu.user_id = n.user_id+ )+ LIMIT :batch_size+ )+ SQL++ break if count == 0+ end+ end++ def down+ raise ActiveRecord::IrreversibleMigration+ end+enddiff --git a/db/structure.sql b/db/structure.sqlindex 69d6ac48aa6..6d1cecf1f6f 100644--- a/db/structure.sql+++ b/db/structure.sql@@ -23153,6 +23153,7 @@ SET search_path TO "$user", public;INSERT INTO "schema_migrations" (version) VALUES('20260810154331'),('20260810012238'),+('20260807182856'),('20260806074210'),('20260806074204'),('20260803015314'),diff --git a/lib/group_manager.rb b/lib/group_manager.rbindex e529587caff..7376eb60019 100644--- a/lib/group_manager.rb+++ b/lib/group_manager.rb@@ -33,6 +33,7 @@ class GroupManagerrecalculate_trust_level(removed_user_ids)bulk_publish_category_updates(removed_user_ids)+ enqueue_pm_notification_cleanup(removed_user_ids)User.where(id: removed_user_ids).find_each { |user| @group.trigger_user_removed_event(user) }enqueue_user_removed_webhook_events(webhook_payloads)@@ -149,6 +150,24 @@ class GroupManagerendend+ def enqueue_pm_notification_cleanup(removed_user_ids)+ Notification+ .where(+ user_id: removed_user_ids,+ topic_id: TopicAllowedGroup.where(group_id: @group.id).select(:topic_id),+ )+ .distinct+ .pluck(:topic_id, :user_id)+ .group_by(&:first)+ .each do |topic_id, pairs|+ Jobs.enqueue(+ :delete_inaccessible_notifications,+ topic_id: topic_id,+ user_ids: pairs.map(&:last),+ )+ end+ end+def build_user_removed_webhook_payloads(group_users_relation)return unless WebHook.active_web_hooks(:group_user)diff --git a/spec/db/post_migrate/20260807182856_cleanup_stranded_pm_notifications_spec.rb b/spec/db/post_migrate/20260807182856_cleanup_stranded_pm_notifications_spec.rbnew file mode 100644index 00000000000..d919e52ab1a--- /dev/null+++ b/spec/db/post_migrate/20260807182856_cleanup_stranded_pm_notifications_spec.rb@@ -0,0 +1,90 @@+# frozen_string_literal: true++require Rails.root.join("db/post_migrate/20260807182856_cleanup_stranded_pm_notifications.rb")++RSpec.describe CleanupStrandedPmNotifications do+ before do+ @original_verbose = ActiveRecord::Migration.verbose+ ActiveRecord::Migration.verbose = false+ end++ after { ActiveRecord::Migration.verbose = @original_verbose }++ fab!(:user1, :user)+ fab!(:user2, :user)+ fab!(:admin)+ fab!(:group)++ fab!(:pm_topic) do+ Fabricate(+ :private_message_topic,+ user: admin,+ topic_allowed_users: [+ Fabricate.build(:topic_allowed_user, user: admin),+ Fabricate.build(:topic_allowed_user, user: user1),+ ],+ )+ end++ fab!(:group_pm_topic) do+ topic =+ Fabricate(+ :private_message_topic,+ user: admin,+ topic_allowed_users: [Fabricate.build(:topic_allowed_user, user: admin)],+ )+ Fabricate(:topic_allowed_group, topic: topic, group: group)+ topic+ end++ it "deletes notifications for users removed from a PM" do+ orphaned = Fabricate(:notification, user: user2, topic: pm_topic)++ described_class.new.up++ expect(Notification.exists?(orphaned.id)).to eq(false)+ end++ it "preserves notifications for users still allowed on the PM" do+ kept = Fabricate(:notification, user: user1, topic: pm_topic)++ described_class.new.up++ expect(Notification.exists?(kept.id)).to eq(true)+ end++ it "preserves notifications for users with access via a group" do+ group.add(user2)+ kept = Fabricate(:notification, user: user2, topic: group_pm_topic)++ described_class.new.up++ expect(Notification.exists?(kept.id)).to eq(true)+ end++ it "deletes all notification types from an inaccessible PM" do+ types = %i[private_message invited_to_private_message mentioned quoted replied]+ orphaned =+ types.map do |type|+ Fabricate(+ :notification,+ user: user2,+ topic: pm_topic,+ notification_type: Notification.types[type],+ )+ end++ described_class.new.up++ orphaned.each { |n| expect(Notification.exists?(n.id)).to eq(false) }+ end++ it "leaves non-PM notifications alone" do+ regular_topic = Fabricate(:topic)+ kept = Fabricate(:notification, user: user2, topic: regular_topic)++ described_class.new.up++ expect(Notification.exists?(kept.id)).to eq(true)+ end+enddiff --git a/spec/jobs/delete_inaccessible_notifications_spec.rb b/spec/jobs/delete_inaccessible_notifications_spec.rbnew file mode 100644index 00000000000..233e4912d7f--- /dev/null+++ b/spec/jobs/delete_inaccessible_notifications_spec.rb@@ -0,0 +1,82 @@+# frozen_string_literal: true++RSpec.describe Jobs::DeleteInaccessibleNotifications do+ fab!(:admin)+ fab!(:user)+ fab!(:other_user, :user)+ fab!(:pm) { Fabricate(:private_message_topic, user: admin, recipient: user) }++ it "raises if topic_id is missing" do+ expect { described_class.new.execute({}) }.to raise_error(Discourse::InvalidParameters)+ end++ it "does nothing when the topic is gone" do+ expect { described_class.new.execute(topic_id: -1) }.not_to raise_error+ end++ it "deletes notifications for users who can no longer see the topic" do+ other_notification = Fabricate(:notification, user: other_user, topic: pm, read: false) # not an allowed user++ expect { described_class.new.execute(topic_id: pm.id) }.to change {+ Notification.exists?(other_notification.id)+ }.from(true).to(false)+ end++ it "preserves notifications for users who can still see the topic" do+ notification = Fabricate(:notification, user: user, topic: pm, read: false)++ expect { described_class.new.execute(topic_id: pm.id) }.not_to change {+ Notification.exists?(notification.id)+ }+ end++ it "publishes notification state once per affected user, not per notification" do+ Fabricate(:notification, user: other_user, topic: pm)+ Fabricate(:notification, user: other_user, topic: pm)+ Fabricate(:notification, user: other_user, topic: pm)++ User.any_instance.expects(:publish_notifications_state).once++ described_class.new.execute(topic_id: pm.id)+ end++ context "with user_ids arg" do+ it "only considers the given users" do+ Fabricate(:notification, user: other_user, topic: pm)+ third_user = Fabricate(:user)+ third_notification = Fabricate(:notification, user: third_user, topic: pm)++ described_class.new.execute(topic_id: pm.id, user_ids: [other_user.id])++ expect(Notification.exists?(third_notification.id)).to eq(true)+ end+ end++ it "resolves PM access without an SQL query per user" do+ group = Fabricate(:group)+ group_pm =+ Fabricate(+ :private_message_topic,+ user: admin,+ topic_allowed_groups: [Fabricate.build(:topic_allowed_group, group: group)],+ )+ users = 5.times.map { Fabricate(:user) }+ users.each { |u| Fabricate(:notification, user: u, topic: group_pm) }++ access_queries = 0+ subscriber =+ ActiveSupport::Notifications.subscribe("sql.active_record") do |*args|+ event = ActiveSupport::Notifications::Event.new(*args)+ # Guardian's per-PM access check hits topic_allowed_users/topic_allowed_groups.+ access_queries += 1 if event.payload[:sql] =~ /topic_allowed_(users|groups)/+ end++ begin+ described_class.new.execute(topic_id: group_pm.id)+ ensure+ ActiveSupport::Notifications.unsubscribe(subscriber)+ end++ expect(access_queries).to be <= 2+ end+enddiff --git a/spec/models/group_spec.rb b/spec/models/group_spec.rbindex a28394028de..61d08bbf12f 100644--- a/spec/models/group_spec.rb+++ b/spec/models/group_spec.rb@@ -1301,6 +1301,20 @@ RSpec.describe Group doexpect(events).to include(:user_removed_from_group)end+ it "enqueues an inaccessible-notifications cleanup for each PM the removed user has notifications on" do+ pm_topic1 = Fabricate(:private_message_topic)+ pm_topic2 = Fabricate(:private_message_topic)+ Fabricate(:topic_allowed_group, topic: pm_topic1, group: group)+ Fabricate(:topic_allowed_group, topic: pm_topic2, group: group)+ Fabricate(:notification, user: user, topic: pm_topic1)+ Fabricate(:notification, user: user, topic: pm_topic2)++ group.remove(user)++ expect_job_enqueued(job: :delete_inaccessible_notifications, args: { topic_id: pm_topic1.id })+ expect_job_enqueued(job: :delete_inaccessible_notifications, args: { topic_id: pm_topic2.id })+ end+describe "with webhook" dofab!(:group_user_web_hook)@@ -1654,6 +1668,20 @@ RSpec.describe Group doexpect(result).to contain_exactly(user.id, admin.id)end+ it "enqueues an inaccessible-notifications cleanup for each PM a removed user has notifications on" do+ pm_topic1 = Fabricate(:private_message_topic)+ pm_topic2 = Fabricate(:private_message_topic)+ Fabricate(:topic_allowed_group, topic: pm_topic1, group: group)+ Fabricate(:topic_allowed_group, topic: pm_topic2, group: group)+ Fabricate(:notification, user: user, topic: pm_topic1)+ Fabricate(:notification, user: admin, topic: pm_topic2)++ group.bulk_remove([user.id, admin.id])++ expect_job_enqueued(job: :delete_inaccessible_notifications, args: { topic_id: pm_topic1.id })+ expect_job_enqueued(job: :delete_inaccessible_notifications, args: { topic_id: pm_topic2.id })+ end+it "clears primary_group_id" dogroup.update!(primary_group: true)User.where(id: [user.id, admin.id]).update_all(primary_group_id: group.id)diff --git a/spec/models/topic_spec.rb b/spec/models/topic_spec.rbindex 1d6d49d4723..f6824d62f8c 100644--- a/spec/models/topic_spec.rb+++ b/spec/models/topic_spec.rb@@ -3614,6 +3614,47 @@ RSpec.describe Topic do}.by(1)endend++ it "removes notifications when the removed user loses access" do+ notification =+ Fabricate(+ :notification,+ user: user1,+ topic: private_topic,+ notification_type: Notification.types[:private_message],+ read: false,+ )++ user1.expects(:publish_notifications_state).once++ expect { private_topic.remove_allowed_user(admin, user1) }.to change {+ Notification.exists?(notification.id)+ }.from(true).to(false)++ expect(Guardian.new(user1).can_see?(private_topic)).to eq(false)+ end++ it "preserves notifications when the removed user still has group access" do+ group.add(user1)+ Fabricate(:topic_allowed_group, topic: private_topic, group: group)++ notification =+ Fabricate(+ :notification,+ user: user1,+ topic: private_topic,+ notification_type: Notification.types[:private_message],+ read: false,+ )++ user1.expects(:publish_notifications_state).never++ expect { private_topic.remove_allowed_user(admin, user1) }.not_to change {+ Notification.exists?(notification.id)+ }++ expect(Guardian.new(user1).can_see?(private_topic)).to eq(true)+ endenddescribe "#remove_allowed_group" do@@ -3646,6 +3687,23 @@ RSpec.describe Topic doexpect(small_action).to be_presentexpect(small_action.user).to eq(moderator)end++ it "enqueues an inaccessible-notifications cleanup for the topic" do+ private_topic =+ Fabricate(+ :private_message_topic,+ user: admin,+ topic_allowed_users: [Fabricate.build(:topic_allowed_user, user: admin)],+ topic_allowed_groups: [Fabricate.build(:topic_allowed_group, group: pm_group)],+ )++ expect_enqueued_with(+ job: :delete_inaccessible_notifications,+ args: {+ topic_id: private_topic.id,+ },+ ) { private_topic.remove_allowed_group(admin, pm_group.name) }+ endenddescribe "#featured_link_root_domain" do