BigW Consortium Gitlab

subscriptions.rb 1.72 KB
Newer Older
1 2 3 4 5
module API
  class Subscriptions < Grape::API
    before { authenticate! }

    subscribable_types = {
6
      'merge_requests' => proc { |id| find_merge_request_with_access(id, :update_merge_request) },
7
      'issues' => proc { |id| find_project_issue(id) },
8
      'labels' => proc { |id| find_project_label(id) }
9 10
    }

11 12 13 14
    params do
      requires :id, type: String, desc: 'The ID of a project'
      requires :subscribable_id, type: String, desc: 'The ID of a resource'
    end
15
    resource :projects, requirements: API::PROJECT_ENDPOINT_REQUIREMENTS do
16 17 18 19
      subscribable_types.each do |type, finder|
        type_singularized = type.singularize
        entity_class = Entities.const_get(type_singularized.camelcase)

20 21 22
        desc 'Subscribe to a resource' do
          success entity_class
        end
23
        post ":id/#{type}/:subscribable_id/subscribe" do
24
          resource = instance_exec(params[:subscribable_id], &finder)
25

26
          if resource.subscribed?(current_user, user_project)
27 28
            not_modified!
          else
29 30
            resource.subscribe(current_user, user_project)
            present resource, with: entity_class, current_user: current_user, project: user_project
31 32 33
          end
        end

34 35 36
        desc 'Unsubscribe from a resource' do
          success entity_class
        end
37
        post ":id/#{type}/:subscribable_id/unsubscribe" do
38
          resource = instance_exec(params[:subscribable_id], &finder)
39

40
          if !resource.subscribed?(current_user, user_project)
41 42
            not_modified!
          else
43 44
            resource.unsubscribe(current_user, user_project)
            present resource, with: entity_class, current_user: current_user, project: user_project
45 46 47 48 49 50
          end
        end
      end
    end
  end
end