Read about our upcoming Code of Conduct on this issue

noteable.rb 3.59 KB
Newer Older
1
2
# frozen_string_literal: true

3
module Noteable
4
5
  extend ActiveSupport::Concern

6
7
8
9
  # 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)

10
11
  MAX_NOTES_LIMIT = 5_000

12
13
14
15
16
  class_methods do
    # `Noteable` class names that support replying to individual notes.
    def replyable_types
      %w(Issue MergeRequest)
    end
17
18
19
20
21

    # `Noteable` class names that support resolvable notes.
    def resolvable_types
      %w(MergeRequest)
    end
22
23
  end

24
  # The timestamp of the note (e.g. the :created_at or :updated_at attribute if provided via
25
26
27
28
29
30
31
  # API call)
  def system_note_timestamp
    @system_note_timestamp || Time.now # rubocop:disable Gitlab/ModuleWithInstanceVariables
  end

  attr_writer :system_note_timestamp

32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
  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?
47
    self.class.resolvable_types.include?(base_class_name)
48
49
50
  end

  def supports_discussions?
51
    DiscussionNote.noteable_types.include?(base_class_name)
52
53
54
55
  end

  def supports_replying_to_individual_notes?
    supports_discussions? && self.class.replyable_types.include?(base_class_name)
56
57
  end

58
59
60
61
  def supports_suggestion?
    false
  end

Douwe Maan's avatar
Douwe Maan committed
62
63
64
65
  def discussions_rendered_on_frontend?
    false
  end

66
67
68
69
  def preloads_discussion_diff_highlighting?
    false
  end

70
71
72
73
  def discussion_notes
    notes
  end

Douwe Maan's avatar
Douwe Maan committed
74
  delegate :find_discussion, to: :discussion_notes
75
76
77
78
79
80
81

  def discussions
    @discussions ||= discussion_notes
      .inc_relations_for_view
      .discussions(self)
  end

82
83
84
85
  def capped_notes_count(max)
    notes.limit(max).count
  end

86
  def grouped_diff_discussions(*args)
Douwe Maan's avatar
Douwe Maan committed
87
    # Doesn't use `discussion_notes`, because this may include commit diff notes
88
    # besides MR diff notes, that we do not want to display on the MR Changes tab.
89
    notes.inc_relations_for_view.grouped_diff_discussions(*args)
90
  end
Douwe Maan's avatar
Douwe Maan committed
91

92
  # rubocop:disable Gitlab/ModuleWithInstanceVariables
Douwe Maan's avatar
Douwe Maan committed
93
  def resolvable_discussions
94
95
96
97
98
99
    @resolvable_discussions ||=
      if defined?(@discussions)
        @discussions.select(&:resolvable?)
      else
        discussion_notes.resolvable.discussions(self)
      end
Douwe Maan's avatar
Douwe Maan committed
100
  end
101
  # rubocop:enable Gitlab/ModuleWithInstanceVariables
Douwe Maan's avatar
Douwe Maan committed
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117

  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
118
119
120
121

  def lockable?
    [MergeRequest, Issue].include?(self.class)
  end
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140

  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
141
end
142
143

Noteable.extend(Noteable::ClassMethods)
144
145
146

Noteable::ClassMethods.prepend_if_ee('EE::Noteable::ClassMethods') # rubocop: disable Cop/InjectEnterpriseEditionModule
Noteable.prepend_if_ee('EE::Noteable')