MAX: Рефактор папок

This commit is contained in:
Alexey Polyakov
2026-04-24 20:54:28 +03:00
parent 56133416e3
commit 227f90c3c3
6 changed files with 60 additions and 33 deletions

View File

@@ -13,6 +13,12 @@ class SQLQueries:
INSERT_USER_DATA = """
INSERT INTO user_data
(phone, folders, user_config, chat_config)
VALUES (%s, %s, %s, %s)
(phone, user_config, chat_config)
VALUES (%s, %s, %s)
"""
INSERT_DEFAULT_FOLDER = """
INSERT INTO user_folders
(id, phone, title, sort_order)
VALUES ('all.chat.folder', %s, 'Все', 0)
"""

View File

@@ -196,25 +196,6 @@ class Static:
]},
]
### Заглушка для папок
ALL_CHAT_FOLDER = [{
"id": "all.chat.folder",
"title": "Все",
"filters": [],
"updateTime": 0,
"options": [],
"sourceId": 1
}]
ALL_CHAT_FOLDER_ORDER = ["all.chat.folder"]
### Стандартные папки с настройками пользователя
USER_FOLDERS = {
"folders": [],
"foldersOrder": [],
"allFilterExcludeFolders": []
}
USER_SETTINGS = {
"CHATS_PUSH_NOTIFICATION": "ON",
"PUSH_DETAILS": True,

View File

@@ -403,17 +403,26 @@ class AuthProcessors(BaseProcessor):
await cursor.execute(
"""
INSERT INTO user_data
(phone, folders, user_config, chat_config)
VALUES (%s, %s, %s, %s)
(phone, user_config, chat_config)
VALUES (%s, %s, %s)
""",
(
phone,
json.dumps(self.static.USER_FOLDERS),
json.dumps(self.static.USER_SETTINGS),
json.dumps({}),
),
)
# Добавляем дефолтную папку
await cursor.execute(
"""
INSERT INTO user_folders
(id, phone, title, sort_order)
VALUES ('all.chat.folder', %s, 'Все', 0)
""",
(phone,),
)
# Удаляем токен
await cursor.execute(
"DELETE FROM auth_tokens WHERE token_hash = %s", (hashed_token,)

View File

@@ -18,16 +18,31 @@ class FoldersProcessors(BaseProcessor):
# Ищем папки в бд
async with self.db_pool.acquire() as conn:
async with conn.cursor() as cursor:
await cursor.execute("SELECT folders FROM user_data WHERE phone = %s", (int(senderPhone),))
result_folders = await cursor.fetchone()
user_folders = json.loads(result_folders.get("folders"))
await cursor.execute(
"SELECT id, title, filters, options, update_time, source_id "
"FROM user_folders WHERE phone = %s ORDER BY sort_order",
(int(senderPhone),)
)
result_folders = await cursor.fetchall()
folders = [
{
"id": folder["id"],
"title": folder["title"],
"filters": json.loads(folder["filters"]),
"updateTime": folder["update_time"],
"options": json.loads(folder["options"]),
"sourceId": folder["source_id"],
}
for folder in result_folders
]
# Создаем данные пакета
payload = {
"folderSync": int(time.time() * 1000),
"folders": self.static.ALL_CHAT_FOLDER + user_folders.get("folders"),
"foldersOrder": self.static.ALL_CHAT_FOLDER_ORDER + user_folders.get("foldersOrder"),
"allFilterExcludeFolders": user_folders.get("allFilterExcludeFolders")
"folders": folders,
"foldersOrder": [folder["id"] for folder in result_folders],
"allFilterExcludeFolders": []
}
# Собираем пакет

View File

@@ -105,12 +105,17 @@ class TelegramBot:
self.sql_queries.INSERT_USER_DATA,
(
new_phone, # phone
json.dumps(self.static.USER_FOLDERS), # folders
json.dumps(self.static.USER_SETTINGS), # user settings
json.dumps({}), # chat_config
),
)
# Добавляем дефолтную папку
await cursor.execute(
self.sql_queries.INSERT_DEFAULT_FOLDER,
(new_phone,),
)
await message.answer(
self.get_bot_message(
self.msg_types.REGISTRATION_SUCCESS

View File

@@ -38,7 +38,6 @@ CREATE TABLE `auth_tokens` (
CREATE TABLE `user_data` (
`phone` VARCHAR(20) NOT NULL UNIQUE,
`folders` JSON NOT NULL,
`user_config` JSON NOT NULL,
`chat_config` JSON NOT NULL,
PRIMARY KEY (`phone`)
@@ -97,3 +96,15 @@ CREATE TABLE `banners` (
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
);
CREATE TABLE `user_folders` (
`id` VARCHAR(64) NOT NULL,
`phone` VARCHAR(20) NOT NULL,
`title` VARCHAR(128) NOT NULL,
`filters` JSON NOT NULL DEFAULT ('[]'),
`options` JSON NOT NULL DEFAULT ('[]'),
`source_id` INT NOT NULL DEFAULT 1,
`update_time` BIGINT NOT NULL DEFAULT 0,
`sort_order` INT NOT NULL DEFAULT 0,
PRIMARY KEY (`id`, `phone`)
);