follow_service.rb 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # frozen_string_literal: true
  2. class FollowService < BaseService
  3. include Redisable
  4. include Payloadable
  5. # Follow a remote user, notify remote user about the follow
  6. # @param [Account] source_account From which to follow
  7. # @param [String, Account] uri User URI to follow in the form of username@domain (or account record)
  8. # @param [true, false, nil] reblogs Whether or not to show reblogs, defaults to true
  9. def call(source_account, target_account, reblogs: nil)
  10. reblogs = true if reblogs.nil?
  11. target_account = ResolveAccountService.new.call(target_account, skip_webfinger: true)
  12. raise ActiveRecord::RecordNotFound if target_account.nil? || target_account.id == source_account.id || target_account.suspended?
  13. raise Mastodon::NotPermittedError if target_account.blocking?(source_account) || source_account.blocking?(target_account) || target_account.moved?
  14. if source_account.following?(target_account)
  15. # We're already following this account, but we'll call follow! again to
  16. # make sure the reblogs status is set correctly.
  17. source_account.follow!(target_account, reblogs: reblogs)
  18. return
  19. elsif source_account.requested?(target_account)
  20. # This isn't managed by a method in AccountInteractions, so we modify it
  21. # ourselves if necessary.
  22. req = source_account.follow_requests.find_by(target_account: target_account)
  23. req.update!(show_reblogs: reblogs)
  24. return
  25. end
  26. ActivityTracker.increment('activity:interactions')
  27. if target_account.locked? || target_account.activitypub?
  28. request_follow(source_account, target_account, reblogs: reblogs)
  29. else
  30. direct_follow(source_account, target_account, reblogs: reblogs)
  31. end
  32. end
  33. private
  34. def request_follow(source_account, target_account, reblogs: true)
  35. follow_request = FollowRequest.create!(account: source_account, target_account: target_account, show_reblogs: reblogs)
  36. if target_account.local?
  37. LocalNotificationWorker.perform_async(target_account.id, follow_request.id, follow_request.class.name)
  38. elsif target_account.ostatus?
  39. NotificationWorker.perform_async(build_follow_request_xml(follow_request), source_account.id, target_account.id)
  40. AfterRemoteFollowRequestWorker.perform_async(follow_request.id)
  41. elsif target_account.activitypub?
  42. ActivityPub::DeliveryWorker.perform_async(build_json(follow_request), source_account.id, target_account.inbox_url)
  43. end
  44. follow_request
  45. end
  46. def direct_follow(source_account, target_account, reblogs: true)
  47. follow = source_account.follow!(target_account, reblogs: reblogs)
  48. if target_account.local?
  49. LocalNotificationWorker.perform_async(target_account.id, follow.id, follow.class.name)
  50. else
  51. Pubsubhubbub::SubscribeWorker.perform_async(target_account.id) unless target_account.subscribed?
  52. NotificationWorker.perform_async(build_follow_xml(follow), source_account.id, target_account.id)
  53. AfterRemoteFollowWorker.perform_async(follow.id)
  54. end
  55. MergeWorker.perform_async(target_account.id, source_account.id)
  56. follow
  57. end
  58. def build_follow_request_xml(follow_request)
  59. OStatus::AtomSerializer.render(OStatus::AtomSerializer.new.follow_request_salmon(follow_request))
  60. end
  61. def build_follow_xml(follow)
  62. OStatus::AtomSerializer.render(OStatus::AtomSerializer.new.follow_salmon(follow))
  63. end
  64. def build_json(follow_request)
  65. Oj.dump(serialize_payload(follow_request, ActivityPub::FollowSerializer))
  66. end
  67. end