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