initClient method

Future<Map?> initClient(
  1. UpdateTd update, {
  2. Map? tdlibParameters,
  3. required int clientId,
  4. bool isVoid = false,
  5. String? extra,
  6. bool? isUseCache,
  7. Duration? durationCacheExpire,
  8. bool isInvokeThrowOnError = true,
})

set up authorizationStateWaitTdlibParameters new client without more code

Implementation

Future<Map?> initClient(
  UpdateTd update, {
  Map? tdlibParameters,
  required int clientId,
  bool isVoid = false,
  String? extra,
  bool? isUseCache,
  Duration? durationCacheExpire,
  bool isInvokeThrowOnError = true,
}) async {
  if (update.raw["authorization_state"] is Map) {
    var authStateType = update.raw["authorization_state"]["@type"];
    if (authStateType == "authorizationStateWaitTdlibParameters") {
      Map optios = client_option.rawData.clone();
      if (tdlibParameters != null) {
        optios.addAll(tdlibParameters);
      }
      return await invoke(
        "setTdlibParameters",
        parameters: optios.cast<String, dynamic>(),
        clientId: clientId,
        isUseCache: false,
        durationCacheExpire: null,
        isVoid: isVoid,
        extra: extra,
        isInvokeThrowOnError: isInvokeThrowOnError,
      );
    }
    if (authStateType == "authorizationStateWaitEncryptionKey") {
      bool isEncrypted = update.raw["authorization_state"]['is_encrypted'];
      if (client_option["database_key"] is String == false) {
        client_option["database_key"] = "";
      }
      if (isEncrypted) {
        return await invoke(
          "checkDatabaseEncryptionKey",
          parameters: {
            "encryption_key":
                TgUtils.stringToBase64(value: client_option["database_key"]),
          },
          isUseCache: false,
          durationCacheExpire: null,
          clientId: clientId,
          isVoid: isVoid,
          extra: extra,
          isInvokeThrowOnError: isInvokeThrowOnError,
        );
      } else {
        return await invoke(
          "setDatabaseEncryptionKey",
          parameters: {
            "new_encryption_key":
                TgUtils.stringToBase64(value: client_option["database_key"]),
          },
          clientId: clientId,
          isVoid: isVoid,
          isUseCache: false,
          durationCacheExpire: null,
          extra: extra,
          isInvokeThrowOnError: isInvokeThrowOnError,
        );
      }
    }

    if (authStateType == "authorizationStateClosed") {
      await exitClientById(update.client_id,
          isInvokeThrowOnError: isInvokeThrowOnError);

      return {"@type": "ok"};
    }

    if (authStateType == "authorizationStateReady") {
      // TdlibClient? tdlibClient = getClientById(update.client_id);
      // if (tdlibClient == null || tdlibClient.client_tg_user_id != 0) {
      //   if (tdlibClient == null) {
      //     return null;
      //   }
      // }
      Map get_me = await getMe(
        clientId: update.client_id,
        isUseCache: false,
        durationCacheExpire: null,
      );

      if (clients[update.client_id] != null) {
        clients[update.client_id]!.client_tg_user_id = get_me["result"]["id"];
      }
      // for (var i = 0; i < clients.length; i++) {
      //   TdlibClient tdlib_client = clients[i];
      //   if (tdlib_client.client_id == update.client_id) {
      //     tdlib_client.client_tg_user_id = get_me["result"]["id"];
      //     clients[i].client_tg_user_id = get_me["result"]["id"];
      //     return null;
      //   }
      // }
    }
  }
  return null;
}