mirror of https://github.com/mastodon/mastodon
Implement backfill requests
parent
8af55c1a34
commit
2b46f1fc3f
@ -1,4 +1,26 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class Api::Fasp::DataSharing::V0::BackfillRequestsController < ApplicationController
|
||||
class Api::Fasp::DataSharing::V0::BackfillRequestsController < Api::Fasp::BaseController
|
||||
def create
|
||||
backfill_request = current_provider.fasp_backfill_requests.new(backfill_request_params)
|
||||
|
||||
respond_to do |format|
|
||||
format.json do
|
||||
if backfill_request.save
|
||||
render json: { backfillRequest: { id: backfill_request.id } }, status: 201
|
||||
else
|
||||
head 422
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def backfill_request_params
|
||||
params
|
||||
.permit(:category, :maxCount)
|
||||
.to_unsafe_h
|
||||
.transform_keys { |k| k.to_s.underscore }
|
||||
end
|
||||
end
|
||||
|
@ -0,0 +1,10 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class Api::Fasp::DataSharing::V0::ContinuationsController < Api::Fasp::BaseController
|
||||
def create
|
||||
backfill_request = current_provider.fasp_backfill_requests.find(params[:backfill_request_id])
|
||||
Fasp::BackfillWorker.perform_async(backfill_request.id)
|
||||
|
||||
head 204
|
||||
end
|
||||
end
|
@ -0,0 +1,67 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# == Schema Information
|
||||
#
|
||||
# Table name: fasp_backfill_requests
|
||||
#
|
||||
# id :bigint(8) not null, primary key
|
||||
# category :string not null
|
||||
# cursor :string
|
||||
# fulfilled :boolean default(FALSE), not null
|
||||
# max_count :integer default(100), not null
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
# fasp_provider_id :bigint(8) not null
|
||||
#
|
||||
class Fasp::BackfillRequest < ApplicationRecord
|
||||
belongs_to :fasp_provider, class_name: 'Fasp::Provider'
|
||||
|
||||
validates :category, presence: true, inclusion: Fasp::DATA_CATEGORIES
|
||||
validates :max_count, presence: true,
|
||||
numericality: { only_integer: true }
|
||||
|
||||
after_commit :queue_fulfillment_job, on: :create
|
||||
|
||||
def next_objects
|
||||
@next_objects ||= base_scope.to_a
|
||||
end
|
||||
|
||||
def next_uris
|
||||
next_objects.map { |o| ActivityPub::TagManager.instance.uri_for(o) }
|
||||
end
|
||||
|
||||
def more_objects_available?
|
||||
return false if next_objects.empty?
|
||||
|
||||
base_scope.where(id: ...(next_objects.last.id)).any?
|
||||
end
|
||||
|
||||
def advance!
|
||||
if more_objects_available?
|
||||
update!(cursor: next_objects.last.id)
|
||||
else
|
||||
update!(fulfilled: true)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def base_scope
|
||||
result = category_scope.limit(max_count).order(id: :desc)
|
||||
result = result.where(id: ...cursor) if cursor.present?
|
||||
result
|
||||
end
|
||||
|
||||
def category_scope
|
||||
case category
|
||||
when 'account'
|
||||
Account.discoverable.without_instance_actor
|
||||
when 'content'
|
||||
Status.indexable
|
||||
end
|
||||
end
|
||||
|
||||
def queue_fulfillment_job
|
||||
Fasp::BackfillWorker.perform_async(id)
|
||||
end
|
||||
end
|
@ -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
|
@ -0,0 +1,15 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class CreateFaspBackfillRequests < ActiveRecord::Migration[7.2]
|
||||
def change
|
||||
create_table :fasp_backfill_requests do |t|
|
||||
t.string :category, null: false
|
||||
t.integer :max_count, null: false, default: 100
|
||||
t.string :cursor
|
||||
t.boolean :fulfilled, null: false, default: false
|
||||
t.references :fasp_provider, null: false, foreign_key: true
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
end
|
||||
end
|
@ -0,0 +1,9 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
Fabricator('Fasp::BackfillRequest') do
|
||||
category 'MyString'
|
||||
max_count 1
|
||||
cursor 'MyString'
|
||||
fulfilled false
|
||||
fasp_provider nil
|
||||
end
|
@ -0,0 +1,7 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Fasp::BackfillRequest do
|
||||
pending "add some examples to (or delete) #{__FILE__}"
|
||||
end
|
@ -0,0 +1,9 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe 'Api::Fasp::DataSharing::V0::Continuations' do
|
||||
describe 'GET /index' do
|
||||
pending "add some examples (or delete) #{__FILE__}"
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue