debian-mirror-gitlab/app/models/todo.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

270 lines
7.6 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2019-07-07 11:18:12 +05:30
class Todo < ApplicationRecord
2016-09-13 17:45:13 +05:30
include Sortable
2018-12-05 23:21:45 +05:30
include FromUnion
2022-01-26 12:08:38 +05:30
include EachBatch
2016-09-13 17:45:13 +05:30
2019-01-03 12:48:30 +05:30
# Time to wait for todos being removed when not visible for user anymore.
# Prevents TODOs being removed by mistake, for example, removing access from a user
# and giving it back again.
2020-07-28 23:09:34 +05:30
WAIT_FOR_DELETE = 1.hour
2019-01-03 12:48:30 +05:30
2020-07-28 23:09:34 +05:30
ASSIGNED = 1
MENTIONED = 2
BUILD_FAILED = 3
MARKED = 4
APPROVAL_REQUIRED = 5 # This is an EE-only feature
UNMERGEABLE = 6
DIRECTLY_ADDRESSED = 7
MERGE_TRAIN_REMOVED = 8 # This is an EE-only feature
2020-11-24 15:15:51 +05:30
REVIEW_REQUESTED = 9
2016-08-24 12:49:21 +05:30
ACTION_NAMES = {
ASSIGNED => :assigned,
2020-11-24 15:15:51 +05:30
REVIEW_REQUESTED => :review_requested,
2016-08-24 12:49:21 +05:30
MENTIONED => :mentioned,
BUILD_FAILED => :build_failed,
MARKED => :marked,
2017-08-17 22:00:37 +05:30
APPROVAL_REQUIRED => :approval_required,
UNMERGEABLE => :unmergeable,
2020-07-28 23:09:34 +05:30
DIRECTLY_ADDRESSED => :directly_addressed,
2022-08-27 11:52:29 +05:30
MERGE_TRAIN_REMOVED => :merge_train_removed
2017-08-17 22:00:37 +05:30
}.freeze
2016-04-02 18:10:28 +05:30
2022-05-07 20:08:51 +05:30
ACTIONS_MULTIPLE_ALLOWED = [Todo::MENTIONED, Todo::DIRECTLY_ADDRESSED].freeze
2016-04-02 18:10:28 +05:30
belongs_to :author, class_name: "User"
belongs_to :note
belongs_to :project
2018-11-18 11:00:15 +05:30
belongs_to :group
2019-07-07 11:18:12 +05:30
belongs_to :target, -> {
if self.klass.respond_to?(:with_api_entity_associations)
self.with_api_entity_associations
else
self
end
}, polymorphic: true, touch: true # rubocop:disable Cop/PolymorphicAssociations
2019-09-04 21:01:54 +05:30
2016-04-02 18:10:28 +05:30
belongs_to :user
2019-09-04 21:01:54 +05:30
belongs_to :issue, -> { where("target_type = 'Issue'") }, foreign_key: :target_id
2016-04-02 18:10:28 +05:30
delegate :name, :email, to: :author, prefix: true, allow_nil: true
2018-11-18 11:00:15 +05:30
validates :action, :target_type, :user, presence: true
2018-03-17 18:26:18 +05:30
validates :author, presence: true
2016-06-02 11:05:42 +05:30
validates :target_id, presence: true, unless: :for_commit?
validates :commit_id, presence: true, if: :for_commit?
2018-11-18 11:00:15 +05:30
validates :project, presence: true, unless: :group_id
validates :group, presence: true, unless: :project_id
2016-04-02 18:10:28 +05:30
scope :pending, -> { with_state(:pending) }
scope :done, -> { with_state(:done) }
2018-12-13 13:39:08 +05:30
scope :for_action, -> (action) { where(action: action) }
scope :for_author, -> (author) { where(author: author) }
2020-03-13 15:44:24 +05:30
scope :for_user, -> (user) { where(user: user) }
2019-12-26 22:10:19 +05:30
scope :for_project, -> (projects) { where(project: projects) }
2021-09-04 01:27:46 +05:30
scope :for_note, -> (notes) { where(note: notes) }
2019-12-26 22:10:19 +05:30
scope :for_undeleted_projects, -> { joins(:project).merge(Project.without_deleted) }
2018-12-13 13:39:08 +05:30
scope :for_group, -> (group) { where(group: group) }
scope :for_type, -> (type) { where(target_type: type) }
scope :for_target, -> (id) { where(target_id: id) }
scope :for_commit, -> (id) { where(commit_id: id) }
2021-12-07 22:27:20 +05:30
scope :with_entity_associations, -> { preload(:target, :author, :note, group: :route, project: [:route, { namespace: [:route, :owner] }]) }
2019-09-04 21:01:54 +05:30
scope :joins_issue_and_assignees, -> { left_joins(issue: :assignees) }
2022-07-29 17:44:30 +05:30
scope :for_internal_notes, -> { joins(:note).where(note: { confidential: true }) }
2016-04-02 18:10:28 +05:30
2020-06-23 00:09:42 +05:30
enum resolved_by_action: { system_done: 0, api_all_done: 1, api_done: 2, mark_all_done: 3, mark_done: 4 }, _prefix: :resolved_by
2016-04-02 18:10:28 +05:30
state_machine :state, initial: :pending do
event :done do
2016-06-02 11:05:42 +05:30
transition [:pending] => :done
2016-04-02 18:10:28 +05:30
end
state :pending
state :done
end
2018-11-18 11:00:15 +05:30
after_save :keep_around_commit, if: :commit_id
2016-08-24 12:49:21 +05:30
2016-09-13 17:45:13 +05:30
class << self
2019-12-21 20:55:43 +05:30
# Returns all todos for the given group ids and their descendants.
2018-12-13 13:39:08 +05:30
#
2019-12-21 20:55:43 +05:30
# group_ids - Group Ids to retrieve todos for.
2018-12-13 13:39:08 +05:30
#
# Returns an `ActiveRecord::Relation`.
2019-12-21 20:55:43 +05:30
def for_group_ids_and_descendants(group_ids)
groups = Group.groups_including_descendants_by(group_ids)
2018-12-13 13:39:08 +05:30
2022-10-11 01:57:18 +05:30
from_union(
[
for_project(Project.for_group(groups)),
for_group(groups)
])
2018-12-13 13:39:08 +05:30
end
2019-12-04 20:38:33 +05:30
# Returns `true` if the current user has any todos for the given target with the optional given state.
2018-12-13 13:39:08 +05:30
#
# target - The value of the `target_type` column, such as `Issue`.
2019-12-04 20:38:33 +05:30
# state - The value of the `state` column, such as `pending` or `done`.
def any_for_target?(target, state = nil)
state.nil? ? exists?(target: target) : exists?(target: target, state: state)
2018-12-13 13:39:08 +05:30
end
2020-06-23 00:09:42 +05:30
# Updates attributes of a relation of todos to the new state.
2018-12-13 13:39:08 +05:30
#
2020-06-23 00:09:42 +05:30
# new_attributes - The new attributes of the todos.
2018-12-13 13:39:08 +05:30
#
# Returns an `Array` containing the IDs of the updated todos.
2020-06-23 00:09:42 +05:30
def batch_update(**new_attributes)
# Only update those that have different state
base = where.not(state: new_attributes[:state]).except(:order)
2018-12-13 13:39:08 +05:30
ids = base.pluck(:id)
2020-06-23 00:09:42 +05:30
base.update_all(new_attributes.merge(updated_at: Time.current))
2018-12-13 13:39:08 +05:30
ids
end
2017-08-17 22:00:37 +05:30
# Priority sorting isn't displayed in the dropdown, because we don't show
# milestones, but still show something if the user has a URL with that
# selected.
2018-05-09 12:01:36 +05:30
def sort_by_attribute(method)
2018-03-27 19:54:05 +05:30
sorted =
case method.to_s
when 'priority', 'label_priority' then order_by_labels_priority
else order_by(method)
end
# Break ties with the ID column for pagination
sorted.order(id: :desc)
2016-09-13 17:45:13 +05:30
end
# Order by priority depending on which issue/merge request the Todo belongs to
# Todos with highest priority first then oldest todos
# Need to order by created_at last because of differences on Mysql and Postgres when joining by type "Merge_request/Issue"
def order_by_labels_priority
2021-02-22 17:27:13 +05:30
highest_priority = highest_label_priority(
2016-11-03 12:29:30 +05:30
target_type_column: "todos.target_type",
target_column: "todos.target_id",
project_column: "todos.project_id"
2022-06-21 17:19:12 +05:30
).arel.as('highest_priority')
2016-09-13 17:45:13 +05:30
2022-06-21 17:19:12 +05:30
select(arel_table[Arel.star], highest_priority)
.order(Arel.sql('highest_priority').asc.nulls_last)
2017-09-10 17:25:29 +05:30
.order('todos.created_at')
2016-09-13 17:45:13 +05:30
end
2021-04-29 21:17:54 +05:30
2021-06-08 01:23:25 +05:30
def distinct_user_ids
distinct.pluck(:user_id)
2021-04-29 21:17:54 +05:30
end
# Count todos grouped by user_id and state, using an UNION query
# so we can utilize the partial indexes for each state.
def count_grouped_by_user_id_and_state
grouped_count = select(:user_id, 'count(id) AS count').group(:user_id)
done = grouped_count.where(state: :done).select("'done' AS state")
pending = grouped_count.where(state: :pending).select("'pending' AS state")
union = unscoped.from_union([done, pending], remove_duplicates: false)
connection.select_all(union).each_with_object({}) do |row, counts|
counts[[row['user_id'], row['state']]] = row['count']
end
end
2016-09-13 17:45:13 +05:30
end
2019-12-21 20:55:43 +05:30
def resource_parent
2018-11-18 11:00:15 +05:30
project
end
2017-08-17 22:00:37 +05:30
def unmergeable?
action == UNMERGEABLE
end
2016-06-02 11:05:42 +05:30
def build_failed?
action == BUILD_FAILED
end
2017-08-17 22:00:37 +05:30
def assigned?
action == ASSIGNED
end
2020-11-24 15:15:51 +05:30
def review_requested?
action == REVIEW_REQUESTED
end
2020-07-28 23:09:34 +05:30
def merge_train_removed?
action == MERGE_TRAIN_REMOVED
end
2019-12-26 22:10:19 +05:30
def done?
state == 'done'
end
2016-08-24 12:49:21 +05:30
def action_name
ACTION_NAMES[action]
end
2016-04-02 18:10:28 +05:30
def body
if note.present?
note.note
else
target.title
end
end
2016-06-02 11:05:42 +05:30
def for_commit?
target_type == "Commit"
end
2020-05-24 23:13:21 +05:30
def for_design?
target_type == DesignManagement::Design.name
end
2020-06-23 00:09:42 +05:30
def for_alert?
target_type == AlertManagement::Alert.name
end
2022-08-13 15:12:31 +05:30
def for_issue_or_work_item?
[Issue.name, WorkItem.name].any? { |klass_name| target_type == klass_name }
end
2016-06-02 11:05:42 +05:30
# override to return commits, which are not active record
def target
if for_commit?
2022-08-27 11:52:29 +05:30
begin
project.commit(commit_id)
rescue StandardError
nil
end
2016-06-02 11:05:42 +05:30
else
super
end
end
def target_reference
if for_commit?
2019-12-04 20:38:33 +05:30
target.reference_link_text
2016-06-02 11:05:42 +05:30
else
2019-12-04 20:38:33 +05:30
target.to_reference
2016-06-02 11:05:42 +05:30
end
end
2016-08-24 12:49:21 +05:30
2017-08-17 22:00:37 +05:30
def self_added?
author == user
end
def self_assigned?
2021-01-03 14:25:43 +05:30
self_added? && (assigned? || review_requested?)
2017-08-17 22:00:37 +05:30
end
2016-08-24 12:49:21 +05:30
private
def keep_around_commit
project.repository.keep_around(self.commit_id)
end
2016-04-02 18:10:28 +05:30
end
2019-12-04 20:38:33 +05:30
2021-06-08 01:23:25 +05:30
Todo.prepend_mod_with('Todo')