follow_service.rb 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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? || (!target_account.local? && target_account.ostatus?)
  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. elsif target_account.local?
  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.activitypub?
  39. ActivityPub::DeliveryWorker.perform_async(build_json(follow_request), source_account.id, target_account.inbox_url)
  40. end
  41. follow_request
  42. end
  43. def direct_follow(source_account, target_account, reblogs: true)
  44. follow = source_account.follow!(target_account, reblogs: reblogs)
  45. LocalNotificationWorker.perform_async(target_account.id, follow.id, follow.class.name)
  46. MergeWorker.perform_async(target_account.id, source_account.id)
  47. follow
  48. end
  49. def build_json(follow_request)
  50. Oj.dump(serialize_payload(follow_request, ActivityPub::FollowSerializer))
  51. end
  52. end