2018-09-29 22:34:47 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-05-12 20:48:09 +00:00
|
|
|
module API
|
|
|
|
class Subscriptions < Grape::API
|
|
|
|
before { authenticate! }
|
|
|
|
|
|
|
|
subscribable_types = {
|
2017-01-03 18:03:13 +00:00
|
|
|
'merge_requests' => proc { |id| find_merge_request_with_access(id, :update_merge_request) },
|
2016-05-12 20:48:09 +00:00
|
|
|
'issues' => proc { |id| find_project_issue(id) },
|
2017-05-03 11:22:03 +00:00
|
|
|
'labels' => proc { |id| find_project_label(id) }
|
2016-05-12 20:48:09 +00:00
|
|
|
}
|
|
|
|
|
2016-11-09 15:57:14 +00:00
|
|
|
params do
|
|
|
|
requires :id, type: String, desc: 'The ID of a project'
|
|
|
|
requires :subscribable_id, type: String, desc: 'The ID of a resource'
|
|
|
|
end
|
2017-08-31 11:44:49 +00:00
|
|
|
resource :projects, requirements: API::PROJECT_ENDPOINT_REQUIREMENTS do
|
2016-05-12 20:48:09 +00:00
|
|
|
subscribable_types.each do |type, finder|
|
|
|
|
type_singularized = type.singularize
|
|
|
|
entity_class = Entities.const_get(type_singularized.camelcase)
|
|
|
|
|
2016-11-09 15:57:14 +00:00
|
|
|
desc 'Subscribe to a resource' do
|
|
|
|
success entity_class
|
|
|
|
end
|
2017-02-17 09:45:05 +00:00
|
|
|
post ":id/#{type}/:subscribable_id/subscribe" do
|
2016-11-09 15:57:14 +00:00
|
|
|
resource = instance_exec(params[:subscribable_id], &finder)
|
2016-05-12 20:48:09 +00:00
|
|
|
|
2016-11-01 16:06:05 +00:00
|
|
|
if resource.subscribed?(current_user, user_project)
|
2016-05-12 20:48:09 +00:00
|
|
|
not_modified!
|
|
|
|
else
|
2016-11-01 16:06:05 +00:00
|
|
|
resource.subscribe(current_user, user_project)
|
|
|
|
present resource, with: entity_class, current_user: current_user, project: user_project
|
2016-05-12 20:48:09 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-09 15:57:14 +00:00
|
|
|
desc 'Unsubscribe from a resource' do
|
|
|
|
success entity_class
|
|
|
|
end
|
2017-02-17 09:45:05 +00:00
|
|
|
post ":id/#{type}/:subscribable_id/unsubscribe" do
|
2016-11-09 15:57:14 +00:00
|
|
|
resource = instance_exec(params[:subscribable_id], &finder)
|
2016-05-12 20:48:09 +00:00
|
|
|
|
2016-11-01 16:06:05 +00:00
|
|
|
if !resource.subscribed?(current_user, user_project)
|
2016-05-12 20:48:09 +00:00
|
|
|
not_modified!
|
|
|
|
else
|
2016-11-01 16:06:05 +00:00
|
|
|
resource.unsubscribe(current_user, user_project)
|
|
|
|
present resource, with: entity_class, current_user: current_user, project: user_project
|
2016-05-12 20:48:09 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|