The code powering m.abunchtell.com https://m.abunchtell.com
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.8 KiB

  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, bypass_locked: false)
  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?) || source_account.domain_blocking?(target_account.domain)
  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. return source_account.follow!(target_account, reblogs: reblogs)
  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 req
  24. end
  25. ActivityTracker.increment('activity:interactions')
  26. if (target_account.locked? && !bypass_locked) || source_account.silenced? || target_account.activitypub?
  27. request_follow(source_account, target_account, reblogs: reblogs)
  28. elsif target_account.local?
  29. direct_follow(source_account, target_account, reblogs: reblogs)
  30. end
  31. end
  32. private
  33. def request_follow(source_account, target_account, reblogs: true)
  34. follow_request = FollowRequest.create!(account: source_account, target_account: target_account, show_reblogs: reblogs)
  35. if target_account.local?
  36. LocalNotificationWorker.perform_async(target_account.id, follow_request.id, follow_request.class.name)
  37. elsif target_account.activitypub?
  38. ActivityPub::DeliveryWorker.perform_async(build_json(follow_request), source_account.id, target_account.inbox_url)
  39. end
  40. follow_request
  41. end
  42. def direct_follow(source_account, target_account, reblogs: true)
  43. follow = source_account.follow!(target_account, reblogs: reblogs)
  44. LocalNotificationWorker.perform_async(target_account.id, follow.id, follow.class.name)
  45. MergeWorker.perform_async(target_account.id, source_account.id)
  46. follow
  47. end
  48. def build_json(follow_request)
  49. Oj.dump(serialize_payload(follow_request, ActivityPub::FollowSerializer))
  50. end
  51. end