2016-02-20 08:59:59 -05:00
|
|
|
# TodoService class
|
2016-02-15 13:13:52 -05:00
|
|
|
#
|
2016-02-20 08:59:59 -05:00
|
|
|
# Used for creating todos after certain user actions
|
2016-02-15 13:13:52 -05:00
|
|
|
#
|
|
|
|
# Ex.
|
2016-02-20 08:59:59 -05:00
|
|
|
# TodoService.new.new_issue(issue, current_user)
|
2016-02-15 13:13:52 -05:00
|
|
|
#
|
2016-02-20 08:59:59 -05:00
|
|
|
class TodoService
|
2016-02-15 13:13:52 -05:00
|
|
|
# When create an issue we should:
|
|
|
|
#
|
2016-02-20 08:59:59 -05:00
|
|
|
# * create a todo for assignee if issue is assigned
|
|
|
|
# * create a todo for each mentioned user on issue
|
2016-02-15 13:13:52 -05:00
|
|
|
#
|
|
|
|
def new_issue(issue, current_user)
|
2016-02-16 18:01:14 -05:00
|
|
|
new_issuable(issue, current_user)
|
2016-02-15 13:13:52 -05:00
|
|
|
end
|
|
|
|
|
2016-02-17 17:04:14 -05:00
|
|
|
# When update an issue we should:
|
|
|
|
#
|
2016-02-20 08:59:59 -05:00
|
|
|
# * mark all pending todos related to the issue for the current user as done
|
2016-02-17 17:04:14 -05:00
|
|
|
#
|
|
|
|
def update_issue(issue, current_user)
|
2016-02-20 08:59:59 -05:00
|
|
|
create_mention_todos(issue.project, issue, current_user)
|
2016-02-17 17:04:14 -05:00
|
|
|
end
|
|
|
|
|
2016-02-16 15:31:40 -05:00
|
|
|
# When close an issue we should:
|
|
|
|
#
|
2016-02-20 08:59:59 -05:00
|
|
|
# * mark all pending todos related to the target for the current user as done
|
2016-02-16 15:31:40 -05:00
|
|
|
#
|
|
|
|
def close_issue(issue, current_user)
|
2016-02-20 08:59:59 -05:00
|
|
|
mark_pending_todos_as_done(issue, current_user)
|
2016-02-16 15:31:40 -05:00
|
|
|
end
|
|
|
|
|
2016-02-15 13:13:52 -05:00
|
|
|
# When we reassign an issue we should:
|
|
|
|
#
|
2016-02-20 08:59:59 -05:00
|
|
|
# * create a pending todo for new assignee if issue is assigned
|
2016-02-15 13:13:52 -05:00
|
|
|
#
|
|
|
|
def reassigned_issue(issue, current_user)
|
2016-02-20 08:59:59 -05:00
|
|
|
create_assignment_todo(issue, current_user)
|
2016-02-16 18:01:14 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# When create a merge request we should:
|
|
|
|
#
|
2016-02-20 08:59:59 -05:00
|
|
|
# * creates a pending todo for assignee if merge request is assigned
|
|
|
|
# * create a todo for each mentioned user on merge request
|
2016-02-16 18:01:14 -05:00
|
|
|
#
|
|
|
|
def new_merge_request(merge_request, current_user)
|
|
|
|
new_issuable(merge_request, current_user)
|
|
|
|
end
|
|
|
|
|
2016-02-17 17:04:14 -05:00
|
|
|
# When update a merge request we should:
|
|
|
|
#
|
2016-02-20 08:59:59 -05:00
|
|
|
# * create a todo for each mentioned user on merge request
|
2016-02-17 17:04:14 -05:00
|
|
|
#
|
|
|
|
def update_merge_request(merge_request, current_user)
|
2016-02-20 08:59:59 -05:00
|
|
|
create_mention_todos(merge_request.project, merge_request, current_user)
|
2016-02-17 17:04:14 -05:00
|
|
|
end
|
|
|
|
|
2016-02-16 18:47:33 -05:00
|
|
|
# When close a merge request we should:
|
|
|
|
#
|
2016-02-20 08:59:59 -05:00
|
|
|
# * mark all pending todos related to the target for the current user as done
|
2016-02-16 18:47:33 -05:00
|
|
|
#
|
|
|
|
def close_merge_request(merge_request, current_user)
|
2016-02-20 08:59:59 -05:00
|
|
|
mark_pending_todos_as_done(merge_request, current_user)
|
2016-02-16 18:47:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# When we reassign a merge request we should:
|
2016-02-16 18:01:14 -05:00
|
|
|
#
|
2016-02-20 08:59:59 -05:00
|
|
|
# * creates a pending todo for new assignee if merge request is assigned
|
2016-02-16 18:01:14 -05:00
|
|
|
#
|
|
|
|
def reassigned_merge_request(merge_request, current_user)
|
2016-02-20 08:59:59 -05:00
|
|
|
create_assignment_todo(merge_request, current_user)
|
2016-02-15 13:13:52 -05:00
|
|
|
end
|
|
|
|
|
2016-02-16 20:54:10 -05:00
|
|
|
# When merge a merge request we should:
|
|
|
|
#
|
2016-02-20 08:59:59 -05:00
|
|
|
# * mark all pending todos related to the target for the current user as done
|
2016-02-16 20:54:10 -05:00
|
|
|
#
|
|
|
|
def merge_merge_request(merge_request, current_user)
|
2016-02-20 08:59:59 -05:00
|
|
|
mark_pending_todos_as_done(merge_request, current_user)
|
2016-02-16 20:54:10 -05:00
|
|
|
end
|
|
|
|
|
2016-02-16 16:29:11 -05:00
|
|
|
# When create a note we should:
|
|
|
|
#
|
2016-02-20 08:59:59 -05:00
|
|
|
# * mark all pending todos related to the noteable for the note author as done
|
|
|
|
# * create a todo for each mentioned user on note
|
2016-02-16 16:29:11 -05:00
|
|
|
#
|
2016-02-18 16:12:52 -05:00
|
|
|
def new_note(note, current_user)
|
|
|
|
handle_note(note, current_user)
|
2016-02-16 16:29:11 -05:00
|
|
|
end
|
|
|
|
|
2016-02-16 17:21:24 -05:00
|
|
|
# When update a note we should:
|
|
|
|
#
|
2016-02-20 08:59:59 -05:00
|
|
|
# * mark all pending todos related to the noteable for the current user as done
|
|
|
|
# * create a todo for each new user mentioned on note
|
2016-02-16 17:21:24 -05:00
|
|
|
#
|
|
|
|
def update_note(note, current_user)
|
2016-02-18 16:12:52 -05:00
|
|
|
handle_note(note, current_user)
|
|
|
|
end
|
|
|
|
|
2016-02-20 08:59:59 -05:00
|
|
|
# When marking pending todos as done we should:
|
2016-02-18 16:12:52 -05:00
|
|
|
#
|
2016-02-20 08:59:59 -05:00
|
|
|
# * mark all pending todos related to the target for the current user as done
|
2016-02-18 16:12:52 -05:00
|
|
|
#
|
2016-02-20 08:59:59 -05:00
|
|
|
def mark_pending_todos_as_done(target, user)
|
2016-03-16 19:31:30 -04:00
|
|
|
attributes = attributes_for_target(target)
|
|
|
|
pending_todos(user, attributes).update_all(state: :done)
|
2016-02-18 16:12:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-03-16 19:31:30 -04:00
|
|
|
def create_todos(users, attributes)
|
2016-02-18 16:12:52 -05:00
|
|
|
Array(users).each do |user|
|
2016-03-16 19:31:30 -04:00
|
|
|
next if pending_todos(user, attributes).exists?
|
|
|
|
Todo.create(attributes.merge(user_id: user.id))
|
2016-02-18 16:12:52 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def new_issuable(issuable, author)
|
2016-02-20 08:59:59 -05:00
|
|
|
create_assignment_todo(issuable, author)
|
|
|
|
create_mention_todos(issuable.project, issuable, author)
|
2016-02-18 16:12:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def handle_note(note, author)
|
2016-03-16 19:31:30 -04:00
|
|
|
# Skip system notes, and notes on project snippet
|
2016-03-31 03:20:27 -04:00
|
|
|
return if note.system? || note.for_snippet?
|
2016-02-17 17:04:14 -05:00
|
|
|
|
2016-02-18 14:26:52 -05:00
|
|
|
project = note.project
|
|
|
|
target = note.noteable
|
2016-02-17 17:04:14 -05:00
|
|
|
|
2016-02-20 08:59:59 -05:00
|
|
|
mark_pending_todos_as_done(target, author)
|
|
|
|
create_mention_todos(project, target, author, note)
|
2016-02-18 16:12:52 -05:00
|
|
|
end
|
2016-02-17 17:04:14 -05:00
|
|
|
|
2016-02-20 08:59:59 -05:00
|
|
|
def create_assignment_todo(issuable, author)
|
2016-02-18 16:12:52 -05:00
|
|
|
if issuable.assignee && issuable.assignee != author
|
2016-03-16 19:31:30 -04:00
|
|
|
attributes = attributes_for_todo(issuable.project, issuable, author, Todo::ASSIGNED)
|
|
|
|
create_todos(issuable.assignee, attributes)
|
2016-02-16 17:21:24 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-16 19:31:30 -04:00
|
|
|
def create_mention_todos(project, target, author, note = nil)
|
|
|
|
mentioned_users = filter_mentioned_users(project, note || target, author)
|
|
|
|
attributes = attributes_for_todo(project, target, author, Todo::MENTIONED, note)
|
|
|
|
create_todos(mentioned_users, attributes)
|
|
|
|
end
|
|
|
|
|
|
|
|
def attributes_for_target(target)
|
|
|
|
attributes = {
|
|
|
|
project_id: target.project.id,
|
|
|
|
target_id: target.id,
|
|
|
|
target_type: target.class.name,
|
|
|
|
commit_id: nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if target.is_a?(Commit)
|
|
|
|
attributes.merge!(target_id: nil, commit_id: target.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
attributes
|
|
|
|
end
|
|
|
|
|
|
|
|
def attributes_for_todo(project, target, author, action, note = nil)
|
|
|
|
attributes_for_target(target).merge!(
|
|
|
|
project_id: project.id,
|
|
|
|
author_id: author.id,
|
|
|
|
action: action,
|
|
|
|
note: note
|
|
|
|
)
|
2016-02-15 13:13:52 -05:00
|
|
|
end
|
2016-02-15 21:26:33 -05:00
|
|
|
|
2016-02-18 16:12:52 -05:00
|
|
|
def filter_mentioned_users(project, target, author)
|
2016-03-30 17:41:21 -04:00
|
|
|
mentioned_users = target.mentioned_users
|
|
|
|
mentioned_users = reject_users_without_access(mentioned_users, project, target)
|
2016-02-17 12:46:26 -05:00
|
|
|
mentioned_users.delete(author)
|
|
|
|
mentioned_users.uniq
|
|
|
|
end
|
|
|
|
|
2016-03-30 17:41:21 -04:00
|
|
|
def reject_users_without_access(users, project, target)
|
|
|
|
if target.is_a?(Note) && target.for_issue?
|
|
|
|
target = target.noteable
|
|
|
|
end
|
|
|
|
|
|
|
|
if target.is_a?(Issue)
|
|
|
|
select_users(users, :read_issue, target)
|
|
|
|
else
|
|
|
|
select_users(users, :read_project, project)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def select_users(users, ability, subject)
|
|
|
|
users.select do |user|
|
|
|
|
user.can?(ability.to_sym, subject)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-16 19:31:30 -04:00
|
|
|
def pending_todos(user, criteria = {})
|
|
|
|
valid_keys = [:project_id, :target_id, :target_type, :commit_id]
|
|
|
|
user.todos.pending.where(criteria.slice(*valid_keys))
|
2016-02-16 18:01:14 -05:00
|
|
|
end
|
2016-02-15 13:13:52 -05:00
|
|
|
end
|