follow_migration_service.rb 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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. languages = follow&.languages
  14. super(source_account, target_account, reblogs: reblogs, notify: notify, languages: languages, bypass_locked: bypass_locked, bypass_limit: true)
  15. end
  16. private
  17. def request_follow!
  18. follow_request = @source_account.request_follow!(@target_account, **follow_options.merge(rate_limit: @options[:with_rate_limit], bypass_limit: @options[:bypass_limit]))
  19. if @target_account.local?
  20. LocalNotificationWorker.perform_async(@target_account.id, follow_request.id, follow_request.class.name, 'follow_request')
  21. UnfollowService.new.call(@source_account, @old_target_account, skip_unmerge: true)
  22. elsif @target_account.activitypub?
  23. ActivityPub::MigratedFollowDeliveryWorker.perform_async(build_json(follow_request), @source_account.id, @target_account.inbox_url, @old_target_account.id)
  24. end
  25. follow_request
  26. end
  27. def direct_follow!
  28. follow = super
  29. UnfollowService.new.call(@source_account, @old_target_account, skip_unmerge: true)
  30. follow
  31. end
  32. end