unfollow_service.rb 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # frozen_string_literal: true
  2. class UnfollowService < BaseService
  3. include Payloadable
  4. include Redisable
  5. include Lockable
  6. # Unfollow and notify the remote user
  7. # @param [Account] source_account Where to unfollow from
  8. # @param [Account] target_account Which to unfollow
  9. # @param [Hash] options
  10. # @option [Boolean] :skip_unmerge
  11. def call(source_account, target_account, options = {})
  12. @source_account = source_account
  13. @target_account = target_account
  14. @options = options
  15. with_redis_lock("relationship:#{[source_account.id, target_account.id].sort.join(':')}") do
  16. unfollow! || undo_follow_request!
  17. end
  18. end
  19. private
  20. def unfollow!
  21. follow = Follow.find_by(account: @source_account, target_account: @target_account)
  22. return unless follow
  23. follow.destroy!
  24. create_notification(follow) if !@target_account.local? && @target_account.activitypub?
  25. create_reject_notification(follow) if @target_account.local? && !@source_account.local? && @source_account.activitypub?
  26. UnmergeWorker.perform_async(@target_account.id, @source_account.id) unless @options[:skip_unmerge]
  27. follow
  28. end
  29. def undo_follow_request!
  30. follow_request = FollowRequest.find_by(account: @source_account, target_account: @target_account)
  31. return unless follow_request
  32. follow_request.destroy!
  33. create_notification(follow_request) unless @target_account.local?
  34. follow_request
  35. end
  36. def create_notification(follow)
  37. ActivityPub::DeliveryWorker.perform_async(build_json(follow), follow.account_id, follow.target_account.inbox_url)
  38. end
  39. def create_reject_notification(follow)
  40. ActivityPub::DeliveryWorker.perform_async(build_reject_json(follow), follow.target_account_id, follow.account.inbox_url)
  41. end
  42. def build_json(follow)
  43. Oj.dump(serialize_payload(follow, ActivityPub::UndoFollowSerializer))
  44. end
  45. def build_reject_json(follow)
  46. Oj.dump(serialize_payload(follow, ActivityPub::RejectFollowSerializer))
  47. end
  48. end