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.

53 lines
1.5 KiB

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