A platform for community discussion. Free, open, simple.
FIX: Delete notifications users can't see after moving posts
Gerhard Schlager271ddac467b2
diff --git a/app/jobs/regular/delete_inaccessible_notifications.rb b/app/jobs/regular/delete_inaccessible_notifications.rbnew file mode 100644index 00000000000..ca38dc4ec82--- /dev/null+++ b/app/jobs/regular/delete_inaccessible_notifications.rb@@ -0,0 +1,17 @@+# frozen_string_literal: true++module Jobs+ class DeleteInaccessibleNotifications < Jobs::Base+ def 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++ if !Guardian.new(notification.user).can_see?(notification.topic)+ notification.destroy+ end+ end+ end+ end+enddiff --git a/app/models/post_mover.rb b/app/models/post_mover.rbindex bac69927b0f..81afb535662 100644--- a/app/models/post_mover.rb+++ b/app/models/post_mover.rb@@ -27,6 +27,7 @@ class PostMovermove_posts_to topicendadd_allowed_users(participants) if participants.present? && @move_to_pm+ enqueue_jobs(topic)topicend@@ -37,7 +38,7 @@ class PostMoverraise Discourse::InvalidParameters unless postarchetype = @move_to_pm ? Archetype.private_message : Archetype.default- Topic.transaction do+ topic = Topic.transaction donew_topic = Topic.create!(user: post.user,title: title,@@ -50,6 +51,8 @@ class PostMoverwatch_new_topicnew_topicend+ enqueue_jobs(topic)+ topicendprivate@@ -77,6 +80,7 @@ class PostMoverdef move_each_postmax_post_number = destination_topic.max_post_number + 1+ @post_creator = nil@move_map = {}@reply_count = {}posts.each_with_index do |post, offset|@@ -110,7 +114,7 @@ class PostMoverdef create_first_post(post)old_post_attributes = post_attributes(post)- new_post = PostCreator.create(+ @post_creator = PostCreator.new(post.user,raw: post.raw,topic_id: destination_topic.id,@@ -120,8 +124,10 @@ class PostMoverraw_email: post.raw_email,skip_validations: true,created_at: post.created_at,- guardian: Guardian.new(user)+ guardian: Guardian.new(user),+ skip_jobs: true)+ new_post = @post_creator.createmove_incoming_emails(post, new_post)move_email_logs(post, new_post)@@ -312,4 +318,13 @@ class PostMoverpost_number: post.post_number}end++ def enqueue_jobs(topic)+ @post_creator.enqueue_jobs if @post_creator++ Jobs.enqueue(+ :delete_inaccessible_notifications,+ topic_id: topic.id+ )+ endenddiff --git a/app/models/topic_converter.rb b/app/models/topic_converter.rbindex d0ce8dacf03..f59e96c4d01 100644--- a/app/models/topic_converter.rb+++ b/app/models/topic_converter.rb@@ -31,6 +31,7 @@ class TopicConverterupdate_user_statsJobs.enqueue(:topic_action_converter, topic_id: @topic.id)+ Jobs.enqueue(:delete_inaccessible_notifications, topic_id: @topic.id)watch_topic(topic)end@@ -50,6 +51,8 @@ class TopicConverteradd_allowed_usersJobs.enqueue(:topic_action_converter, topic_id: @topic.id)+ Jobs.enqueue(:delete_inaccessible_notifications, topic_id: @topic.id)+watch_topic(topic)end@topicdiff --git a/spec/models/post_mover_spec.rb b/spec/models/post_mover_spec.rbindex 833705e9a56..ce6495356d8 100644--- a/spec/models/post_mover_spec.rb+++ b/spec/models/post_mover_spec.rb@@ -308,6 +308,18 @@ describe PostMover doexpect(n4.topic_id).to eq(topic.id)expect(n4.post_number).to eq(4)end++ it "deletes notifications for users not allowed to see the topic" do+ another_admin = Fabricate(:admin)+ staff_category = Fabricate(:private_category, group: Group[:staff])+ user_notification = Fabricate(:mentioned_notification, post: p3, user: another_user)+ admin_notification = Fabricate(:mentioned_notification, post: p3, user: another_admin)++ topic.move_posts(user, [p3.id], title: "new testing topic name", category_id: staff_category.id)++ expect(Notification.exists?(user_notification.id)).to eq(false)+ expect(Notification.exists?(admin_notification.id)).to eq(true)+ endendcontext "to an existing topic" do@@ -401,6 +413,19 @@ describe PostMover doexpect(n4.topic_id).to eq(topic.id)expect(n4.post_number).to eq(4)end++ it "deletes notifications for users not allowed to see the topic" do+ another_admin = Fabricate(:admin)+ staff_category = Fabricate(:private_category, group: Group[:staff])+ user_notification = Fabricate(:mentioned_notification, post: p3, user: another_user)+ admin_notification = Fabricate(:mentioned_notification, post: p3, user: another_admin)++ destination_topic.update!(category_id: staff_category.id)+ topic.move_posts(user, [p3.id], destination_topic_id: destination_topic.id)++ expect(Notification.exists?(user_notification.id)).to eq(false)+ expect(Notification.exists?(admin_notification.id)).to eq(true)+ endendcontext "to a message" dodiff --git a/spec/models/topic_converter_spec.rb b/spec/models/topic_converter_spec.rbindex 15fadd18be2..fb4b351d21f 100644--- a/spec/models/topic_converter_spec.rb+++ b/spec/models/topic_converter_spec.rb@@ -90,6 +90,18 @@ describe TopicConverter doexpect(other_user.user_actions.where(action_type: UserAction::REPLY).count).to eq(1)endend++ it "deletes notifications for users not allowed to see the topic" do+ staff_category = Fabricate(:private_category, group: Group[:staff])+ user_notification = Fabricate(:mentioned_notification, post: first_post, user: Fabricate(:user))+ admin_notification = Fabricate(:mentioned_notification, post: first_post, user: Fabricate(:admin))++ Jobs.run_immediately!+ TopicConverter.new(first_post.topic, admin).convert_to_public_topic(staff_category.id)++ expect(Notification.exists?(user_notification.id)).to eq(false)+ expect(Notification.exists?(admin_notification.id)).to eq(true)+ endendend@@ -129,6 +141,17 @@ describe TopicConverter doexpect(author.user_actions.where(action_type: UserAction::NEW_TOPIC).count).to eq(0)expect(author.user_actions.where(action_type: UserAction::NEW_PRIVATE_MESSAGE).count).to eq(1)end++ it "deletes notifications for users not allowed to see the message" do+ user_notification = Fabricate(:mentioned_notification, post: post, user: Fabricate(:user))+ admin_notification = Fabricate(:mentioned_notification, post: post, user: Fabricate(:admin))++ Jobs.run_immediately!+ topic.convert_to_private_message(admin)++ expect(Notification.exists?(user_notification.id)).to eq(false)+ expect(Notification.exists?(admin_notification.id)).to eq(true)+ endendcontext 'topic has replies' do