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.

56 lines
1.7 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. bump_potential_friendship(account, status)
  15. favourite
  16. end
  17. private
  18. def create_notification(favourite)
  19. status = favourite.status
  20. if status.account.local?
  21. NotifyService.new.call(status.account, favourite)
  22. elsif status.account.ostatus?
  23. NotificationWorker.perform_async(build_xml(favourite), favourite.account_id, status.account_id)
  24. elsif status.account.activitypub?
  25. ActivityPub::DeliveryWorker.perform_async(build_json(favourite), favourite.account_id, status.account.inbox_url)
  26. end
  27. end
  28. def bump_potential_friendship(account, status)
  29. ActivityTracker.increment('activity:interactions')
  30. return if account.following?(status.account_id)
  31. PotentialFriendshipTracker.record(account.id, status.account_id, :favourite)
  32. end
  33. def build_json(favourite)
  34. Oj.dump(ActivityPub::LinkedDataSignature.new(ActiveModelSerializers::SerializableResource.new(
  35. favourite,
  36. serializer: ActivityPub::LikeSerializer,
  37. adapter: ActivityPub::Adapter
  38. ).as_json).sign!(favourite.account))
  39. end
  40. def build_xml(favourite)
  41. OStatus::AtomSerializer.render(OStatus::AtomSerializer.new.favourite_salmon(favourite))
  42. end
  43. end