2018-08-03 17:22:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-03-10 01:29:11 +00:00
|
|
|
module Noteable
|
2019-02-06 10:31:46 +00:00
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2019-09-02 11:12:20 +00: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 21:05:59 +00:00
|
|
|
MAX_NOTES_LIMIT = 5_000
|
|
|
|
|
2019-02-06 10:31:46 +00:00
|
|
|
class_methods do
|
|
|
|
# `Noteable` class names that support replying to individual notes.
|
|
|
|
def replyable_types
|
|
|
|
%w(Issue MergeRequest)
|
|
|
|
end
|
2019-06-03 14:41:05 +00:00
|
|
|
|
|
|
|
# `Noteable` class names that support resolvable notes.
|
|
|
|
def resolvable_types
|
|
|
|
%w(MergeRequest)
|
|
|
|
end
|
2019-02-06 10:31:46 +00:00
|
|
|
end
|
|
|
|
|
2019-04-16 07:40:25 +00:00
|
|
|
# The timestamp of the note (e.g. the :created_at or :updated_at attribute if provided via
|
2019-04-08 15:33:30 +00:00
|
|
|
# API call)
|
|
|
|
def system_note_timestamp
|
|
|
|
@system_note_timestamp || Time.now # rubocop:disable Gitlab/ModuleWithInstanceVariables
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_writer :system_note_timestamp
|
|
|
|
|
2017-04-06 15:05:57 +00: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 14:41:05 +00:00
|
|
|
self.class.resolvable_types.include?(base_class_name)
|
2017-04-06 15:05:57 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def supports_discussions?
|
2018-10-26 13:23:58 +00:00
|
|
|
DiscussionNote.noteable_types.include?(base_class_name)
|
2019-02-06 10:31:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def supports_replying_to_individual_notes?
|
|
|
|
supports_discussions? && self.class.replyable_types.include?(base_class_name)
|
2017-04-06 15:05:57 +00:00
|
|
|
end
|
|
|
|
|
2018-12-13 19:17:19 +00:00
|
|
|
def supports_suggestion?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2017-08-17 17:26:45 +00:00
|
|
|
def discussions_rendered_on_frontend?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2018-12-16 16:00:43 +00:00
|
|
|
def preloads_discussion_diff_highlighting?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2017-03-10 01:29:11 +00:00
|
|
|
def discussion_notes
|
|
|
|
notes
|
|
|
|
end
|
|
|
|
|
2017-03-31 01:33:45 +00:00
|
|
|
delegate :find_discussion, to: :discussion_notes
|
2017-03-10 01:29:11 +00:00
|
|
|
|
|
|
|
def discussions
|
|
|
|
@discussions ||= discussion_notes
|
|
|
|
.inc_relations_for_view
|
|
|
|
.discussions(self)
|
|
|
|
end
|
|
|
|
|
2019-08-25 23:55:00 +00:00
|
|
|
def capped_notes_count(max)
|
|
|
|
notes.limit(max).count
|
|
|
|
end
|
|
|
|
|
2017-03-31 23:39:14 +00:00
|
|
|
def grouped_diff_discussions(*args)
|
2017-04-04 22:27:23 +00:00
|
|
|
# Doesn't use `discussion_notes`, because this may include commit diff notes
|
2017-08-17 16:58:28 +00:00
|
|
|
# besides MR diff notes, that we do not want to display on the MR Changes tab.
|
2017-03-31 23:39:14 +00:00
|
|
|
notes.inc_relations_for_view.grouped_diff_discussions(*args)
|
2017-03-10 01:29:11 +00:00
|
|
|
end
|
2017-04-04 22:27:23 +00:00
|
|
|
|
2017-11-22 07:50:36 +00:00
|
|
|
# rubocop:disable Gitlab/ModuleWithInstanceVariables
|
2017-04-04 22:27:23 +00:00
|
|
|
def resolvable_discussions
|
2017-05-29 21:07:57 +00:00
|
|
|
@resolvable_discussions ||=
|
|
|
|
if defined?(@discussions)
|
|
|
|
@discussions.select(&:resolvable?)
|
|
|
|
else
|
|
|
|
discussion_notes.resolvable.discussions(self)
|
|
|
|
end
|
2017-04-04 22:27:23 +00:00
|
|
|
end
|
2017-11-22 07:50:36 +00:00
|
|
|
# rubocop:enable Gitlab/ModuleWithInstanceVariables
|
2017-04-04 22:27:23 +00: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 12:03:57 +00:00
|
|
|
|
|
|
|
def lockable?
|
|
|
|
[MergeRequest, Issue].include?(self.class)
|
|
|
|
end
|
2018-09-07 13:39:20 +00: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
|
|
|
|
Gitlab::Routing.url_helpers.project_noteable_notes_path(
|
|
|
|
project,
|
|
|
|
target_type: self.class.name.underscore,
|
|
|
|
target_id: id
|
|
|
|
)
|
|
|
|
end
|
2017-03-10 01:29:11 +00:00
|
|
|
end
|
2019-06-03 14:41:05 +00:00
|
|
|
|
|
|
|
Noteable.extend(Noteable::ClassMethods)
|
2019-09-13 13:26:31 +00:00
|
|
|
|
|
|
|
Noteable::ClassMethods.prepend_if_ee('EE::Noteable::ClassMethods') # rubocop: disable Cop/InjectEnterpriseEditionModule
|
|
|
|
Noteable.prepend_if_ee('EE::Noteable')
|