A platform for community discussion. Free, open, simple.
DEV: Add include_pms option to TopicQuery (#10647)
David Taylor66eda8c9df8b
diff --git a/lib/topic_query.rb b/lib/topic_query.rbindex 73aa372ee58..a9ae9786f11 100644--- a/lib/topic_query.rb+++ b/lib/topic_query.rb@@ -70,7 +70,8 @@ class TopicQueryvisibleguardianno_definitions- destination_category_id)+ destination_category_id+ include_pms)end# Maps `order` to a columns in `topics`@@ -724,7 +725,15 @@ class TopicQueryendresult = apply_ordering(result, options)- result = result.listable_topics++ all_listable_topics = @guardian.filter_allowed_categories(Topic.unscoped.listable_topics)++ if options[:include_pms]+ all_pm_topics = Topic.unscoped.private_messages_for_user(@user)+ result = result.merge(all_listable_topics.or(all_pm_topics))+ else+ result = result.merge(all_listable_topics)+ end# Don't include the category topics if excludedif options[:no_definitions]@@ -851,7 +860,7 @@ class TopicQueryresult = TopicQuery.apply_custom_filters(result, self)- @guardian.filter_allowed_categories(result)+ resultenddef remove_muted_topics(list, user)diff --git a/spec/components/topic_query_spec.rb b/spec/components/topic_query_spec.rbindex f1ee8566217..31ade884680 100644--- a/spec/components/topic_query_spec.rb+++ b/spec/components/topic_query_spec.rb@@ -9,7 +9,7 @@ describe TopicQuery do# it indeed happens first, but is not obvious later in the tests we depend on the user being# created so early otherwise finding new topics does not work# we should remove the let! here and use freeze time to communicate how the clock moves- let!(:user) { Fabricate(:coding_horror) }+ let!(:user) { Fabricate(:user) }fab!(:creator) { Fabricate(:user) }let(:topic_query) { TopicQuery.new(user) }@@ -162,6 +162,18 @@ describe TopicQuery doendend+ describe 'include_pms option' do+ it "includes users own pms in regular topic lists" do+ topic = Fabricate(:topic)+ own_pm = Fabricate(:private_message_topic, user: user)+ other_pm = Fabricate(:private_message_topic, user: Fabricate(:user))++ expect(TopicQuery.new(user).list_latest.topics).to contain_exactly(topic)+ expect(TopicQuery.new(admin).list_latest.topics).to contain_exactly(topic)+ expect(TopicQuery.new(user, include_pms: true).list_latest.topics).to contain_exactly(topic, own_pm)+ end+ end+context 'category filter' dolet(:category) { Fabricate(:category_with_definition) }let(:diff_category) { Fabricate(:category_with_definition, name: "Different Category") }diff --git a/spec/fabricators/topic_fabricator.rb b/spec/fabricators/topic_fabricator.rbindex 3bcee07d2e7..b65cd48d7fa 100644--- a/spec/fabricators/topic_fabricator.rb+++ b/spec/fabricators/topic_fabricator.rb@@ -21,11 +21,12 @@ Fabricator(:banner_topic, from: :topic) doendFabricator(:private_message_topic, from: :topic) do+ transient :recipientcategory_id { nil }title { sequence(:title) { |i| "This is a private message #{i}" } }archetype "private_message"topic_allowed_users { |t| [Fabricate.build(:topic_allowed_user, user: t[:user]),- Fabricate.build(:topic_allowed_user, user: Fabricate(:coding_horror))+ Fabricate.build(:topic_allowed_user, user: t[:recipient] || Fabricate(:user))]}enddiff --git a/spec/models/topic_spec.rb b/spec/models/topic_spec.rbindex 17346ce41e8..72d1583790d 100644--- a/spec/models/topic_spec.rb+++ b/spec/models/topic_spec.rb@@ -894,9 +894,9 @@ describe Topic doendcontext 'private message' do- let(:coding_horror) { User.find_by(username: "CodingHorror") }+ let(:coding_horror) { Fabricate(:coding_horror) }fab!(:evil_trout) { Fabricate(:evil_trout) }- let(:topic) { Fabricate(:private_message_topic) }+ let(:topic) { Fabricate(:private_message_topic, recipient: coding_horror) }it "should integrate correctly" doexpect(Guardian.new(topic.user).can_see?(topic)).to eq(true)