A platform for community discussion. Free, open, simple.
FIX: watching_first_post topic filter & query (#35353)
Régis Hanol4d4de3e63d4a
diff --git a/lib/topic_query.rb b/lib/topic_query.rbindex ccb1efbc7e1..91dbc2327c8 100644--- a/lib/topic_query.rb+++ b/lib/topic_query.rb@@ -496,6 +496,40 @@ class TopicQuerylist.where(sql, user_id: user_id, tracking: NotificationLevels.all[:tracking])end+ def self.watching_first_post_filter(list, user)+ return list.none if user.nil?++ category_scope =+ list.where(+ "topics.category_id IN (+ SELECT category_id+ FROM category_users+ WHERE user_id = ? AND notification_level = ?+ )",+ user.id,+ CategoryUser.notification_levels[:watching_first_post],+ )++ return category_scope if !SiteSetting.tagging_enabled++ tag_scope =+ list.where(+ "topics.id IN (+ SELECT topic_id+ FROM topic_tags+ WHERE tag_id IN (+ SELECT tag_id+ FROM tag_users+ WHERE user_id = ? AND notification_level = ?+ )+ )",+ user.id,+ TagUser.notification_levels[:watching_first_post],+ )++ category_scope.or(tag_scope)+ end+def prioritize_pinned_topics(topics, options)pinned_clause =if options[:category_id]@@ -880,21 +914,23 @@ class TopicQuery)end- # NOTE protect against SYM attack can be removed with Ruby 2.2- #- state = options[:state]- if @user && state && TopicUser.notification_levels.keys.map(&:to_s).include?(state)- level = TopicUser.notification_levels[state.to_sym]- result =- result.where(- "topics.id IN (- SELECT topic_id- FROM topic_users- WHERE user_id = ? AND- notification_level = ?)",- @user.id,- level,- )+ if state = options[:state]+ # Special handling for watching_first_post - it's a category/tag level notification+ # not a topic level notification, so we need to query CategoryUser and TagUser tables+ if state == "watching_first_post"+ result = TopicQuery.watching_first_post_filter(result, @user)+ elsif @user && TopicUser.notification_levels.keys.map(&:to_s).include?(state)+ result =+ result.where(+ "topics.id IN (+ SELECT topic_id+ FROM topic_users+ WHERE user_id = ? AND notification_level = ?+ )",+ @user.id,+ TopicUser.notification_levels[state.to_sym],+ )+ endendif before = options[:before]diff --git a/lib/topics_filter.rb b/lib/topics_filter.rbindex f60e3959cf6..fbdb9ef5a20 100644--- a/lib/topics_filter.rb+++ b/lib/topics_filter.rb@@ -690,6 +690,22 @@ class TopicsFilterendend+ def topic_user_scope+ @scope.where(+ "tu.notification_level IN (:topic_notification_levels)",+ topic_notification_levels: @topic_notification_levels.to_a,+ )+ end++ def watching_first_post_scope+ TopicQuery.watching_first_post_filter(@scope, @guardian.user)+ end++ def combine_scopes_with_or(scope1, scope2)+ @scope.joins_values.concat(scope1.joins_values, scope2.joins_values).uniq!+ @scope.merge(scope1.or(scope2))+ end+def filter_in(values:)values.uniq!@@ -715,10 +731,12 @@ class TopicsFiltertreat_as_new_topic_start_date: @guardian.user.user_option.treat_as_new_topic_start_date,)end+if values.delete("new-replies")ensure_topic_users_reference!@scope = TopicQuery.unread_filter(@scope, whisperer: @guardian.user.whisperer?)end+if values.delete("new")ensure_topic_users_reference!new_topics =@@ -727,10 +745,7 @@ class TopicsFiltertreat_as_new_topic_start_date: @guardian.user.user_option.treat_as_new_topic_start_date,)unread_topics = TopicQuery.unread_filter(@scope, whisperer: @guardian.user.whisperer?)- base = @scope- base.joins_values.dup.concat(new_topics.joins_values, unread_topics.joins_values)- base.joins_values.uniq!- @scope = base.merge(new_topics.or(unread_topics))+ @scope = combine_scopes_with_or(new_topics, unread_topics)endif values.delete("unseen")@@ -753,15 +768,20 @@ class TopicsFilterendendend+ end- if @topic_notification_levels.present?- ensure_topic_users_reference!- @scope =- @scope.where(- "tu.notification_level IN (:topic_notification_levels)",- topic_notification_levels: @topic_notification_levels.to_a,- )- end+ # watching_first_post is a category/tag-level notification, not a topic-level one+ # We need to handle it separately from regular notification levels and combine with OR+ has_watching_first_post = values.delete("watching_first_post")++ if has_watching_first_post && @topic_notification_levels.present?+ ensure_topic_users_reference!+ @scope = combine_scopes_with_or(topic_user_scope, watching_first_post_scope)+ elsif has_watching_first_post+ @scope = @scope.merge(watching_first_post_scope)+ elsif @topic_notification_levels.present?+ ensure_topic_users_reference!+ @scope = @scope.merge(topic_user_scope)endelsif values.present?@scope = @scope.nonediff --git a/spec/lib/topic_query_spec.rb b/spec/lib/topic_query_spec.rbindex 7948f62c5a8..593a9aa25bc 100644--- a/spec/lib/topic_query_spec.rb+++ b/spec/lib/topic_query_spec.rb@@ -2386,6 +2386,69 @@ RSpec.describe TopicQuery doendend+ describe "state parameter" do+ describe "state=watching_first_post" do+ fab!(:test_user, :user)+ fab!(:category_watching_first_post, :category)+ fab!(:category_regular, :category)+ fab!(:tag_watching_first_post, :tag)+ fab!(:tag_regular, :tag)++ fab!(:topic_in_watched_category) { Fabricate(:topic, category: category_watching_first_post) }++ fab!(:topic_in_regular_category) { Fabricate(:topic, category: category_regular) }+ fab!(:topic_with_watched_tag) { Fabricate(:topic, tags: [tag_watching_first_post]) }+ fab!(:topic_with_regular_tag) { Fabricate(:topic, tags: [tag_regular]) }++ fab!(:topic_with_both) do+ Fabricate(:topic, category: category_watching_first_post, tags: [tag_watching_first_post])+ end++ before do+ SiteSetting.tagging_enabled = true+ CategoryUser.set_notification_level_for_category(+ test_user,+ CategoryUser.notification_levels[:watching_first_post],+ category_watching_first_post.id,+ )+ TagUser.change(+ test_user.id,+ tag_watching_first_post.id,+ TagUser.notification_levels[:watching_first_post],+ )+ end++ it "should not return any topics if the user is anonymous" do+ expect(+ TopicQuery.new(nil, state: "watching_first_post").list_latest.topics.map(&:id),+ ).to eq([])+ end++ it "should return the union of topics in watched categories and topics with watched tags" do+ ids = TopicQuery.new(test_user, state: "watching_first_post").list_latest.topics.map(&:id)++ expect(ids).to contain_exactly(+ topic_in_watched_category.id,+ topic_with_watched_tag.id,+ topic_with_both.id,+ )+ end++ it "should work when combined with other filters" do+ topic_in_watched_category.update!(closed: true)++ ids =+ TopicQuery+ .new(test_user, state: "watching_first_post", status: "closed")+ .list_latest+ .topics+ .map(&:id)++ expect(ids).to contain_exactly(topic_in_watched_category.id)+ end+ end+ end+describe "content_localization enabled" dofab!(:user)fab!(:topics) { Fabricate.times(3, :topic) }diff --git a/spec/lib/topics_filter_spec.rb b/spec/lib/topics_filter_spec.rbindex 84997afdfff..a10a37b506e 100644--- a/spec/lib/topics_filter_spec.rb+++ b/spec/lib/topics_filter_spec.rb@@ -490,6 +490,95 @@ RSpec.describe TopicsFilter doendendend++ describe "when query string is `in:watching_first_post`" do+ fab!(:category_watching_first_post, :category)+ fab!(:category_regular, :category)+ fab!(:tag_watching_first_post, :tag)+ fab!(:tag_regular, :tag)++ fab!(:topic_in_watched_category) do+ Fabricate(:topic, category: category_watching_first_post)+ end+ fab!(:topic_in_regular_category) { Fabricate(:topic, category: category_regular) }+ fab!(:topic_with_watched_tag) { Fabricate(:topic, tags: [tag_watching_first_post]) }+ fab!(:topic_with_regular_tag) { Fabricate(:topic, tags: [tag_regular]) }+ fab!(:topic_with_both) do+ Fabricate(:topic, category: category_watching_first_post, tags: [tag_watching_first_post])+ end++ before do+ CategoryUser.set_notification_level_for_category(+ user,+ CategoryUser.notification_levels[:watching_first_post],+ category_watching_first_post.id,+ )+ TagUser.change(+ user.id,+ tag_watching_first_post.id,+ TagUser.notification_levels[:watching_first_post],+ )+ end++ it "should not return any topics if the user is anonymous" do+ expect(+ TopicsFilter+ .new(guardian: Guardian.new)+ .filter_from_query_string("in:watching_first_post")+ .pluck(:id),+ ).to be_empty+ end++ it "should return the union of topics in watched categories and topics with watched tags" do+ ids =+ TopicsFilter+ .new(guardian: Guardian.new(user))+ .filter_from_query_string("in:watching_first_post")+ .pluck(:id)++ expect(ids).to contain_exactly(+ topic_in_watched_category.id,+ topic_with_watched_tag.id,+ topic_with_both.id,+ )+ end++ it "should work when combined with other filters" do+ topic_in_watched_category.update!(closed: true)++ ids =+ TopicsFilter+ .new(guardian: Guardian.new(user))+ .filter_from_query_string("in:watching_first_post status:closed")+ .pluck(:id)++ expect(ids).to contain_exactly(topic_in_watched_category.id)+ end++ it "should work with comma-separated notification levels" do+ user_watching_topic =+ Fabricate(:topic).tap do |topic|+ TopicUser.change(+ user.id,+ topic.id,+ notification_level: TopicUser.notification_levels[:watching],+ )+ end++ ids =+ TopicsFilter+ .new(guardian: Guardian.new(user))+ .filter_from_query_string("in:watching,watching_first_post")+ .pluck(:id)++ expect(ids).to contain_exactly(+ user_watching_topic.id,+ topic_in_watched_category.id,+ topic_with_watched_tag.id,+ topic_with_both.id,+ )+ end+ endenddescribe "when filtering with custom filters" do