2018-08-03 13:22:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-03-09 20:29:11 -05:00
|
|
|
module Noteable
|
2017-04-06 11:05:57 -04:00
|
|
|
# Names of all implementers of `Noteable` that support resolvable notes.
|
|
|
|
RESOLVABLE_TYPES = %w(MergeRequest).freeze
|
|
|
|
|
|
|
|
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?
|
|
|
|
RESOLVABLE_TYPES.include?(base_class_name)
|
|
|
|
end
|
|
|
|
|
|
|
|
def supports_discussions?
|
|
|
|
DiscussionNote::NOTEABLE_TYPES.include?(base_class_name)
|
|
|
|
end
|
|
|
|
|
2017-08-17 13:26:45 -04:00
|
|
|
def discussions_rendered_on_frontend?
|
|
|
|
false
|
|
|
|
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
|
|
|
|
|
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_resolvable? && !discussions_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
|
|
|
|
Gitlab::Routing.url_helpers.project_noteable_notes_path(
|
|
|
|
project,
|
|
|
|
target_type: self.class.name.underscore,
|
|
|
|
target_id: id
|
|
|
|
)
|
|
|
|
end
|
2017-03-09 20:29:11 -05:00
|
|
|
end
|