unfollow_service.rb 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. # @param [Hash] options
  8. # @option [Boolean] :skip_unmerge
  9. def call(source_account, target_account, options = {})
  10. @source_account = source_account
  11. @target_account = target_account
  12. @options = options
  13. unfollow! || undo_follow_request!
  14. end
  15. private
  16. def unfollow!
  17. follow = Follow.find_by(account: @source_account, target_account: @target_account)
  18. return unless follow
  19. follow.destroy!
  20. create_notification(follow) if !@target_account.local? && @target_account.activitypub?
  21. create_reject_notification(follow) if @target_account.local? && !@source_account.local? && @source_account.activitypub?
  22. UnmergeWorker.perform_async(@target_account.id, @source_account.id) unless @options[:skip_unmerge]
  23. follow
  24. end
  25. def undo_follow_request!
  26. follow_request = FollowRequest.find_by(account: @source_account, target_account: @target_account)
  27. return unless follow_request
  28. follow_request.destroy!
  29. create_notification(follow_request) unless @target_account.local?
  30. follow_request
  31. end
  32. def create_notification(follow)
  33. ActivityPub::DeliveryWorker.perform_async(build_json(follow), follow.account_id, follow.target_account.inbox_url)
  34. end
  35. def create_reject_notification(follow)
  36. ActivityPub::DeliveryWorker.perform_async(build_reject_json(follow), follow.target_account_id, follow.account.inbox_url)
  37. end
  38. def build_json(follow)
  39. Oj.dump(serialize_payload(follow, ActivityPub::UndoFollowSerializer))
  40. end
  41. def build_reject_json(follow)
  42. Oj.dump(serialize_payload(follow, ActivityPub::RejectFollowSerializer))
  43. end
  44. end