setMessageExtensions static method

Future<V2TimValueCallback<List<V2TimMessageExtensionResult>>> setMessageExtensions({
  1. required String msgID,
  2. required List<V2TimMessageExtension> extensions,
})

Implementation

static Future<V2TimValueCallback<List<V2TimMessageExtensionResult>>> setMessageExtensions({
  required String msgID,
  required List<V2TimMessageExtension> extensions,
}) async {
  Map<String, dynamic> message = Map.from({});
  String userDatafind = Tools.generateUserData(methodName: "findMessage");
  Pointer<Uint8> user_data_find = Tools.string2PointerInt8(userDatafind);
  Pointer<Uint8> json_message_id_array = Tools.string2PointerInt8(json.encode(List<String>.from([msgID])));
  int res = desktopSDK.D_TIMMsgFindMessages(json_message_id_array, user_data_find);
  if (res == TIMResult.TIM_SUCC) {
    Map<String, dynamic> data = await getAsyncData(apiKey: userDatafind);

    if (data["code"] == 0) {
      List<Map<String, dynamic>> messageList = List<Map<String, dynamic>>.from(json.decode(data["json_param"].isEmpty ? json.encode([]) : data["json_param"]));
      if (messageList.isNotEmpty) {
        message = messageList.first;
      }
    } else {
      return V2TimValueCallback<List<V2TimMessageExtensionResult>>.fromJson({
        "code": data["code"],
        "desc": data["desc"],
        "data": null,
      });
    }
  } else {
    return V2TimValueCallback<List<V2TimMessageExtensionResult>>.fromJson({
      "code": -1,
      "desc": "message not found",
      "data": null,
    });
  }
  String userData = Tools.generateUserData();
  Pointer<Uint8> user_data_ptr = Tools.string2PointerInt8(userData);
  List<Map<String, dynamic>> nativeextrnsionList = List<Map<String, dynamic>>.from([]);
  for (var element in extensions) {
    nativeextrnsionList.add(Map<String, dynamic>.from({
      "message_extension_key": element.extensionKey,
      "message_extension_value": element.extensionValue,
    }));
  }
  int res1 = desktopSDK.D_TIMMsgSetMessageExtensions(Tools.string2PointerInt8(json.encode(message)), Tools.string2PointerInt8(json.encode(nativeextrnsionList)), user_data_ptr);
  if (res1 != TIMResult.TIM_SUCC) {
    return V2TimValueCallback<List<V2TimMessageExtensionResult>>.fromJson({
      "code": res1,
      "desc": "",
    });
  } else {
    Map<String, dynamic> data = await getAsyncData(apiKey: userData);

    List<Map<String, dynamic>> dataList = List<Map<String, dynamic>>.from(
      json.decode(data["json_param"].isEmpty ? json.encode([]) : data["json_param"]),
    );
    List<Map<String, dynamic>> list = List<Map<String, dynamic>>.empty(growable: true);
    for (var element in dataList) {
      Map<String, dynamic> res = element;
      res["resultCode"] = element["message_extension_result_code"];
      res["resultInfo"] = element["message_extension_result_info"];
      Map<String, dynamic> lstmap = Map<String, dynamic>.from(element["message_extension_item"]);
      Map<String, dynamic> reslst = Map<String, dynamic>.from({});
      reslst["extensionKey"] = lstmap["message_extension_key"];
      reslst["extensionValue"] = lstmap["message_extension_value"];
      res["extension"] = reslst;
      list.add(res);
    }
    return V2TimValueCallback<List<V2TimMessageExtensionResult>>.fromJson({
      "code": data["code"],
      "desc": data["desc"],
      "data": list,
    });
  }
}