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.

47 lines
1.4 KiB

  1. # frozen_string_literal: true
  2. class FavouriteService < BaseService
  3. include Authorization
  4. # Favourite a status and notify remote user
  5. # @param [Account] account
  6. # @param [Status] status
  7. # @return [Favourite]
  8. def call(account, status)
  9. authorize_with account, status, :favourite?
  10. favourite = Favourite.find_by(account: account, status: status)
  11. return favourite unless favourite.nil?
  12. favourite = Favourite.create!(account: account, status: status)
  13. create_notification(favourite)
  14. favourite
  15. end
  16. private
  17. def create_notification(favourite)
  18. status = favourite.status
  19. if status.account.local?
  20. NotifyService.new.call(status.account, favourite)
  21. elsif status.account.ostatus?
  22. NotificationWorker.perform_async(build_xml(favourite), favourite.account_id, status.account_id)
  23. elsif status.account.activitypub?
  24. ActivityPub::DeliveryWorker.perform_async(build_json(favourite), favourite.account_id, status.account.inbox_url)
  25. end
  26. end
  27. def build_json(favourite)
  28. Oj.dump(ActivityPub::LinkedDataSignature.new(ActiveModelSerializers::SerializableResource.new(
  29. favourite,
  30. serializer: ActivityPub::LikeSerializer,
  31. adapter: ActivityPub::Adapter
  32. ).as_json).sign!(favourite.account))
  33. end
  34. def build_xml(favourite)
  35. OStatus::AtomSerializer.render(OStatus::AtomSerializer.new.favourite_salmon(favourite))
  36. end
  37. end