2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-03-06 16:44:54 -05:00
|
|
|
class UsernameChanger
|
|
|
|
def initialize(user, new_username, actor = nil)
|
|
|
|
@user = user
|
2018-04-30 19:54:36 -04:00
|
|
|
@old_username = user.username
|
2015-03-06 16:44:54 -05:00
|
|
|
@new_username = new_username
|
|
|
|
@actor = actor
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.change(user, new_username, actor = nil)
|
|
|
|
self.new(user, new_username, actor).change
|
|
|
|
end
|
|
|
|
|
2021-12-02 08:42:23 -05: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)
|
2021-12-21 12:13:05 -05:00
|
|
|
suggested_username = UserNameSuggester.suggest(new_username, current_username: user.username)
|
2021-12-02 08:42:23 -05:00
|
|
|
UsernameChanger.change(user, suggested_username, user)
|
|
|
|
true
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-08 08:31:15 -04:00
|
|
|
def change(asynchronous: true, run_update_job: true)
|
2021-12-02 08:42:23 -05:00
|
|
|
return false if @user.username == @new_username
|
|
|
|
|
2015-03-06 16:44:54 -05:00
|
|
|
@user.username = @new_username
|
2018-04-30 19:54:36 -04:00
|
|
|
|
2018-06-08 08:31:15 -04:00
|
|
|
if @user.save
|
2019-05-13 12:49:42 -04:00
|
|
|
if @actor && @old_username != @new_username
|
|
|
|
StaffActionLogger.new(@actor).log_username_change(@user, @old_username, @new_username)
|
|
|
|
end
|
|
|
|
|
2018-06-08 08:31:15 -04:00
|
|
|
if run_update_job
|
|
|
|
UsernameChanger.update_username(
|
|
|
|
user_id: @user.id,
|
|
|
|
old_username: @old_username,
|
|
|
|
new_username: @new_username,
|
2021-11-25 07:07:34 -05:00
|
|
|
avatar_template: @user.avatar_template_url,
|
2018-06-08 08:31:15 -04:00
|
|
|
asynchronous: asynchronous,
|
|
|
|
)
|
2023-01-09 07:20:10 -05:00
|
|
|
end
|
2018-04-30 19:54:36 -04:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
false
|
2015-03-06 16:44:54 -05:00
|
|
|
end
|
2018-04-30 19:54:36 -04:00
|
|
|
|
2018-06-08 08:31:15 -04: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 16:44:54 -05:00
|
|
|
end
|