convertMessageFromWebToDart static method

Future<Map<String, dynamic>> convertMessageFromWebToDart(
  1. dynamic jsMessage, {
  2. dynamic isFromDownloadMergerMessage = false,
})

Implementation

static Future<Map<String, dynamic>> convertMessageFromWebToDart(jsMessage,
    {isFromDownloadMergerMessage = false}) async {
  final message = jsMessage is Map ? jsMessage : jsToMap(jsMessage);
  if (message.isEmpty) {
    return {};
  }
  final elementType = MsgType.convertMsgType(message['type']);
  final formatedMsg = <String, dynamic>{
    "msgID": message["ID"] ?? '',
    "timestamp": message["time"],
    "progress": 100,
    "sender": message["from"] ?? message["fromAccount"] ?? '',
    "nickName": message["nick"] ?? '',
    "friendRemark": "",
    "faceUrl": message["avatar"] ?? '',
    "nameCard": message["nameCard"] ?? '',
    "groupID": message["conversationType"] == "GROUP" ? message["to"] : null,
    "userID": message["conversationType"] == "C2C"
        ? message["conversationID"].replaceAll("C2C", "")
        : null,
    "status": isFromDownloadMergerMessage
        ? MessageStatus.V2TIM_MSG_STATUS_SEND_SUCC
        : MessageStatusWeb.convertMessageStatus(message),
    "elemType": elementType,
    "localCustomData": "",
    "localCustomInt": 0,
    "cloudCustomData": message["cloudCustomData"] ?? '',
    "isSelf": isFromDownloadMergerMessage
        ? V2TIMManager.loginUserID == message['messageSender']
        : (message["flow"] == "out" ? true : false),
    "isRead": message["isRead"] ?? false,
    "isPeerRead": message["isPeerRead"] ?? false,
    "priority": isFromDownloadMergerMessage
        ? MessagePriority.V2TIM_PRIORITY_DEFAULT
        : MessagePriorityWeb.convertMsgPriority(message["priority"]),
    "groupAtUserList":
        isFromDownloadMergerMessage ? [] : message["atUserList"],
    "seq": isFromDownloadMergerMessage
        ? message['clientSequence'].toString()
        : message["sequence"].toString(),
    "random": isFromDownloadMergerMessage
        ? message['messageRandom']
        : message["random"],
    "isExcludedFromUnreadCount": false,
    "messageFromWeb": isFromDownloadMergerMessage ? '' : stringify(jsMessage),
    "needReadReceipt": message["needReadReceipt"] ?? false
  };

  final messagePayload = jsToMap(message["payload"]);

  // 文本消息
  if (elementType == MessageElemType.V2TIM_ELEM_TYPE_TEXT) {
    final textElem = V2TimTextElem(text: messagePayload['text']).toJson();
    formatedMsg['textElem'] = textElem;
  }

  // 音频消息
  if (elementType == MessageElemType.V2TIM_ELEM_TYPE_SOUND) {
    final soundElem = V2TimSoundElem(
      localUrl: messagePayload['url'],
      url: messagePayload['remoteAudioUrl'],
      dataSize: messagePayload['size'],
      duration: messagePayload['second'],
      UUID: messagePayload['uuid'],
    ).toJson();
    formatedMsg['soundElem'] = soundElem;
  }

  // 自定义消息
  if (elementType == MessageElemType.V2TIM_ELEM_TYPE_CUSTOM) {
    final customElem = V2TimCustomElem(
            data: messagePayload['data'],
            desc: messagePayload['discription'],
            extension: messagePayload['extension'])
        .toJson();
    formatedMsg['customElem'] = customElem;
  }

  // 群系统通知消息
  if (elementType == MessageElemType.V2TIM_ELEM_TYPE_GROUP_TIPS) {
    final groupTipsElement = await convertGroupTipsMessage(
        message as Map<String, dynamic>,
        groupID: formatedMsg['groupID']);
    formatedMsg['groupTipsElem'] = groupTipsElement?.toJson();
  }

  // 图片消息
  if (elementType == MessageElemType.V2TIM_ELEM_TYPE_IMAGE) {
    final imageInfoArray = messagePayload['imageInfoArray'] as List;
    final imagePath = jsToMap(imageInfoArray[0])['url'];
    final uuid = messagePayload['uuid'];
    final imageList = imageInfoArray.map((e) {
      final element = jsToMap(e);
      return V2TimImage(
          type: element['type'],
          height: element['height'],
          width: element['width'],
          size: element['size'],
          uuid: uuid,
          url: element['imageUrl']);
    }).toList();
    final imageElem = V2TimImageElem(path: imagePath, imageList: imageList);
    formatedMsg['imageElem'] = imageElem.toJson();
  }

  //视频消息
  if (elementType == MessageElemType.V2TIM_ELEM_TYPE_VIDEO) {
    final videoElem = V2TimVideoElem(
        videoPath: messagePayload['videoUrl'],
        duration: int.tryParse(messagePayload['videoSecond'].toString()),
        UUID: messagePayload['videoUUID'],
        snapshotPath: messagePayload['thumbUrl'],
        snapshotUUID: messagePayload['thumbUUID'],
        snapshotSize: messagePayload['thumbSize'],
        snapshotWidth: messagePayload['thumbWidth'],
        snapshotHeight: messagePayload['thumbHeight'],
        snapshotUrl: messagePayload['thumbUrl'],
        videoUrl: messagePayload['remoteVideoUrl'],
        videoSize: messagePayload['videoSize']);
    formatedMsg['videoElem'] = videoElem.toJson();
  }

  // 文件消息
  if (elementType == MessageElemType.V2TIM_ELEM_TYPE_FILE) {
    final fileElem = V2TimFileElem(
            path: messagePayload['fileUrl'],
            fileName: messagePayload['fileName'],
            UUID: messagePayload['uuid'],
            fileSize: messagePayload['fileSize'])
        .toJson();
    formatedMsg['fileElem'] = fileElem;
  }

  // 地理位置消息
  if (elementType == MessageElemType.V2TIM_ELEM_TYPE_LOCATION) {
    final locationElem = V2TimLocationElem(
        desc: messagePayload['description'],
        longitude: messagePayload['longitude'],
        latitude: messagePayload['latitude']);
    formatedMsg['locationElem'] = locationElem.toJson();
  }

  List<String> formateList(List<dynamic> list) =>
      list.map((e) => e.toString()).toList();

  // 合并消息
  if (elementType == MessageElemType.V2TIM_ELEM_TYPE_MERGER) {
    final mergerElem = V2TimMergerElem(
        title: messagePayload['title'],
        abstractList: formateList(messagePayload['abstractList']),
        isLayersOverLimit: messagePayload['layersOverLimit']);
    formatedMsg['mergerElem'] = mergerElem.toJson();
  }

  // 表情消息
  if (elementType == MessageElemType.V2TIM_ELEM_TYPE_FACE) {
    final faceMessage = V2TimFaceElem(
        index: messagePayload['index'], data: messagePayload['data']);
    formatedMsg['faceElem'] = faceMessage.toJson();
  }

  return formatedMsg;
}