2018-12-05 23:21:45 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
module API
|
|
|
|
class Subscriptions < Grape::API
|
2019-03-02 22:35:43 +05:30
|
|
|
helpers ::API::Helpers::LabelHelpers
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
before { authenticate! }
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
subscribables = [
|
|
|
|
{
|
|
|
|
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) }
|
|
|
|
}
|
|
|
|
]
|
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
|
|
|
|
resource source_type.pluralize, requirements: API::NAMESPACE_OR_PROJECT_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
|
2019-03-02 22:35:43 +05:30
|
|
|
post ":id/#{subscribable[:type]}/:subscribable_id/subscribe" do
|
|
|
|
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
|
2019-03-02 22:35:43 +05:30
|
|
|
post ":id/#{subscribable[:type]}/:subscribable_id/unsubscribe" do
|
|
|
|
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
|