migrations_controller.rb 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # frozen_string_literal: true
  2. class Settings::MigrationsController < Settings::BaseController
  3. layout 'admin'
  4. before_action :authenticate_user!
  5. before_action :require_not_suspended!
  6. before_action :set_migrations
  7. before_action :set_cooldown
  8. skip_before_action :require_functional!
  9. def show
  10. @migration = current_account.migrations.build
  11. end
  12. def create
  13. @migration = current_account.migrations.build(resource_params)
  14. if @migration.save_with_challenge(current_user)
  15. current_account.update!(moved_to_account: @migration.target_account)
  16. ActivityPub::UpdateDistributionWorker.perform_async(current_account.id)
  17. ActivityPub::MoveDistributionWorker.perform_async(@migration.id)
  18. redirect_to settings_migration_path, notice: I18n.t('migrations.moved_msg', acct: current_account.moved_to_account.acct)
  19. else
  20. render :show
  21. end
  22. end
  23. def cancel
  24. if current_account.moved_to_account_id.present?
  25. current_account.update!(moved_to_account: nil)
  26. ActivityPub::UpdateDistributionWorker.perform_async(current_account.id)
  27. end
  28. redirect_to settings_migration_path, notice: I18n.t('migrations.cancelled_msg')
  29. end
  30. helper_method :on_cooldown?
  31. private
  32. def resource_params
  33. params.require(:account_migration).permit(:acct, :current_password, :current_username)
  34. end
  35. def set_migrations
  36. @migrations = current_account.migrations.includes(:target_account).order(id: :desc).reject(&:new_record?)
  37. end
  38. def set_cooldown
  39. @cooldown = current_account.migrations.within_cooldown.first
  40. end
  41. def on_cooldown?
  42. @cooldown.present?
  43. end
  44. def require_not_suspended!
  45. forbidden if current_account.suspended?
  46. end
  47. end