diff --git a/src/common/sql_queries.py b/src/common/sql_queries.py index 8415a07..d551077 100644 --- a/src/common/sql_queries.py +++ b/src/common/sql_queries.py @@ -6,9 +6,9 @@ class SQLQueries: INSERT_USER = """ INSERT INTO users - (phone, telegram_id, firstname, lastname, username, + (id, phone, telegram_id, firstname, lastname, username, profileoptions, options, accountstatus, updatetime, lastseen) - VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s) + VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) """ INSERT_USER_DATA = """ diff --git a/src/common/tools.py b/src/common/tools.py index 9bcff1f..447fe08 100644 --- a/src/common/tools.py +++ b/src/common/tools.py @@ -1,6 +1,6 @@ import json +import secrets import time -import os import geoip2.database @@ -562,4 +562,14 @@ class Tools: response = reader.country(ip) return response.country.name or "Localhost Federation" except Exception: - return "Localhost Federation" \ No newline at end of file + return "Localhost Federation" + + async def generate_user_id(self, db_pool): + """Генерация id пользователя""" + async with db_pool.acquire() as conn: + async with conn.cursor() as cursor: + while True: + user_id = secrets.randbelow(2_147_383_647) + 100_000 + await cursor.execute("SELECT id FROM users WHERE id = %s", (user_id,)) + if not await cursor.fetchone(): + return user_id diff --git a/src/oneme/processors/auth.py b/src/oneme/processors/auth.py index 56b047c..da646c1 100644 --- a/src/oneme/processors/auth.py +++ b/src/oneme/processors/auth.py @@ -397,15 +397,19 @@ class AuthProcessors(BaseProcessor): now_ms = int(time.time() * 1000) now_s = int(time.time()) + # Генерируем ID пользователя + user_id = await self.tools.generate_user_id(self.db_pool) + # Создаем пользователя await cursor.execute( """ INSERT INTO users - (phone, telegram_id, firstname, lastname, username, + (id, phone, telegram_id, firstname, lastname, username, profileoptions, options, accountstatus, updatetime, lastseen) - VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s) + VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) """, ( + user_id, phone, None, first_name, @@ -419,8 +423,6 @@ class AuthProcessors(BaseProcessor): ), ) - user_id = cursor.lastrowid - # Добавляем данные аккаунта await cursor.execute( """ diff --git a/src/telegrambot/bot.py b/src/telegrambot/bot.py index 21494e6..94e1759 100644 --- a/src/telegrambot/bot.py +++ b/src/telegrambot/bot.py @@ -88,10 +88,14 @@ class TelegramBot: username = (message.from_user.username or f"user{int(time.time() * 1000)}")[:60] try: + # Генерируем ID пользователя + user_id = await self.tools.generate_user_id(self.db_pool) + # Создаем юзера await cursor.execute( self.sql_queries.INSERT_USER, ( + user_id, # id new_phone, # phone tg_id, # telegram_id firstname, # firstname diff --git a/tables.sql b/tables.sql index f183022..5cdc104 100644 --- a/tables.sql +++ b/tables.sql @@ -1,5 +1,5 @@ CREATE TABLE `users` ( - `id` INT NOT NULL AUTO_INCREMENT, + `id` INT NOT NULL, `phone` VARCHAR(20) UNIQUE, `telegram_id` VARCHAR(64) UNIQUE, `firstname` VARCHAR(59) NOT NULL,