2017-08-09 04:52:15 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class ActivityPub::ProcessCollectionService < BaseService
|
|
|
|
include JsonLdHelper
|
|
|
|
|
2017-12-06 19:41:57 +09:00
|
|
|
def call(body, account, **options)
|
2017-08-09 04:52:15 +09:00
|
|
|
@account = account
|
|
|
|
@json = Oj.load(body, mode: :strict)
|
2017-10-09 00:34:34 +09:00
|
|
|
@options = options
|
2017-08-09 04:52:15 +09:00
|
|
|
|
2022-01-22 22:48:27 +09:00
|
|
|
@json = compact(@json) if @json['signature'].is_a?(Hash)
|
|
|
|
|
2020-11-08 08:28:39 +09:00
|
|
|
return if !supported_context? || (different_actor? && verify_account!.nil?) || suspended_actor? || @account.local?
|
2017-08-26 20:47:38 +09:00
|
|
|
|
2017-08-09 04:52:15 +09: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 20:47:38 +09:00
|
|
|
def different_actor?
|
2018-12-30 17:48:59 +09:00
|
|
|
@json['actor'].present? && value_or_id(@json['actor']) != @account.uri
|
2017-08-26 20:47:38 +09:00
|
|
|
end
|
|
|
|
|
2020-11-08 08:28:39 +09:00
|
|
|
def suspended_actor?
|
|
|
|
@account.suspended? && !activity_allowed_while_suspended?
|
|
|
|
end
|
|
|
|
|
|
|
|
def activity_allowed_while_suspended?
|
|
|
|
%w(Delete Reject Undo Update).include?(@json['type'])
|
|
|
|
end
|
|
|
|
|
2017-08-09 04:52:15 +09:00
|
|
|
def process_items(items)
|
2021-01-10 08:32:01 +09:00
|
|
|
items.reverse_each.filter_map { |item| process_item(item) }
|
2017-08-09 04:52:15 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
def supported_context?
|
|
|
|
super(@json)
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_item(item)
|
2020-01-11 05:57:05 +09:00
|
|
|
activity = ActivityPub::Activity.factory(item, @account, **@options)
|
2017-08-09 04:52:15 +09:00
|
|
|
activity&.perform
|
|
|
|
end
|
2017-08-26 20:47:38 +09:00
|
|
|
|
|
|
|
def verify_account!
|
2019-02-14 02:42:47 +09:00
|
|
|
@options[:relayed_through_account] = @account
|
2017-09-01 00:18:49 +09:00
|
|
|
@account = ActivityPub::LinkedDataSignature.new(@json).verify_account!
|
2018-05-06 01:22:34 +09: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 20:47:38 +09:00
|
|
|
end
|
2017-08-09 04:52:15 +09:00
|
|
|
end
|