follow_service.rb 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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)
  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. FeedManager.instance.merge_into_timeline(target_account, source_account)
  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