2017-08-08 12:52:15 -07:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class ActivityPub::ProcessCollectionService < BaseService
|
|
|
|
include JsonLdHelper
|
|
|
|
|
|
|
|
def call(body, account)
|
|
|
|
@account = account
|
|
|
|
@json = Oj.load(body, mode: :strict)
|
|
|
|
|
|
|
|
return if @account.suspended? || !supported_context?
|
|
|
|
|
2017-08-26 04:47:38 -07:00
|
|
|
verify_account! if different_actor?
|
|
|
|
|
2017-08-08 12:52:15 -07:00
|
|
|
case @json['type']
|
|
|
|
when 'Collection', 'CollectionPage'
|
|
|
|
process_items @json['items']
|
|
|
|
when 'OrderedCollection', 'OrderedCollectionPage'
|
|
|
|
process_items @json['orderedItems']
|
|
|
|
else
|
|
|
|
process_items [@json]
|
|
|
|
end
|
|
|
|
rescue Oj::ParseError
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-08-26 04:47:38 -07:00
|
|
|
def different_actor?
|
|
|
|
@json['actor'].present? && value_or_id(@json['actor']) != @account.uri && @json['signature'].present?
|
|
|
|
end
|
|
|
|
|
2017-08-08 12:52:15 -07:00
|
|
|
def process_items(items)
|
|
|
|
items.reverse_each.map { |item| process_item(item) }.compact
|
|
|
|
end
|
|
|
|
|
|
|
|
def supported_context?
|
|
|
|
super(@json)
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_item(item)
|
|
|
|
activity = ActivityPub::Activity.factory(item, @account)
|
|
|
|
activity&.perform
|
|
|
|
end
|
2017-08-26 04:47:38 -07:00
|
|
|
|
|
|
|
def verify_account!
|
|
|
|
account = ActivityPub::LinkedDataSignature.new(@json).verify_account!
|
|
|
|
@account = account unless account.nil?
|
|
|
|
end
|
2017-08-08 12:52:15 -07:00
|
|
|
end
|