闭社主体 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.

64 lines
2.1 KiB

  1. # frozen_string_literal: true
  2. class FollowService < BaseService
  3. include StreamEntryRenderer
  4. # Follow a remote user, notify remote user about the follow
  5. # @param [Account] source_account From which to follow
  6. # @param [String] uri User URI to follow in the form of username@domain
  7. def call(source_account, uri)
  8. target_account = FollowRemoteAccountService.new.call(uri)
  9. raise ActiveRecord::RecordNotFound if target_account.nil? || target_account.id == source_account.id || target_account.suspended?
  10. raise Mastodon::NotPermittedError if target_account.blocking?(source_account) || source_account.blocking?(target_account)
  11. if target_account.locked?
  12. request_follow(source_account, target_account)
  13. else
  14. direct_follow(source_account, target_account)
  15. end
  16. end
  17. private
  18. def request_follow(source_account, target_account)
  19. follow_request = FollowRequest.create!(account: source_account, target_account: target_account)
  20. if target_account.local?
  21. NotifyService.new.call(target_account, follow_request)
  22. else
  23. NotificationWorker.perform_async(build_follow_request_xml(follow_request), source_account.id, target_account.id)
  24. AfterRemoteFollowRequestWorker.perform_async(follow_request.id)
  25. end
  26. follow_request
  27. end
  28. def direct_follow(source_account, target_account)
  29. follow = source_account.follow!(target_account)
  30. if target_account.local?
  31. NotifyService.new.call(target_account, follow)
  32. else
  33. Pubsubhubbub::SubscribeWorker.perform_async(target_account.id) unless target_account.subscribed?
  34. NotificationWorker.perform_async(build_follow_xml(follow), source_account.id, target_account.id)
  35. AfterRemoteFollowWorker.perform_async(follow.id)
  36. end
  37. MergeWorker.perform_async(target_account.id, source_account.id)
  38. follow
  39. end
  40. def redis
  41. Redis.current
  42. end
  43. def build_follow_request_xml(follow_request)
  44. AtomSerializer.render(AtomSerializer.new.follow_request_salmon(follow_request))
  45. end
  46. def build_follow_xml(follow)
  47. AtomSerializer.render(AtomSerializer.new.follow_salmon(follow))
  48. end
  49. end