Class: Collavre::CommentReadPointer

Inherits:
ApplicationRecord show all
Defined in:
app/models/collavre/comment_read_pointer.rb

Instance Method Summary collapse

Instance Method Details

#effective_comment_id(sorted_visible_ids) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/models/collavre/comment_read_pointer.rb', line 11

def effective_comment_id(sorted_visible_ids)
  return nil unless last_read_comment_id

  # Find the nearest visible comment ID <= last_read_comment_id
  idx = sorted_visible_ids.bsearch_index { |x| x > last_read_comment_id }

  if idx
    # If idx is 0, target is smaller than all visible IDs
    idx > 0 ? sorted_visible_ids[idx - 1] : nil
  else
    # target is >= all visible IDs
    sorted_visible_ids.last
  end
end