2018-09-29 18:34:47 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-02 16:52:55 -04:00
|
|
|
module API
|
|
|
|
# notification_settings API
|
2020-04-27 23:09:53 -04:00
|
|
|
class NotificationSettings < Grape::API
|
2016-08-02 16:52:55 -04:00
|
|
|
before { authenticate! }
|
|
|
|
|
|
|
|
helpers ::API::Helpers::MembersHelpers
|
|
|
|
|
|
|
|
resource :notification_settings do
|
|
|
|
desc 'Get global notification level settings and email, defaults to Participate' do
|
|
|
|
detail 'This feature was introduced in GitLab 8.12'
|
|
|
|
success Entities::GlobalNotificationSetting
|
|
|
|
end
|
|
|
|
get do
|
|
|
|
notification_setting = current_user.global_notification_setting
|
|
|
|
|
|
|
|
present notification_setting, with: Entities::GlobalNotificationSetting
|
|
|
|
end
|
|
|
|
|
|
|
|
desc 'Update global notification level settings and email, defaults to Participate' do
|
|
|
|
detail 'This feature was introduced in GitLab 8.12'
|
|
|
|
success Entities::GlobalNotificationSetting
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
optional :level, type: String, desc: 'The global notification level'
|
|
|
|
optional :notification_email, type: String, desc: 'The email address to send notifications'
|
2018-07-22 22:42:19 -04:00
|
|
|
NotificationSetting.email_events.each do |event|
|
2016-08-02 16:52:55 -04:00
|
|
|
optional event, type: Boolean, desc: 'Enable/disable this notification'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
put do
|
|
|
|
notification_setting = current_user.global_notification_setting
|
|
|
|
|
|
|
|
begin
|
|
|
|
notification_setting.transaction do
|
|
|
|
new_notification_email = params.delete(:notification_email)
|
|
|
|
|
2017-06-15 10:42:14 -04:00
|
|
|
if new_notification_email
|
2017-09-27 05:48:33 -04:00
|
|
|
::Users::UpdateService.new(current_user, user: current_user, notification_email: new_notification_email).execute
|
2017-06-15 10:42:14 -04:00
|
|
|
end
|
|
|
|
|
2016-11-14 08:44:27 -05:00
|
|
|
notification_setting.update(declared_params(include_missing: false))
|
2016-08-02 16:52:55 -04:00
|
|
|
end
|
|
|
|
rescue ArgumentError => e # catch level enum error
|
|
|
|
render_api_error! e.to_s, 400
|
|
|
|
end
|
|
|
|
|
|
|
|
render_validation_error! current_user
|
|
|
|
render_validation_error! notification_setting
|
|
|
|
present notification_setting, with: Entities::GlobalNotificationSetting
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-23 08:51:42 -04:00
|
|
|
[Group, Project].each do |source_class|
|
|
|
|
source_type = source_class.name.underscore
|
|
|
|
|
2017-03-15 14:09:24 -04:00
|
|
|
params do
|
|
|
|
requires :id, type: String, desc: "The #{source_type} ID"
|
|
|
|
end
|
2018-11-08 07:18:17 -05:00
|
|
|
resource source_type.pluralize, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
|
2016-08-02 16:52:55 -04:00
|
|
|
desc "Get #{source_type} level notification level settings, defaults to Global" do
|
|
|
|
detail 'This feature was introduced in GitLab 8.12'
|
|
|
|
success Entities::NotificationSetting
|
|
|
|
end
|
|
|
|
get ":id/notification_settings" do
|
|
|
|
source = find_source(source_type, params[:id])
|
|
|
|
|
|
|
|
notification_setting = current_user.notification_settings_for(source)
|
|
|
|
|
|
|
|
present notification_setting, with: Entities::NotificationSetting
|
|
|
|
end
|
|
|
|
|
|
|
|
desc "Update #{source_type} level notification level settings, defaults to Global" do
|
|
|
|
detail 'This feature was introduced in GitLab 8.12'
|
|
|
|
success Entities::NotificationSetting
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
optional :level, type: String, desc: "The #{source_type} notification level"
|
2018-07-23 08:51:42 -04:00
|
|
|
NotificationSetting.email_events(source_class).each do |event|
|
2016-08-02 16:52:55 -04:00
|
|
|
optional event, type: Boolean, desc: 'Enable/disable this notification'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
put ":id/notification_settings" do
|
|
|
|
source = find_source(source_type, params.delete(:id))
|
|
|
|
notification_setting = current_user.notification_settings_for(source)
|
|
|
|
|
|
|
|
begin
|
2016-11-14 08:44:27 -05:00
|
|
|
notification_setting.update(declared_params(include_missing: false))
|
2016-08-02 16:52:55 -04:00
|
|
|
rescue ArgumentError => e # catch level enum error
|
|
|
|
render_api_error! e.to_s, 400
|
|
|
|
end
|
|
|
|
|
|
|
|
render_validation_error! notification_setting
|
|
|
|
present notification_setting, with: Entities::NotificationSetting
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|