From 34cebfd86777bfe1da422515f45b062e8be60125 Mon Sep 17 00:00:00 2001 From: Alan Guo Xiang Tan Date: Mon, 4 Oct 2021 11:55:35 +0800 Subject: [PATCH] FIX: Exclude PMs that user sent to themselves. (#14496) Regression from 016efeadf6f242e04daf5ef8e18c2ca708a1392d Follow-up to 016efeadf6f242e04daf5ef8e18c2ca708a1392d --- lib/topic_query/private_message_lists.rb | 9 +++++++++ spec/lib/topic_query/private_message_lists_spec.rb | 12 ++++++++++++ 2 files changed, 21 insertions(+) diff --git a/lib/topic_query/private_message_lists.rb b/lib/topic_query/private_message_lists.rb index 949f67d125c..4f0c39ab605 100644 --- a/lib/topic_query/private_message_lists.rb +++ b/lib/topic_query/private_message_lists.rb @@ -5,6 +5,15 @@ class TopicQuery def list_private_messages(user) list = private_messages_for(user, :user) list = not_archived(list, user) + + list = list.where(<<~SQL) + NOT ( + topics.participant_count = 1 + AND topics.user_id = #{user.id.to_i} + AND topics.moderator_posts_count = 0 + ) + SQL + create_list(:private_messages, {}, list) end diff --git a/spec/lib/topic_query/private_message_lists_spec.rb b/spec/lib/topic_query/private_message_lists_spec.rb index a2bf68eb5e9..0708b2e6c2d 100644 --- a/spec/lib/topic_query/private_message_lists_spec.rb +++ b/spec/lib/topic_query/private_message_lists_spec.rb @@ -3,6 +3,7 @@ require 'rails_helper' describe TopicQuery::PrivateMessageLists do + fab!(:admin) { Fabricate(:admin) } fab!(:user) { Fabricate(:user) } fab!(:user_2) { Fabricate(:user) } fab!(:user_3) { Fabricate(:user) } @@ -50,6 +51,17 @@ describe TopicQuery::PrivateMessageLists do expect(topics).to contain_exactly(private_message) end + + it "includes topics with moderator posts" do + pm = Fabricate(:private_message_post, user: user_4).topic + + expect(TopicQuery.new(user_4).list_private_messages(user_4).topics).to be_empty + + pm.add_moderator_post(admin, "Thank you for your flag") + + expect(TopicQuery.new(user_4).list_private_messages(user_4).topics) + .to contain_exactly(pm) + end end describe '#list_private_messages_group' do