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

58 lines
1.7 KiB

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