Humble beginnings of fasp data sharing.

Subscriptions can be made and new statuses will be announced to
subscribed fasp.
feat/fasp
David Roetzel 2024-12-16 10:50:01 +01:00
parent 48a85ba15a
commit 1927c880ee
No known key found for this signature in database
16 changed files with 195 additions and 0 deletions

View File

@ -0,0 +1,4 @@
# frozen_string_literal: true
class Api::Fasp::DataSharing::V0::BackfillRequestsController < ApplicationController
end

View File

@ -0,0 +1,25 @@
# frozen_string_literal: true
class Api::Fasp::DataSharing::V0::EventSubscriptionsController < Api::Fasp::BaseController
def create
subscription = current_provider.fasp_subscriptions.create!(subscription_params)
render json: { subscription: { id: subscription.id } }, status: 201
end
def destroy
subscription = current_provider.fasp_subscriptions.find(params[:id])
subscription.destroy
head 204
end
private
def subscription_params
params
.permit(:category, :subscriptionType, :maxBatchSize, threshold: {})
.to_unsafe_h
.transform_keys { |k| k.to_s.underscore }
end
end

View File

@ -26,6 +26,7 @@ class Fasp::Request
def headers(verb, url, body = '')
result = {
'accept' => 'application/json',
'content-type' => 'application/json',
'content-digest' => content_digest(body),
}
result.merge(signature_headers(verb, url, result))

View File

@ -0,0 +1,16 @@
# frozen_string_literal: true
module Status::FaspConcern
extend ActiveSupport::Concern
included do
after_commit :announce_new_content_to_subscribed_fasp, on: :create
end
private
def announce_new_content_to_subscribed_fasp
store_uri unless uri # TODO: solve this more elegantly
Fasp::AnnounceNewContentWorker.perform_async(uri)
end
end

View File

@ -23,6 +23,7 @@ class Fasp::Provider < ApplicationRecord
include DebugConcern
has_many :fasp_debug_callbacks, inverse_of: :fasp_provider, class_name: 'Fasp::DebugCallback', dependent: :delete_all
has_many :fasp_subscriptions, inverse_of: :fasp_provider, class_name: 'Fasp::Subscription', dependent: :delete_all
before_create :create_keypair

View File

@ -0,0 +1,40 @@
# frozen_string_literal: true
# == Schema Information
#
# Table name: fasp_subscriptions
#
# id :bigint(8) not null, primary key
# category :string not null
# max_batch_size :integer not null
# subscription_type :string not null
# threshold_likes :integer
# threshold_replies :integer
# threshold_shares :integer
# threshold_timeframe :integer
# created_at :datetime not null
# updated_at :datetime not null
# fasp_provider_id :bigint(8) not null
#
class Fasp::Subscription < ApplicationRecord
CATEGORIES = %w(account content).freeze
TYPES = %w(lifecycle trends).freeze
belongs_to :fasp_provider, class_name: 'Fasp::Provider'
validates :category, presence: true, inclusion: CATEGORIES
validates :subscription_type, presence: true,
inclusion: TYPES
scope :content, -> { where(category: 'content') }
scope :account, -> { where(category: 'account') }
scope :lifecycle, -> { where(subscription_type: 'lifecycle') }
scope :trends, -> { where(subscription_type: 'trends') }
def threshold=(threshold)
self.threshold_timeframe = threshold['timeframe'] || 15
self.threshold_shares = threshold['shares'] || 3
self.threshold_likes = threshold['likes'] || 3
self.threshold_replies = threshold['replies'] || 3
end
end

View File

@ -34,6 +34,7 @@ class Status < ApplicationRecord
include Discard::Model
include Paginable
include RateLimitable
include Status::FaspConcern
include Status::SafeReblogInsert
include Status::SearchConcern
include Status::SnapshotConcern

View File

@ -0,0 +1,28 @@
# frozen_string_literal: true
class Fasp::AnnounceNewContentWorker
include Sidekiq::Worker
sidekiq_options queue: 'fasp', retry: 5
def perform(uri)
Fasp::Subscription.includes(:fasp_provider).content.lifecycle.each do |subscription|
announce(subscription, uri)
end
end
private
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: 'new',
objectUris: [uri],
})
end
end

View File

@ -10,6 +10,14 @@ namespace :api, format: false do
end
end
namespace :data_sharing do
namespace :v0 do
resources :backfill_requests, only: [:create]
resources :event_subscriptions, only: [:create, :destroy]
end
end
resource :registration, only: [:create]
end
end

View File

@ -7,6 +7,7 @@
- [mailers, 2]
- [pull]
- [scheduler]
- [fasp]
:scheduler:
:listened_queues_only: true

View File

@ -0,0 +1,18 @@
# frozen_string_literal: true
class CreateFaspSubscriptions < ActiveRecord::Migration[7.2]
def change
create_table :fasp_subscriptions do |t|
t.string :category, null: false
t.string :subscription_type, null: false
t.integer :max_batch_size, null: false
t.integer :threshold_timeframe
t.integer :threshold_shares
t.integer :threshold_likes
t.integer :threshold_replies
t.references :fasp_provider, null: false, foreign_key: true
t.timestamps
end
end
end

View File

@ -470,6 +470,20 @@ ActiveRecord::Schema[7.2].define(version: 2024_12_13_170053) do
t.index ["base_url"], name: "index_fasp_providers_on_base_url", unique: true
end
create_table "fasp_subscriptions", force: :cascade do |t|
t.string "category", null: false
t.string "subscription_type", null: false
t.integer "max_batch_size", null: false
t.integer "threshold_timeframe"
t.integer "threshold_shares"
t.integer "threshold_likes"
t.integer "threshold_replies"
t.bigint "fasp_provider_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["fasp_provider_id"], name: "index_fasp_subscriptions_on_fasp_provider_id"
end
create_table "favourites", force: :cascade do |t|
t.datetime "created_at", precision: nil, null: false
t.datetime "updated_at", precision: nil, null: false
@ -1310,6 +1324,7 @@ ActiveRecord::Schema[7.2].define(version: 2024_12_13_170053) do
add_foreign_key "custom_filters", "accounts", on_delete: :cascade
add_foreign_key "email_domain_blocks", "email_domain_blocks", column: "parent_id", on_delete: :cascade
add_foreign_key "fasp_debug_callbacks", "fasp_providers"
add_foreign_key "fasp_subscriptions", "fasp_providers"
add_foreign_key "favourites", "accounts", name: "fk_5eb6c2b873", on_delete: :cascade
add_foreign_key "favourites", "statuses", name: "fk_b0e856845e", on_delete: :cascade
add_foreign_key "featured_tags", "accounts", on_delete: :cascade

View File

@ -0,0 +1,12 @@
# frozen_string_literal: true
Fabricator('Fasp::Subscription') do
category 'MyString'
subscription_type 'MyString'
max_batch_size 1
threshold_timeframe 1
threshold_shares 1
threshold_likes 1
threshold_replies 1
fasp_provider nil
end

View File

@ -0,0 +1,7 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe Fasp::Subscription do
pending "add some examples to (or delete) #{__FILE__}"
end

View File

@ -0,0 +1,9 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe 'Api::Fasp::DataSharing::V0::BackfillRequests' do
describe 'GET /index' do
pending "add some examples (or delete) #{__FILE__}"
end
end

View File

@ -0,0 +1,9 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe 'Api::Fasp::DataSharing::V0::EventSubscriptions' do
describe 'GET /index' do
pending "add some examples (or delete) #{__FILE__}"
end
end