logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

follow_service.rb (3533B)


  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, Account] uri User URI to follow in the form of username@domain (or account record)
  7. # @param [true, false, nil] reblogs Whether or not to show reblogs, defaults to true
  8. def call(source_account, uri, reblogs: nil)
  9. reblogs = true if reblogs.nil?
  10. target_account = uri.is_a?(Account) ? uri : ResolveAccountService.new.call(uri)
  11. raise ActiveRecord::RecordNotFound if target_account.nil? || target_account.id == source_account.id || target_account.suspended?
  12. raise Mastodon::NotPermittedError if target_account.blocking?(source_account) || source_account.blocking?(target_account)
  13. if source_account.following?(target_account)
  14. # We're already following this account, but we'll call follow! again to
  15. # make sure the reblogs status is set correctly.
  16. source_account.follow!(target_account, reblogs: reblogs)
  17. return
  18. elsif source_account.requested?(target_account)
  19. # This isn't managed by a method in AccountInteractions, so we modify it
  20. # ourselves if necessary.
  21. req = source_account.follow_requests.find_by(target_account: target_account)
  22. req.update!(show_reblogs: reblogs)
  23. return
  24. end
  25. if target_account.locked? || target_account.activitypub?
  26. request_follow(source_account, target_account, reblogs: reblogs)
  27. else
  28. direct_follow(source_account, target_account, reblogs: reblogs)
  29. end
  30. end
  31. private
  32. def request_follow(source_account, target_account, reblogs: true)
  33. follow_request = FollowRequest.create!(account: source_account, target_account: target_account, show_reblogs: reblogs)
  34. if target_account.local?
  35. NotifyService.new.call(target_account, follow_request)
  36. elsif target_account.ostatus?
  37. NotificationWorker.perform_async(build_follow_request_xml(follow_request), source_account.id, target_account.id)
  38. AfterRemoteFollowRequestWorker.perform_async(follow_request.id)
  39. elsif target_account.activitypub?
  40. ActivityPub::DeliveryWorker.perform_async(build_json(follow_request), source_account.id, target_account.inbox_url)
  41. end
  42. follow_request
  43. end
  44. def direct_follow(source_account, target_account, reblogs: true)
  45. follow = source_account.follow!(target_account, reblogs: reblogs)
  46. if target_account.local?
  47. NotifyService.new.call(target_account, follow)
  48. else
  49. Pubsubhubbub::SubscribeWorker.perform_async(target_account.id) unless target_account.subscribed?
  50. NotificationWorker.perform_async(build_follow_xml(follow), source_account.id, target_account.id)
  51. AfterRemoteFollowWorker.perform_async(follow.id)
  52. end
  53. MergeWorker.perform_async(target_account.id, source_account.id)
  54. follow
  55. end
  56. def redis
  57. Redis.current
  58. end
  59. def build_follow_request_xml(follow_request)
  60. OStatus::AtomSerializer.render(OStatus::AtomSerializer.new.follow_request_salmon(follow_request))
  61. end
  62. def build_follow_xml(follow)
  63. OStatus::AtomSerializer.render(OStatus::AtomSerializer.new.follow_salmon(follow))
  64. end
  65. def build_json(follow_request)
  66. Oj.dump(ActivityPub::LinkedDataSignature.new(ActiveModelSerializers::SerializableResource.new(
  67. follow_request,
  68. serializer: ActivityPub::FollowSerializer,
  69. adapter: ActivityPub::Adapter
  70. ).as_json).sign!(follow_request.account))
  71. end
  72. end