2018-09-29 22:34:47 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-03-11 19:04:42 +00:00
|
|
|
module API
|
|
|
|
class Todos < Grape::API
|
2016-12-04 17:11:19 +00:00
|
|
|
include PaginationParams
|
|
|
|
|
2016-03-11 19:04:42 +00:00
|
|
|
before { authenticate! }
|
|
|
|
|
2019-03-02 17:31:36 +00:00
|
|
|
helpers ::Gitlab::IssuableMetadata
|
|
|
|
|
2016-06-28 16:04:44 +00:00
|
|
|
ISSUABLE_TYPES = {
|
2017-02-28 08:29:14 +00:00
|
|
|
'merge_requests' => ->(iid) { find_merge_request_with_access(iid) },
|
|
|
|
'issues' => ->(iid) { find_project_issue(iid) }
|
2017-02-21 23:32:18 +00:00
|
|
|
}.freeze
|
2016-06-28 16:04:44 +00:00
|
|
|
|
2016-10-14 07:16:55 +00:00
|
|
|
params do
|
|
|
|
requires :id, type: String, desc: 'The ID of a project'
|
|
|
|
end
|
2019-01-16 12:09:29 +00:00
|
|
|
resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
|
2016-06-28 16:04:44 +00:00
|
|
|
ISSUABLE_TYPES.each do |type, finder|
|
2017-02-28 08:29:14 +00:00
|
|
|
type_id_str = "#{type.singularize}_iid".to_sym
|
2016-06-28 16:04:44 +00:00
|
|
|
|
2016-10-14 07:16:55 +00:00
|
|
|
desc 'Create a todo on an issuable' do
|
|
|
|
success Entities::Todo
|
|
|
|
end
|
|
|
|
params do
|
2017-02-28 08:29:14 +00:00
|
|
|
requires type_id_str, type: Integer, desc: 'The IID of an issuable'
|
2016-10-14 07:16:55 +00:00
|
|
|
end
|
2016-06-28 16:04:44 +00:00
|
|
|
post ":id/#{type}/:#{type_id_str}/todo" do
|
|
|
|
issuable = instance_exec(params[type_id_str], &finder)
|
|
|
|
todo = TodoService.new.mark_todo(issuable, current_user).first
|
|
|
|
|
|
|
|
if todo
|
|
|
|
present todo, with: Entities::Todo, current_user: current_user
|
|
|
|
else
|
|
|
|
not_modified!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-11 19:04:42 +00:00
|
|
|
resource :todos do
|
|
|
|
helpers do
|
2020-03-05 09:08:31 +00:00
|
|
|
def find_todos
|
|
|
|
TodosFinder.new(current_user, params).execute
|
|
|
|
end
|
|
|
|
|
2019-03-02 17:31:36 +00:00
|
|
|
def issuable_and_awardable?(type)
|
2019-10-16 03:06:12 +00:00
|
|
|
obj_type = Object.const_get(type, false)
|
2019-03-02 17:31:36 +00:00
|
|
|
|
|
|
|
(obj_type < Issuable) && (obj_type < Awardable)
|
|
|
|
rescue NameError
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def batch_load_issuable_metadata(todos, options)
|
|
|
|
# This should be paginated and will cause Rails to SELECT for all the Todos
|
|
|
|
todos_by_type = todos.group_by(&:target_type)
|
|
|
|
|
|
|
|
todos_by_type.keys.each do |type|
|
|
|
|
next unless issuable_and_awardable?(type)
|
|
|
|
|
|
|
|
collection = todos_by_type[type]
|
|
|
|
|
|
|
|
next unless collection
|
|
|
|
|
|
|
|
targets = collection.map(&:target)
|
2019-06-12 16:28:25 +00:00
|
|
|
options[type] = { issuable_metadata: issuable_meta_data(targets, type, current_user) }
|
2019-03-02 17:31:36 +00:00
|
|
|
end
|
|
|
|
end
|
2016-03-11 19:04:42 +00:00
|
|
|
end
|
|
|
|
|
2016-10-14 07:16:55 +00:00
|
|
|
desc 'Get a todo list' do
|
|
|
|
success Entities::Todo
|
|
|
|
end
|
2016-12-04 17:11:19 +00:00
|
|
|
params do
|
|
|
|
use :pagination
|
|
|
|
end
|
2016-03-11 19:04:42 +00:00
|
|
|
get do
|
2019-06-24 17:48:50 +00:00
|
|
|
todos = paginate(find_todos.with_entity_associations)
|
2019-03-02 17:31:36 +00:00
|
|
|
options = { with: Entities::Todo, current_user: current_user }
|
|
|
|
batch_load_issuable_metadata(todos, options)
|
|
|
|
|
|
|
|
present todos, options
|
2016-03-11 19:04:42 +00:00
|
|
|
end
|
|
|
|
|
2016-10-14 07:16:55 +00:00
|
|
|
desc 'Mark a todo as done' do
|
|
|
|
success Entities::Todo
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :id, type: Integer, desc: 'The ID of the todo being marked as done'
|
|
|
|
end
|
2017-02-21 09:00:33 +00:00
|
|
|
post ':id/mark_as_done' do
|
2017-04-21 09:36:34 +00:00
|
|
|
TodoService.new.mark_todos_as_done_by_ids(params[:id], current_user)
|
2018-01-18 23:10:19 +00:00
|
|
|
todo = current_user.todos.find(params[:id])
|
2016-03-11 19:04:42 +00:00
|
|
|
|
2017-04-21 09:36:34 +00:00
|
|
|
present todo, with: Entities::Todo, current_user: current_user
|
2016-03-11 19:04:42 +00:00
|
|
|
end
|
|
|
|
|
2016-10-14 07:16:55 +00:00
|
|
|
desc 'Mark all todos as done'
|
2017-02-21 09:00:33 +00:00
|
|
|
post '/mark_as_done' do
|
2016-05-21 17:01:11 +00:00
|
|
|
todos = find_todos
|
2016-07-08 16:42:47 +00:00
|
|
|
TodoService.new.mark_todos_as_done(todos, current_user)
|
2017-02-21 09:00:33 +00:00
|
|
|
|
|
|
|
no_content!
|
2016-03-11 19:04:42 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-08-15 13:16:29 +00:00
|
|
|
|
|
|
|
API::Todos.prepend_if_ee('EE::API::Todos')
|