summaryrefslogtreecommitdiff
path: root/app/services/todo_service.rb
diff options
context:
space:
mode:
Diffstat (limited to 'app/services/todo_service.rb')
-rw-r--r--app/services/todo_service.rb98
1 files changed, 45 insertions, 53 deletions
diff --git a/app/services/todo_service.rb b/app/services/todo_service.rb
index 55f888d5664..e6fb0d3c72e 100644
--- a/app/services/todo_service.rb
+++ b/app/services/todo_service.rb
@@ -30,7 +30,7 @@ class TodoService
# * mark all pending todos related to the target for the current user as done
#
def close_issue(issue, current_user)
- mark_pending_todos_as_done(issue, current_user)
+ resolve_todos_for_target(issue, current_user)
end
# When we destroy a todo target we should:
@@ -79,7 +79,7 @@ class TodoService
# * mark all pending todos related to the target for the current user as done
#
def close_merge_request(merge_request, current_user)
- mark_pending_todos_as_done(merge_request, current_user)
+ resolve_todos_for_target(merge_request, current_user)
end
# When merge a merge request we should:
@@ -87,7 +87,7 @@ class TodoService
# * mark all pending todos related to the target for the current user as done
#
def merge_merge_request(merge_request, current_user)
- mark_pending_todos_as_done(merge_request, current_user)
+ resolve_todos_for_target(merge_request, current_user)
end
# When a build fails on the HEAD of a merge request we should:
@@ -105,7 +105,7 @@ class TodoService
# * mark all pending todos related to the merge request for that user as done
#
def merge_request_push(merge_request, current_user)
- mark_pending_todos_as_done(merge_request, current_user)
+ resolve_todos_for_target(merge_request, current_user)
end
# When a build is retried to a merge request we should:
@@ -114,7 +114,7 @@ class TodoService
#
def merge_request_build_retried(merge_request)
merge_request.merge_participants.each do |user|
- mark_pending_todos_as_done(merge_request, user)
+ resolve_todos_for_target(merge_request, user)
end
end
@@ -151,76 +151,68 @@ class TodoService
# * mark all pending todos related to the awardable for the current user as done
#
def new_award_emoji(awardable, current_user)
- mark_pending_todos_as_done(awardable, current_user)
+ resolve_todos_for_target(awardable, current_user)
end
- # When marking pending todos as done we should:
+ # When assigning an alert we should:
#
- # * mark all pending todos related to the target for the current user as done
+ # * create a pending todo for new assignee if alert is assigned
#
- def mark_pending_todos_as_done(target, user)
- attributes = attributes_for_target(target)
- pending_todos(user, attributes).update_all(state: :done)
- user.update_todos_count_cache
+ def assign_alert(alert, current_user)
+ create_assignment_todo(alert, current_user, [])
end
- # When user marks some todos as done
- def mark_todos_as_done(todos, current_user)
- update_todos_state(todos, current_user, :done)
+ # When user marks an issue as todo
+ def mark_todo(issuable, current_user)
+ attributes = attributes_for_todo(issuable.project, issuable, current_user, Todo::MARKED)
+ create_todos(current_user, attributes)
end
- def mark_todos_as_done_by_ids(ids, current_user)
- todos = todos_by_ids(ids, current_user)
- mark_todos_as_done(todos, current_user)
+ def todo_exist?(issuable, current_user)
+ TodosFinder.new(current_user).any_for_target?(issuable, :pending)
end
- def mark_all_todos_as_done_by_user(current_user)
- todos = TodosFinder.new(current_user).execute
- mark_todos_as_done(todos, current_user)
- end
+ # Resolves all todos related to target
+ def resolve_todos_for_target(target, current_user)
+ attributes = attributes_for_target(target)
- def mark_todo_as_done(todo, current_user)
- return if todo.done?
+ resolve_todos(pending_todos(current_user, attributes), current_user)
+ end
- todo.update(state: :done)
+ def resolve_todos(todos, current_user, resolution: :done, resolved_by_action: :system_done)
+ todos_ids = todos.batch_update(state: resolution, resolved_by_action: resolved_by_action)
current_user.update_todos_count_cache
- end
- # When user marks some todos as pending
- def mark_todos_as_pending(todos, current_user)
- update_todos_state(todos, current_user, :pending)
+ todos_ids
end
- def mark_todos_as_pending_by_ids(ids, current_user)
- todos = todos_by_ids(ids, current_user)
- mark_todos_as_pending(todos, current_user)
- end
+ def resolve_todo(todo, current_user, resolution: :done, resolved_by_action: :system_done)
+ return if todo.done?
- # When user marks an issue as todo
- def mark_todo(issuable, current_user)
- attributes = attributes_for_todo(issuable.project, issuable, current_user, Todo::MARKED)
- create_todos(current_user, attributes)
- end
+ todo.update(state: resolution, resolved_by_action: resolved_by_action)
- def todo_exist?(issuable, current_user)
- TodosFinder.new(current_user).any_for_target?(issuable, :pending)
+ current_user.update_todos_count_cache
end
- private
+ def restore_todos(todos, current_user)
+ todos_ids = todos.batch_update(state: :pending)
- def todos_by_ids(ids, current_user)
- current_user.todos_limited_to(Array(ids))
+ current_user.update_todos_count_cache
+
+ todos_ids
end
- def update_todos_state(todos, current_user, state)
- todos_ids = todos.update_state(state)
+ def restore_todo(todo, current_user)
+ return if todo.pending?
- current_user.update_todos_count_cache
+ todo.update(state: :pending)
- todos_ids
+ current_user.update_todos_count_cache
end
+ private
+
def create_todos(users, attributes)
Array(users).map do |user|
next if pending_todos(user, attributes).exists?
@@ -252,16 +244,16 @@ class TodoService
return unless note.can_create_todo?
project = note.project
- target = note.noteable
+ target = note.noteable
- mark_pending_todos_as_done(target, author)
+ resolve_todos_for_target(target, author)
create_mention_todos(project, target, author, note, skip_users)
end
- def create_assignment_todo(issuable, author, old_assignees = [])
- if issuable.assignees.any?
- assignees = issuable.assignees - old_assignees
- attributes = attributes_for_todo(issuable.project, issuable, author, Todo::ASSIGNED)
+ def create_assignment_todo(target, author, old_assignees = [])
+ if target.assignees.any?
+ assignees = target.assignees - old_assignees
+ attributes = attributes_for_todo(target.project, target, author, Todo::ASSIGNED)
create_todos(assignees, attributes)
end
end