Skip to content

Conversation

Copilot
Copy link

@Copilot Copilot AI commented Aug 31, 2025

Problem

When a user deletes messages from a conversation, the unread message counters for other users are not updated to reflect that some of their unread messages have been deleted. This causes incorrect unread counts to be displayed in the UI.

Scenario:

  1. User A sends messages 1, 2, 3, 4, 5 to User B
  2. User B reads messages 1, 2 (readID = 2, unread count = 3)
  3. User A deletes messages 3, 4 (soft delete)
  4. User B should now have only 1 unread message (message 5), but the counter still shows 3

This affects both online users and users who come back online later.

Root Cause

The handleNoteBroadcast function calculates unread counts using simple arithmetic (pud.readID - msg.Note.SeqId) without considering that some messages in the range may have been deleted. The replyDelMsg function successfully deletes messages but doesn't update unread counters for affected users.

Solution

Added logic to update unread counters when messages are soft-deleted:

  1. Helper function calculateUnreadInRanges: Efficiently calculates how many unread messages fall within given deletion ranges using intersection logic.

  2. Enhanced replyDelMsg: After successful soft deletion, iterates through all users with read permissions and decreases their unread count by the number of unread messages that were deleted.

  3. Minimal performance impact: Only runs during message deletion operations (rare events), not during normal read operations.

Technical Details

  • Only affects soft deletes (per-user deletions) since hard deletes are handled differently
  • Uses existing usersUpdateUnread infrastructure for consistency
  • Preserves all existing functionality and API compatibility
  • Zero impact on read notification performance

Testing

  • Added comprehensive unit tests for calculateUnreadInRanges covering edge cases
  • Added integration test simulating the exact scenario from the issue
  • All existing tests continue to pass

Example calculation:

// User has readID=5, lastID=10 (unread messages: 6,7,8,9,10)
// Delete range [7,9) removes messages 7,8
// Result: 2 unread messages removed from count
ranges := []types.Range{{Low: 7, Hi: 9}}
deleted := calculateUnreadInRanges(5, 10, ranges) // Returns 2

Fixes #898


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

@Copilot Copilot AI changed the title [WIP] Propose a solution to this bug: @tinode/chat/issues/898 Fix unread message counter not accounting for deleted messages Aug 31, 2025
@Copilot Copilot AI requested a review from or-else August 31, 2025 13:05
Copilot finished work on behalf of or-else August 31, 2025 13:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

In-app unread message counter does not account for deleted messages

2 participants