1
0

unfollow_service.rb 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. unless @options[:skip_unmerge]
  27. UnmergeWorker.perform_async(@target_account.id, @source_account.id, 'home')
  28. UnmergeWorker.push_bulk(List.where(account: @source_account).joins(:list_accounts).where(list_accounts: { account_id: @target_account.id }).pluck(:list_id)) do |list_id|
  29. [@target_account.id, list_id, 'list']
  30. end
  31. end
  32. follow
  33. end
  34. def undo_follow_request!
  35. follow_request = FollowRequest.find_by(account: @source_account, target_account: @target_account)
  36. return unless follow_request
  37. follow_request.destroy!
  38. create_notification(follow_request) unless @target_account.local?
  39. follow_request
  40. end
  41. def create_notification(follow)
  42. ActivityPub::DeliveryWorker.perform_async(build_json(follow), follow.account_id, follow.target_account.inbox_url)
  43. end
  44. def create_reject_notification(follow)
  45. ActivityPub::DeliveryWorker.perform_async(build_reject_json(follow), follow.target_account_id, follow.account.inbox_url)
  46. end
  47. def build_json(follow)
  48. Oj.dump(serialize_payload(follow, ActivityPub::UndoFollowSerializer))
  49. end
  50. def build_reject_json(follow)
  51. Oj.dump(serialize_payload(follow, ActivityPub::RejectFollowSerializer))
  52. end
  53. end