summaryrefslogtreecommitdiff
path: root/lib/api/notification_settings.rb
blob: 0266bf2f71715efbdd6b98b9eaa57ac7a7d6cfc0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
module API
  # notification_settings API
  class NotificationSettings < Grape::API
    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'
        NotificationSetting::EMAIL_EVENTS.each do |event|
          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)

            if new_notification_email
              ::Users::UpdateService.new(current_user, user: current_user, notification_email: new_notification_email).execute
            end

            notification_setting.update(declared_params(include_missing: false))
          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

    %w[group project].each do |source_type|
      params do
        requires :id, type: String, desc: "The #{source_type} ID"
      end
      resource source_type.pluralize, requirements: API::PROJECT_ENDPOINT_REQUIREMENTS do
        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"
          NotificationSetting::EMAIL_EVENTS.each do |event|
            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
            notification_setting.update(declared_params(include_missing: false))
          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