diff --git a/app/controllers/api/v1/trends/links_controller.rb b/app/controllers/api/v1/trends/links_controller.rb index 3c5aecff43..56dbc935fe 100644 --- a/app/controllers/api/v1/trends/links_controller.rb +++ b/app/controllers/api/v1/trends/links_controller.rb @@ -29,9 +29,13 @@ class Api::V1::Trends::LinksController < Api::BaseController end def links_from_trends - scope = Trends.links.query.allowed.in_locale(content_locale) - scope = scope.filtered_for(current_account) if user_signed_in? - scope + if Fasp.capability_enabled?('trends') + Fasp::PreviewCardTrend.preview_cards(language: user_signed_in? ? current_user.chosen_languages : content_locale) + else + scope = Trends.links.query.allowed.in_locale(content_locale) + scope = scope.filtered_for(current_account) if user_signed_in? + scope + end end def next_path diff --git a/app/controllers/api/v1/trends/statuses_controller.rb b/app/controllers/api/v1/trends/statuses_controller.rb index cdbfce0685..70ad2c7437 100644 --- a/app/controllers/api/v1/trends/statuses_controller.rb +++ b/app/controllers/api/v1/trends/statuses_controller.rb @@ -27,9 +27,16 @@ class Api::V1::Trends::StatusesController < Api::BaseController end def statuses_from_trends - scope = Trends.statuses.query.allowed.in_locale(content_locale) - scope = scope.filtered_for(current_account) if user_signed_in? - scope + if Fasp.capability_enabled?('trends') + Fasp::StatusTrend.statuses( + language: user_signed_in? ? current_user.chosen_languages : content_locale, + filtered_for: current_account + ) + else + scope = Trends.statuses.query.allowed.in_locale(content_locale) + scope = scope.filtered_for(current_account) if user_signed_in? + scope + end end def next_path diff --git a/app/controllers/api/v1/trends/tags_controller.rb b/app/controllers/api/v1/trends/tags_controller.rb index 10a3442344..9e833e5e78 100644 --- a/app/controllers/api/v1/trends/tags_controller.rb +++ b/app/controllers/api/v1/trends/tags_controller.rb @@ -27,9 +27,13 @@ class Api::V1::Trends::TagsController < Api::BaseController end def tags_from_trends - scope = Trends.tags.query.allowed.in_locale(content_locale) - scope = scope.filtered_for(current_account) if user_signed_in? - scope + if Fasp.capability_enabled?('trends') + Fasp::TagTrend.tags(language: user_signed_in? ? current_user.chosen_languages : content_locale) + else + scope = Trends.tags.query.allowed.in_locale(content_locale) + scope = scope.filtered_for(current_account) if user_signed_in? + scope + end end def next_path diff --git a/app/models/fasp.rb b/app/models/fasp.rb index e4e73a2312..11f5933ac3 100644 --- a/app/models/fasp.rb +++ b/app/models/fasp.rb @@ -6,4 +6,9 @@ module Fasp def self.table_name_prefix 'fasp_' end + + def self.capability_enabled?(capability_name) + Mastodon::Feature.fasp_enabled? && + Provider.with_capability(capability_name).any? + end end diff --git a/app/models/fasp/preview_card_trend.rb b/app/models/fasp/preview_card_trend.rb index ba16662b60..f1c34a816c 100644 --- a/app/models/fasp/preview_card_trend.rb +++ b/app/models/fasp/preview_card_trend.rb @@ -16,4 +16,16 @@ class Fasp::PreviewCardTrend < ApplicationRecord belongs_to :preview_card belongs_to :fasp_provider, class_name: 'Fasp::Provider' + + scope :allowed, -> { where(allowed: true) } + scope :in_language, ->(language) { where(language:) } + scope :ranked, -> { order(rank: :desc) } + + def self.preview_cards(language:) + scope = PreviewCard.joins(:fasp_preview_card_trends) + .merge(allowed) + .merge(ranked) + scope = scope.merge(in_language(language)) if language + scope + end end diff --git a/app/models/fasp/status_trend.rb b/app/models/fasp/status_trend.rb index ad151c19a4..aee8b6c8d2 100644 --- a/app/models/fasp/status_trend.rb +++ b/app/models/fasp/status_trend.rb @@ -16,4 +16,17 @@ class Fasp::StatusTrend < ApplicationRecord belongs_to :status belongs_to :fasp_provider, class_name: 'Fasp::Provider' + + scope :allowed, -> { where(allowed: true) } + scope :in_language, ->(language) { where(language:) } + scope :ranked, -> { order(rank: :desc) } + + def self.statuses(language:, filtered_for: nil) + scope = Status.joins(:fasp_status_trends) + .merge(allowed) + .merge(ranked) + scope = scope.not_excluded_by_account(filtered_for).not_domain_blocked_by_account(filtered_for) if filtered_for + scope = scope.merge(in_language(language)) if language + scope + end end diff --git a/app/models/fasp/tag_trend.rb b/app/models/fasp/tag_trend.rb index 59206632f8..cf5576dd00 100644 --- a/app/models/fasp/tag_trend.rb +++ b/app/models/fasp/tag_trend.rb @@ -16,4 +16,16 @@ class Fasp::TagTrend < ApplicationRecord belongs_to :tag belongs_to :fasp_provider, class_name: 'Fasp::Provider' + + scope :allowed, -> { where(allowed: true) } + scope :in_language, ->(language) { where(language:) } + scope :ranked, -> { order(rank: :desc) } + + def self.tags(language:) + scope = Tag.joins(:fasp_tag_trends) + .merge(allowed) + .merge(ranked) + scope = scope.merge(in_language(language)) if language + scope + end end diff --git a/app/models/preview_card.rb b/app/models/preview_card.rb index 56fe483635..4f9117a0cc 100644 --- a/app/models/preview_card.rb +++ b/app/models/preview_card.rb @@ -58,6 +58,7 @@ class PreviewCard < ApplicationRecord has_many :preview_cards_statuses, dependent: :delete_all, inverse_of: :preview_card has_many :statuses, through: :preview_cards_statuses + has_many :fasp_preview_card_trends, class_name: 'Fasp::PreviewCardTrend', dependent: :delete_all has_one :trend, class_name: 'PreviewCardTrend', inverse_of: :preview_card, dependent: :destroy belongs_to :author_account, class_name: 'Account', optional: true diff --git a/app/models/status.rb b/app/models/status.rb index 2d19dde04b..27e89fbd05 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -74,6 +74,7 @@ class Status < ApplicationRecord has_many :mentions, dependent: :destroy, inverse_of: :status has_many :mentioned_accounts, through: :mentions, source: :account, class_name: 'Account' has_many :media_attachments, dependent: :nullify + has_many :fasp_status_trends, class_name: 'Fasp::StatusTrend', dependent: :delete_all # The `dependent` option is enabled by the initial `mentions` association declaration has_many :active_mentions, -> { active }, class_name: 'Mention', inverse_of: :status # rubocop:disable Rails/HasManyOrHasOneDependent diff --git a/app/models/tag.rb b/app/models/tag.rb index a3ccdd8ac6..2536b1fa08 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -31,6 +31,7 @@ class Tag < ApplicationRecord has_many :passive_relationships, class_name: 'TagFollow', inverse_of: :tag, dependent: :destroy has_many :featured_tags, dependent: :destroy, inverse_of: :tag has_many :followers, through: :passive_relationships, source: :account + has_many :fasp_tag_trends, class_name: 'Fasp::TagTrend', dependent: :delete_all has_one :trend, class_name: 'TagTrend', inverse_of: :tag, dependent: :destroy