Skip to content

bridge: 新 peer 的第一条消息被当成已读,没有未读标识 - #24

Open
std-external wants to merge 1 commit into
std-microblock:mainfrom
std-external:fix/new-peer-unread
Open

std-external wants to merge 1 commit into
std-microblock:mainfrom
std-external:fix/new-peer-unread

Conversation

@std-external

Copy link
Copy Markdown
Contributor

问题

之前没在 crossgram 聊过的人(全新 peer)发来第一条消息时,客户端没有未读标识:会话列表不显示未读数,也没有未读分割线,看起来像已经读过了。老会话再来消息一切正常。

来源:群里 Anillc 报的(「如果有人之前没在 crossgram 给我发过消息,然后给我发第一条消息之后,似乎不会有未读的标识」)。

成因

这条消息本身没丢,是服务端把他报成了「已读」。

  1. 实时消息落库只写消息表:packages/bridge/src/platform-manager.ts:428 的 event.type === 'message' 分支只有 store.ingest(),从来没动过会话的 unreadCount。全仓库只有请求收件箱(message-store.ts:178)和 markRead(message-store.ts:601)会写这个计数。
  2. 会话首次创建时计数默认 0:message-store.ts:1754。
  3. 客户端第一次遇到这个会话(未读数未知),会反过来问服务端要 dialog 条目:messages.getPeerDialogs(Telegram Desktop 的 Histories::requestDialogEntry → history.cpp:1712-1723)。
  4. 桥从库里回答:dialogs.ts:519 用 store.readDialogs(...),_hydrateDialogs 只带 unreadCount 和最新一条 lastMessage,不带 readInboxMaxMessage;于是 dialogs.ts:4007 算出 readInboxMaxId = source.unreadCount > 0 ? 0 : topMessage,返回 unreadCount: 0, readInboxMaxId: topMessage。
  5. 客户端 applyDialogFields(history.cpp:3666-3681)拿到 0 + 「读到最新」,把这条消息标记成已读,标识就没有了。

老会话不受影响:客户端已知它的未读数(哪怕 0),来信由客户端本地 +1,不会发第 3 步那轮询问。

修复

来信落库后,如果这条消息是新行、不是自己发的、不是请求收件箱,并且该会话在库里还是 0 未读,就记成 1 未读。

packages/bridge/src/platform-manager.ts:428-446

} else if (event.type === 'message') {
  const result = await this._store.ingest(session, event.conversation, event.message)
  if (result.created
    && !event.message.outgoing
    && event.conversation.metadata?.requestInbox !== true) {
    await this._store.incrementConversationUnread(
      session, event.conversation, { onlyWhenRead: true },
    )
  }

incrementConversationUnread 本来就在(message-store.ts:766,之前没有任何调用点),这里给它加一个 onlyWhenRead 选项:只在计数为 0 时加 1。

为什么只在 0 时加:

  • 计数 > 0 的会话不需要补。客户端已经知道这个会话,来信由客户端本地 +1,服务端再补一次就是双计。
  • 重连/backlog 重放时平台快照已经把这几条算进 unreadCount 了,无脑 +1 会把 5 条未读变成 10 条。
  • 代价是「库里 2,实际 3」时会报 2,差一条不会造成「没有未读」,下一次 getDialogs 快照或 markRead 会覆盖成准确值。

验证

新增 packages/bridge/src/new-peer-first-unread.e2e.test.ts,用真 store + 真事件管线(PlatformSubscriptionManager)复现,再通过 messages.getPeerDialogs 看客户端拿到什么:

  • 新 peer 首条来信 → unreadCount: 1 且 readInboxMaxId < topMessage。修复前是 {"unreadCount":0,"readInboxMaxId":1073741824}(等于最新消息 id)。
  • 平台快照说 2 条未读、随后同批消息再以实时事件补发一遍 → 计数保持 2,不双计。
  • 自己发的消息 → 计数保持 0。

跑过的:

  • npx vitest run packages/bridge/src:855 通过,1 失败(voice/voice-worker-client,本机缺 cargo)。
  • npx vitest run --config vitest.mtproto-e2e.config.mts packages/bridge/src:74 通过,8 失败(android-rpc / member-pagination / notification-settings ×3 / send-observer / unread-mentions ×2),与本改动无关,把改动 stash 掉后同样失败。
  • npx tsc -p tsconfig.json --noEmit 通过。

后续(不在这个 PR 里)

精确计数可以把平台给的 readInboxMaxMessage / firstUnread(platform.ts:645-648 已经在传)持久化到会话行,未读数改成「读点之后的来信条数」,markRead 已经是这个口径(message-store.ts:601-660)。那需要加列并改 ingestDialogs / readDialogs,可以另开一个任务。

The stored conversation counter is the only unread state messages.getPeerDialogs
can answer with. A live inbound message never touched it, so the first message
of a peer the client has never seen made it request the dialog entry and get
unread_count=0 with read_inbox_max_id at the message itself, which the client
applied as already read: no badge, no unread divider.
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.

1 participant