mastodon/app/workers/fasp/announce_trend_worker.rb
David Roetzel 75be830f4f
Select what to share with fasp
Only share statuses where the account has `#indexable` set
to `true`.

Only share accounts where `#discoverable` is set to `true`, with
one exception: If `#discoverable` has just been set to `false`
this is an important information for the fasp.
2025-01-06 08:58:08 +01:00

62 lines
1.6 KiB
Ruby

# frozen_string_literal: true
class Fasp::AnnounceTrendWorker
include Sidekiq::Worker
sidekiq_options queue: 'fasp', retry: 5
def perform(status_id, trend_source)
status = ::Status.includes(:account).find(status_id)
return unless status.account.indexable?
Fasp::Subscription.includes(:fasp_provider).content.trends.each do |subscription|
announce(subscription, status.uri) if trending?(subscription, status, trend_source)
end
rescue ActiveRecord::RecordNotFound
# status might not exist anymore, in which case there is nothing to do
end
private
def trending?(subscription, status, trend_source)
scope = scope_for(status, trend_source)
threshold = threshold_for(subscription, trend_source)
scope.where(created_at: subscription.timeframe_start..).count >= threshold
end
def scope_for(status, trend_source)
case trend_source
when 'favourite'
status.favourites
when 'reblog'
status.reblogs
when 'reply'
status.replies
end
end
def threshold_for(subscription, trend_source)
case trend_source
when 'favourite'
subscription.threshold_likes
when 'reblog'
subscription.threshold_shares
when 'reply'
subscription.threshold_replies
end
end
def announce(subscription, uri)
Fasp::Request.new(subscription.fasp_provider).post('/data_sharing/v0/announcements', body: {
source: {
subscription: {
id: subscription.id.to_s,
},
},
category: 'content',
eventType: 'trending',
objectUris: [uri],
})
end
end