debian-mirror-gitlab/app/services/users/set_status_service.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

61 lines
1.5 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
module Users
class SetStatusService
include Gitlab::Allowable
attr_reader :current_user, :target_user, :params
def initialize(current_user, params)
2021-04-29 21:17:54 +05:30
@current_user = current_user
@params = params.dup
2018-11-18 11:00:15 +05:30
@target_user = params.delete(:user) || current_user
end
def execute
return false unless can?(current_user, :update_user_status, target_user)
2021-02-22 17:27:13 +05:30
if status_cleared?
2018-11-18 11:00:15 +05:30
remove_status
2021-02-22 17:27:13 +05:30
else
set_status
2018-11-18 11:00:15 +05:30
end
end
private
def set_status
params[:emoji] = UserStatus::DEFAULT_EMOJI if params[:emoji].blank?
2021-02-22 17:27:13 +05:30
params[:availability] = UserStatus.availabilities[:not_set] unless new_user_availability
2021-01-29 00:20:46 +05:30
2021-10-27 15:23:28 +05:30
bump_user if user_status.update(params)
2018-11-18 11:00:15 +05:30
end
def remove_status
2021-10-27 15:23:28 +05:30
bump_user if UserStatus.delete(target_user.id).nonzero?
true
2018-11-18 11:00:15 +05:30
end
def user_status
target_user.status || target_user.build_status
end
2021-02-22 17:27:13 +05:30
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
2021-10-27 15:23:28 +05:30
def bump_user
# Intentionally not calling `touch` as that will trigger other callbacks
# on target_user (e.g. after_touch, after_commit, after_rollback) and we
# don't need them to happen here.
target_user.update_column(:updated_at, Time.current)
end
2018-11-18 11:00:15 +05:30
end
end