根因 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>
91 lines
2.6 KiB
Dart
91 lines
2.6 KiB
Dart
import 'package:networks_sdk/networks_sdk.dart';
|
||
|
||
import 'package:im_app/core/foundation/api_paths.dart';
|
||
import 'package:im_app/domain/entities/message.dart';
|
||
|
||
// ── 发送消息响应 ───────────────────────────────────────────────────────────────
|
||
|
||
/// 发送消息接口响应
|
||
class SendMessageResponse {
|
||
final int messageId;
|
||
final int chatIdx;
|
||
final int sendTime;
|
||
|
||
const SendMessageResponse({
|
||
required this.messageId,
|
||
required this.chatIdx,
|
||
required this.sendTime,
|
||
});
|
||
|
||
factory SendMessageResponse.fromJson(Map<String, dynamic> json) =>
|
||
SendMessageResponse(
|
||
messageId:
|
||
(json['message_id'] ?? json['messageId'] ?? json['id'] ?? 0)
|
||
as int,
|
||
chatIdx:
|
||
(json['chat_idx'] ?? json['chatIdx'] ?? 0) as int,
|
||
sendTime:
|
||
(json['send_time'] ?? json['sendTime'] ?? 0) as int,
|
||
);
|
||
|
||
Message toEntity({
|
||
required int chatId,
|
||
required int sendId,
|
||
required String content,
|
||
required int typ,
|
||
}) =>
|
||
Message(
|
||
id: messageId > 0
|
||
? messageId
|
||
: -(DateTime.now().microsecondsSinceEpoch),
|
||
messageId: messageId,
|
||
chatId: chatId,
|
||
chatIdx: chatIdx,
|
||
sendId: sendId,
|
||
content: content,
|
||
typ: typ,
|
||
sendTime: sendTime,
|
||
);
|
||
}
|
||
|
||
// ── 发送消息请求 ───────────────────────────────────────────────────────────────
|
||
|
||
/// POST /app/api/chat/send-message — 发送文本消息
|
||
///
|
||
/// [typ] 消息类型:1 = 文本,详见服务端 MessageType 枚举。
|
||
/// [sendTime] Unix 时间戳(秒),由客户端生成,用于服务端去重。
|
||
class SendMessageRequest extends ApiRequestable<SendMessageResponse> {
|
||
final int chatId;
|
||
final String content;
|
||
final int typ;
|
||
final int sendTime;
|
||
|
||
const SendMessageRequest({
|
||
required this.chatId,
|
||
required this.content,
|
||
required this.typ,
|
||
required this.sendTime,
|
||
});
|
||
|
||
@override
|
||
String get path => ApiPaths.chatSendMessage;
|
||
|
||
@override
|
||
HttpMethod get method => HttpMethod.post;
|
||
|
||
@override
|
||
Map<String, dynamic> get parameters => {
|
||
'chat_id': chatId,
|
||
'content': content,
|
||
'typ': typ,
|
||
'send_time': sendTime,
|
||
};
|
||
|
||
@override
|
||
SendMessageResponse? decodeResponse(dynamic response) {
|
||
final data = (response as dynamic).data;
|
||
if (data is! Map<String, dynamic>) return null;
|
||
return SendMessageResponse.fromJson(data);
|
||
}
|
||
}
|