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-04-30 19:54:36 -04:00
|
|
|
def change(asynchronous: true)
|
|
|
|
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
|
|
|
if @user.save
|
|
|
|
|
|
|
|
args = {
|
|
|
|
user_id: @user.id,
|
|
|
|
old_username: @old_username,
|
2018-05-14 11:13:25 -04:00
|
|
|
new_username: @new_username,
|
|
|
|
avatar_template: @user.avatar_template
|
2018-04-30 19:54:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if asynchronous
|
|
|
|
Jobs.enqueue(:update_username, args)
|
|
|
|
else
|
|
|
|
Jobs::UpdateUsername.new.execute(args)
|
|
|
|
end
|
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
false
|
2015-03-06 16:44:54 -05:00
|
|
|
end
|
2018-04-30 19:54:36 -04:00
|
|
|
|
2015-03-06 16:44:54 -05:00
|
|
|
end
|