Skip to content

feat: list unread messages and mark chats read - #167

Merged
neoromantic merged 1 commit into
mainfrom
feat/unread
Nov 17, 2026
Merged

neoromantic merged 1 commit into
mainfrom
feat/unread

Conversation

@neoromantic

Copy link
Copy Markdown
Owner

What and why

"Summarize my unread messages" is a headline agent workflow, but tg could neither list unread chats or messages nor mark them read. The cache did not store any unread state either: chats_cache had no unread column.

  • tg chats list --unread lists only chats with unread messages. The order is the same as without the flag, and every chat item now has unreadCount.
  • tg messages unread [--chat <peer>] [--limit N] shows, for each chat with unread messages, its oldest unread messages from the cache: incoming ones above the read boundary, newest first, at most --limit per chat. Each chat also gets unreadCount, readInboxMaxId, returned, missing (unread messages the cache doesn't hold, which is common in large groups and channels the daemon doesn't sync) and nextAfter. --fresh reads the counts from Telegram and fetches the first unread page of each chat that is missing messages.
  • tg messages read --chat <peer> [--max-id <id>] calls messages.readHistory for private chats, basic groups and Saved Messages, and channels.readHistory for supergroups and channels. The default max_id is 0, which marks everything read. It then reads the dialog back (messages.getPeerDialogs) and caches the new unread count and boundary. Marking read happens only in the CLI. The daemon stays read-only.
  • chats_cache gets two new columns, unread_count and read_inbox_max_id. Old caches get them through ensureColumn and they start as NULL. Every dialog list sets them. Other writes to the row keep them.
  • The daemon keeps the counts current between dialog passes:
    • A new incoming message above both the read boundary and last_message_id adds one, so a message delivered twice counts once.
    • updateReadHistoryInbox and updateReadChannelInbox set both values from stillUnreadCount. Reads inside a single forum topic are skipped.
    • Live messages now also raise last_message_id and last_message_at.

Before / After

Before: tg chats list --unread failed with an unknown flag. tg messages unread and tg messages read did not exist.

After:

{ "chats": [ { "chat": { "id": -1001234567890, "title": "Team Chat", "username": "teamchat", "type": "supergroup" },
    "unreadCount": 5, "readInboxMaxId": 20, "missing": 3, "returned": 2, "nextAfter": 25, "items": [ … ], "source": "cache" } ],
  "unreadCount": 5, "missing": 3, "returned": 2, "limit": 20, "source": "cache", "stale": false }
{ "chatId": -1001234567890, "maxId": null, "method": "channels.readHistory", "unreadCount": 0, "readInboxMaxId": 30 }

Testing

  • bun run check
  • bun run test: 2266 pass. The new src/__tests__/read-state.test.ts uses a mocked client and checks:
    • the method and max_id for each peer type, plus the channel-from-message form
    • the cached state after a read
    • what is read from the cache, and when --fresh fetches
    • that dialog state survives other upserts and the --unread filter works
    • how the daemon applies new messages and read updates
  • bun run test:e2e: 379 pass. The new src/__e2e__/unread.e2e.test.ts covers:
    • chats list --unread and messages unread from a seeded cache with no credentials
    • messages read for a private chat, a basic group, a supergroup and a channel. These run through the fake Telegram client that tg api's e2e already uses, extended to answer readHistory and getPeerDialogs. Each test then checks that the cached count changed.
    • argument errors, and the credential error for --fresh

Not checked offline: real Telegram behaviour of the read calls and updates. That needs a live run.

Fixes #25

🤖 Generated with Claude Code

tg chats list --unread lists only chats with unread messages, and every
chat item has unreadCount. tg messages unread prints each chat's oldest
unread messages from the cache, with the unread count and how many of
them the cache lacks; --fresh reads both from Telegram. tg messages read
marks a chat read with messages.readHistory or channels.readHistory and
caches the chat's new unread state.

chats_cache gets unread_count and read_inbox_max_id from the dialog
list. The daemon raises the count for each new incoming message and takes
reads on other devices from updateReadHistoryInbox and
updateReadChannelInbox, so the counts stay current between dialog passes.

Fixes #25

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@neoromantic
neoromantic merged commit dff78b6 into main Nov 17, 2026
5 checks passed
@neoromantic
neoromantic deleted the feat/unread branch November 17, 2026 20:43
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.

Unread messages and mark as read

1 participant