From d8b26688376b9f533597dd5f77169257f6873e42 Mon Sep 17 00:00:00 2001 From: Renaud Chaput Date: Tue, 23 Jun 2026 11:39:31 +0200 Subject: [PATCH] Change featured tag recommendations (#39567) --- app/models/tag.rb | 5 +++-- spec/requests/api/v1/featured_tags/suggestions_spec.rb | 6 ++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/app/models/tag.rb b/app/models/tag.rb index a5fbf2f683..bc0ccdfb58 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -46,6 +46,7 @@ class Tag < ApplicationRecord HASHTAG_INVALID_CHARS_RE = /[^[:alnum:]\u0E47-\u0E4E#{HASHTAG_SEPARATORS}]/ RECENT_STATUS_LIMIT = 1000 + RECENT_STATUS_MAX_AGE = 1.year validates :name, presence: true, format: { with: HASHTAG_NAME_RE } validates :display_name, format: { with: HASHTAG_NAME_RE } @@ -58,11 +59,11 @@ class Tag < ApplicationRecord scope :listable, -> { where(listable: [true, nil]) } scope :trendable, -> { Setting.trendable_by_default ? where(trendable: [true, nil]) : where(trendable: true) } scope :not_trendable, -> { where(trendable: false) } - scope :suggestions_for_account, ->(account) { recently_used(account).not_featured_by(account) } + scope :suggestions_for_account, ->(account) { recently_used(account).having('count(statuses.id) > 1').not_featured_by(account) } scope :not_featured_by, ->(account) { where.not(id: account.featured_tags.select(:tag_id)) } scope :recently_used, lambda { |account| joins(:statuses) - .where(statuses: { id: account.statuses.select(:id).limit(RECENT_STATUS_LIMIT) }) + .where(statuses: { id: account.statuses.select(:id).where(id: (Mastodon::Snowflake.id_at(RECENT_STATUS_MAX_AGE.ago)..)).limit(RECENT_STATUS_LIMIT) }) .group(:id).order(Arel.star.count.desc) } scope :matches_name, ->(term) { where(arel_table[:name].lower.matches(arel_table.lower("#{sanitize_sql_like(normalize_value_for(:name, term))}%"), nil, true)) } # Search with case-sensitive to use B-tree index diff --git a/spec/requests/api/v1/featured_tags/suggestions_spec.rb b/spec/requests/api/v1/featured_tags/suggestions_spec.rb index 5fbbec5097..8f2b64cb4a 100644 --- a/spec/requests/api/v1/featured_tags/suggestions_spec.rb +++ b/spec/requests/api/v1/featured_tags/suggestions_spec.rb @@ -12,6 +12,7 @@ RSpec.describe 'Featured Tags Suggestions API' do describe 'GET /api/v1/featured_tags/suggestions' do let!(:unused_featured_tag) { Fabricate(:tag, name: 'unused_featured_tag') } let!(:used_tag) { Fabricate(:tag, name: 'used_tag') } + let!(:used_tag_once) { Fabricate(:tag, name: 'used_tag_once') } let!(:used_featured_tag) { Fabricate(:tag, name: 'used_featured_tag') } before do @@ -20,8 +21,13 @@ RSpec.describe 'Featured Tags Suggestions API' do # Make relevant tags used by account status = Fabricate(:status, account: account) status.tags << used_tag + status.tags << used_tag_once status.tags << used_featured_tag + status2 = Fabricate(:status, account: account) + status2.tags << used_tag + status2.tags << used_featured_tag + # Feature the relevant tags Fabricate :featured_tag, account: account, name: unused_featured_tag.name Fabricate :featured_tag, account: account, name: used_featured_tag.name