2017-08-09 05:52:15 +10:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class ActivityPub::ProcessCollectionService < BaseService
|
|
|
|
include JsonLdHelper
|
|
|
|
|
2017-10-09 02:34:34 +11:00
|
|
|
def call(body, account, options = {})
|
2017-08-09 05:52:15 +10:00
|
|
|
@account = account
|
|
|
|
@json = Oj.load(body, mode: :strict)
|
2017-10-09 02:34:34 +11:00
|
|
|
@options = options
|
2017-08-09 05:52:15 +10:00
|
|
|
|
2017-09-13 19:05:02 +10:00
|
|
|
return unless supported_context?
|
2017-09-01 01:18:49 +10:00
|
|
|
return if different_actor? && verify_account!.nil?
|
2017-09-26 09:06:13 +10:00
|
|
|
return if @account.suspended? || @account.local?
|
2017-08-26 21:47:38 +10:00
|
|
|
|
2017-08-09 05:52:15 +10: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 21:47:38 +10:00
|
|
|
def different_actor?
|
|
|
|
@json['actor'].present? && value_or_id(@json['actor']) != @account.uri && @json['signature'].present?
|
|
|
|
end
|
|
|
|
|
2017-08-09 05:52:15 +10: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)
|
2017-10-09 02:34:34 +11:00
|
|
|
activity = ActivityPub::Activity.factory(item, @account, @options)
|
2017-08-09 05:52:15 +10:00
|
|
|
activity&.perform
|
|
|
|
end
|
2017-08-26 21:47:38 +10:00
|
|
|
|
|
|
|
def verify_account!
|
2017-09-01 01:18:49 +10:00
|
|
|
@account = ActivityPub::LinkedDataSignature.new(@json).verify_account!
|
2017-08-26 21:47:38 +10:00
|
|
|
end
|
2017-08-09 05:52:15 +10:00
|
|
|
end
|