getHistoryMessageList method

Future<MessageListResult> getHistoryMessageList({
  1. required int talkMode,
  2. required int toFromId,
  3. String cursor = '0',
  4. int count = 10,
  5. bool reportRead = false,
})

统一历史入口(cache-first):

  1. 先查本地缓存
  2. 有网则拉本页 → 增量 upsert
  3. 再查缓存返回(写库不完整时回退本页远端) reportRead:私聊打开时上报已读并拉取对端已读水位

Implementation

Future<MessageListResult> getHistoryMessageList({
  required int talkMode,
  required int toFromId,
  String cursor = '0',
  int count = 10,
  bool reportRead = false,
}) async {
  if (_net.isOnline) {
    try {
      final remote = await _fetchRecordsPage(
        talkMode: talkMode,
        toFromId: toFromId,
        cursor: cursor,
        limit: count,
      );
      if (remote.isNotEmpty) {
        final emptyIds = remote.where((m) => m.msgId.isEmpty).length;
        if (emptyIds > 0) {
          GoChatLogger.e(
            'getHistoryMessageList: $emptyIds/${remote.length} '
            'remote msgs missing msg_id',
          );
        }
        await _store.upsertMessages(remote);
      }
    } catch (e) {
      GoChatLogger.e('getHistoryMessageList network failed, use local', e);
    }
  }
  // 1) 先读本地
  var result = await _store.queryMessages(
    talkMode: talkMode,
    toFromId: toFromId,
    cursor: cursor,
    limit: count,
  );
  result = _resultFromPage(result.items, cursor: cursor, count: count);
  if (reportRead && talkMode == TalkMode.private) {
    unawaited(_reportAndFetchRead(talkMode, toFromId, result.items));
  }
  return result;
}