unfavourite_service.rb 1.04 KB
Newer Older
1 2
# frozen_string_literal: true

3 4 5 6
class UnfavouriteService < BaseService
  def call(account, status)
    favourite = Favourite.find_by!(account: account, status: status)
    favourite.destroy!
7
    create_notification(favourite) unless status.local?
8 9
    favourite
  end
10 11 12

  private

13 14 15 16 17 18 19 20 21 22 23
  def create_notification(favourite)
    status = favourite.status

    if status.account.ostatus?
      NotificationWorker.perform_async(build_xml(favourite), favourite.account_id, status.account_id)
    elsif status.account.activitypub?
      ActivityPub::DeliveryWorker.perform_async(build_json(favourite), favourite.account_id, status.account.inbox_url)
    end
  end

  def build_json(favourite)
24
    Oj.dump(ActivityPub::LinkedDataSignature.new(ActiveModelSerializers::SerializableResource.new(
25 26 27
      favourite,
      serializer: ActivityPub::UndoLikeSerializer,
      adapter: ActivityPub::Adapter
28
    ).as_json).sign!(favourite.account))
29 30
  end

31
  def build_xml(favourite)
32
    OStatus::AtomSerializer.render(OStatus::AtomSerializer.new.unfavourite_salmon(favourite))
33
  end
34
end