messageLoad method

Future<EasyPacket<bool>> messageLoad({
  1. required Session session,
  2. required bool reload,
})

加载会话消息列表,reload为true时清除缓存重新加载,返回值EasyPacket.extra字段为true时表示已加载全部数据

Implementation

Future<EasyPacket<bool>> messageLoad({required Session session, required bool reload}) async {
  if (reload) session.msgcache.clear(); //清除缓存
  final last = session.msgcache.isEmpty ? 3742732800000 : session.msgcache.last.time; //2088-08-08 00:00:00 毫秒值 3742732800000
  final nin = <ObjectId>[]; //排除重复
  for (int i = session.msgcache.length - 1; i >= 0; i--) {
    final item = session.msgcache[i];
    if (item.time != last) break;
    nin.add(item.id);
  }
  session.msgasync = DateTime.now().microsecondsSinceEpoch; //设置最近一次异步加载的识别号(防止并发加载导致数据混乱)
  final response = await _aliveClient.websocketRequest('messageLoad', data: {'bsid': bsid, 'sid': session.sid, 'from': session.msgfrom, 'last': last, 'nin': nin, 'msgasync': session.msgasync});
  if (response.ok) {
    final msgasync = response.data!['msgasync'] as int;
    final msgList = response.data!['msgList'] as List;
    final shipList = response.data!['shipList'] as List?;
    final userList = response.data!['userList'] as List?;
    if (shipList != null && session.msgfrom == Constant.msgFromTeam) {
      _cacheTeamUserList(session.rid, shipList);
    }
    if (userList != null) {
      _cacheUserList(userList);
    }
    if (msgasync == session.msgasync) {
      for (var data in msgList) {
        final message = Message.fromJson(data);
        _fillMessage(message);
        session.msgcache.add(message);
      }
      return response.cloneExtra(msgList.isEmpty); //是否已加载全部数据
    } else {
      _aliveClient.logError(['messageLoad =>', '远程响应号已过期 $msgasync']);
      return response.requestTimeoutError().cloneExtra(null); //说明本次响应不是最近一次异步加载,直接遗弃数据,当成超时错误处理
    }
  } else {
    return response.cloneExtra(null);
  }
}