2018-12-05 23:21:45 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
module API
|
2021-01-03 14:25:43 +05:30
|
|
|
class Subscriptions < ::API::Base
|
2019-03-02 22:35:43 +05:30
|
|
|
helpers ::API::Helpers::LabelHelpers
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
before { authenticate! }
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
SUBSCRIBE_ENDPOINT_REQUIREMENTS = API::NAMESPACE_OR_PROJECT_REQUIREMENTS.merge(
|
|
|
|
subscribable_id: API::NO_SLASH_URL_PART_REGEX)
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
subscribables = [
|
|
|
|
{
|
|
|
|
type: 'merge_requests',
|
|
|
|
entity: Entities::MergeRequest,
|
|
|
|
source: Project,
|
2021-01-29 00:20:46 +05:30
|
|
|
finder: ->(id) { find_merge_request_with_access(id, :update_merge_request) },
|
|
|
|
feature_category: :code_review
|
2019-03-02 22:35:43 +05:30
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'issues',
|
|
|
|
entity: Entities::Issue,
|
|
|
|
source: Project,
|
2021-01-29 00:20:46 +05:30
|
|
|
finder: ->(id) { find_project_issue(id) },
|
|
|
|
feature_category: :issue_tracking
|
2019-03-02 22:35:43 +05:30
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'labels',
|
|
|
|
entity: Entities::ProjectLabel,
|
|
|
|
source: Project,
|
2021-01-29 00:20:46 +05:30
|
|
|
finder: ->(id) { find_label(user_project, id) },
|
|
|
|
feature_category: :issue_tracking
|
2019-03-02 22:35:43 +05:30
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'labels',
|
|
|
|
entity: Entities::GroupLabel,
|
|
|
|
source: Group,
|
2021-01-29 00:20:46 +05:30
|
|
|
finder: ->(id) { find_label(user_group, id) },
|
|
|
|
feature_category: :issue_tracking
|
2019-03-02 22:35:43 +05:30
|
|
|
}
|
|
|
|
]
|
2016-06-02 11:05:42 +05:30
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
subscribables.each do |subscribable|
|
|
|
|
source_type = subscribable[:source].name.underscore
|
2016-06-02 11:05:42 +05:30
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
params do
|
|
|
|
requires :id, type: String, desc: "The #{source_type} ID"
|
|
|
|
requires :subscribable_id, type: String, desc: 'The ID of a resource'
|
|
|
|
end
|
2021-03-11 19:13:27 +05:30
|
|
|
resource source_type.pluralize, requirements: SUBSCRIBE_ENDPOINT_REQUIREMENTS do
|
2017-08-17 22:00:37 +05:30
|
|
|
desc 'Subscribe to a resource' do
|
2019-03-02 22:35:43 +05:30
|
|
|
success subscribable[:entity]
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2021-01-29 00:20:46 +05:30
|
|
|
post ":id/#{subscribable[:type]}/:subscribable_id/subscribe", subscribable.slice(:feature_category) do
|
2019-03-02 22:35:43 +05:30
|
|
|
parent = parent_resource(source_type)
|
|
|
|
resource = instance_exec(params[:subscribable_id], &subscribable[:finder])
|
2016-06-02 11:05:42 +05:30
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
if resource.subscribed?(current_user, parent)
|
2016-06-02 11:05:42 +05:30
|
|
|
not_modified!
|
|
|
|
else
|
2019-03-02 22:35:43 +05:30
|
|
|
resource.subscribe(current_user, parent)
|
|
|
|
present resource, with: subscribable[:entity], current_user: current_user, project: parent, parent: parent
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
desc 'Unsubscribe from a resource' do
|
2019-03-02 22:35:43 +05:30
|
|
|
success subscribable[:entity]
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2021-01-29 00:20:46 +05:30
|
|
|
post ":id/#{subscribable[:type]}/:subscribable_id/unsubscribe", subscribable.slice(:feature_category) do
|
2019-03-02 22:35:43 +05:30
|
|
|
parent = parent_resource(source_type)
|
|
|
|
resource = instance_exec(params[:subscribable_id], &subscribable[:finder])
|
2016-06-02 11:05:42 +05:30
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
if !resource.subscribed?(current_user, parent)
|
2016-06-02 11:05:42 +05:30
|
|
|
not_modified!
|
|
|
|
else
|
2019-03-02 22:35:43 +05:30
|
|
|
resource.unsubscribe(current_user, parent)
|
|
|
|
present resource, with: subscribable[:entity], current_user: current_user, project: parent, parent: parent
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-03-02 22:35:43 +05:30
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
helpers do
|
|
|
|
def parent_resource(source_type)
|
|
|
|
case source_type
|
|
|
|
when 'project'
|
|
|
|
user_project
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
end
|