getMsgList method

Future<List<MsgModel>> getMsgList({
  1. required String convId,
  2. int? contentType,
  3. int? maxSeq,
  4. int size = 25,
  5. bool padding = true,
})

获取消息列表

Implementation

Future<List<MsgModel>> getMsgList({
  required String convId,
  int? contentType,
  int? maxSeq,
  int size = 25,
  bool padding = true,
}) async {
  bool includeUpper = maxSeq == null;
  if (maxSeq == null) {
    MsgModel? msgModel = await getFirstMsg(
      convId: convId,
    );
    if (msgModel != null) {
      maxSeq = msgModel.seq;
    } else {
      RecordModel? recordModel = await _sdkManager.findFirst(
        query: _sdkManager
            .recordModels()
            .filter()
            .convIdEqualTo(
              convId,
            )
            .build(),
      );
      if (recordModel != null) {
        maxSeq = recordModel.maxSeq;
      }
    }
  }
  if (maxSeq == null) return [];
  RecordModel? recordModel = await _sdkManager.findFirst(
    query: _sdkManager
        .recordModels()
        .filter()
        .convIdEqualTo(
          convId,
        )
        .build(),
  );
  int minSeq = maxSeq - size;
  if (recordModel != null) {
    minSeq = minSeq > recordModel.minSeq ? minSeq : recordModel.minSeq;
  } else {
    minSeq = minSeq > 0 ? minSeq : 0;
  }
  if (minSeq < 0) minSeq = 0;
  if (maxSeq <= minSeq) return [];
  if (padding) {
    List<String> expectList = SDKTool.generateSeqList(minSeq, maxSeq);
    List<MsgModel> localList = await _getMsgList(
      convId,
      contentType,
      minSeq,
      maxSeq,
    );
    if (expectList.length - localList.length != 0) {
      List<String> seqList = [];
      for (String seq in expectList) {
        int index = localList.indexWhere((msgModel) {
          return seq == msgModel.seq.toString();
        });
        if (index == -1) {
          seqList.add(seq);
        }
      }
      if (seqList.isNotEmpty) {
        await _sdkManager.pullMsgDataList(
          [
            BatchGetMsgListByConvIdReq_Item(
              convId: convId,
              seqList: seqList,
            ),
          ],
        );
      }
    }
  }
  return _getMsgList(
    convId,
    contentType,
    minSeq,
    maxSeq,
    includeUpper: includeUpper,
    deleted: false,
  );
}