diff --git a/app/controllers/api/v1/featured_tags/suggestions_controller.rb b/app/controllers/api/v1/featured_tags/suggestions_controller.rb index 4f732ed2d..9c72e4380 100644 --- a/app/controllers/api/v1/featured_tags/suggestions_controller.rb +++ b/app/controllers/api/v1/featured_tags/suggestions_controller.rb @@ -12,10 +12,6 @@ class Api::V1::FeaturedTags::SuggestionsController < Api::BaseController private def set_recently_used_tags - @recently_used_tags = Tag.recently_used(current_account).where.not(id: featured_tag_ids).limit(10) - end - - def featured_tag_ids - current_account.featured_tags.pluck(:tag_id) + @recently_used_tags = Tag.suggestions_for_account(current_account).limit(10) end end diff --git a/app/controllers/settings/featured_tags_controller.rb b/app/controllers/settings/featured_tags_controller.rb index c38440265..90c112e21 100644 --- a/app/controllers/settings/featured_tags_controller.rb +++ b/app/controllers/settings/featured_tags_controller.rb @@ -38,7 +38,7 @@ class Settings::FeaturedTagsController < Settings::BaseController end def set_recently_used_tags - @recently_used_tags = Tag.recently_used(current_account).where.not(id: @featured_tags.map(&:id)).limit(10) + @recently_used_tags = Tag.suggestions_for_account(current_account).limit(10) end def featured_tag_params diff --git a/app/models/tag.rb b/app/models/tag.rb index f2168ae90..58baa48c0 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -53,6 +53,8 @@ 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 :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) }) diff --git a/spec/models/tag_spec.rb b/spec/models/tag_spec.rb index 5a1d62088..4c2bdd52f 100644 --- a/spec/models/tag_spec.rb +++ b/spec/models/tag_spec.rb @@ -142,6 +142,25 @@ RSpec.describe Tag do end end + describe '.not_featured_by' do + let!(:account) { Fabricate(:account) } + let!(:fun) { Fabricate(:tag, name: 'fun') } + let!(:games) { Fabricate(:tag, name: 'games') } + + before do + Fabricate :featured_tag, account: account, name: 'games' + Fabricate :featured_tag, name: 'fun' + end + + it 'returns tags not featured by the account' do + results = described_class.not_featured_by(account) + + expect(results) + .to include(fun) + .and not_include(games) + end + end + describe '.matches_name' do it 'returns tags for multibyte case-insensitive names' do upcase_string = 'abcABCabcABCやゆよ'