Class: Collavre::Creatives::CommentBadgeIndex

Inherits:
Object
  • Object
show all
Defined in:
app/services/collavre/creatives/comment_badge_index.rb

Overview

Batches the browse tree's comment badge. Per node this was two queries — a read-pointer lookup and an unread COUNT — which is what made the badge cost scale with the size of the rendered tree.

Keyed by origin: a linked shell shows its origin's comments.

Instance Method Summary collapse

Constructor Details

#initialize(user:) ⇒ CommentBadgeIndex

Returns a new instance of CommentBadgeIndex.



9
10
11
12
# File 'app/services/collavre/creatives/comment_badge_index.rb', line 9

def initialize(user:)
  @user = user
  @unread_by_origin_id = {}
end

Instance Method Details

#index(origins) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/services/collavre/creatives/comment_badge_index.rb', line 14

def index(origins)
  pending = origins.uniq(&:id).reject { |o| @unread_by_origin_id.key?(o.id) }
  return if pending.empty?

  watermarks = read_watermarks(pending.map(&:id))

  # No read pointer means nothing has been read yet, so every comment counts
  # as unread — including private ones, matching the counter cache.
  pending.each do |origin|
    @unread_by_origin_id[origin.id] = origin.comments_count unless watermarks.key?(origin.id)
  end

  counts = unread_counts(watermarks)
  watermarks.each_key { |origin_id| @unread_by_origin_id[origin_id] = counts.fetch(origin_id, 0) }
end

#unread_count_for(origin) ⇒ Object

nil when the origin was never indexed, which tells the caller to fall back to the single-creative path rather than silently render a zero badge.



32
33
34
# File 'app/services/collavre/creatives/comment_badge_index.rb', line 32

def unread_count_for(origin)
  @unread_by_origin_id[origin.id]
end