2018-07-13 11:52:31 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Users
|
|
|
|
class SetStatusService
|
|
|
|
include Gitlab::Allowable
|
|
|
|
|
|
|
|
attr_reader :current_user, :target_user, :params
|
|
|
|
|
|
|
|
def initialize(current_user, params)
|
|
|
|
@current_user, @params = current_user, params.dup
|
|
|
|
@target_user = params.delete(:user) || current_user
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
return false unless can?(current_user, :update_user_status, target_user)
|
|
|
|
|
2020-12-11 04:09:48 -05:00
|
|
|
if status_cleared?
|
2018-07-13 11:52:31 -04:00
|
|
|
remove_status
|
2020-12-11 04:09:48 -05:00
|
|
|
else
|
|
|
|
set_status
|
2018-07-13 11:52:31 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def set_status
|
2018-07-16 12:18:52 -04:00
|
|
|
params[:emoji] = UserStatus::DEFAULT_EMOJI if params[:emoji].blank?
|
2020-12-11 04:09:48 -05:00
|
|
|
params[:availability] = UserStatus.availabilities[:not_set] unless new_user_availability
|
2020-10-29 14:09:11 -04:00
|
|
|
|
2018-07-13 11:52:31 -04:00
|
|
|
user_status.update(params)
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_status
|
|
|
|
UserStatus.delete(target_user.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_status
|
|
|
|
target_user.status || target_user.build_status
|
|
|
|
end
|
2020-12-11 04:09:48 -05:00
|
|
|
|
|
|
|
def status_cleared?
|
|
|
|
params[:emoji].blank? &&
|
|
|
|
params[:message].blank? &&
|
|
|
|
(new_user_availability.blank? || new_user_availability == UserStatus.availabilities[:not_set])
|
|
|
|
end
|
|
|
|
|
|
|
|
def new_user_availability
|
|
|
|
UserStatus.availabilities[params[:availability]]
|
|
|
|
end
|
2018-07-13 11:52:31 -04:00
|
|
|
end
|
|
|
|
end
|