summaryrefslogtreecommitdiff
path: root/lib/api/todos.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/api/todos.rb')
-rw-r--r--lib/api/todos.rb32
1 files changed, 31 insertions, 1 deletions
diff --git a/lib/api/todos.rb b/lib/api/todos.rb
index 64ac8ece56c..d2196f05173 100644
--- a/lib/api/todos.rb
+++ b/lib/api/todos.rb
@@ -6,6 +6,8 @@ module API
before { authenticate! }
+ helpers ::Gitlab::IssuableMetadata
+
ISSUABLE_TYPES = {
'merge_requests' => ->(iid) { find_merge_request_with_access(iid) },
'issues' => ->(iid) { find_project_issue(iid) }
@@ -42,6 +44,30 @@ module API
def find_todos
TodosFinder.new(current_user, params).execute
end
+
+ def issuable_and_awardable?(type)
+ obj_type = Object.const_get(type)
+
+ (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)
+ options[type] = { issuable_metadata: issuable_meta_data(targets, type) }
+ end
+ end
end
desc 'Get a todo list' do
@@ -51,7 +77,11 @@ module API
use :pagination
end
get do
- present paginate(find_todos), with: Entities::Todo, current_user: current_user
+ todos = paginate(find_todos.with_api_entity_associations)
+ options = { with: Entities::Todo, current_user: current_user }
+ batch_load_issuable_metadata(todos, options)
+
+ present todos, options
end
desc 'Mark a todo as done' do