fix: 修复多图消息无法显示的三个根因

根因 1 — MessageItem.toEntity() id=0 主键碰撞:
  WS 拉取的每条消息均用 id=0 insertOrReplace,批量消息相互覆盖,
  DB 中只留最后一条。改为 id=messageId(服务端唯一 ID)。

根因 2 — SendMessageUseCase 乐观写入 id=0 碰撞:
  批量图片发送时所有乐观行共享 id=0,逐条覆盖。
  改用负微秒时间戳作为临时唯一 id,HTTP 确认后用真实 messageId 替换。

根因 3 — watchByChatId 无 ORDER BY:
  DB 消息顺序不确定,宫格分组算法依赖时间升序失败。
  在 MessageRepositoryImpl.watchByChatId 及 _buildDisplayItems 中
  分别按 sendTime ASC + chatIdx ASC 排序。

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
pp-bot
2026-03-24 15:45:41 +09:00
parent 2354e92c64
commit 2eb2299709
5 changed files with 68 additions and 13 deletions

View File

@@ -59,7 +59,17 @@ class MessageRepositoryImpl implements MessageRepository {
.watchWhere<DriftMessage, $MessagesTable>(
(t) => t.chatId.equals(chatId),
)
.map((rows) => rows.map(_toEntity).toList());
.map((rows) {
final entities = rows.map(_toEntity).toList();
// 按 sendTime ASC 排序sendTime 相同时按 chatIdx ASC
// 确保连续图片消息的宫格分组逻辑正确工作
entities.sort((a, b) {
final st = (a.sendTime ?? 0).compareTo(b.sendTime ?? 0);
if (st != 0) return st;
return (a.chatIdx ?? 0).compareTo(b.chatIdx ?? 0);
});
return entities;
});
}
@override