Module: Noteable

Extended by:
ActiveSupport::Concern, ClassMethods
Included in:
AbuseReport, AlertManagement::Alert, Commit, DesignManagement::Design, Issue, MergeRequest, Snippet, WikiPage::Meta
Defined in:
app/models/concerns/noteable.rb,
app/channels/noteable/notes_channel.rb

Defined Under Namespace

Classes: NoteableMeta, NotesChannel

Constant Summary collapse

MAX_NOTES_LIMIT =
5_000

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#system_note_timestampObject

The timestamp of the note (e.g. the :created_at or :updated_at attribute if provided via API call)



31
32
33
# File 'app/models/concerns/noteable.rb', line 31

def system_note_timestamp
  @system_note_timestamp || Time.current # rubocop:disable Gitlab/ModuleWithInstanceVariables
end

Instance Method Details

#after_note_created(_note) ⇒ Object



176
177
178
# File 'app/models/concerns/noteable.rb', line 176

def after_note_created(_note)
  # no-op
end

#after_note_destroyed(_note) ⇒ Object



180
181
182
# File 'app/models/concerns/noteable.rb', line 180

def after_note_destroyed(_note)
  # no-op
end

#base_class_nameObject



37
38
39
# File 'app/models/concerns/noteable.rb', line 37

def base_class_name
  self.class.base_class.name
end

#broadcast_notes_changedObject



169
170
171
172
173
174
# File 'app/models/concerns/noteable.rb', line 169

def broadcast_notes_changed
  return unless discussions_rendered_on_frontend?
  return unless real_time_notes_enabled?

  Noteable::NotesChannel.broadcast_to(self, event: 'updated')
end

#capped_notes_count(max) ⇒ Object



124
125
126
# File 'app/models/concerns/noteable.rb', line 124

def capped_notes_count(max)
  notes.limit(max).count
end

#commenters(user: nil) ⇒ Object



200
201
202
203
204
205
206
# File 'app/models/concerns/noteable.rb', line 200

def commenters(user: nil)
  eligable_notes = notes.user

  eligable_notes = eligable_notes.not_internal unless user&.can?(:read_internal_note, self)

  User.where(id: eligable_notes.select(:author_id).distinct)
end

#creatable_note_email_address(author) ⇒ Object

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



187
188
189
190
191
192
193
194
# File 'app/models/concerns/noteable.rb', line 187

def creatable_note_email_address(author)
  return unless supports_creating_notes_by_email?

  project_email = project&.new_issuable_address(author, base_class_name.underscore)
  return unless project_email

  project_email.sub('@', "-#{iid}@")
end

#discussion_ids_relationObject



95
96
97
98
99
# File 'app/models/concerns/noteable.rb', line 95

def discussion_ids_relation
  notes.select(:discussion_id)
    .group(:discussion_id)
    .order('MIN(created_at), MIN(id)')
end

#discussion_notesObject



83
84
85
# File 'app/models/concerns/noteable.rb', line 83

def discussion_notes
  notes
end

#discussion_root_note_ids(notes_filter:) ⇒ Object

This does not consider OutOfContextDiscussions in MRs where notes from commits are overriden so that they have the same discussion_id



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'app/models/concerns/noteable.rb', line 104

def discussion_root_note_ids(notes_filter:)
  relations = []

  relations << discussion_notes.select(
    "'notes' AS table_name",
    'MIN(id) AS id',
    'MIN(created_at) AS created_at',
    'ARRAY_AGG(id) AS ids'
  ).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)
    .select(:table_name, :id, :created_at, :ids)
    .fresh
end

#discussionsObject



89
90
91
92
93
# File 'app/models/concerns/noteable.rb', line 89

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

#discussions_can_be_resolved_by?(user) ⇒ Boolean

Returns:

  • (Boolean)


157
158
159
# File 'app/models/concerns/noteable.rb', line 157

def discussions_can_be_resolved_by?(user)
  discussions_to_be_resolved.all? { |discussion| discussion.can_resolve?(user) }
end

#discussions_rendered_on_frontend?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'app/models/concerns/noteable.rb', line 71

def discussions_rendered_on_frontend?
  false
end

#discussions_resolvable?Boolean

rubocop:enable Gitlab/ModuleWithInstanceVariables

Returns:

  • (Boolean)


145
146
147
# File 'app/models/concerns/noteable.rb', line 145

def discussions_resolvable?
  resolvable_discussions.any?(&:resolvable?)
end

#discussions_resolved?Boolean

Returns:

  • (Boolean)


149
150
151
# File 'app/models/concerns/noteable.rb', line 149

def discussions_resolved?
  discussions_resolvable? && resolvable_discussions.none?(&:to_be_resolved?)
end

#discussions_to_be_resolvedObject



153
154
155
# File 'app/models/concerns/noteable.rb', line 153

def discussions_to_be_resolved
  @discussions_to_be_resolved ||= resolvable_discussions.select(&:to_be_resolved?)
end

#grouped_diff_discussionsObject



128
129
130
131
132
# File 'app/models/concerns/noteable.rb', line 128

def grouped_diff_discussions(...)
  # Doesn't use `discussion_notes`, because this may include commit diff notes
  # besides MR diff notes, that we do not want to display on the MR Changes tab.
  notes.inc_relations_for_view(self).grouped_diff_discussions(...)
end

#has_any_diff_note_positions?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'app/models/concerns/noteable.rb', line 79

def has_any_diff_note_positions?
  notes.any? && DiffNotePosition.where(note: notes).exists?
end

#human_class_nameObject

Convert this Noteable class name to a format usable by notifications.

Examples:

noteable.class           # => MergeRequest
noteable.human_class_name # => "merge request"


47
48
49
# File 'app/models/concerns/noteable.rb', line 47

def human_class_name
  @human_class_name ||= base_class_name.titleize.downcase
end

#lockable?Boolean

Returns:

  • (Boolean)


161
162
163
# File 'app/models/concerns/noteable.rb', line 161

def lockable?
  [MergeRequest, Issue].include?(self.class)
end

#noteable_target_type_nameObject



196
197
198
# File 'app/models/concerns/noteable.rb', line 196

def noteable_target_type_name
  model_name.singular
end

#preloads_discussion_diff_highlighting?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'app/models/concerns/noteable.rb', line 75

def preloads_discussion_diff_highlighting?
  false
end

#real_time_notes_enabled?Boolean

Returns:

  • (Boolean)


165
166
167
# File 'app/models/concerns/noteable.rb', line 165

def real_time_notes_enabled?
  false
end

#resolvable_discussionsObject

rubocop:disable Gitlab/ModuleWithInstanceVariables



135
136
137
138
139
140
141
142
# File 'app/models/concerns/noteable.rb', line 135

def resolvable_discussions
  @resolvable_discussions ||=
    if defined?(@discussions)
      @discussions.select(&:resolvable?)
    else
      discussion_notes.resolvable.discussions(self)
    end
end

#supports_creating_notes_by_email?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'app/models/concerns/noteable.rb', line 63

def supports_creating_notes_by_email?
  self.class.email_creatable_types.include?(base_class_name)
end

#supports_discussions?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'app/models/concerns/noteable.rb', line 55

def supports_discussions?
  DiscussionNote.noteable_types.include?(base_class_name)
end

#supports_replying_to_individual_notes?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'app/models/concerns/noteable.rb', line 59

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

#supports_resolvable_notes?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'app/models/concerns/noteable.rb', line 51

def supports_resolvable_notes?
  self.class.resolvable_types.include?(base_class_name)
end

#supports_suggestion?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'app/models/concerns/noteable.rb', line 67

def supports_suggestion?
  false
end