Method: Noteable#discussion_root_note_ids

Defined in:
app/models/concerns/noteable.rb

#discussion_root_note_ids(notes_filter:, sort: :created_asc) ⇒ 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
123
124
125
126
127
128
129
130
131
# File 'app/models/concerns/noteable.rb', line 104

def discussion_root_note_ids(notes_filter:, sort: :created_asc)
  sort = :created_asc if sort.nil?
  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

  discussions = Note.from_union(relations, remove_duplicates: false)
                  .select(:table_name, :id, :created_at, :ids)

  case sort
  when :created_asc
    discussions.order_created_at_id_asc
  when :created_desc
    discussions.order_created_at_id_desc
  else
    raise ArgumentError, 'Invalid sort order'
  end
end