/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

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