follow_migration_service.rb 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # frozen_string_literal: true
  2. class FollowMigrationService < FollowService
  3. # Follow an account with the same settings as another account, and unfollow the old account once the request is sent
  4. # @param [Account] source_account From which to follow
  5. # @param [Account] target_account Account to follow
  6. # @param [Account] old_target_account Account to unfollow once the follow request has been sent to the new one
  7. # @option [Boolean] bypass_locked Whether to immediately follow the new account even if it is locked
  8. def call(source_account, target_account, old_target_account, bypass_locked: false)
  9. @old_target_account = old_target_account
  10. follow = source_account.active_relationships.find_by(target_account: old_target_account)
  11. reblogs = follow&.show_reblogs?
  12. notify = follow&.notify?
  13. super(source_account, target_account, reblogs: reblogs, notify: notify, bypass_locked: bypass_locked, bypass_limit: true)
  14. end
  15. private
  16. def request_follow!
  17. follow_request = @source_account.request_follow!(@target_account, **follow_options.merge(rate_limit: @options[:with_rate_limit], bypass_limit: @options[:bypass_limit]))
  18. if @target_account.local?
  19. LocalNotificationWorker.perform_async(@target_account.id, follow_request.id, follow_request.class.name, 'follow_request')
  20. UnfollowService.new.call(@source_account, @old_target_account, skip_unmerge: true)
  21. elsif @target_account.activitypub?
  22. ActivityPub::MigratedFollowDeliveryWorker.perform_async(build_json(follow_request), @source_account.id, @target_account.inbox_url, @old_target_account.id)
  23. end
  24. follow_request
  25. end
  26. def direct_follow!
  27. follow = super
  28. UnfollowService.new.call(@source_account, @old_target_account, skip_unmerge: true)
  29. follow
  30. end
  31. def follow_options
  32. @options.slice(:reblogs, :notify)
  33. end
  34. end