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.

49 lines
1.3 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. Trends.statuses.register(status)
  15. create_notification(favourite)
  16. bump_potential_friendship(account, status)
  17. favourite
  18. end
  19. private
  20. def create_notification(favourite)
  21. status = favourite.status
  22. if status.account.local?
  23. LocalNotificationWorker.perform_async(status.account_id, favourite.id, 'Favourite', 'favourite')
  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(serialize_payload(favourite, ActivityPub::LikeSerializer))
  35. end
  36. end