favourite_service.rb 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. increment_statistics
  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 increment_statistics
  29. ActivityTracker.increment('activity:interactions')
  30. end
  31. def build_json(favourite)
  32. Oj.dump(serialize_payload(favourite, ActivityPub::LikeSerializer))
  33. end
  34. end