2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-04-30 19:54:36 -04:00
|
|
|
require_dependency 'jobs/regular/update_username'
|
|
|
|
|
2015-03-06 16:44:54 -05:00
|
|
|
class UsernameChanger
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def initialize(user, new_username, actor = nil)
|
2015-03-06 16:44:54 -05:00
|
|
|
@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
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def self.change(user, new_username, actor = nil)
|
2015-03-06 16:44:54 -05:00
|
|
|
self.new(user, new_username, actor).change
|
|
|
|
end
|
|
|
|
|
2018-06-08 08:31:15 -04:00
|
|
|
def change(asynchronous: true, run_update_job: true)
|
2018-04-30 19:54:36 -04:00
|
|
|
if @actor && @old_username != @new_username
|
|
|
|
StaffActionLogger.new(@actor).log_username_change(@user, @old_username, @new_username)
|
2015-03-06 16:44:54 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
@user.username = @new_username
|
2018-04-30 19:54:36 -04:00
|
|
|
|
2018-06-08 08:31:15 -04: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-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
|