2018-08-03 13:22:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-03-09 20:29:11 -05:00
|
|
|
module Noteable
|
2019-02-06 05:31:46 -05:00
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2019-09-02 07:12:20 -04:00
|
|
|
# This object is used to gather noteable meta data for list displays
|
|
|
|
# avoiding n+1 queries and improving performance.
|
|
|
|
NoteableMeta = Struct.new(:user_notes_count)
|
|
|
|
|
2019-10-11 17:05:59 -04:00
|
|
|
MAX_NOTES_LIMIT = 5_000
|
|
|
|
|
2019-02-06 05:31:46 -05:00
|
|
|
class_methods do
|
|
|
|
# `Noteable` class names that support replying to individual notes.
|
|
|
|
def replyable_types
|
|
|
|
%w(Issue MergeRequest)
|
|
|
|
end
|
2019-06-03 10:41:05 -04:00
|
|
|
|
|
|
|
# `Noteable` class names that support resolvable notes.
|
|
|
|
def resolvable_types
|
2020-05-07 08:09:46 -04:00
|
|
|
%w(MergeRequest DesignManagement::Design)
|
2019-06-03 10:41:05 -04:00
|
|
|
end
|
2021-01-04 22:10:19 -05:00
|
|
|
|
|
|
|
# `Noteable` class names that support creating/forwarding individual notes.
|
|
|
|
def email_creatable_types
|
|
|
|
%w(Issue)
|
|
|
|
end
|
2019-02-06 05:31:46 -05:00
|
|
|
end
|
|
|
|
|
2019-04-16 03:40:25 -04:00
|
|
|
# The timestamp of the note (e.g. the :created_at or :updated_at attribute if provided via
|
2019-04-08 11:33:30 -04:00
|
|
|
# API call)
|
|
|
|
def system_note_timestamp
|
2020-05-22 05:08:09 -04:00
|
|
|
@system_note_timestamp || Time.current # rubocop:disable Gitlab/ModuleWithInstanceVariables
|
2019-04-08 11:33:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
attr_writer :system_note_timestamp
|
|
|
|
|
2017-04-06 11:05:57 -04:00
|
|
|
def base_class_name
|
|
|
|
self.class.base_class.name
|
|
|
|
end
|
|
|
|
|
|
|
|
# Convert this Noteable class name to a format usable by notifications.
|
|
|
|
#
|
|
|
|
# Examples:
|
|
|
|
#
|
|
|
|
# noteable.class # => MergeRequest
|
|
|
|
# noteable.human_class_name # => "merge request"
|
|
|
|
def human_class_name
|
|
|
|
@human_class_name ||= base_class_name.titleize.downcase
|
|
|
|
end
|
|
|
|
|
|
|
|
def supports_resolvable_notes?
|
2019-06-03 10:41:05 -04:00
|
|
|
self.class.resolvable_types.include?(base_class_name)
|
2017-04-06 11:05:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def supports_discussions?
|
2018-10-26 09:23:58 -04:00
|
|
|
DiscussionNote.noteable_types.include?(base_class_name)
|
2019-02-06 05:31:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def supports_replying_to_individual_notes?
|
|
|
|
supports_discussions? && self.class.replyable_types.include?(base_class_name)
|
2017-04-06 11:05:57 -04:00
|
|
|
end
|
|
|
|
|
2021-01-04 22:10:19 -05:00
|
|
|
def supports_creating_notes_by_email?
|
|
|
|
self.class.email_creatable_types.include?(base_class_name)
|
|
|
|
end
|
|
|
|
|
2018-12-13 14:17:19 -05:00
|
|
|
def supports_suggestion?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2017-08-17 13:26:45 -04:00
|
|
|
def discussions_rendered_on_frontend?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2018-12-16 11:00:43 -05:00
|
|
|
def preloads_discussion_diff_highlighting?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2020-06-18 14:08:58 -04:00
|
|
|
def has_any_diff_note_positions?
|
|
|
|
notes.any? && DiffNotePosition.where(note: notes).exists?
|
|
|
|
end
|
|
|
|
|
2017-03-09 20:29:11 -05:00
|
|
|
def discussion_notes
|
|
|
|
notes
|
|
|
|
end
|
|
|
|
|
2017-03-30 21:33:45 -04:00
|
|
|
delegate :find_discussion, to: :discussion_notes
|
2017-03-09 20:29:11 -05:00
|
|
|
|
|
|
|
def discussions
|
|
|
|
@discussions ||= discussion_notes
|
|
|
|
.inc_relations_for_view
|
|
|
|
.discussions(self)
|
|
|
|
end
|
|
|
|
|
2020-03-25 05:08:11 -04:00
|
|
|
def discussion_ids_relation
|
|
|
|
notes.select(:discussion_id)
|
|
|
|
.group(:discussion_id)
|
|
|
|
.order('MIN(created_at), MIN(id)')
|
|
|
|
end
|
|
|
|
|
2021-11-16 04:13:21 -05:00
|
|
|
# This does not consider OutOfContextDiscussions in MRs
|
|
|
|
# where notes from commits are overriden so that they have
|
|
|
|
# the same discussion_id
|
|
|
|
def discussion_root_note_ids(notes_filter:)
|
|
|
|
relations = []
|
|
|
|
|
|
|
|
relations << discussion_notes.select(
|
|
|
|
"'notes' AS table_name",
|
|
|
|
'discussion_id',
|
|
|
|
'MIN(id) AS id',
|
|
|
|
'MIN(created_at) AS created_at'
|
|
|
|
).with_notes_filter(notes_filter)
|
|
|
|
.group(:discussion_id)
|
|
|
|
|
|
|
|
if notes_filter != UserPreference::NOTES_FILTERS[:only_comments]
|
|
|
|
relations += synthetic_note_ids_relations
|
|
|
|
end
|
|
|
|
|
|
|
|
Note.from_union(relations, remove_duplicates: false).fresh
|
|
|
|
end
|
|
|
|
|
2019-08-25 19:55:00 -04:00
|
|
|
def capped_notes_count(max)
|
|
|
|
notes.limit(max).count
|
|
|
|
end
|
|
|
|
|
2017-03-31 19:39:14 -04:00
|
|
|
def grouped_diff_discussions(*args)
|
2017-04-04 18:27:23 -04:00
|
|
|
# Doesn't use `discussion_notes`, because this may include commit diff notes
|
2017-08-17 12:58:28 -04:00
|
|
|
# besides MR diff notes, that we do not want to display on the MR Changes tab.
|
2017-03-31 19:39:14 -04:00
|
|
|
notes.inc_relations_for_view.grouped_diff_discussions(*args)
|
2017-03-09 20:29:11 -05:00
|
|
|
end
|
2017-04-04 18:27:23 -04:00
|
|
|
|
2017-11-22 02:50:36 -05:00
|
|
|
# rubocop:disable Gitlab/ModuleWithInstanceVariables
|
2017-04-04 18:27:23 -04:00
|
|
|
def resolvable_discussions
|
2017-05-29 17:07:57 -04:00
|
|
|
@resolvable_discussions ||=
|
|
|
|
if defined?(@discussions)
|
|
|
|
@discussions.select(&:resolvable?)
|
|
|
|
else
|
|
|
|
discussion_notes.resolvable.discussions(self)
|
|
|
|
end
|
2017-04-04 18:27:23 -04:00
|
|
|
end
|
2017-11-22 02:50:36 -05:00
|
|
|
# rubocop:enable Gitlab/ModuleWithInstanceVariables
|
2017-04-04 18:27:23 -04:00
|
|
|
|
|
|
|
def discussions_resolvable?
|
|
|
|
resolvable_discussions.any?(&:resolvable?)
|
|
|
|
end
|
|
|
|
|
|
|
|
def discussions_resolved?
|
|
|
|
discussions_resolvable? && resolvable_discussions.none?(&:to_be_resolved?)
|
|
|
|
end
|
|
|
|
|
|
|
|
def discussions_to_be_resolved
|
|
|
|
@discussions_to_be_resolved ||= resolvable_discussions.select(&:to_be_resolved?)
|
|
|
|
end
|
|
|
|
|
|
|
|
def discussions_can_be_resolved_by?(user)
|
|
|
|
discussions_to_be_resolved.all? { |discussion| discussion.can_resolve?(user) }
|
|
|
|
end
|
2017-09-01 08:03:57 -04:00
|
|
|
|
|
|
|
def lockable?
|
|
|
|
[MergeRequest, Issue].include?(self.class)
|
|
|
|
end
|
2018-09-07 09:39:20 -04:00
|
|
|
|
|
|
|
def etag_caching_enabled?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def expire_note_etag_cache
|
|
|
|
return unless discussions_rendered_on_frontend?
|
|
|
|
return unless etag_caching_enabled?
|
|
|
|
|
|
|
|
Gitlab::EtagCaching::Store.new.touch(note_etag_key)
|
|
|
|
end
|
|
|
|
|
|
|
|
def note_etag_key
|
2020-05-07 08:09:46 -04:00
|
|
|
return Gitlab::Routing.url_helpers.designs_project_issue_path(project, issue, { vueroute: filename }) if self.is_a?(DesignManagement::Design)
|
|
|
|
|
2018-09-07 09:39:20 -04:00
|
|
|
Gitlab::Routing.url_helpers.project_noteable_notes_path(
|
|
|
|
project,
|
|
|
|
target_type: self.class.name.underscore,
|
|
|
|
target_id: id
|
|
|
|
)
|
|
|
|
end
|
2020-05-13 14:08:47 -04:00
|
|
|
|
|
|
|
def after_note_created(_note)
|
|
|
|
# no-op
|
|
|
|
end
|
|
|
|
|
|
|
|
def after_note_destroyed(_note)
|
|
|
|
# no-op
|
|
|
|
end
|
2021-01-04 22:10:19 -05:00
|
|
|
|
|
|
|
# Email address that an authorized user can send/forward an email to be added directly
|
|
|
|
# to an issue or merge request.
|
|
|
|
# example: incoming+h5bp-html5-boilerplate-8-1234567890abcdef123456789-issue-34@localhost.com
|
|
|
|
def creatable_note_email_address(author)
|
|
|
|
return unless supports_creating_notes_by_email?
|
|
|
|
|
|
|
|
project_email = project.new_issuable_address(author, self.class.name.underscore)
|
|
|
|
return unless project_email
|
|
|
|
|
|
|
|
project_email.sub('@', "-#{iid}@")
|
|
|
|
end
|
2021-11-16 04:13:21 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# Synthetic system notes don't have discussion IDs because these are generated dynamically
|
|
|
|
# in Ruby. These are always root notes anyway so we don't need to group by discussion ID.
|
|
|
|
def synthetic_note_ids_relations
|
|
|
|
[
|
|
|
|
resource_label_events.select("'resource_label_events'", "'NULL'", :id, :created_at),
|
|
|
|
resource_milestone_events.select("'resource_milestone_events'", "'NULL'", :id, :created_at),
|
|
|
|
resource_state_events.select("'resource_state_events'", "'NULL'", :id, :created_at)
|
|
|
|
]
|
|
|
|
end
|
2017-03-09 20:29:11 -05:00
|
|
|
end
|
2019-06-03 10:41:05 -04:00
|
|
|
|
|
|
|
Noteable.extend(Noteable::ClassMethods)
|
2019-09-13 09:26:31 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
Noteable::ClassMethods.prepend_mod_with('Noteable::ClassMethods')
|
|
|
|
Noteable.prepend_mod_with('Noteable')
|