convertElemMsg2Dart static method

Future<Map<String, dynamic>> convertElemMsg2Dart(
  1. dynamic jsonMessage
)

Implementation

static Future<Map<String, dynamic>> convertElemMsg2Dart(jsonMessage) async {
  // var jsonMessage = json.decode(message);
  int type = messageElemType2DartMessageElemType(jsonMessage["elem_type"]);
  // dynamic res;
  Map<String, dynamic> res = Map.from({});
  String key = "";
  switch (type) {
    case MessageElemType.V2TIM_ELEM_TYPE_TEXT:
      {
        key = "textElem";
        res = {"text": jsonMessage["text_elem_content"]};
      }
      break;
    case MessageElemType.V2TIM_ELEM_TYPE_CUSTOM:
      {
        key = "customElem";
        res = {
          "data": jsonMessage["custom_elem_data"],
          "desc": jsonMessage["custom_elem_desc"],
          "extension": jsonMessage["custom_elem_ext"],
        };
      }
      break;
    case MessageElemType.V2TIM_ELEM_TYPE_IMAGE:
      {
        var dataorigpath = getMulitiMessageFileIDV2(
          jsonMessage,
          0,
          false,
        );
        String origpath = await getDownloadPathV2(
          dataorigpath["uuid"] ?? "",
          dataorigpath["name"] ?? "",
        );
        var datathumpath = getMulitiMessageFileIDV2(
          jsonMessage,
          1,
          false,
        );
        String thumpath = await getDownloadPathV2(
          datathumpath["uuid"] ?? "",
          datathumpath["name"] ?? "",
        );
        var datalagrpath = getMulitiMessageFileIDV2(
          jsonMessage,
          2,
          false,
        );
        String lagrpath = await getDownloadPathV2(
          datalagrpath["uuid"] ?? "",
          datalagrpath["name"] ?? "",
        );

        File orig = File(origpath);
        File large = File(lagrpath);
        File thumb = File(thumpath);

        key = "imageElem";
        res = {
          "path": jsonMessage["image_elem_orig_path"],
          "imageList": [
            {
              "uuid": jsonMessage["image_elem_orig_id"],
              "size": jsonMessage["image_elem_orig_pic_size"],
              "width": jsonMessage["image_elem_orig_pic_width"],
              "height": jsonMessage["image_elem_orig_pic_height"],
              "url": jsonMessage["image_elem_orig_url"],
              "type": 0,
              "localUrl": orig.existsSync() ? origpath : "",
            },
            {
              "uuid": jsonMessage["image_elem_thumb_id"],
              "size": jsonMessage["image_elem_thumb_pic_size"],
              "width": jsonMessage["image_elem_thumb_pic_width"],
              "height": jsonMessage["image_elem_thumb_pic_height"],
              "url": jsonMessage["image_elem_thumb_url"],
              "type": 1,
              "localUrl": thumb.existsSync() ? thumpath : "",
            },
            {
              "uuid": jsonMessage["image_elem_large_id"],
              "size": jsonMessage["image_elem_large_pic_size"],
              "width": jsonMessage["image_elem_large_pic_width"],
              "height": jsonMessage["image_elem_large_pic_height"],
              "url": jsonMessage["image_elem_large_url"],
              "type": 2,
              "localUrl": large.existsSync() ? lagrpath : "",
            },
          ]
        };
      }
      break;
    case MessageElemType.V2TIM_ELEM_TYPE_SOUND:
      {
        var data = getMulitiMessageFileIDV2(
          jsonMessage,
          1,
          false,
        );
        String soundpath = await getDownloadPathV2(
          data["uuid"] ?? "",
          data["name"] ?? "",
        );
        File sound = File(soundpath);
        key = "soundElem";
        res = {
          "path": jsonMessage["sound_elem_file_path"],
          "UUID": jsonMessage["sound_elem_file_id"],
          "dataSize": jsonMessage["sound_elem_file_size"],
          "duration": jsonMessage["sound_elem_file_time"],
          "url": jsonMessage["sound_elem_url"],
          "localUrl": sound.existsSync() ? soundpath : ""
        };
      }
      break;
    case MessageElemType.V2TIM_ELEM_TYPE_VIDEO:
      {
        var data = getMulitiMessageFileIDV2(
          jsonMessage,
          1,
          false,
        );
        String videopath = await getDownloadPathV2(
          data["uuid"] ?? "",
          data["name"] ?? "",
        );
        File videos = File(videopath);
        var snapdata = getMulitiMessageFileIDV2(
          jsonMessage,
          1,
          true,
        );
        String videsnapopath = await getDownloadPathV2(
          snapdata["uuid"] ?? "",
          snapdata["name"] ?? "",
        );
        File videosnap = File(videsnapopath);
        key = "videoElem";
        res = {
          "videoPath": jsonMessage["video_elem_video_path"],
          "UUID": jsonMessage["video_elem_video_id"],
          "videoSize": jsonMessage["video_elem_video_size"],
          "duration": jsonMessage["video_elem_video_duration"],
          "snapshotPath": jsonMessage["video_elem_image_path"],
          "snapshotUUID": jsonMessage["video_elem_image_id"],
          "snapshotSize": jsonMessage["video_elem_image_size"],
          "snapshotWidth": jsonMessage["video_elem_image_width"],
          "snapshotHeight": jsonMessage["video_elem_image_height"],
          "videoUrl": jsonMessage["video_elem_video_url"],
          "localVideoUrl": videos.existsSync() ? videopath : "",
          "snapshotUrl": jsonMessage["video_elem_image_url"],
          "localSnapshotUrl": videosnap.existsSync() ? videsnapopath : "",
        };
      }
      break;
    case MessageElemType.V2TIM_ELEM_TYPE_FILE:
      {
        var data = getMulitiMessageFileIDV2(
          jsonMessage,
          1,
          false,
        );
        String filepath = await getDownloadPathV2(
          data["uuid"] ?? "",
          data["name"] ?? "",
        );
        File files = File(filepath);
        key = "fileElem";
        res = {
          "path": jsonMessage["file_elem_file_path"],
          "fileName": jsonMessage["file_elem_file_name"],
          "UUID": jsonMessage["file_elem_file_id"],
          "url": jsonMessage["file_elem_url"],
          "fileSize": jsonMessage["file_elem_file_size"],
          "localUrl": files.existsSync() ? filepath : "",
        };
      }
      break;
    case MessageElemType.V2TIM_ELEM_TYPE_LOCATION:
      {
        key = "locationElem";
        res = {
          "desc": jsonMessage["location_elem_desc"],
          "longitude": jsonMessage["location_elem_longitude"],
          "latitude": jsonMessage["location_elem_latitude"],
        };
      }
      break;
    case MessageElemType.V2TIM_ELEM_TYPE_FACE:
      {
        key = "faceElem";
        res = {
          "index": jsonMessage["face_elem_index"],
          "data": jsonMessage["face_elem_buf"],
        };
      }
      break;
    case MessageElemType.V2TIM_ELEM_TYPE_GROUP_TIPS:
      {
        key = "groupTipsElem";
        List groupChange = List.empty(growable: true);
        if (jsonMessage["group_tips_elem_group_change_info_array"] != null) {
          for (Map<String, dynamic> info in List<dynamic>.from(jsonMessage["group_tips_elem_group_change_info_array"] ?? [])) {
            groupChange.add(groupChangeInfo2dartGroupChangeInfo(info).toJson());
          }
        }
        List memberChange = List.empty(growable: true);
        if (jsonMessage["group_tips_elem_member_change_info_array"] != null) {
          for (Map<String, dynamic> info in List<dynamic>.from(jsonMessage["group_tips_elem_member_change_info_array"] ?? [])) {
            memberChange.add(dartMemberChange2MemberChangeInfo(info).toJson());
          }
        }
        res = {
          "groupID": jsonMessage["group_tips_elem_group_id"] ?? "",
          "type": jsonMessage["group_tips_elem_tip_type"] == null ? 0 : (jsonMessage["group_tips_elem_tip_type"] + 1),
          "opMember": jsonMessage["group_tips_elem_op_user_info"] != null
              ? groupMemberInfo2dartGroupMemberInfo(
                  jsonMessage["group_tips_elem_op_group_memberinfo"],
                ).toJson()
              : null,
          "memberList": List<dynamic>.from(jsonMessage["group_tips_elem_changed_group_memberinfo_array"] ?? []).map((e) => groupMemberInfo2dartGroupMemberInfo(e).toJson()),
          "groupChangeInfoList": groupChange,
          "memberChangeInfoList": memberChange,
          "memberCount": jsonMessage["group_tips_elem_member_num"] ?? 0,
        };
      }
      break;
    case MessageElemType.V2TIM_ELEM_TYPE_MERGER:
      {
        key = "mergerElem";
        res = {
          "isLayersOverLimit": jsonMessage["merge_elem_layer_over_limit"],
          "title": jsonMessage["merge_elem_title"],
          "abstractList": jsonMessage["merge_elem_abstract_array"],
        };
      }
      break;
    default:
      break;
  }
  return {key: res};
}