diff options
author | Bob Van Landuyt <bob@vanlanduyt.co> | 2018-07-13 18:52:31 +0300 |
---|---|---|
committer | Bob Van Landuyt <bob@vanlanduyt.co> | 2018-07-30 16:01:26 +0300 |
commit | b4c4b48a8c0258ff266c523488aa169a1b5ea0f3 (patch) | |
tree | 7b2c5a6b1f3bc7e672cb3e64600e6bd2403d8748 /app/services | |
parent | 812bfb158b70b09cfd438379a4b9446aa85b52ec (diff) |
Allow users to set a status
This can be done trough the API for the current user, or on the
profile page.
Diffstat (limited to 'app/services')
-rw-r--r-- | app/services/users/set_status_service.rb | 38 | ||||
-rw-r--r-- | app/services/users/update_service.rb | 12 |
2 files changed, 48 insertions, 2 deletions
diff --git a/app/services/users/set_status_service.rb b/app/services/users/set_status_service.rb new file mode 100644 index 00000000000..275e13faf59 --- /dev/null +++ b/app/services/users/set_status_service.rb @@ -0,0 +1,38 @@ +# 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) + + if params[:emoji].present? || params[:message].present? + set_status + else + remove_status + end + end + + private + + def set_status + 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 + end +end diff --git a/app/services/users/update_service.rb b/app/services/users/update_service.rb index 6dadb5a4eac..a897e4bd56a 100644 --- a/app/services/users/update_service.rb +++ b/app/services/users/update_service.rb @@ -7,6 +7,7 @@ module Users def initialize(current_user, params = {}) @current_user = current_user @user = params.delete(:user) + @status_params = params.delete(:status) @params = params.dup end @@ -17,10 +18,11 @@ module Users assign_attributes(&block) - if @user.save(validate: validate) + if @user.save(validate: validate) && update_status notify_success(user_exists) else - error(@user.errors.full_messages.uniq.join('. ')) + messages = @user.errors.full_messages + Array(@user.status&.errors&.full_messages) + error(messages.uniq.join('. ')) end end @@ -34,6 +36,12 @@ module Users private + def update_status + return true unless @status_params + + Users::SetStatusService.new(current_user, @status_params.merge(user: @user)).execute + end + def notify_success(user_exists) notify_new_user(@user, nil) unless user_exists |