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.

66 lines
2.2 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 = ResolveRemoteAccountService.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. return if source_account.following?(target_account)
  12. if target_account.locked?
  13. request_follow(source_account, target_account)
  14. else
  15. direct_follow(source_account, target_account)
  16. end
  17. end
  18. private
  19. def request_follow(source_account, target_account)
  20. follow_request = FollowRequest.create!(account: source_account, target_account: target_account)
  21. if target_account.local?
  22. NotifyService.new.call(target_account, follow_request)
  23. else
  24. NotificationWorker.perform_async(build_follow_request_xml(follow_request), source_account.id, target_account.id)
  25. AfterRemoteFollowRequestWorker.perform_async(follow_request.id)
  26. end
  27. follow_request
  28. end
  29. def direct_follow(source_account, target_account)
  30. follow = source_account.follow!(target_account)
  31. if target_account.local?
  32. NotifyService.new.call(target_account, follow)
  33. else
  34. Pubsubhubbub::SubscribeWorker.perform_async(target_account.id) unless target_account.subscribed?
  35. NotificationWorker.perform_async(build_follow_xml(follow), source_account.id, target_account.id)
  36. AfterRemoteFollowWorker.perform_async(follow.id)
  37. end
  38. MergeWorker.perform_async(target_account.id, source_account.id)
  39. follow
  40. end
  41. def redis
  42. Redis.current
  43. end
  44. def build_follow_request_xml(follow_request)
  45. OStatus::AtomSerializer.render(OStatus::AtomSerializer.new.follow_request_salmon(follow_request))
  46. end
  47. def build_follow_xml(follow)
  48. OStatus::AtomSerializer.render(OStatus::AtomSerializer.new.follow_salmon(follow))
  49. end
  50. end