Skip to content

message.read event for thread reads incorrectly resets channel unreadCount to 0 #1676

@maximetinu

Description

@maximetinu

Description

When marking a thread as read via channel.markRead({ thread_id }), the resulting message.read event incorrectly resets channelState.unreadCount = 0, even though only the thread was marked as read, not the channel.

Location

stream-chat-js/src/channel.ts

Lines 1947 to 1950 in 32705e1

if (isOwnEvent) {
channelState.unreadCount = 0;
client.syncDeliveredCandidates([this]);
}

if (isOwnEvent) {
  channelState.unreadCount = 0;
  client.syncDeliveredCandidates([this]);
}

Steps to Reproduce

  1. User A sends a message M1 to User B in a 1:1 DM channel
  2. User A replies in a thread to M1 (creates thread T1)
  3. User B has: 1 unread channel message (M1) + 1 unread thread (T1)
  4. User B marks thread T1 as read via channel.markRead({ thread_id: T1.id })
  5. Bug: channel.state.unreadCount becomes 0, even though M1 is still unread

Expected Behavior

channel.state.unreadCount should remain unchanged when marking a thread as read. The channel unread count should only reset when the channel itself is marked as read (i.e., when markRead() is called without a thread_id).

Suggested Fix

Check if the message.read event is for a thread (via event.thread) before resetting the channel unread count:

case 'message.read':
  if (event.user?.id && event.created_at) {
    // ... existing read state update code ...

    const isOwnEvent = event.user?.id === client.user?.id;
    const isThreadRead = Boolean(event.thread);

    if (isOwnEvent && !isThreadRead) {  // ← Add thread check
      channelState.unreadCount = 0;
      client.syncDeliveredCandidates([this]);
    }
  }
  break;

Impact

This bug affects any application that:

  • Uses threads alongside channel messages
  • Displays separate unread counts for channels and threads
  • Expects marking a thread as read to NOT affect channel unread state

Workaround

We're currently working around this by maintaining our own lastRead timestamp that only updates on channel reads (not thread reads), and manually calculating unread count from messages.

Environment

  • stream-chat version: Latest (verified bug exists in current main branch)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions