闭社主体 forked from https://github.com/tootsuite/mastodon
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.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. 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.activitypub?
  24. ActivityPub::DeliveryWorker.perform_async(build_json(favourite), favourite.account_id, status.account.inbox_url)
  25. end
  26. end
  27. def bump_potential_friendship(account, status)
  28. ActivityTracker.increment('activity:interactions')
  29. return if account.following?(status.account_id)
  30. PotentialFriendshipTracker.record(account.id, status.account_id, :favourite)
  31. end
  32. def build_json(favourite)
  33. Oj.dump(serialize_payload(favourite, ActivityPub::LikeSerializer))
  34. end
  35. end