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.

65 lines
2.0 KiB

  1. # frozen_string_literal: true
  2. class UnfollowService < BaseService
  3. include Payloadable
  4. # Unfollow and notify the remote user
  5. # @param [Account] source_account Where to unfollow from
  6. # @param [Account] target_account Which to unfollow
  7. def call(source_account, target_account)
  8. @source_account = source_account
  9. @target_account = target_account
  10. unfollow! || undo_follow_request!
  11. end
  12. private
  13. def unfollow!
  14. follow = Follow.find_by(account: @source_account, target_account: @target_account)
  15. return unless follow
  16. follow.destroy!
  17. create_notification(follow) unless @target_account.local?
  18. create_reject_notification(follow) if @target_account.local? && !@source_account.local?
  19. UnmergeWorker.perform_async(@target_account.id, @source_account.id)
  20. follow
  21. end
  22. def undo_follow_request!
  23. follow_request = FollowRequest.find_by(account: @source_account, target_account: @target_account)
  24. return unless follow_request
  25. follow_request.destroy!
  26. create_notification(follow_request) unless @target_account.local?
  27. follow_request
  28. end
  29. def create_notification(follow)
  30. if follow.target_account.ostatus?
  31. NotificationWorker.perform_async(build_xml(follow), follow.account_id, follow.target_account_id)
  32. elsif follow.target_account.activitypub?
  33. ActivityPub::DeliveryWorker.perform_async(build_json(follow), follow.account_id, follow.target_account.inbox_url)
  34. end
  35. end
  36. def create_reject_notification(follow)
  37. # Rejecting an already-existing follow request
  38. return unless follow.account.activitypub?
  39. ActivityPub::DeliveryWorker.perform_async(build_reject_json(follow), follow.target_account_id, follow.account.inbox_url)
  40. end
  41. def build_json(follow)
  42. Oj.dump(serialize_payload(follow, ActivityPub::UndoFollowSerializer))
  43. end
  44. def build_reject_json(follow)
  45. Oj.dump(serialize_payload(follow, ActivityPub::RejectFollowSerializer))
  46. end
  47. def build_xml(follow)
  48. OStatus::AtomSerializer.render(OStatus::AtomSerializer.new.unfollow_salmon(follow))
  49. end
  50. end