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.

42 lines
1.0 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::NotPermitted 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. Nokogiri::XML::Builder.new do |xml|
  20. entry(xml, true) do
  21. title xml, "#{favourite.account.acct} favourited a status by #{favourite.status.account.acct}"
  22. author(xml) do
  23. include_author xml, favourite.account
  24. end
  25. object_type xml, :activity
  26. verb xml, :favorite
  27. target(xml) do
  28. include_target xml, favourite.status
  29. end
  30. end
  31. end.to_xml
  32. end
  33. end