From 99330594263f9c4de9a03d10d6d61c403708d7b2 Mon Sep 17 00:00:00 2001 From: Sam Date: Mon, 29 Oct 2018 10:47:59 +1100 Subject: [PATCH] FEATURE: push related PMs to take first 3 slots Previously the related PMs were last meaning you would have to work through all unread to see them. Also amends it so it either asks for related by group OR user not both. --- lib/topic_query.rb | 30 ++++++++++++++--------------- spec/components/topic_query_spec.rb | 10 +++++----- 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/lib/topic_query.rb b/lib/topic_query.rb index 39495cb2f94..eb631adda7c 100644 --- a/lib/topic_query.rb +++ b/lib/topic_query.rb @@ -22,10 +22,6 @@ class TopicQuery int.call(x) && x.to_i.between?(0, PG_MAX_INT) end - one_up_to_max_int = lambda do |x| - int.call(x) && x.to_i.between?(1, PG_MAX_INT) - end - array_int_or_int = lambda do |x| int.call(x) || ( Array === x && x.length > 0 && x.all?(&int) @@ -173,6 +169,19 @@ class TopicQuery if @user if topic.private_message? + # we start with related conversations cause they are the most relevant + if pm_params[:my_group_ids].present? + builder.add_results(related_messages_group( + pm_params.merge(count: [3, builder.results_left].max, + exclude: builder.excluded_topic_ids) + )) + else + builder.add_results(related_messages_user( + pm_params.merge(count: [3, builder.results_left].max, + exclude: builder.excluded_topic_ids) + )) + end + builder.add_results(new_messages( pm_params.merge(count: builder.results_left) )) unless builder.full? @@ -187,18 +196,7 @@ class TopicQuery end end - if topic.private_message? - - builder.add_results(related_messages_group( - pm_params.merge(count: [3, builder.results_left].max, - exclude: builder.excluded_topic_ids) - )) if pm_params[:my_group_ids].present? - - builder.add_results(related_messages_user( - pm_params.merge(count: [3, builder.results_left].max, - exclude: builder.excluded_topic_ids) - )) - else + if !topic.private_message? builder.add_results(random_suggested(topic, builder.results_left, builder.excluded_topic_ids)) unless builder.full? end diff --git a/spec/components/topic_query_spec.rb b/spec/components/topic_query_spec.rb index 7c70d4a049f..07e96aee9fa 100644 --- a/spec/components/topic_query_spec.rb +++ b/spec/components/topic_query_spec.rb @@ -212,9 +212,9 @@ describe TopicQuery do it "returns topics in the given category with the given tag" do tagged_topic1 = Fabricate(:topic, category: category1, tags: [tag]) - tagged_topic2 = Fabricate(:topic, category: category2, tags: [tag]) + _tagged_topic2 = Fabricate(:topic, category: category2, tags: [tag]) tagged_topic3 = Fabricate(:topic, category: category1, tags: [tag, other_tag]) - no_tags_topic = Fabricate(:topic, category: category1) + _no_tags_topic = Fabricate(:topic, category: category1) expect(TopicQuery.new(moderator, category: category1.id, tags: [tag.name]).list_latest.topics.map(&:id).sort).to eq([tagged_topic1.id, tagged_topic3.id].sort) expect(TopicQuery.new(moderator, category: category2.id, tags: [other_tag.name]).list_latest.topics.size).to eq(0) @@ -489,7 +489,7 @@ describe TopicQuery do context 'list_unread' do it 'lists topics correctly' do - new_topic = Fabricate(:post, user: creator).topic + _new_topic = Fabricate(:post, user: creator).topic expect(topic_query.list_unread.topics).to eq([]) expect(topic_query.list_read.topics).to match_array([fully_read, partially_read]) @@ -694,11 +694,11 @@ describe TopicQuery do read(user, related_by_group_pm, 1) expect(TopicQuery.new(user).list_suggested_for(pm_to_group).topics.map(&:id)).to( - eq([related_by_group_pm.id, related_by_user_pm.id, pm_to_user.id]) + eq([related_by_group_pm.id]) ) expect(TopicQuery.new(user).list_suggested_for(pm_to_user).topics.map(&:id)).to( - eq([new_pm.id, unread_pm.id, related_by_user_pm.id]) + eq([related_by_user_pm.id, new_pm.id, unread_pm.id]) ) SiteSetting.enable_personal_messages = false