Skip to content

Persist chat unread counts from sync events #253

@drelum

Description

@drelum

Context

wacli already stores chat state locally and exposes chats list --unread, but the local chats.unread value currently behaves like a boolean. That makes downstream read-only integrations unable to reproduce the WhatsApp unread badge/count from wacli.db.

I hit this while building against the local SQLite store: I needed to list unread conversations and know how many unread messages each chat had, without mutating WhatsApp state.

Problem

Unread state can be incomplete or too lossy today:

  • history sync conversations can include an unread count, but it is not persisted as a count in the chat row;
  • live incoming messages should increment local unread state for that chat;
  • chats list --json / chats show --json expose only unread: true|false, so integrations cannot read the actual badge count;
  • --unread is more useful if it means unread_count > 0, not only an exact boolean value.

Proposed behavior

Treat chats.unread as the local unread-message count while preserving the existing boolean convenience field in CLI JSON:

  • add unread_count to chat JSON output;
  • keep unread as unread_count > 0 for backward compatibility;
  • persist Conversation.unread_count during history sync;
  • if WhatsApp marks a conversation as unread but count is absent/zero, store count 1;
  • increment unread count for live inbound messages, excluding own messages and status broadcasts;
  • keep mark-read setting count to 0 and mark-unread setting count to at least 1;
  • make chats list --unread filter unread > 0 and --no-unread filter unread = 0;
  • when PN/LID duplicate chat rows are merged for display, merge the unread counts too.

Patch reference

I have a working patch in my fork here:

drelum@2fd404e

The patch includes focused tests around:

  • live sync incrementing unread count;
  • history sync storing Conversation.unread_count;
  • store filtering/count behavior;
  • chat display flag formatting (unread:3);
  • PN/LID display merge preserving unread totals.

Why this helps

This makes wacli.db more useful for local agents, dashboards, and read-only integrations that need to prioritize unread conversations without connecting to WhatsApp or changing read state.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions