unfollow_service.rb 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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) if !@target_account.local? && @target_account.activitypub?
  18. create_reject_notification(follow) if @target_account.local? && !@source_account.local? && @source_account.activitypub?
  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. ActivityPub::DeliveryWorker.perform_async(build_json(follow), follow.account_id, follow.target_account.inbox_url)
  31. end
  32. def create_reject_notification(follow)
  33. ActivityPub::DeliveryWorker.perform_async(build_reject_json(follow), follow.target_account_id, follow.account.inbox_url)
  34. end
  35. def build_json(follow)
  36. Oj.dump(serialize_payload(follow, ActivityPub::UndoFollowSerializer))
  37. end
  38. def build_reject_json(follow)
  39. Oj.dump(serialize_payload(follow, ActivityPub::RejectFollowSerializer))
  40. end
  41. end