diff --git a/src/common/sql_queries.py b/src/common/sql_queries.py index 1834dbb..73d637d 100644 --- a/src/common/sql_queries.py +++ b/src/common/sql_queries.py @@ -13,6 +13,12 @@ class SQLQueries: INSERT_USER_DATA = """ INSERT INTO user_data - (phone, folders, user_config, chat_config) - VALUES (%s, %s, %s, %s) - """ \ No newline at end of file + (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) + """ diff --git a/src/common/static.py b/src/common/static.py index 1028796..5b9fef1 100644 --- a/src/common/static.py +++ b/src/common/static.py @@ -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, diff --git a/src/oneme/processors/auth.py b/src/oneme/processors/auth.py index 9bad7bd..ec6fc29 100644 --- a/src/oneme/processors/auth.py +++ b/src/oneme/processors/auth.py @@ -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,) diff --git a/src/oneme/processors/folders.py b/src/oneme/processors/folders.py index fccda9f..f94f953 100644 --- a/src/oneme/processors/folders.py +++ b/src/oneme/processors/folders.py @@ -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": [] } # Собираем пакет diff --git a/src/telegrambot/bot.py b/src/telegrambot/bot.py index 23e06e1..7dd614f 100644 --- a/src/telegrambot/bot.py +++ b/src/telegrambot/bot.py @@ -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 diff --git a/tables.sql b/tables.sql index 3185d22..95884b4 100644 --- a/tables.sql +++ b/tables.sql @@ -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`) +);