Add support for FASP data sharing (#34415)
This commit is contained in:
parent
3ea1f074ab
commit
a5a2c6dc7e
38 changed files with 1140 additions and 1 deletions
28
app/workers/fasp/announce_account_lifecycle_event_worker.rb
Normal file
28
app/workers/fasp/announce_account_lifecycle_event_worker.rb
Normal file
|
@ -0,0 +1,28 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class Fasp::AnnounceAccountLifecycleEventWorker
|
||||
include Sidekiq::Worker
|
||||
|
||||
sidekiq_options queue: 'fasp', retry: 5
|
||||
|
||||
def perform(uri, event_type)
|
||||
Fasp::Subscription.includes(:fasp_provider).category_account.lifecycle.each do |subscription|
|
||||
announce(subscription, uri, event_type)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def announce(subscription, uri, event_type)
|
||||
Fasp::Request.new(subscription.fasp_provider).post('/data_sharing/v0/announcements', body: {
|
||||
source: {
|
||||
subscription: {
|
||||
id: subscription.id.to_s,
|
||||
},
|
||||
},
|
||||
category: 'account',
|
||||
eventType: event_type,
|
||||
objectUris: [uri],
|
||||
})
|
||||
end
|
||||
end
|
28
app/workers/fasp/announce_content_lifecycle_event_worker.rb
Normal file
28
app/workers/fasp/announce_content_lifecycle_event_worker.rb
Normal file
|
@ -0,0 +1,28 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class Fasp::AnnounceContentLifecycleEventWorker
|
||||
include Sidekiq::Worker
|
||||
|
||||
sidekiq_options queue: 'fasp', retry: 5
|
||||
|
||||
def perform(uri, event_type)
|
||||
Fasp::Subscription.includes(:fasp_provider).category_content.lifecycle.each do |subscription|
|
||||
announce(subscription, uri, event_type)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def announce(subscription, uri, event_type)
|
||||
Fasp::Request.new(subscription.fasp_provider).post('/data_sharing/v0/announcements', body: {
|
||||
source: {
|
||||
subscription: {
|
||||
id: subscription.id.to_s,
|
||||
},
|
||||
},
|
||||
category: 'content',
|
||||
eventType: event_type,
|
||||
objectUris: [uri],
|
||||
})
|
||||
end
|
||||
end
|
61
app/workers/fasp/announce_trend_worker.rb
Normal file
61
app/workers/fasp/announce_trend_worker.rb
Normal file
|
@ -0,0 +1,61 @@
|
|||
# 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).category_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
|
32
app/workers/fasp/backfill_worker.rb
Normal file
32
app/workers/fasp/backfill_worker.rb
Normal file
|
@ -0,0 +1,32 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class Fasp::BackfillWorker
|
||||
include Sidekiq::Worker
|
||||
|
||||
sidekiq_options queue: 'fasp', retry: 5
|
||||
|
||||
def perform(backfill_request_id)
|
||||
backfill_request = Fasp::BackfillRequest.find(backfill_request_id)
|
||||
|
||||
announce(backfill_request)
|
||||
|
||||
backfill_request.advance!
|
||||
rescue ActiveRecord::RecordNotFound
|
||||
# ignore missing backfill requests
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def announce(backfill_request)
|
||||
Fasp::Request.new(backfill_request.fasp_provider).post('/data_sharing/v0/announcements', body: {
|
||||
source: {
|
||||
backfillRequest: {
|
||||
id: backfill_request.id.to_s,
|
||||
},
|
||||
},
|
||||
category: backfill_request.category,
|
||||
objectUris: backfill_request.next_uris,
|
||||
moreObjectsAvailable: backfill_request.more_objects_available?,
|
||||
})
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue