2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-03-06 15:44:54 -06:00
|
|
|
class UsernameChanger
|
|
|
|
|
2017-07-27 20:20:09 -05:00
|
|
|
def initialize(user, new_username, actor = nil)
|
2015-03-06 15:44:54 -06:00
|
|
|
@user = user
|
2018-04-30 18:54:36 -05:00
|
|
|
@old_username = user.username
|
2015-03-06 15:44:54 -06:00
|
|
|
@new_username = new_username
|
|
|
|
@actor = actor
|
|
|
|
end
|
|
|
|
|
2017-07-27 20:20:09 -05:00
|
|
|
def self.change(user, new_username, actor = nil)
|
2015-03-06 15:44:54 -06:00
|
|
|
self.new(user, new_username, actor).change
|
|
|
|
end
|
|
|
|
|
2021-12-02 07:42:23 -06:00
|
|
|
def self.override(user, new_username)
|
|
|
|
if user.username_equals_to?(new_username)
|
|
|
|
# override anyway since case could've been changed:
|
|
|
|
UsernameChanger.change(user, new_username, user)
|
|
|
|
true
|
|
|
|
elsif user.username != UserNameSuggester.fix_username(new_username)
|
|
|
|
suggested_username = UserNameSuggester.suggest(new_username)
|
|
|
|
UsernameChanger.change(user, suggested_username, user)
|
|
|
|
true
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-08 07:31:15 -05:00
|
|
|
def change(asynchronous: true, run_update_job: true)
|
2021-12-02 07:42:23 -06:00
|
|
|
return false if @user.username == @new_username
|
|
|
|
|
2015-03-06 15:44:54 -06:00
|
|
|
@user.username = @new_username
|
2018-04-30 18:54:36 -05:00
|
|
|
|
2018-06-08 07:31:15 -05:00
|
|
|
if @user.save
|
2019-05-13 11:49:42 -05:00
|
|
|
if @actor && @old_username != @new_username
|
|
|
|
StaffActionLogger.new(@actor).log_username_change(@user, @old_username, @new_username)
|
|
|
|
end
|
|
|
|
|
2018-06-08 07:31:15 -05:00
|
|
|
UsernameChanger.update_username(user_id: @user.id,
|
|
|
|
old_username: @old_username,
|
|
|
|
new_username: @new_username,
|
2021-11-25 06:07:34 -06:00
|
|
|
avatar_template: @user.avatar_template_url,
|
2018-06-08 07:31:15 -05:00
|
|
|
asynchronous: asynchronous) if run_update_job
|
2018-04-30 18:54:36 -05:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
false
|
2015-03-06 15:44:54 -06:00
|
|
|
end
|
2018-04-30 18:54:36 -05:00
|
|
|
|
2018-06-08 07:31:15 -05:00
|
|
|
def self.update_username(user_id:, old_username:, new_username:, avatar_template:, asynchronous: true)
|
|
|
|
args = {
|
|
|
|
user_id: user_id,
|
|
|
|
old_username: old_username,
|
|
|
|
new_username: new_username,
|
|
|
|
avatar_template: avatar_template
|
|
|
|
}
|
|
|
|
|
|
|
|
if asynchronous
|
|
|
|
Jobs.enqueue(:update_username, args)
|
|
|
|
else
|
|
|
|
Jobs::UpdateUsername.new.execute(args)
|
|
|
|
end
|
|
|
|
end
|
2015-03-06 15:44:54 -06:00
|
|
|
end
|