FIX: fix notifications for flag PMs and show topics with moderator posts in inbox (#7331)

FIX: fix notifications for flag PMs and show topics with moderator posts in inbox (#7331)

Some old moderator posts that were not correctly processed before this change and 993d8f34 are not listed under /u/username/messages

diff --git a/app/models/reviewable_flagged_post.rb b/app/models/reviewable_flagged_post.rb
index be98034..4ea646a 100644
--- a/app/models/reviewable_flagged_post.rb
+++ b/app/models/reviewable_flagged_post.rb
@@ -243,12 +243,16 @@ protected
 
     trigger_spam = false
     actions.each do |action|
-      action.agreed_at = Time.zone.now
-      action.agreed_by_id = performed_by.id
-      # so callback is called
-      action.save
-      action.add_moderator_post_if_needed(performed_by, :agreed, args[:post_was_deleted])
-      trigger_spam = true if action.post_action_type_id == PostActionType.types[:spam]
+      ActiveRecord::Base.transaction do
+        action.agreed_at = Time.zone.now
+        action.agreed_by_id = performed_by.id
+        # so callback is called
+        action.save
+        DB.after_commit do
+          action.add_moderator_post_if_needed(performed_by, :agreed, args[:post_was_deleted])
+          trigger_spam = true if action.post_action_type_id == PostActionType.types[:spam]
+        end
+      end
     end
 
     DiscourseEvent.trigger(:confirmed_spam_post, post) if trigger_spam
diff --git a/lib/topic_query.rb b/lib/topic_query.rb
index b004583..e7950d4 100644
--- a/lib/topic_query.rb
+++ b/lib/topic_query.rb
@@ -312,7 +312,7 @@ class TopicQuery
     list = private_messages_for(user, :user)
 
     list = not_archived(list, user)
-      .where('NOT (topics.participant_count = 1 AND topics.user_id = ?)', user.id)
+      .where('NOT (topics.participant_count = 1 AND topics.user_id = ? AND topics.moderator_posts_count = 0)', user.id)
 
     create_list(:private_messages, {}, list)
   end
diff --git a/spec/components/topic_query_spec.rb b/spec/components/topic_query_spec.rb
index c41e87b..31138e6 100644
--- a/spec/components/topic_query_spec.rb
+++ b/spec/components/topic_query_spec.rb
@@ -1070,4 +1070,16 @@ describe TopicQuery do
       end
     end
   end
+
+  describe '#list_private_messages' do
+    it "includes topics with moderator posts" do
+      private_message_topic = Fabricate(:private_message_post, user: user).topic
+
+      expect(TopicQuery.new(user).list_private_messages(user).topics).to be_empty
+
+      private_message_topic.add_moderator_post(admin, "Thank you for your flag")
+
+      expect(TopicQuery.new(user).list_private_messages(user).topics).to eq([private_message_topic])
+    end
+  end
 end

GitHub sha: 42223739

1 Like

This commit has been mentioned on Discourse Meta. There might be relevant details there: