2018-05-01 07:54:36 +08:00
|
|
|
require_dependency 'jobs/regular/update_username'
|
|
|
|
|
2015-03-07 05:44:54 +08:00
|
|
|
class UsernameChanger
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
def initialize(user, new_username, actor = nil)
|
2015-03-07 05:44:54 +08:00
|
|
|
@user = user
|
2018-05-01 07:54:36 +08:00
|
|
|
@old_username = user.username
|
2015-03-07 05:44:54 +08:00
|
|
|
@new_username = new_username
|
|
|
|
@actor = actor
|
|
|
|
end
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
def self.change(user, new_username, actor = nil)
|
2015-03-07 05:44:54 +08:00
|
|
|
self.new(user, new_username, actor).change
|
|
|
|
end
|
|
|
|
|
2018-06-08 20:31:15 +08:00
|
|
|
def change(asynchronous: true, run_update_job: true)
|
2018-05-01 07:54:36 +08:00
|
|
|
if @actor && @old_username != @new_username
|
|
|
|
StaffActionLogger.new(@actor).log_username_change(@user, @old_username, @new_username)
|
2015-03-07 05:44:54 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
@user.username = @new_username
|
2018-05-01 07:54:36 +08:00
|
|
|
|
2018-06-08 20:31:15 +08:00
|
|
|
if @user.save
|
|
|
|
UsernameChanger.update_username(user_id: @user.id,
|
|
|
|
old_username: @old_username,
|
|
|
|
new_username: @new_username,
|
|
|
|
avatar_template: @user.avatar_template,
|
|
|
|
asynchronous: asynchronous) if run_update_job
|
2018-05-01 07:54:36 +08:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
false
|
2015-03-07 05:44:54 +08:00
|
|
|
end
|
2018-05-01 07:54:36 +08:00
|
|
|
|
2018-06-08 20:31:15 +08: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-07 05:44:54 +08:00
|
|
|
end
|