PageRenderTime 41ms CodeModel.GetById 25ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/api/notification_settings.rb

https://gitlab.com/vicvega/gitlab-ce
Ruby | 97 lines | 82 code | 14 blank | 1 comment | 1 complexity | 25a1fba6b310841174c504a134ffded4 MD5 | raw file
 1module API
 2  # notification_settings API
 3  class NotificationSettings < Grape::API
 4    before { authenticate! }
 5
 6    helpers ::API::Helpers::MembersHelpers
 7
 8    resource :notification_settings do
 9      desc 'Get global notification level settings and email, defaults to Participate' do
10        detail 'This feature was introduced in GitLab 8.12'
11        success Entities::GlobalNotificationSetting
12      end
13      get do
14        notification_setting = current_user.global_notification_setting
15
16        present notification_setting, with: Entities::GlobalNotificationSetting
17      end
18
19      desc 'Update global notification level settings and email, defaults to Participate' do
20        detail 'This feature was introduced in GitLab 8.12'
21        success Entities::GlobalNotificationSetting
22      end
23      params do
24        optional :level, type: String, desc: 'The global notification level'
25        optional :notification_email, type: String, desc: 'The email address to send notifications'
26        NotificationSetting::EMAIL_EVENTS.each do |event|
27          optional event, type: Boolean, desc: 'Enable/disable this notification'
28        end
29      end
30      put do
31        notification_setting = current_user.global_notification_setting
32
33        begin
34          notification_setting.transaction do
35            new_notification_email = params.delete(:notification_email)
36            declared_params = declared(params, include_missing: false).to_h
37
38            current_user.update(notification_email: new_notification_email) if new_notification_email
39            notification_setting.update(declared_params)
40          end
41        rescue ArgumentError => e # catch level enum error
42          render_api_error! e.to_s, 400
43        end
44
45        render_validation_error! current_user
46        render_validation_error! notification_setting
47        present notification_setting, with: Entities::GlobalNotificationSetting
48      end
49    end
50
51    %w[group project].each do |source_type|
52      resource source_type.pluralize do
53        desc "Get #{source_type} level notification level settings, defaults to Global" do
54          detail 'This feature was introduced in GitLab 8.12'
55          success Entities::NotificationSetting
56        end
57        params do
58          requires :id, type: String, desc: 'The group ID or project ID or project NAMESPACE/PROJECT_NAME'
59        end
60        get ":id/notification_settings" do
61          source = find_source(source_type, params[:id])
62
63          notification_setting = current_user.notification_settings_for(source)
64
65          present notification_setting, with: Entities::NotificationSetting
66        end
67
68        desc "Update #{source_type} level notification level settings, defaults to Global" do
69          detail 'This feature was introduced in GitLab 8.12'
70          success Entities::NotificationSetting
71        end
72        params do
73          requires :id, type: String, desc: 'The group ID or project ID or project NAMESPACE/PROJECT_NAME'
74          optional :level, type: String, desc: "The #{source_type} notification level"
75          NotificationSetting::EMAIL_EVENTS.each do |event|
76            optional event, type: Boolean, desc: 'Enable/disable this notification'
77          end
78        end
79        put ":id/notification_settings" do
80          source = find_source(source_type, params.delete(:id))
81          notification_setting = current_user.notification_settings_for(source)
82
83          begin
84            declared_params = declared(params, include_missing: false).to_h
85
86            notification_setting.update(declared_params)
87          rescue ArgumentError => e # catch level enum error
88            render_api_error! e.to_s, 400
89          end
90
91          render_validation_error! notification_setting
92          present notification_setting, with: Entities::NotificationSetting
93        end
94      end
95    end
96  end
97end