debian-mirror-gitlab/app/models/concerns/noteable.rb

188 lines
4.8 KiB
Ruby
Raw Normal View History

2018-11-20 20:47:30 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
module Noteable
2019-03-02 22:35:43 +05:30
extend ActiveSupport::Concern
2019-12-04 20:38:33 +05:30
# 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-12-21 20:55:43 +05:30
MAX_NOTES_LIMIT = 5_000
2019-03-02 22:35:43 +05:30
class_methods do
# `Noteable` class names that support replying to individual notes.
def replyable_types
%w(Issue MergeRequest)
end
2019-09-04 21:01:54 +05:30
# `Noteable` class names that support resolvable notes.
def resolvable_types
2020-05-24 23:13:21 +05:30
%w(MergeRequest DesignManagement::Design)
2019-09-04 21:01:54 +05:30
end
2021-03-08 18:12:59 +05:30
# `Noteable` class names that support creating/forwarding individual notes.
def email_creatable_types
%w(Issue)
end
2019-03-02 22:35:43 +05:30
end
2019-07-31 22:56:46 +05:30
# The timestamp of the note (e.g. the :created_at or :updated_at attribute if provided via
# API call)
def system_note_timestamp
2020-06-23 00:09:42 +05:30
@system_note_timestamp || Time.current # rubocop:disable Gitlab/ModuleWithInstanceVariables
2019-07-31 22:56:46 +05:30
end
attr_writer :system_note_timestamp
2017-08-17 22:00:37 +05:30
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-09-04 21:01:54 +05:30
self.class.resolvable_types.include?(base_class_name)
2017-08-17 22:00:37 +05:30
end
def supports_discussions?
2018-12-13 13:39:08 +05:30
DiscussionNote.noteable_types.include?(base_class_name)
2019-03-02 22:35:43 +05:30
end
def supports_replying_to_individual_notes?
supports_discussions? && self.class.replyable_types.include?(base_class_name)
2017-08-17 22:00:37 +05:30
end
2021-03-08 18:12:59 +05:30
def supports_creating_notes_by_email?
self.class.email_creatable_types.include?(base_class_name)
end
2019-02-15 15:39:39 +05:30
def supports_suggestion?
false
end
2018-03-17 18:26:18 +05:30
def discussions_rendered_on_frontend?
false
end
2019-02-15 15:39:39 +05:30
def preloads_discussion_diff_highlighting?
false
end
2020-07-28 23:09:34 +05:30
def has_any_diff_note_positions?
notes.any? && DiffNotePosition.where(note: notes).exists?
end
2017-08-17 22:00:37 +05:30
def discussion_notes
notes
end
delegate :find_discussion, to: :discussion_notes
def discussions
@discussions ||= discussion_notes
.inc_relations_for_view
.discussions(self)
end
2020-04-22 19:07:51 +05:30
def discussion_ids_relation
notes.select(:discussion_id)
.group(:discussion_id)
.order('MIN(created_at), MIN(id)')
end
2019-12-04 20:38:33 +05:30
def capped_notes_count(max)
notes.limit(max).count
end
2017-08-17 22:00:37 +05:30
def grouped_diff_discussions(*args)
# Doesn't use `discussion_notes`, because this may include commit diff notes
2018-03-17 18:26:18 +05:30
# besides MR diff notes, that we do not want to display on the MR Changes tab.
2017-08-17 22:00:37 +05:30
notes.inc_relations_for_view.grouped_diff_discussions(*args)
end
2018-03-17 18:26:18 +05:30
# rubocop:disable Gitlab/ModuleWithInstanceVariables
2017-08-17 22:00:37 +05:30
def resolvable_discussions
2017-09-10 17:25:29 +05:30
@resolvable_discussions ||=
if defined?(@discussions)
@discussions.select(&:resolvable?)
else
discussion_notes.resolvable.discussions(self)
end
2017-08-17 22:00:37 +05:30
end
2018-03-17 18:26:18 +05:30
# rubocop:enable Gitlab/ModuleWithInstanceVariables
2017-08-17 22:00:37 +05:30
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
2018-03-17 18:26:18 +05:30
def lockable?
[MergeRequest, Issue].include?(self.class)
end
2018-11-20 20:47:30 +05:30
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-24 23:13:21 +05:30
return Gitlab::Routing.url_helpers.designs_project_issue_path(project, issue, { vueroute: filename }) if self.is_a?(DesignManagement::Design)
2018-11-20 20:47:30 +05:30
Gitlab::Routing.url_helpers.project_noteable_notes_path(
project,
target_type: self.class.name.underscore,
target_id: id
)
end
2020-05-24 23:13:21 +05:30
def after_note_created(_note)
# no-op
end
def after_note_destroyed(_note)
# no-op
end
2021-03-08 18:12:59 +05:30
# 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
2017-08-17 22:00:37 +05:30
end
2019-09-04 21:01:54 +05:30
Noteable.extend(Noteable::ClassMethods)
2019-12-04 20:38:33 +05:30
2021-06-08 01:23:25 +05:30
Noteable::ClassMethods.prepend_mod_with('Noteable::ClassMethods')
Noteable.prepend_mod_with('Noteable')