2018-09-29 22:34:47 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-05-12 20:48:09 +00:00
|
|
|
module API
|
2020-10-15 00:08:42 +00:00
|
|
|
class Subscriptions < ::API::Base
|
2019-01-08 19:59:08 +00:00
|
|
|
helpers ::API::Helpers::LabelHelpers
|
2019-01-06 19:31:37 +00:00
|
|
|
|
2016-05-12 20:48:09 +00:00
|
|
|
before { authenticate! }
|
|
|
|
|
2021-02-01 09:09:28 +00:00
|
|
|
SUBSCRIBE_ENDPOINT_REQUIREMENTS = API::NAMESPACE_OR_PROJECT_REQUIREMENTS.merge(
|
|
|
|
subscribable_id: API::NO_SLASH_URL_PART_REGEX)
|
|
|
|
|
2018-08-25 20:29:27 +00:00
|
|
|
subscribables = [
|
2019-01-06 17:56:02 +00:00
|
|
|
{
|
|
|
|
type: 'merge_requests',
|
|
|
|
entity: Entities::MergeRequest,
|
|
|
|
source: Project,
|
2020-10-30 18:08:56 +00:00
|
|
|
finder: ->(id) { find_merge_request_with_access(id, :update_merge_request) },
|
|
|
|
feature_category: :code_review
|
2019-01-06 17:56:02 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'issues',
|
|
|
|
entity: Entities::Issue,
|
|
|
|
source: Project,
|
2020-10-30 18:08:56 +00:00
|
|
|
finder: ->(id) { find_project_issue(id) },
|
2021-10-27 15:13:41 +00:00
|
|
|
feature_category: :team_planning
|
2019-01-06 17:56:02 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'labels',
|
|
|
|
entity: Entities::ProjectLabel,
|
|
|
|
source: Project,
|
2020-10-30 18:08:56 +00:00
|
|
|
finder: ->(id) { find_label(user_project, id) },
|
2021-10-27 15:13:41 +00:00
|
|
|
feature_category: :team_planning
|
2019-01-06 17:56:02 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'labels',
|
|
|
|
entity: Entities::GroupLabel,
|
|
|
|
source: Group,
|
2020-10-30 18:08:56 +00:00
|
|
|
finder: ->(id) { find_label(user_group, id) },
|
2021-10-27 15:13:41 +00:00
|
|
|
feature_category: :team_planning
|
2019-01-06 17:56:02 +00:00
|
|
|
}
|
2018-08-25 20:29:27 +00:00
|
|
|
]
|
2016-05-12 20:48:09 +00:00
|
|
|
|
2018-08-25 20:29:27 +00: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
|
2021-02-01 09:09:28 +00:00
|
|
|
resource source_type.pluralize, requirements: SUBSCRIBE_ENDPOINT_REQUIREMENTS do
|
2016-11-09 15:57:14 +00:00
|
|
|
desc 'Subscribe to a resource' do
|
2018-12-28 09:47:00 +00:00
|
|
|
success subscribable[:entity]
|
2016-11-09 15:57:14 +00:00
|
|
|
end
|
2020-10-30 18:08:56 +00:00
|
|
|
post ":id/#{subscribable[:type]}/:subscribable_id/subscribe", subscribable.slice(:feature_category) do
|
2019-01-06 17:56:02 +00:00
|
|
|
parent = parent_resource(source_type)
|
2018-09-08 08:36:45 +00:00
|
|
|
resource = instance_exec(params[:subscribable_id], &subscribable[:finder])
|
2016-05-12 20:48:09 +00:00
|
|
|
|
2018-08-25 20:29:27 +00:00
|
|
|
if resource.subscribed?(current_user, parent)
|
2016-05-12 20:48:09 +00:00
|
|
|
not_modified!
|
|
|
|
else
|
2018-08-25 20:29:27 +00:00
|
|
|
resource.subscribe(current_user, parent)
|
2019-01-06 19:31:37 +00:00
|
|
|
present resource, with: subscribable[:entity], current_user: current_user, project: parent, parent: parent
|
2016-05-12 20:48:09 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-09 15:57:14 +00:00
|
|
|
desc 'Unsubscribe from a resource' do
|
2018-12-28 09:47:00 +00:00
|
|
|
success subscribable[:entity]
|
2016-11-09 15:57:14 +00:00
|
|
|
end
|
2020-10-30 18:08:56 +00:00
|
|
|
post ":id/#{subscribable[:type]}/:subscribable_id/unsubscribe", subscribable.slice(:feature_category) do
|
2019-01-06 17:56:02 +00:00
|
|
|
parent = parent_resource(source_type)
|
2018-09-08 08:36:45 +00:00
|
|
|
resource = instance_exec(params[:subscribable_id], &subscribable[:finder])
|
2018-08-25 20:29:27 +00:00
|
|
|
|
|
|
|
if !resource.subscribed?(current_user, parent)
|
2016-05-12 20:48:09 +00:00
|
|
|
not_modified!
|
|
|
|
else
|
2018-08-25 20:29:27 +00:00
|
|
|
resource.unsubscribe(current_user, parent)
|
2019-01-06 19:31:37 +00:00
|
|
|
present resource, with: subscribable[:entity], current_user: current_user, project: parent, parent: parent
|
2016-05-12 20:48:09 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-01-06 17:56:02 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
helpers do
|
|
|
|
def parent_resource(source_type)
|
|
|
|
case source_type
|
|
|
|
when 'project'
|
|
|
|
user_project
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-05-12 20:48:09 +00:00
|
|
|
end
|
|
|
|
end
|