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

102 lines
3.0 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. SubscribeService.new.call(target_account) 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. description = "#{follow_request.account.acct} requested to follow #{follow_request.target_account.acct}"
  45. Nokogiri::XML::Builder.new do |xml|
  46. entry(xml, true) do
  47. unique_id xml, follow_request.created_at, follow_request.id, 'FollowRequest'
  48. title xml, description
  49. content xml, description
  50. author(xml) do
  51. include_author xml, follow_request.account
  52. end
  53. object_type xml, :activity
  54. verb xml, :request_friend
  55. target(xml) do
  56. include_author xml, follow_request.target_account
  57. end
  58. end
  59. end.to_xml
  60. end
  61. def build_follow_xml(follow)
  62. description = "#{follow.account.acct} started following #{follow.target_account.acct}"
  63. Nokogiri::XML::Builder.new do |xml|
  64. entry(xml, true) do
  65. unique_id xml, follow.created_at, follow.id, 'Follow'
  66. title xml, description
  67. content xml, description
  68. author(xml) do
  69. include_author xml, follow.account
  70. end
  71. object_type xml, :activity
  72. verb xml, :follow
  73. target(xml) do
  74. include_author xml, follow.target_account
  75. end
  76. end
  77. end.to_xml
  78. end
  79. end