闭社主体 forked from https://github.com/tootsuite/mastodon
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.

56 lines
1.7 KiB

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