1
0

favourite_service.rb 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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, :show?
  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