mirror of
https://github.com/glitch-soc/mastodon.git
synced 2025-12-13 07:49:29 +00:00
Add FASP follow recommendation support (#34964)
This commit is contained in:
@@ -2,11 +2,13 @@
|
||||
|
||||
class Api::V2::SuggestionsController < Api::BaseController
|
||||
include Authorization
|
||||
include AsyncRefreshesConcern
|
||||
|
||||
before_action -> { doorkeeper_authorize! :read, :'read:accounts' }, only: :index
|
||||
before_action -> { doorkeeper_authorize! :write, :'write:accounts' }, except: :index
|
||||
before_action :require_user!
|
||||
before_action :set_suggestions
|
||||
before_action :schedule_fasp_retrieval
|
||||
|
||||
def index
|
||||
render json: @suggestions.get(limit_param(DEFAULT_ACCOUNTS_LIMIT), params[:offset].to_i), each_serializer: REST::SuggestionSerializer
|
||||
@@ -22,4 +24,18 @@ class Api::V2::SuggestionsController < Api::BaseController
|
||||
def set_suggestions
|
||||
@suggestions = AccountSuggestions.new(current_account)
|
||||
end
|
||||
|
||||
def schedule_fasp_retrieval
|
||||
return unless Mastodon::Feature.fasp_enabled?
|
||||
# Do not schedule a new retrieval if the request is a follow-up
|
||||
# to an earlier retrieval
|
||||
return if request.headers['Mastodon-Async-Refresh-Id'].present?
|
||||
|
||||
refresh_key = "fasp:follow_recommendation:#{current_account.id}"
|
||||
return if AsyncRefresh.new(refresh_key).running?
|
||||
|
||||
add_async_refresh_header(AsyncRefresh.create(refresh_key))
|
||||
|
||||
Fasp::FollowRecommendationWorker.perform_async(current_account.id)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -22,6 +22,10 @@ class AsyncRefresh
|
||||
new(redis_key)
|
||||
end
|
||||
|
||||
def self.exists?(redis_key)
|
||||
redis.exists?(redis_key)
|
||||
end
|
||||
|
||||
attr_reader :status, :result_count
|
||||
|
||||
def initialize(redis_key)
|
||||
@@ -49,6 +53,11 @@ class AsyncRefresh
|
||||
@status = 'finished'
|
||||
end
|
||||
|
||||
def increment_result_count(by: 1)
|
||||
redis.hincrby(@redis_key, 'result_count', by)
|
||||
fetch_data_from_redis
|
||||
end
|
||||
|
||||
def reload
|
||||
fetch_data_from_redis
|
||||
self
|
||||
|
||||
@@ -34,6 +34,10 @@ class Fasp::Provider < ApplicationRecord
|
||||
before_create :create_keypair
|
||||
after_commit :update_remote_capabilities
|
||||
|
||||
scope :with_capability, lambda { |capability_name|
|
||||
where('fasp_providers.capabilities @> ?::jsonb', "[{\"id\": \"#{capability_name}\", \"enabled\": true}]")
|
||||
}
|
||||
|
||||
def capabilities
|
||||
read_attribute(:capabilities).map do |attributes|
|
||||
Fasp::Capability.new(attributes)
|
||||
|
||||
35
app/workers/fasp/follow_recommendation_worker.rb
Normal file
35
app/workers/fasp/follow_recommendation_worker.rb
Normal file
@@ -0,0 +1,35 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class Fasp::FollowRecommendationWorker
|
||||
include Sidekiq::Worker
|
||||
|
||||
sidekiq_options queue: 'fasp', retry: 0
|
||||
|
||||
def perform(account_id)
|
||||
return unless Mastodon::Feature.fasp_enabled?
|
||||
|
||||
async_refresh = AsyncRefresh.new("fasp:follow_recommendation:#{account_id}")
|
||||
|
||||
account = Account.find(account_id)
|
||||
|
||||
follow_recommendation_providers = Fasp::Provider.with_capability('follow_recommendation')
|
||||
return if follow_recommendation_providers.none?
|
||||
|
||||
account_uri = ActivityPub::TagManager.instance.uri_for(account)
|
||||
params = { accountUri: account_uri }.to_query
|
||||
fetch_service = ActivityPub::FetchRemoteActorService.new
|
||||
|
||||
follow_recommendation_providers.each do |provider|
|
||||
Fasp::Request.new(provider).get("/follow_recommendation/v0/accounts?#{params}").each do |uri|
|
||||
next if Account.where(uri:).any?
|
||||
|
||||
account = fetch_service.call(uri)
|
||||
async_refresh.increment_result_count(by: 1) if account.present?
|
||||
end
|
||||
end
|
||||
rescue ActiveRecord::RecordNotFound
|
||||
# Nothing to be done
|
||||
ensure
|
||||
async_refresh.finish!
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user