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:
@@ -199,18 +199,27 @@ class _ChatDisplayItem {
|
||||
/// - 相邻消息 sendTime 差 < 5 秒
|
||||
/// - 最多 9 条一组
|
||||
List<_ChatDisplayItem> _buildDisplayItems(List<Message> msgs) {
|
||||
// 防御性排序:watchByChatId 已排序,此处确保即使上游未排序也能正确分组
|
||||
final sorted = [...msgs]..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);
|
||||
});
|
||||
|
||||
final items = <_ChatDisplayItem>[];
|
||||
int i = 0;
|
||||
// 使用 sorted 而非原始 msgs
|
||||
final src = sorted;
|
||||
|
||||
while (i < msgs.length) {
|
||||
final curr = msgs[i];
|
||||
while (i < src.length) {
|
||||
final curr = src[i];
|
||||
if ((curr.typ ?? 1) == 2) {
|
||||
final batch = <Message>[curr];
|
||||
int j = i + 1;
|
||||
|
||||
while (j < msgs.length && batch.length < 9) {
|
||||
final next = msgs[j];
|
||||
final prev = msgs[j - 1];
|
||||
while (j < src.length && batch.length < 9) {
|
||||
final next = src[j];
|
||||
final prev = src[j - 1];
|
||||
final timeDiff = ((next.sendTime ?? 0) - (prev.sendTime ?? 0)).abs();
|
||||
|
||||
if ((next.typ ?? 1) == 2 &&
|
||||
@@ -237,6 +246,7 @@ List<_ChatDisplayItem> _buildDisplayItems(List<Message> msgs) {
|
||||
return items;
|
||||
}
|
||||
|
||||
|
||||
// ── 显示项渲染 ────────────────────────────────────────────────────────────────
|
||||
|
||||
class _DisplayItemWidget extends StatelessWidget {
|
||||
|
||||
Reference in New Issue
Block a user