autoSetData method

Future<void> autoSetData(
  1. UpdateTelegramClient updateTelegramClient
)

Implementation

Future<void> autoSetData(UpdateTelegramClient updateTelegramClient) async {
  if (updateTelegramClient.telegramClientData.telegramClientType ==
      TelegramClientType.tdlib) {
    final Map update_raw = updateTelegramClient.rawData;

    if (update_raw["@type"] == "updateAuthorizationState") {
      if (update_raw["authorization_state"] is Map) {
        final Map authorization_state = update_raw["authorization_state"];

        if (authorization_state["@type"] == "authorizationStateReady") {
          final Map user = await invoke(
            parameters: {
              "@type": "getMe",
            },
            isUseCache: false,
            durationCacheExpire: null,
            telegramClientData: updateTelegramClient.telegramClientData,
          );

          final String user_usename = () {
            if (user["usernames"] is Map) {
              if (user["usernames"]["editable_username"] is String) {
                return (user["usernames"]["editable_username"] as String);
              }
            }
            return "";
          }();

          TdlibClient? tdlibClient = tdlib.clients[
              updateTelegramClient.telegramClientData.tdlib_client_id];
          if (tdlibClient == null) {
            tdlibClient ??= TdlibClient(
              client_id:
                  updateTelegramClient.telegramClientData.tdlib_client_id,
              client_option: updateTelegramClient.client_option,
            );
            tdlib.clients[updateTelegramClient
                .telegramClientData.tdlib_client_id] = tdlibClient;
          }

          bool is_constain_update = false;

          tdlibClient.client_tg_user_id = user["id"];

          if (user["type"] is Map) {
            if (user["type"]["@type"] == "userTypeBot") {
              is_constain_update = true;
              tdlibClient.is_bot = true;

              updateTelegramClient.telegramClientData.is_bot = true;
            }
          }
          if (updateTelegramClient.client_option["client_first_name"] !=
              user["first_name"]) {
            is_constain_update = true;
            tdlibClient.client_option["client_first_name"] =
                user["first_name"];
          }
          if (updateTelegramClient.client_option["client_first_name"] !=
              user["last_name"]) {
            is_constain_update = true;
            tdlibClient.client_option["client_last_name"] = user["last_name"];
          }

          if (updateTelegramClient.client_option["client_title"] !=
              "${user["first_name"]} ${user["last_name"]}".trim()) {
            is_constain_update = true;
            tdlibClient.client_option["client_title"] =
                "${user["first_name"]} ${user["last_name"]}".trim();
          }
          if (tdlibClient.client_option["client_tg_user_id"] != user["id"]) {
            is_constain_update = true;
            tdlibClient.client_option["client_tg_user_id"] = user["id"];
          }

          if (updateTelegramClient.client_option["client_username"] !=
              user_usename) {
            is_constain_update = true;
            tdlibClient.client_option["client_username"] = user_usename;
            updateTelegramClient.telegramClientData.client_user_name =
                user_usename;
          }

          if (is_constain_update) {
            await tdlib.updateClientById(
                updateTelegramClient.telegramClientData.tdlib_client_id,
                newTdlibClient: tdlibClient);
          }
        }
      }
    }

    if (update_raw["@type"] == "updateUser") {
      if (update_raw["user"] is Map) {
        final Map user = update_raw["user"];

        if (user["id"] ==
            updateTelegramClient.telegramClientData.client_tg_user_id) {
          // int user_id = user["id"];
          final String user_usename = () {
            if (user["usernames"] is Map) {
              if (user["usernames"]["editable_username"] is String) {
                return (user["usernames"]["editable_username"] as String);
              }
            }
            return "";
          }();

          TdlibClient? tdlibClient = tdlib.clients[
              updateTelegramClient.telegramClientData.tdlib_client_id];

          bool is_constain_update = false;
          if (tdlibClient == null) {
            tdlibClient ??= TdlibClient(
              client_id:
                  updateTelegramClient.telegramClientData.tdlib_client_id,
              client_option: updateTelegramClient.client_option,
            );
            tdlib.clients[updateTelegramClient
                .telegramClientData.tdlib_client_id] = tdlibClient;
            is_constain_update = true;
          }

          if (user["type"] is Map) {
            if (user["type"]["@type"] == "userTypeBot") {
              is_constain_update = true;
              tdlibClient.is_bot = true;

              updateTelegramClient.telegramClientData.is_bot = true;
            }
          }
          if (updateTelegramClient.client_option["client_first_name"] !=
              user["first_name"]) {
            is_constain_update = true;
            tdlibClient.client_option["client_first_name"] =
                user["first_name"];
          }
          if (updateTelegramClient.client_option["client_first_name"] !=
              user["last_name"]) {
            is_constain_update = true;
            tdlibClient.client_option["client_last_name"] = user["last_name"];
          }

          if (updateTelegramClient.client_option["client_title"] !=
              "${user["first_name"]} ${user["last_name"]}".trim()) {
            is_constain_update = true;
            tdlibClient.client_option["client_title"] =
                "${user["first_name"]} ${user["last_name"]}".trim();
          }
          if (tdlibClient.client_option["client_tg_user_id"] != user["id"]) {
            is_constain_update = true;
            tdlibClient.client_option["client_tg_user_id"] = user["id"];
          }

          if (updateTelegramClient.client_option["client_username"] !=
              user_usename) {
            is_constain_update = true;
            tdlibClient.client_option["client_username"] = user_usename;
            updateTelegramClient.telegramClientData.client_user_name =
                user_usename;
          }

          if (is_constain_update) {
            await tdlib.updateClientById(
                updateTelegramClient.telegramClientData.tdlib_client_id,
                newTdlibClient: tdlibClient);
          }
        }
      }
    }
  }
}