1
0

follow_migration_service.rb 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. @original_follow = source_account.active_relationships.find_by(target_account: old_target_account)
  11. reblogs = @original_follow&.show_reblogs?
  12. notify = @original_follow&.notify?
  13. languages = @original_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. migrate_list_accounts!
  20. if @target_account.local?
  21. LocalNotificationWorker.perform_async(@target_account.id, follow_request.id, follow_request.class.name, 'follow_request')
  22. UnfollowService.new.call(@source_account, @old_target_account, skip_unmerge: true)
  23. elsif @target_account.activitypub?
  24. ActivityPub::MigratedFollowDeliveryWorker.perform_async(build_json(follow_request), @source_account.id, @target_account.inbox_url, @old_target_account.id)
  25. end
  26. follow_request
  27. end
  28. def change_follow_options!
  29. migrate_list_accounts!
  30. super
  31. end
  32. def change_follow_request_options!
  33. migrate_list_accounts!
  34. super
  35. end
  36. def direct_follow!
  37. follow = super
  38. migrate_list_accounts!
  39. UnfollowService.new.call(@source_account, @old_target_account, skip_unmerge: true)
  40. follow
  41. end
  42. def migrate_list_accounts!
  43. ListAccount.where(follow_id: @original_follow.id).includes(:list).find_each do |list_account|
  44. list_account.list.accounts << @target_account
  45. rescue ActiveRecord::RecordInvalid
  46. nil
  47. end
  48. end
  49. end