You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

34 lines
1.0 KiB

  1. # frozen_string_literal: true
  2. class UnfavouriteService < BaseService
  3. def call(account, status)
  4. favourite = Favourite.find_by!(account: account, status: status)
  5. favourite.destroy!
  6. create_notification(favourite) unless status.local?
  7. favourite
  8. end
  9. private
  10. def create_notification(favourite)
  11. status = favourite.status
  12. if status.account.ostatus?
  13. NotificationWorker.perform_async(build_xml(favourite), favourite.account_id, status.account_id)
  14. elsif status.account.activitypub?
  15. ActivityPub::DeliveryWorker.perform_async(build_json(favourite), favourite.account_id, status.account.inbox_url)
  16. end
  17. end
  18. def build_json(favourite)
  19. Oj.dump(ActivityPub::LinkedDataSignature.new(ActiveModelSerializers::SerializableResource.new(
  20. favourite,
  21. serializer: ActivityPub::UndoLikeSerializer,
  22. adapter: ActivityPub::Adapter
  23. ).as_json).sign!(favourite.account))
  24. end
  25. def build_xml(favourite)
  26. OStatus::AtomSerializer.render(OStatus::AtomSerializer.new.unfavourite_salmon(favourite))
  27. end
  28. end