diff options
author | Robert Schilling <rschilling@student.tugraz.at> | 2017-02-17 12:45:05 +0300 |
---|---|---|
committer | Robert Schilling <rschilling@student.tugraz.at> | 2017-02-21 19:44:44 +0300 |
commit | 55f2425a678b61178d46e50f2b5a2da929228f52 (patch) | |
tree | 467d77c5a37a2101fda87be193563b11987c4f35 /lib | |
parent | 316a7312341fd2d359b44da3f386c3739c1bdb4d (diff) |
API: Make subscription API more RESTfuL
Diffstat (limited to 'lib')
-rw-r--r-- | lib/api/api.rb | 1 | ||||
-rw-r--r-- | lib/api/subscriptions.rb | 4 | ||||
-rw-r--r-- | lib/api/v3/subscriptions.rb | 53 |
3 files changed, 56 insertions, 2 deletions
diff --git a/lib/api/api.rb b/lib/api/api.rb index e729c07f8c3..2e51be9fff3 100644 --- a/lib/api/api.rb +++ b/lib/api/api.rb @@ -17,6 +17,7 @@ module API mount ::API::V3::Projects mount ::API::V3::ProjectSnippets mount ::API::V3::Repositories + mount ::API::V3::Subscriptions mount ::API::V3::SystemHooks mount ::API::V3::Tags mount ::API::V3::Todos diff --git a/lib/api/subscriptions.rb b/lib/api/subscriptions.rb index e11d7537cc9..acf11dbdf26 100644 --- a/lib/api/subscriptions.rb +++ b/lib/api/subscriptions.rb @@ -21,7 +21,7 @@ module API desc 'Subscribe to a resource' do success entity_class end - post ":id/#{type}/:subscribable_id/subscription" do + post ":id/#{type}/:subscribable_id/subscribe" do resource = instance_exec(params[:subscribable_id], &finder) if resource.subscribed?(current_user, user_project) @@ -35,7 +35,7 @@ module API desc 'Unsubscribe from a resource' do success entity_class end - delete ":id/#{type}/:subscribable_id/subscription" do + post ":id/#{type}/:subscribable_id/unsubscribe" do resource = instance_exec(params[:subscribable_id], &finder) if !resource.subscribed?(current_user, user_project) diff --git a/lib/api/v3/subscriptions.rb b/lib/api/v3/subscriptions.rb new file mode 100644 index 00000000000..02a4157c26e --- /dev/null +++ b/lib/api/v3/subscriptions.rb @@ -0,0 +1,53 @@ +module API + module V3 + class Subscriptions < Grape::API + before { authenticate! } + + subscribable_types = { + 'merge_request' => proc { |id| find_merge_request_with_access(id, :update_merge_request) }, + 'merge_requests' => proc { |id| find_merge_request_with_access(id, :update_merge_request) }, + 'issues' => proc { |id| find_project_issue(id) }, + 'labels' => proc { |id| find_project_label(id) }, + } + + params do + requires :id, type: String, desc: 'The ID of a project' + requires :subscribable_id, type: String, desc: 'The ID of a resource' + end + resource :projects do + subscribable_types.each do |type, finder| + type_singularized = type.singularize + entity_class = ::API::Entities.const_get(type_singularized.camelcase) + + desc 'Subscribe to a resource' do + success entity_class + end + post ":id/#{type}/:subscribable_id/subscription" do + resource = instance_exec(params[:subscribable_id], &finder) + + if resource.subscribed?(current_user, user_project) + not_modified! + else + resource.subscribe(current_user, user_project) + present resource, with: entity_class, current_user: current_user, project: user_project + end + end + + desc 'Unsubscribe from a resource' do + success entity_class + end + delete ":id/#{type}/:subscribable_id/subscription" do + resource = instance_exec(params[:subscribable_id], &finder) + + if !resource.subscribed?(current_user, user_project) + not_modified! + else + resource.unsubscribe(current_user, user_project) + present resource, with: entity_class, current_user: current_user, project: user_project + end + end + end + end + end + end +end |