block_service.rb 1.25 KB
Newer Older
1 2
# frozen_string_literal: true

3
class BlockService < BaseService
4 5
  include StreamEntryRenderer

6 7 8 9
  def call(account, target_account)
    return if account.id == target_account.id

    UnfollowService.new.call(account, target_account) if account.following?(target_account)
10 11
    UnfollowService.new.call(target_account, account) if target_account.following?(account)

12
    block = account.block!(target_account)
13

14
    BlockWorker.perform_async(account.id, target_account.id)
15 16
    create_notification(block) unless target_account.local?
    block
17 18 19 20
  end

  private

21 22 23 24 25 26 27 28 29
  def create_notification(block)
    if block.target_account.ostatus?
      NotificationWorker.perform_async(build_xml(block), block.account_id, block.target_account_id)
    elsif block.target_account.activitypub?
      ActivityPub::DeliveryWorker.perform_async(build_json(block), block.account_id, block.target_account.inbox_url)
    end
  end

  def build_json(block)
30
    Oj.dump(ActivityPub::LinkedDataSignature.new(ActiveModelSerializers::SerializableResource.new(
31 32 33
      block,
      serializer: ActivityPub::BlockSerializer,
      adapter: ActivityPub::Adapter
34
    ).as_json).sign!(block.account))
35 36
  end

37
  def build_xml(block)
38
    OStatus::AtomSerializer.render(OStatus::AtomSerializer.new.block_salmon(block))
39 40
  end
end