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. # Favourite a status and notify remote user
  4. # @param [Account] account
  5. # @param [Status] status
  6. # @return [Favourite]
  7. def call(account, status)
  8. raise Mastodon::NotPermittedError unless status.permitted?(account)
  9. favourite = Favourite.create!(account: account, status: status)
  10. if status.local?
  11. NotifyService.new.call(favourite.status.account, favourite)
  12. else
  13. NotificationWorker.perform_async(build_xml(favourite), account.id, status.account_id)
  14. end
  15. favourite
  16. end
  17. private
  18. def build_xml(favourite)
  19. description = "#{favourite.account.acct} favourited a status by #{favourite.status.account.acct}"
  20. Nokogiri::XML::Builder.new do |xml|
  21. entry(xml, true) do
  22. unique_id xml, favourite.created_at, favourite.id, 'Favourite'
  23. title xml, description
  24. content xml, description
  25. author(xml) do
  26. include_author xml, favourite.account
  27. end
  28. object_type xml, :activity
  29. verb xml, :favorite
  30. in_reply_to xml, TagManager.instance.uri_for(favourite.status), TagManager.instance.url_for(favourite.status)
  31. target(xml) do
  32. include_target xml, favourite.status
  33. end
  34. end
  35. end.to_xml
  36. end
  37. end