From d81eec5532a88dd766e2c2ddd48d4fa120276030 Mon Sep 17 00:00:00 2001 From: Alexey Polyakov Date: Sat, 9 May 2026 18:16:32 +0300 Subject: [PATCH] =?UTF-8?q?=D0=93=D0=B5=D0=BD=D0=B5=D1=80=D0=B8=D1=80?= =?UTF-8?q?=D1=83=D0=B5=D0=BC=20=D0=B0=D0=B9=D0=B4=D0=B8=20=D0=BF=D0=BE?= =?UTF-8?q?=D0=BB=D1=8C=D0=B7=D0=BE=D0=B2=D0=B0=D1=82=D0=B5=D0=BB=D0=B5?= =?UTF-8?q?=D0=B9=20=D1=80=D0=B0=D0=BD=D0=B4=D0=BE=D0=BC=D0=BD=D0=BE=20(?= =?UTF-8?q?=D0=BE=D0=BF=D1=8F=D1=82=D1=8C,=20=D0=B4=D0=B0)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/sql_queries.py | 4 ++-- src/common/tools.py | 14 ++++++++++++-- src/oneme/processors/auth.py | 10 ++++++---- src/telegrambot/bot.py | 4 ++++ tables.sql | 2 +- 5 files changed, 25 insertions(+), 9 deletions(-) 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,