Module: Collavre::Comment::Broadcastable::ClassMethods

Defined in:
app/models/collavre/comment/broadcastable.rb

Instance Method Summary collapse

Instance Method Details

#broadcast_badge(creative, user) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/models/collavre/comment/broadcastable.rb', line 87

def broadcast_badge(creative, user)
  origin = creative.effective_origin
  visible_comments = origin.comments.visible_to(user)
  comments_count = visible_comments.count
  pointer = CommentReadPointer.find_by(user: user, creative: origin)
  last_read_id = pointer&.last_read_comment_id
  unread_scope = last_read_id ? visible_comments.where("comments.id > ?", last_read_id) : visible_comments
  unread_count = unread_scope.count
  unread_count = 0 if CommentPresenceStore.list(origin.id).include?(user.id)
  Turbo::StreamsChannel.broadcast_replace_to(
    [ user, origin, :comment_badge ],
    target: "comment-badge-#{origin.id}",
    partial: "inbox/badge_component/count",
    locals: {
      count: unread_count,
      badge_id: "comment-badge-#{origin.id}",
      show_zero: comments_count.positive?
    }
  )

  # Also update the global inbox badge when the creative is an inbox
  broadcast_inbox_badge(origin, user, count: unread_count) if origin.inbox?
end

#broadcast_badges(creative) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/models/collavre/comment/broadcastable.rb', line 21

def broadcast_badges(creative)
  origin = creative.effective_origin
  users = [ origin.user ].compact + origin.all_shared_users(:feedback).map(&:user)
  users.compact!
  users.uniq!
  return if users.empty?

  user_ids = users.map(&:id)

  pointers = CommentReadPointer.where(user_id: user_ids, creative: origin).index_by(&:user_id)
  present_user_ids = CommentPresenceStore.list(origin.id)

  public_count = origin.comments.public_only.count
  private_counts = origin.comments
    .where(private: true, user_id: user_ids)
    .group(:user_id)
    .count

  last_read_ids = pointers.transform_values { |p| p.last_read_comment_id || 0 }

  unread_public_by_threshold = {}
  # Include 0 for users without a read pointer (never opened chat)
  all_thresholds = (last_read_ids.values + [ 0 ]).uniq
  all_thresholds.each do |threshold|
    unread_public_by_threshold[threshold] = origin.comments
      .where(private: false)
      .where("comments.id > ?", threshold)
      .count
  end

  unread_private_counts = {}
  user_ids.each do |uid|
    threshold = last_read_ids[uid] || 0
    unread_private_counts[uid] = origin.comments
      .where(private: true, user_id: uid)
      .where("comments.id > ?", threshold)
      .count
  end

  users.each do |u|
    user_private_count = private_counts[u.id] || 0
    total_count = public_count + user_private_count

    threshold = last_read_ids[u.id] || 0
    unread_public = unread_public_by_threshold[threshold] || 0
    unread_private = unread_private_counts[u.id] || 0
    unread_count = unread_public + unread_private

    unread_count = 0 if present_user_ids.include?(u.id)

    Turbo::StreamsChannel.broadcast_replace_to(
      [ u, origin, :comment_badge ],
      target: "comment-badge-#{origin.id}",
      partial: "inbox/badge_component/count",
      locals: {
        count: unread_count,
        badge_id: "comment-badge-#{origin.id}",
        show_zero: total_count.positive?
      }
    )

    # Also update the global inbox badge when the creative is an inbox
    broadcast_inbox_badge(origin, u, count: unread_count) if origin.inbox?
  end
end

#broadcast_inbox_badge(inbox_creative, owner, count: nil) ⇒ Object

Broadcast updated inbox badge count to the user’s global inbox badge. Called when inbox comments are created (via Notifiable) and when the user reads their inbox (via read pointer update / presence unsubscribe). Accepts an optional pre-computed count to avoid duplicate queries.



115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'app/models/collavre/comment/broadcastable.rb', line 115

def broadcast_inbox_badge(inbox_creative, owner, count: nil)
  return unless inbox_creative && owner

  count ||= inbox_badge_count(inbox_creative, owner)

  INBOX_BADGE_TARGETS.each do |target_id|
    Turbo::StreamsChannel.broadcast_replace_to(
      [ "inbox", owner ],
      target: target_id,
      partial: "inbox/badge_component/count",
      locals: inbox_badge_locals(count, target_id)
    )
  end
end

#inbox_badge_turbo_stream(inbox_creative, owner, count: nil) ⇒ Object

Render the same inbox badge replacements as a Turbo Stream string so a channel can transmit them straight to its own confirmed subscriber (see InboxBadgeChannel), instead of re-broadcasting to the sibling

“inbox”, user

stream and risking a reconnect race. Returns nil when

there is nothing to render.



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'app/models/collavre/comment/broadcastable.rb', line 135

def inbox_badge_turbo_stream(inbox_creative, owner, count: nil)
  return unless inbox_creative && owner

  count ||= inbox_badge_count(inbox_creative, owner)

  INBOX_BADGE_TARGETS.map do |target_id|
    Turbo::StreamsChannel.turbo_stream_action_tag(
      :replace,
      target: target_id,
      template: ApplicationController.render(
        partial: "inbox/badge_component/count",
        formats: [ :html ],
        locals: inbox_badge_locals(count, target_id)
      )
    )
  end.join.html_safe
end