2018-09-29 18:34:47 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-05-12 16:48:09 -04:00
|
|
|
module API
|
2020-04-27 23:09:53 -04:00
|
|
|
class Subscriptions < Grape::API
|
2019-01-08 14:59:08 -05:00
|
|
|
helpers ::API::Helpers::LabelHelpers
|
2019-01-06 14:31:37 -05:00
|
|
|
|
2016-05-12 16:48:09 -04:00
|
|
|
before { authenticate! }
|
|
|
|
|
2018-08-25 16:29:27 -04:00
|
|
|
subscribables = [
|
2019-01-06 12:56:02 -05:00
|
|
|
{
|
|
|
|
type: 'merge_requests',
|
|
|
|
entity: Entities::MergeRequest,
|
|
|
|
source: Project,
|
|
|
|
finder: ->(id) { find_merge_request_with_access(id, :update_merge_request) }
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'issues',
|
|
|
|
entity: Entities::Issue,
|
|
|
|
source: Project,
|
|
|
|
finder: ->(id) { find_project_issue(id) }
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'labels',
|
|
|
|
entity: Entities::ProjectLabel,
|
|
|
|
source: Project,
|
|
|
|
finder: ->(id) { find_label(user_project, id) }
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'labels',
|
|
|
|
entity: Entities::GroupLabel,
|
|
|
|
source: Group,
|
|
|
|
finder: ->(id) { find_label(user_group, id) }
|
|
|
|
}
|
2018-08-25 16:29:27 -04:00
|
|
|
]
|
2016-05-12 16:48:09 -04:00
|
|
|
|
2018-08-25 16:29:27 -04:00
|
|
|
subscribables.each do |subscribable|
|
|
|
|
source_type = subscribable[:source].name.underscore
|
|
|
|
|
|
|
|
params do
|
|
|
|
requires :id, type: String, desc: "The #{source_type} ID"
|
|
|
|
requires :subscribable_id, type: String, desc: 'The ID of a resource'
|
|
|
|
end
|
|
|
|
resource source_type.pluralize, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
|
2016-11-09 10:57:14 -05:00
|
|
|
desc 'Subscribe to a resource' do
|
2018-12-28 04:47:00 -05:00
|
|
|
success subscribable[:entity]
|
2016-11-09 10:57:14 -05:00
|
|
|
end
|
2018-09-08 04:36:45 -04:00
|
|
|
post ":id/#{subscribable[:type]}/:subscribable_id/subscribe" do
|
2019-01-06 12:56:02 -05:00
|
|
|
parent = parent_resource(source_type)
|
2018-09-08 04:36:45 -04:00
|
|
|
resource = instance_exec(params[:subscribable_id], &subscribable[:finder])
|
2016-05-12 16:48:09 -04:00
|
|
|
|
2018-08-25 16:29:27 -04:00
|
|
|
if resource.subscribed?(current_user, parent)
|
2016-05-12 16:48:09 -04:00
|
|
|
not_modified!
|
|
|
|
else
|
2018-08-25 16:29:27 -04:00
|
|
|
resource.subscribe(current_user, parent)
|
2019-01-06 14:31:37 -05:00
|
|
|
present resource, with: subscribable[:entity], current_user: current_user, project: parent, parent: parent
|
2016-05-12 16:48:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-09 10:57:14 -05:00
|
|
|
desc 'Unsubscribe from a resource' do
|
2018-12-28 04:47:00 -05:00
|
|
|
success subscribable[:entity]
|
2016-11-09 10:57:14 -05:00
|
|
|
end
|
2018-09-08 04:36:45 -04:00
|
|
|
post ":id/#{subscribable[:type]}/:subscribable_id/unsubscribe" do
|
2019-01-06 12:56:02 -05:00
|
|
|
parent = parent_resource(source_type)
|
2018-09-08 04:36:45 -04:00
|
|
|
resource = instance_exec(params[:subscribable_id], &subscribable[:finder])
|
2018-08-25 16:29:27 -04:00
|
|
|
|
|
|
|
if !resource.subscribed?(current_user, parent)
|
2016-05-12 16:48:09 -04:00
|
|
|
not_modified!
|
|
|
|
else
|
2018-08-25 16:29:27 -04:00
|
|
|
resource.unsubscribe(current_user, parent)
|
2019-01-06 14:31:37 -05:00
|
|
|
present resource, with: subscribable[:entity], current_user: current_user, project: parent, parent: parent
|
2016-05-12 16:48:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-01-06 12:56:02 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
helpers do
|
|
|
|
def parent_resource(source_type)
|
|
|
|
case source_type
|
|
|
|
when 'project'
|
|
|
|
user_project
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-05-12 16:48:09 -04:00
|
|
|
end
|
|
|
|
end
|