From f38ea42abfa6bba26a86d9d2f5d339a44c88ba60 Mon Sep 17 00:00:00 2001 From: Hugo Gameiro Date: Mon, 19 Oct 2026 08:20:46 +0000 Subject: [PATCH] Fix slow hashtag timelines with any[] when using min_id (#41132) --- app/models/tag_feed.rb | 9 +++++++++ spec/models/tag_feed_spec.rb | 15 +++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/app/models/tag_feed.rb b/app/models/tag_feed.rb index 171fe5e158..290dbd5022 100644 --- a/app/models/tag_feed.rb +++ b/app/models/tag_feed.rb @@ -30,6 +30,7 @@ class TagFeed < PublicFeed scope.merge!(tagged_with_any_scope) scope.merge!(tagged_with_all_scope) scope.merge!(tagged_with_none_scope) + scope.merge!(tagged_with_cursor_scope(max_id, since_id, min_id)) scope.merge!(local_only_scope) if local_only? scope.merge!(remote_only_scope) if remote_only? scope.merge!(account_filters_scope) if account? @@ -60,6 +61,14 @@ class TagFeed < PublicFeed Status.group(:id).tagged_with_none(tags_for(options[:none])) end + def tagged_with_cursor_scope(max_id, since_id, min_id) + lower_id = min_id.presence || since_id.presence + scope = Status.all + scope = scope.where(statuses_tags: { status_id: ...max_id }) if max_id.present? + scope = scope.where('statuses_tags.status_id > ?', lower_id) if lower_id.present? + scope + end + def tags_for(names) Tag.matching_name(Array(names).take(LIMIT_PER_MODE)).pluck(:id) if names.present? end diff --git a/spec/models/tag_feed_spec.rb b/spec/models/tag_feed_spec.rb index 8618b67334..b9cf07c01f 100644 --- a/spec/models/tag_feed_spec.rb +++ b/spec/models/tag_feed_spec.rb @@ -67,6 +67,21 @@ RSpec.describe TagFeed do expect(results).to include(status) end + it 'paginates with max_id' do + results = described_class.new(tag_cats, nil, any: [tag_dogs.name]).get(20, both.id) + expect(results).to contain_exactly(status_tagged_with_cats, status_tagged_with_dogs) + end + + it 'paginates with since_id' do + results = described_class.new(tag_cats, nil, any: [tag_dogs.name]).get(20, nil, status_tagged_with_cats.id) + expect(results).to contain_exactly(status_tagged_with_dogs, both) + end + + it 'paginates with min_id' do + results = described_class.new(tag_cats, nil, any: [tag_dogs.name]).get(20, nil, nil, status_tagged_with_cats.id) + expect(results).to contain_exactly(status_tagged_with_dogs, both) + end + context 'when both local_topic_feed_access and remote_topic_feed_access are disabled' do before do Setting.local_topic_feed_access = 'disabled'