getMessagesFromMergedMessage static method

Future<NIMResult<List<NIMMessage>>> getMessagesFromMergedMessage(
  1. MergedMessage mergedMsg
)

获取合并转发消息中的消息列表

Implementation

static Future<NIMResult<List<NIMMessage>>> getMessagesFromMergedMessage(
    MergedMessage mergedMsg) async {
  var directory;
  if (Platform.isIOS) {
    directory = await getApplicationDocumentsDirectory();
  } else {
    directory = await getExternalStorageDirectory();
  }
  if (directory == null) {
    return NIMResult.failure(message: 'directory is null');
  }
  final fileName = '$mergeFileName${mergedMsg.messageId}';
  final filePath = '${directory.path}/$fileName';
  //先下载文件
  final downloadRes = await NimCore.instance.nosService
      .download(url: mergedMsg.url, path: filePath);
  if (downloadRes.isSuccess) {
    //读取文件
    final file = File(filePath);
    if (file.existsSync()) {
      //校验md5
      var fileMd5 = await file.md5;
      if (fileMd5 != mergedMsg.md5) {
        return NIMResult.failure(message: 'file md5 not match');
      }
      //读取文件内容
      final List<int> fileStream = file.readAsBytesSync();
      final fileContent = utf8.decode(fileStream);
      final lines = fileContent.split('\n');
      List<NIMMessage> messages = List.empty(growable: true);
      if (lines.length > 1) {
        for (int i = 1; i < lines.length; i++) {
          //反序列化
          final msg =
              await MessageBuilder.convertJsonToMessage(json: lines[i]);
          if (msg.isSuccess && msg.data != null) {
            messages.add(msg.data!);
          }
        }
        return NIMResult.success(data: messages);
      } else {
        return NIMResult.failure(message: 'file is empty');
      }
    } else {
      return NIMResult.failure(message: 'file not exist');
    }
  } else {
    return NIMResult.failure(message: downloadRes.errorDetails);
  }
}