2017-08-08 20:52:15 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class ActivityPub::ProcessCollectionService < BaseService
|
|
|
|
include JsonLdHelper
|
|
|
|
|
2017-12-06 10:41:57 +00:00
|
|
|
def call(body, account, **options)
|
2017-08-08 20:52:15 +01:00
|
|
|
@account = account
|
|
|
|
@json = Oj.load(body, mode: :strict)
|
2017-10-08 16:34:34 +01:00
|
|
|
@options = options
|
2017-08-08 20:52:15 +01:00
|
|
|
|
2019-07-09 02:27:35 +01:00
|
|
|
return if !supported_context? || (different_actor? && verify_account!.nil?) || @account.suspended? || @account.local?
|
2017-08-26 12:47:38 +01:00
|
|
|
|
2017-08-08 20:52:15 +01: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 12:47:38 +01:00
|
|
|
def different_actor?
|
2018-12-30 08:48:59 +00:00
|
|
|
@json['actor'].present? && value_or_id(@json['actor']) != @account.uri
|
2017-08-26 12:47:38 +01:00
|
|
|
end
|
|
|
|
|
2017-08-08 20:52:15 +01: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)
|
2020-01-10 20:57:05 +00:00
|
|
|
activity = ActivityPub::Activity.factory(item, @account, **@options)
|
2017-08-08 20:52:15 +01:00
|
|
|
activity&.perform
|
|
|
|
end
|
2017-08-26 12:47:38 +01:00
|
|
|
|
|
|
|
def verify_account!
|
2019-02-13 17:42:47 +00:00
|
|
|
@options[:relayed_through_account] = @account
|
2017-08-31 16:18:49 +01:00
|
|
|
@account = ActivityPub::LinkedDataSignature.new(@json).verify_account!
|
2018-05-05 17:22:34 +01:00
|
|
|
rescue JSON::LD::JsonLdError => e
|
|
|
|
Rails.logger.debug "Could not verify LD-Signature for #{value_or_id(@json['actor'])}: #{e.message}"
|
|
|
|
nil
|
2017-08-26 12:47:38 +01:00
|
|
|
end
|
2017-08-08 20:52:15 +01:00
|
|
|
end
|