mirror of
https://github.com/openmax-server/server.git
synced 2026-05-22 19:41:41 +03:00
MAX: создание папок
This commit is contained in:
@@ -75,6 +75,12 @@ class SendMessagePayloadModel(pydantic.BaseModel):
|
|||||||
class SyncFoldersPayloadModel(pydantic.BaseModel):
|
class SyncFoldersPayloadModel(pydantic.BaseModel):
|
||||||
folderSync: int
|
folderSync: int
|
||||||
|
|
||||||
|
class CreateFolderPayloadModel(pydantic.BaseModel):
|
||||||
|
id: str
|
||||||
|
title: str
|
||||||
|
filters: list = []
|
||||||
|
include: list = []
|
||||||
|
|
||||||
class SearchChatsPayloadModel(pydantic.BaseModel):
|
class SearchChatsPayloadModel(pydantic.BaseModel):
|
||||||
chatIds: list
|
chatIds: list
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import pydantic
|
|||||||
import json
|
import json
|
||||||
import time
|
import time
|
||||||
from classes.baseprocessor import BaseProcessor
|
from classes.baseprocessor import BaseProcessor
|
||||||
from oneme.models import SyncFoldersPayloadModel
|
from oneme.models import SyncFoldersPayloadModel, CreateFolderPayloadModel
|
||||||
|
|
||||||
class FoldersProcessors(BaseProcessor):
|
class FoldersProcessors(BaseProcessor):
|
||||||
async def folders_get(self, payload, seq, writer, senderPhone):
|
async def folders_get(self, payload, seq, writer, senderPhone):
|
||||||
@@ -19,7 +19,7 @@ class FoldersProcessors(BaseProcessor):
|
|||||||
async with self.db_pool.acquire() as conn:
|
async with self.db_pool.acquire() as conn:
|
||||||
async with conn.cursor() as cursor:
|
async with conn.cursor() as cursor:
|
||||||
await cursor.execute(
|
await cursor.execute(
|
||||||
"SELECT id, title, filters, options, update_time, source_id "
|
"SELECT id, title, filters, `include`, options, update_time, source_id "
|
||||||
"FROM user_folders WHERE phone = %s ORDER BY sort_order",
|
"FROM user_folders WHERE phone = %s ORDER BY sort_order",
|
||||||
(int(senderPhone),)
|
(int(senderPhone),)
|
||||||
)
|
)
|
||||||
@@ -30,9 +30,10 @@ class FoldersProcessors(BaseProcessor):
|
|||||||
"id": folder["id"],
|
"id": folder["id"],
|
||||||
"title": folder["title"],
|
"title": folder["title"],
|
||||||
"filters": json.loads(folder["filters"]),
|
"filters": json.loads(folder["filters"]),
|
||||||
|
"include": json.loads(folder["include"]),
|
||||||
"updateTime": folder["update_time"],
|
"updateTime": folder["update_time"],
|
||||||
"options": json.loads(folder["options"]),
|
"options": json.loads(folder["options"]),
|
||||||
"sourceId": folder["source_id"],
|
"sourceId": folder["source_id"]
|
||||||
}
|
}
|
||||||
for folder in result_folders
|
for folder in result_folders
|
||||||
]
|
]
|
||||||
@@ -45,6 +46,10 @@ class FoldersProcessors(BaseProcessor):
|
|||||||
"allFilterExcludeFolders": []
|
"allFilterExcludeFolders": []
|
||||||
}
|
}
|
||||||
|
|
||||||
|
print(
|
||||||
|
json.dumps(payload, indent=4)
|
||||||
|
)
|
||||||
|
|
||||||
# Собираем пакет
|
# Собираем пакет
|
||||||
packet = self.proto.pack_packet(
|
packet = self.proto.pack_packet(
|
||||||
cmd=self.proto.CMD_OK, seq=seq, opcode=self.opcodes.FOLDERS_GET, payload=payload
|
cmd=self.proto.CMD_OK, seq=seq, opcode=self.opcodes.FOLDERS_GET, payload=payload
|
||||||
@@ -52,3 +57,81 @@ class FoldersProcessors(BaseProcessor):
|
|||||||
|
|
||||||
# Отправляем
|
# Отправляем
|
||||||
await self._send(writer, packet)
|
await self._send(writer, packet)
|
||||||
|
|
||||||
|
async def folders_update(self, payload, seq, writer, senderPhone):
|
||||||
|
"""Создание папки"""
|
||||||
|
# Валидируем данные пакета
|
||||||
|
try:
|
||||||
|
CreateFolderPayloadModel.model_validate(payload)
|
||||||
|
except pydantic.ValidationError as error:
|
||||||
|
self.logger.error(f"Возникли ошибки при валидации пакета: {error}")
|
||||||
|
await self._send_error(seq, self.opcodes.FOLDERS_UPDATE, self.error_types.INVALID_PAYLOAD, writer)
|
||||||
|
return
|
||||||
|
|
||||||
|
update_time = int(time.time() * 1000)
|
||||||
|
|
||||||
|
async with self.db_pool.acquire() as conn:
|
||||||
|
async with conn.cursor() as cursor:
|
||||||
|
await cursor.execute(
|
||||||
|
"SELECT COALESCE(MAX(sort_order), -1) as max_order FROM user_folders WHERE phone = %s",
|
||||||
|
(int(senderPhone),)
|
||||||
|
)
|
||||||
|
row = await cursor.fetchone()
|
||||||
|
next_order = row["max_order"] + 1
|
||||||
|
|
||||||
|
# Создаем новую папку
|
||||||
|
await cursor.execute(
|
||||||
|
"INSERT INTO user_folders (id, phone, title, filters, `include`, options, source_id, update_time, sort_order) "
|
||||||
|
"VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)",
|
||||||
|
(
|
||||||
|
payload.get("id"),
|
||||||
|
int(senderPhone),
|
||||||
|
payload.get("title"),
|
||||||
|
json.dumps(payload.get("filters")),
|
||||||
|
json.dumps(payload.get("include", [])),
|
||||||
|
json.dumps([]),
|
||||||
|
1,
|
||||||
|
update_time,
|
||||||
|
next_order,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
await conn.commit()
|
||||||
|
|
||||||
|
# Получаем обновленный порядок папок
|
||||||
|
await cursor.execute(
|
||||||
|
"SELECT id FROM user_folders WHERE phone = %s ORDER BY sort_order",
|
||||||
|
(int(senderPhone),)
|
||||||
|
)
|
||||||
|
all_folders = await cursor.fetchall()
|
||||||
|
|
||||||
|
folders_order = [f["id"] for f in all_folders]
|
||||||
|
|
||||||
|
# Формируем данные пакета
|
||||||
|
response_payload = {
|
||||||
|
"folder": {
|
||||||
|
"id": payload.get("id"),
|
||||||
|
"title": payload.get("title"),
|
||||||
|
"include": payload.get("include"),
|
||||||
|
"filters": payload.get("filters"),
|
||||||
|
"updateTime": update_time,
|
||||||
|
"options": [],
|
||||||
|
"sourceId": 1,
|
||||||
|
},
|
||||||
|
"folderSync": update_time,
|
||||||
|
"foldersOrder": folders_order,
|
||||||
|
}
|
||||||
|
|
||||||
|
# Формируем пакет
|
||||||
|
packet = self.proto.pack_packet(
|
||||||
|
cmd=self.proto.CMD_OK, seq=seq, opcode=self.opcodes.FOLDERS_UPDATE, payload=response_payload
|
||||||
|
)
|
||||||
|
|
||||||
|
await self._send(writer, packet)
|
||||||
|
|
||||||
|
# Разработчики протокола, объяснитесь, что за хеш !!! а еще подарите нам способ его формирования
|
||||||
|
notify_about_hash = self.proto.pack_packet(
|
||||||
|
cmd=0, seq=1, opcode=self.opcodes.NOTIF_CONFIG,
|
||||||
|
payload={"config": {"hash": "0"}}
|
||||||
|
)
|
||||||
|
|
||||||
|
await self._send(writer, notify_about_hash)
|
||||||
@@ -194,6 +194,15 @@ class OnemeMobile:
|
|||||||
writer,
|
writer,
|
||||||
userPhone,
|
userPhone,
|
||||||
)
|
)
|
||||||
|
case self.opcodes.FOLDERS_UPDATE:
|
||||||
|
await self.auth_required(
|
||||||
|
userPhone,
|
||||||
|
self.processors.folders_update,
|
||||||
|
payload,
|
||||||
|
seq,
|
||||||
|
writer,
|
||||||
|
userPhone,
|
||||||
|
)
|
||||||
case self.opcodes.SESSIONS_INFO:
|
case self.opcodes.SESSIONS_INFO:
|
||||||
await self.auth_required(
|
await self.auth_required(
|
||||||
userPhone,
|
userPhone,
|
||||||
|
|||||||
@@ -168,6 +168,15 @@ class OnemeWS:
|
|||||||
websocket,
|
websocket,
|
||||||
userPhone,
|
userPhone,
|
||||||
)
|
)
|
||||||
|
case self.opcodes.FOLDERS_UPDATE:
|
||||||
|
await self.auth_required(
|
||||||
|
userPhone,
|
||||||
|
self.processors.folders_update,
|
||||||
|
payload,
|
||||||
|
seq,
|
||||||
|
websocket,
|
||||||
|
userPhone,
|
||||||
|
)
|
||||||
case self.opcodes.SESSIONS_INFO:
|
case self.opcodes.SESSIONS_INFO:
|
||||||
await self.auth_required(
|
await self.auth_required(
|
||||||
userPhone,
|
userPhone,
|
||||||
|
|||||||
@@ -102,6 +102,7 @@ CREATE TABLE `user_folders` (
|
|||||||
`phone` VARCHAR(20) NOT NULL,
|
`phone` VARCHAR(20) NOT NULL,
|
||||||
`title` VARCHAR(128) NOT NULL,
|
`title` VARCHAR(128) NOT NULL,
|
||||||
`filters` JSON NOT NULL DEFAULT ('[]'),
|
`filters` JSON NOT NULL DEFAULT ('[]'),
|
||||||
|
`include` JSON NOT NULL DEFAULT ('[]'),
|
||||||
`options` JSON NOT NULL DEFAULT ('[]'),
|
`options` JSON NOT NULL DEFAULT ('[]'),
|
||||||
`source_id` INT NOT NULL DEFAULT 1,
|
`source_id` INT NOT NULL DEFAULT 1,
|
||||||
`update_time` BIGINT NOT NULL DEFAULT 0,
|
`update_time` BIGINT NOT NULL DEFAULT 0,
|
||||||
|
|||||||
Reference in New Issue
Block a user