mirror of
https://github.com/openmax-server/server.git
synced 2026-06-18 17:08:26 +03:00
Compare commits
14 Commits
dev/0.1.0
...
968b7132b0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
968b7132b0 | ||
|
|
c53c3cd195 | ||
|
|
77da4a1b61 | ||
|
|
f9b6b2c75e | ||
|
|
930a95453f | ||
|
|
2776051b17 | ||
|
|
1d4cb2d248 | ||
|
|
ba6191d7e3 | ||
|
|
48c74153ec | ||
|
|
ea9e4761bc | ||
|
|
e6d693efbd | ||
|
|
dae26f02a8 | ||
|
|
ca652aab03 | ||
|
|
2ce4ac64e2 |
@@ -24,9 +24,6 @@ domain = "openmax.su"
|
|||||||
avatar_base_url = "http://127.0.0.1/avatar/"
|
avatar_base_url = "http://127.0.0.1/avatar/"
|
||||||
telegram_bot_token = "123456789:ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
telegram_bot_token = "123456789:ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||||
telegram_bot_enabled = "1"
|
telegram_bot_enabled = "1"
|
||||||
telegram_whitelist_enabled = "1"
|
|
||||||
telegram_whitelist_ids = "1,2,3"
|
telegram_whitelist_ids = "1,2,3"
|
||||||
origins="http://127.0.0.1,https://web.openmax.su"
|
origins="http://127.0.0.1,https://web.openmax.su"
|
||||||
sms_gateway_url = "http://127.0.0.1:8100/sms-gateway"
|
sms_gateway_url = "http://127.0.0.1:8100/sms-gateway"
|
||||||
firebase_credentials_path = ""
|
|
||||||
geo_db_path = ""
|
|
||||||
4
.gitignore
vendored
4
.gitignore
vendored
@@ -2,6 +2,4 @@ __pycache__
|
|||||||
.env
|
.env
|
||||||
*.pem
|
*.pem
|
||||||
*.sqlite
|
*.sqlite
|
||||||
*.crt
|
*.crt
|
||||||
*-adminsdk-*.json
|
|
||||||
*.mmdb
|
|
||||||
@@ -23,25 +23,5 @@
|
|||||||
3. Заходим в папку проекта и заменяем во всех классах "api.oneme.ru" на свой адрес сервера
|
3. Заходим в папку проекта и заменяем во всех классах "api.oneme.ru" на свой адрес сервера
|
||||||
4. Производим повторную сборку с помощью команды: `apktool b max -o max_modified.apk`
|
4. Производим повторную сборку с помощью команды: `apktool b max -o max_modified.apk`
|
||||||
|
|
||||||
---
|
> [!WARNING]
|
||||||
|
> Если у вас возникает ошибка при при повторной сборке, попробуйте декомпилировать клиент с параметром -r
|
||||||
# Патчинг Firebase для push-уведомлений
|
|
||||||
|
|
||||||
> [!Important]
|
|
||||||
> Без замены Firebase-конфига пуши от вашего сервера не будут работать.
|
|
||||||
|
|
||||||
1. Создайте проект в [Firebase Console](https://console.firebase.google.com/) и добавьте Android-приложение с пакетом `ru.oneme.app`
|
|
||||||
2. Скачайте `google-services.json`
|
|
||||||
3. В декомпилированном APK откройте `res/values/strings.xml` и замените следующие строки на значения из вашего `google-services.json`:
|
|
||||||
|
|
||||||
| Строка | Оригинал | Откуда взять |
|
|
||||||
|---|---|---|
|
|
||||||
| `google_api_key` | `AIzaSyABuDYeeDXIOrKTXLkUj30Ii143ofPe63Q` | `client[0].api_key[0].current_key` |
|
|
||||||
| `google_app_id` | `1:659634599081:android:9605285443b661167225b8` | `client[0].client_info.mobilesdk_app_id` |
|
|
||||||
| `gcm_defaultSenderId` | `659634599081` | `project_info.project_number` |
|
|
||||||
| `project_id` | `max-messenger-app` | `project_info.project_id` |
|
|
||||||
| `google_crash_reporting_api_key` | `AIzaSyABuDYeeDXIOrKTXLkUj30Ii143ofPe63Q` | `client[0].api_key[0].current_key` |
|
|
||||||
| `google_storage_bucket` | `max-messenger-app.firebasestorage.app` | `project_info.storage_bucket` |
|
|
||||||
|
|
||||||
4. Соберите и подпишите APK
|
|
||||||
5. В настройках проекта Firebase создайте сервисный аккаунт и укажите путь в `.env`
|
|
||||||
|
|||||||
8
faq/patch_ipa.md
Normal file
8
faq/patch_ipa.md
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
# Патч MAX для IOS
|
||||||
|
|
||||||
|
1) Распаковываете IPA
|
||||||
|
2) Находите файл MAX в Payload/Max.app
|
||||||
|
3) Открываете hex-редактор и находите api.oneme.ru и меняете на свой
|
||||||
|
При желании можете поменять другие URL
|
||||||
|
URL не должен привышать количество символов которое было у изначального URL!
|
||||||
|
4) Открываете IPA как архив и добавляете патченный файл MAX в Payload/Max.app
|
||||||
@@ -1,7 +1,8 @@
|
|||||||
# Навигация по faq
|
# Навигация по faq
|
||||||
|
|
||||||
## Работа с сервером
|
## Работа с сервером
|
||||||
[Установка сервера](install.md)
|
- [Установка сервера](install.md)
|
||||||
|
|
||||||
## Патчинг клиентов
|
## Патчинг клиентов
|
||||||
[Патч apk](patch_apk.md)
|
- [Патч мобильного клиента для Android](patch_apk.md)
|
||||||
|
- [Патч мобильного клиента для IOS](patch_ipa.md)
|
||||||
|
|||||||
11
faq/servers.md
Normal file
11
faq/servers.md
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
> [!Caution]
|
||||||
|
>
|
||||||
|
> На данной странице представлены инстансы от сторонних разработчиков.
|
||||||
|
>
|
||||||
|
> Используйте на свой страх и риск
|
||||||
|
>
|
||||||
|
|
||||||
|
# Открытые сервера
|
||||||
|
|
||||||
|
* [JustMAX](https://t.me/justmax_official)
|
||||||
|
* [mox.nyako.tk](https://t.me/opengrame/296), порт 443 (регистрация по реальному номеру)
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
> [!Caution]
|
> [!Caution]
|
||||||
>
|
>
|
||||||
> Проект находится на ранней стадии разработки и вероятно полон багов.
|
> Проект находится на ранней стадии разработки и, вероятно, полон багов.
|
||||||
>
|
>
|
||||||
> Использование в профессиональных средах не рекомендовано.
|
> Использование в профессиональных средах не рекомендовано.
|
||||||
|
|
||||||
@@ -13,7 +13,7 @@ https://t.me/openmax_alerts
|
|||||||
# Требования
|
# Требования
|
||||||
|
|
||||||
- Python 3.12+ (поддержка версий ниже не гарантирована)
|
- Python 3.12+ (поддержка версий ниже не гарантирована)
|
||||||
- MariaDB, MySQL или SQLite
|
- MariaDB, MySQL или SQLite (использование последнего не рекомендуется, так как поддержка ещё в разработке)
|
||||||
- Уметь патчить клиент MAX или собирать Komet из исходного кода (естественно с заменой сервера)
|
- Уметь патчить клиент MAX или собирать Komet из исходного кода (естественно с заменой сервера)
|
||||||
- Сертификат и приватный ключ X.509 (для тестирования сервера можно сгенерировать самоподписанный: ```openssl req -x509 -newkey rsa:2048 -nodes -keyout key.pem -out cert.pem -days 365```)
|
- Сертификат и приватный ключ X.509 (для тестирования сервера можно сгенерировать самоподписанный: ```openssl req -x509 -newkey rsa:2048 -nodes -keyout key.pem -out cert.pem -days 365```)
|
||||||
|
|
||||||
@@ -22,4 +22,6 @@ https://t.me/openmax_alerts
|
|||||||
Клиент может быть практически любым, главное условие - чтобы он был совместим с официальным сервером (`api.oneme.ru` / `api.tamtam.chat`).
|
Клиент может быть практически любым, главное условие - чтобы он был совместим с официальным сервером (`api.oneme.ru` / `api.tamtam.chat`).
|
||||||
|
|
||||||
# Дополнительная информация
|
# Дополнительная информация
|
||||||
[Faq](faq/readme.md)
|
- [FAQ](faq/readme.md)
|
||||||
|
- [Документация проекта](https://github.com/openmax-server/docs)
|
||||||
|
- [Публичные сервера](faq/servers.md)
|
||||||
|
|||||||
@@ -1,12 +1,10 @@
|
|||||||
aiogram==3.26.0
|
aiogram
|
||||||
aiomysql==0.3.2
|
aiomysql
|
||||||
msgpack==1.1.2
|
msgpack
|
||||||
lz4==4.4.5
|
lz4
|
||||||
websockets==16.0
|
websockets
|
||||||
pydantic==2.12.5
|
pydantic
|
||||||
aiosqlite==0.22.1
|
aiosqlite
|
||||||
aiohttp==3.13.5
|
aiohttp
|
||||||
python-dotenv==1.2.2
|
python-dotenv
|
||||||
cryptography==46.0.6
|
cryptography
|
||||||
firebase-admin==7.4.0
|
|
||||||
geoip2==5.2.0
|
|
||||||
@@ -1,12 +1,10 @@
|
|||||||
import logging
|
import logging
|
||||||
|
|
||||||
from common.config import ServerConfig
|
from common.config import ServerConfig
|
||||||
from common.opcodes import Opcodes
|
|
||||||
from common.proto_tcp import MobileProto
|
|
||||||
from common.proto_web import WebProto
|
|
||||||
from common.static import Static
|
from common.static import Static
|
||||||
from common.tools import Tools
|
from common.tools import Tools
|
||||||
|
from common.proto_tcp import MobileProto
|
||||||
|
from common.proto_web import WebProto
|
||||||
|
from common.opcodes import Opcodes
|
||||||
|
|
||||||
class BaseProcessor:
|
class BaseProcessor:
|
||||||
def __init__(self, db_pool=None, clients=None, send_event=None, type="socket"):
|
def __init__(self, db_pool=None, clients=None, send_event=None, type="socket"):
|
||||||
@@ -23,7 +21,7 @@ class BaseProcessor:
|
|||||||
self.event = send_event
|
self.event = send_event
|
||||||
self.logger = logging.getLogger(__name__)
|
self.logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
self.type = "mobile" if type == "socket" else type
|
self.type = type
|
||||||
|
|
||||||
if type == "socket":
|
if type == "socket":
|
||||||
self.proto = MobileProto()
|
self.proto = MobileProto()
|
||||||
@@ -33,26 +31,23 @@ class BaseProcessor:
|
|||||||
async def _send(self, writer, packet):
|
async def _send(self, writer, packet):
|
||||||
try:
|
try:
|
||||||
# Если объектом является вебсокет, то используем функцию send для отправки
|
# Если объектом является вебсокет, то используем функцию send для отправки
|
||||||
if hasattr(writer, "send"):
|
if hasattr(writer, 'send'):
|
||||||
await writer.send(packet)
|
await writer.send(packet)
|
||||||
else: # В ином случае отправляем как в обычный сокет
|
else: # В ином случае отправляем как в обычный сокет
|
||||||
writer.write(packet)
|
writer.write(packet)
|
||||||
await writer.drain()
|
await writer.drain()
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
async def _send_error(self, seq, opcode, error_type, writer):
|
async def _send_error(self, seq, opcode, error_type, writer):
|
||||||
payload = self.static.ERROR_TYPES.get(
|
payload = self.static.ERROR_TYPES.get(error_type, {
|
||||||
error_type,
|
"localizedMessage": "Неизвестная ошибка",
|
||||||
{
|
"error": "unknown.error",
|
||||||
"localizedMessage": "Неизвестная ошибка",
|
"message": "Unknown error",
|
||||||
"error": "unknown.error",
|
"title": "Неизвестная ошибка"
|
||||||
"message": "Unknown error",
|
})
|
||||||
"title": "Неизвестная ошибка",
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
packet = self.proto.pack_packet(
|
packet = self.proto.pack_packet(
|
||||||
cmd=self.proto.CMD_ERR, seq=seq, opcode=opcode, payload=payload
|
cmd=self.proto.CMD_ERR, seq=seq, opcode=opcode, payload=payload
|
||||||
)
|
)
|
||||||
await self._send(writer, packet)
|
await self._send(writer, packet)
|
||||||
@@ -43,18 +43,11 @@ class ServerConfig:
|
|||||||
|
|
||||||
### Telegram bot
|
### Telegram bot
|
||||||
telegram_bot_token = os.getenv("telegram_bot_token") or "123456789:ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
telegram_bot_token = os.getenv("telegram_bot_token") or "123456789:ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||||
telegram_bot_enabled = bool(int(os.getenv("telegram_bot_enabled", 0)))
|
telegram_bot_enabled = bool(os.getenv("telegram_bot_enabled")) or True
|
||||||
telegram_whitelist_ids = [x.strip() for x in os.getenv("telegram_whitelist_ids", "").split(",") if x.strip()]
|
telegram_whitelist_ids = [x.strip() for x in os.getenv("telegram_whitelist_ids", "").split(",") if x.strip()]
|
||||||
telegram_whitelist_enabled = bool(int(os.getenv("telegram_whitelist_enabled", 0)))
|
|
||||||
|
|
||||||
### origins
|
### origins
|
||||||
origins = [x.strip() for x in os.getenv("origins", "").split(",") if x.strip()] if os.getenv("origins") else None
|
origins = [x.strip() for x in os.getenv("origins", "").split(",") if x.strip()] if os.getenv("origins") else None
|
||||||
|
|
||||||
### sms шлюз
|
### sms шлюз
|
||||||
sms_gateway_url = os.getenv("sms_gateway_url", "")
|
sms_gateway_url = os.getenv("sms_gateway_url") or "http://127.0.0.1/sms-gateway"
|
||||||
|
|
||||||
### Firebase
|
|
||||||
firebase_credentials_path = os.getenv("firebase_credentials_path", "")
|
|
||||||
|
|
||||||
### Путь к гео бд
|
|
||||||
geo_db_path = os.getenv("geo_db_path", "")
|
|
||||||
@@ -44,7 +44,6 @@ class Opcodes:
|
|||||||
CONTACT_MUTUAL = 38
|
CONTACT_MUTUAL = 38
|
||||||
CONTACT_PHOTOS = 39
|
CONTACT_PHOTOS = 39
|
||||||
CONTACT_SORT = 40
|
CONTACT_SORT = 40
|
||||||
CONTACT_ADD_BY_PHONE = 41
|
|
||||||
CONTACT_VERIFY = 42
|
CONTACT_VERIFY = 42
|
||||||
REMOVE_CONTACT_PHOTO = 43
|
REMOVE_CONTACT_PHOTO = 43
|
||||||
CHAT_INFO = 48
|
CHAT_INFO = 48
|
||||||
|
|||||||
@@ -1,92 +0,0 @@
|
|||||||
import asyncio
|
|
||||||
import logging
|
|
||||||
import time
|
|
||||||
import firebase_admin
|
|
||||||
from firebase_admin import credentials, messaging
|
|
||||||
|
|
||||||
class PushService:
|
|
||||||
def __init__(self, credentials_path):
|
|
||||||
self.logger = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
if not credentials_path:
|
|
||||||
self.logger.warning("Огненная база сегодня не работает, укажите путь к файлу с ключами")
|
|
||||||
self.enabled = False
|
|
||||||
return
|
|
||||||
|
|
||||||
cred = credentials.Certificate(credentials_path)
|
|
||||||
firebase_admin.initialize_app(cred)
|
|
||||||
self.enabled = True
|
|
||||||
self.logger.info("Огненная база инициализирована")
|
|
||||||
|
|
||||||
async def send(self, push_token, data):
|
|
||||||
"""Отправка пуша"""
|
|
||||||
if not self.enabled:
|
|
||||||
return None
|
|
||||||
|
|
||||||
str_data = {k: str(v) for k, v in data.items() if v is not None}
|
|
||||||
|
|
||||||
message = messaging.Message(
|
|
||||||
data=str_data,
|
|
||||||
token=push_token,
|
|
||||||
android=messaging.AndroidConfig(
|
|
||||||
priority="high",
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
try:
|
|
||||||
loop = asyncio.get_event_loop()
|
|
||||||
response = await loop.run_in_executor(None, messaging.send, message)
|
|
||||||
self.logger.debug(f"Отправил пуш: {response}")
|
|
||||||
return response
|
|
||||||
except messaging.UnregisteredError:
|
|
||||||
self.logger.warning(f"Пуш-токен не зарегистрирован: {push_token}")
|
|
||||||
return None
|
|
||||||
except Exception as e:
|
|
||||||
self.logger.error(f"Не удалось отправить пуш: {e}")
|
|
||||||
return None
|
|
||||||
|
|
||||||
async def send_to_user(self, db_pool, phone, sender_id=None, msg_id=None,
|
|
||||||
chat_id=None, text="", is_group=False):
|
|
||||||
"""Отправка пушей на все устройства пользователя"""
|
|
||||||
if not self.enabled:
|
|
||||||
return
|
|
||||||
|
|
||||||
# Получаем имя отправителя
|
|
||||||
user_name = ""
|
|
||||||
if sender_id:
|
|
||||||
async with db_pool.acquire() as conn:
|
|
||||||
async with conn.cursor() as cursor:
|
|
||||||
await cursor.execute(
|
|
||||||
"SELECT firstname, lastname FROM users WHERE id = %s",
|
|
||||||
(sender_id,)
|
|
||||||
)
|
|
||||||
sender = await cursor.fetchone()
|
|
||||||
if sender:
|
|
||||||
firstname = sender.get("firstname", "")
|
|
||||||
lastname = sender.get("lastname", "")
|
|
||||||
user_name = f"{firstname} {lastname}".strip()
|
|
||||||
|
|
||||||
now_ms = str(int(time.time() * 1000))
|
|
||||||
msg_type = "ChatMessage" if is_group else "Message"
|
|
||||||
data = {
|
|
||||||
"type": msg_type,
|
|
||||||
"msgid": str(msg_id) if msg_id else "0",
|
|
||||||
"suid": str(sender_id) if sender_id else None,
|
|
||||||
"mc": str(chat_id) if chat_id else None,
|
|
||||||
"msg": text,
|
|
||||||
"userName": user_name,
|
|
||||||
"ttime": now_ms,
|
|
||||||
"ctime": now_ms,
|
|
||||||
}
|
|
||||||
|
|
||||||
# Получаем все пуш-токены пользователя
|
|
||||||
async with db_pool.acquire() as conn:
|
|
||||||
async with conn.cursor() as cursor:
|
|
||||||
await cursor.execute(
|
|
||||||
"SELECT push_token FROM tokens WHERE phone = %s AND push_token IS NOT NULL",
|
|
||||||
(phone,)
|
|
||||||
)
|
|
||||||
rows = await cursor.fetchall()
|
|
||||||
|
|
||||||
for row in rows:
|
|
||||||
await self.send(row["push_token"], data)
|
|
||||||
@@ -5,20 +5,14 @@ class SQLQueries:
|
|||||||
SELECT_USER_BY_TG_ID = "SELECT * FROM users WHERE telegram_id = %s"
|
SELECT_USER_BY_TG_ID = "SELECT * FROM users WHERE telegram_id = %s"
|
||||||
|
|
||||||
INSERT_USER = """
|
INSERT_USER = """
|
||||||
INSERT INTO users
|
INSERT INTO users
|
||||||
(id, phone, telegram_id, firstname, lastname, username,
|
(id, phone, telegram_id, firstname, lastname, username,
|
||||||
profileoptions, options, accountstatus, updatetime, lastseen)
|
profileoptions, options, accountstatus, updatetime, lastseen)
|
||||||
VALUES (%s, %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 = """
|
INSERT_USER_DATA = """
|
||||||
INSERT INTO user_data
|
INSERT INTO user_data
|
||||||
(phone, user_config, chat_config)
|
(phone, contacts, folders, user_config, chat_config)
|
||||||
VALUES (%s, %s, %s)
|
VALUES (%s, %s, %s, %s, %s)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
INSERT_DEFAULT_FOLDER = """
|
|
||||||
INSERT INTO user_folders
|
|
||||||
(id, phone, title, sort_order)
|
|
||||||
VALUES ('all.chat.folder', %s, 'Все', 0)
|
|
||||||
"""
|
|
||||||
@@ -1,63 +0,0 @@
|
|||||||
class SQLiteCursorCompat:
|
|
||||||
def __init__(self, connection):
|
|
||||||
self.connection = connection
|
|
||||||
self.cursor = None
|
|
||||||
|
|
||||||
async def __aenter__(self):
|
|
||||||
self.cursor = await self.connection.cursor()
|
|
||||||
return self
|
|
||||||
|
|
||||||
async def __aexit__(self, exc_type, exc, tb):
|
|
||||||
if self.cursor is not None:
|
|
||||||
await self.cursor.close()
|
|
||||||
self.cursor = None
|
|
||||||
|
|
||||||
@property
|
|
||||||
def lastrowid(self):
|
|
||||||
return None if self.cursor is None else self.cursor.lastrowid
|
|
||||||
|
|
||||||
def _normalize_query(self, query):
|
|
||||||
return query.replace("%s", "?").replace(
|
|
||||||
"UNIX_TIMESTAMP()", "CAST(strftime('%s','now') AS INTEGER)"
|
|
||||||
)
|
|
||||||
|
|
||||||
async def execute(self, query, params=()):
|
|
||||||
normalized_query = self._normalize_query(query)
|
|
||||||
if params is None:
|
|
||||||
params = ()
|
|
||||||
elif not isinstance(params, (tuple, list, dict)):
|
|
||||||
params = (params,)
|
|
||||||
await self.cursor.execute(normalized_query, params)
|
|
||||||
|
|
||||||
async def fetchone(self):
|
|
||||||
row = await self.cursor.fetchone()
|
|
||||||
if row is None:
|
|
||||||
return None
|
|
||||||
return dict(row)
|
|
||||||
|
|
||||||
async def fetchall(self):
|
|
||||||
rows = await self.cursor.fetchall()
|
|
||||||
return [dict(row) for row in rows]
|
|
||||||
|
|
||||||
class SQLiteConnectionCompat:
|
|
||||||
def __init__(self, connection):
|
|
||||||
self.connection = connection
|
|
||||||
|
|
||||||
async def __aenter__(self):
|
|
||||||
return self
|
|
||||||
|
|
||||||
async def __aexit__(self, exc_type, exc, tb):
|
|
||||||
return False
|
|
||||||
|
|
||||||
async def commit(self):
|
|
||||||
await self.connection.commit()
|
|
||||||
|
|
||||||
def cursor(self):
|
|
||||||
return SQLiteCursorCompat(self.connection)
|
|
||||||
|
|
||||||
class SQLitePoolCompat:
|
|
||||||
def __init__(self, connection):
|
|
||||||
self.connection = connection
|
|
||||||
|
|
||||||
def acquire(self):
|
|
||||||
return SQLiteConnectionCompat(self.connection)
|
|
||||||
@@ -13,9 +13,6 @@ class Static:
|
|||||||
CHAT_NOT_FOUND = "chat_not_found"
|
CHAT_NOT_FOUND = "chat_not_found"
|
||||||
CHAT_NOT_ACCESS = "chat_not_access"
|
CHAT_NOT_ACCESS = "chat_not_access"
|
||||||
RATE_LIMITED = "rate_limited"
|
RATE_LIMITED = "rate_limited"
|
||||||
CONTACT_NOT_FOUND = "contact_not_found"
|
|
||||||
CONTACT_ALREADY_ADDED = "contact_already_added"
|
|
||||||
CONTACT_BLOCKED = "contact_blocked"
|
|
||||||
|
|
||||||
class ChatTypes:
|
class ChatTypes:
|
||||||
DIALOG = "DIALOG"
|
DIALOG = "DIALOG"
|
||||||
@@ -83,25 +80,7 @@ class Static:
|
|||||||
"error": "error.rate_limited",
|
"error": "error.rate_limited",
|
||||||
"message": "Too many attempts. Please try again later",
|
"message": "Too many attempts. Please try again later",
|
||||||
"title": "Слишком много попыток"
|
"title": "Слишком много попыток"
|
||||||
},
|
}
|
||||||
"contact_not_found": {
|
|
||||||
"localizedMessage": "Контакт не найден",
|
|
||||||
"error": "contact.not.found",
|
|
||||||
"message": "Contact not found",
|
|
||||||
"title": "Контакт не найден"
|
|
||||||
},
|
|
||||||
"contact_already_added": {
|
|
||||||
"localizedMessage": "Контакт уже добавлен",
|
|
||||||
"error": "contact.already.added",
|
|
||||||
"message": "Contact already added",
|
|
||||||
"title": "Контакт уже добавлен"
|
|
||||||
},
|
|
||||||
"contact_blocked": {
|
|
||||||
"localizedMessage": "Вы не можете написать этому пользователю",
|
|
||||||
"error": "contact.blocked",
|
|
||||||
"message": "Contact is blocked",
|
|
||||||
"title": "Вы не можете написать этому пользователю"
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
### Сообщения бота
|
### Сообщения бота
|
||||||
@@ -137,86 +116,36 @@ class Static:
|
|||||||
|
|
||||||
### Причины для жалоб
|
### Причины для жалоб
|
||||||
COMPLAIN_REASONS = [
|
COMPLAIN_REASONS = [
|
||||||
{"typeId": 5, "reasons": [
|
"Порнография или эротика",
|
||||||
{"reasonTitle": "Мошенничество", "reasonId": 8},
|
"Экстремизм или терроризм",
|
||||||
{"reasonTitle": "Спам", "reasonId": 9},
|
"Фейк",
|
||||||
{"reasonTitle": "Порнографический контент", "reasonId": 23},
|
"Мошенничество",
|
||||||
{"reasonTitle": "Насилие", "reasonId": 18},
|
"Нарушение авторского права",
|
||||||
{"reasonTitle": "Оскорбления", "reasonId": 11},
|
"Шокирующий контент",
|
||||||
{"reasonTitle": "Экстремизм", "reasonId": 20},
|
"Персональные данные",
|
||||||
{"reasonTitle": "Запрещенные товары", "reasonId": 21},
|
"Незаконная услуга",
|
||||||
{"reasonTitle": "Мне не нравится", "reasonId": 22},
|
"Это законно, но надо удалить"
|
||||||
{"reasonTitle": "Другое", "reasonId": 7},
|
|
||||||
]},
|
|
||||||
{"typeId": 4, "reasons": [
|
|
||||||
{"reasonTitle": "Мошенничество", "reasonId": 8},
|
|
||||||
{"reasonTitle": "Спам", "reasonId": 9},
|
|
||||||
{"reasonTitle": "Порнографический контент", "reasonId": 23},
|
|
||||||
{"reasonTitle": "Насилие", "reasonId": 18},
|
|
||||||
{"reasonTitle": "Оскорбления", "reasonId": 11},
|
|
||||||
{"reasonTitle": "Экстремизм", "reasonId": 20},
|
|
||||||
{"reasonTitle": "Запрещенные товары", "reasonId": 21},
|
|
||||||
{"reasonTitle": "Другое", "reasonId": 7},
|
|
||||||
]},
|
|
||||||
{"typeId": 3, "reasons": [
|
|
||||||
{"reasonTitle": "Мошенничество", "reasonId": 8},
|
|
||||||
{"reasonTitle": "Спам", "reasonId": 9},
|
|
||||||
{"reasonTitle": "Порнографический контент", "reasonId": 23},
|
|
||||||
{"reasonTitle": "Насилие", "reasonId": 18},
|
|
||||||
{"reasonTitle": "Оскорбления", "reasonId": 11},
|
|
||||||
{"reasonTitle": "Экстремизм", "reasonId": 20},
|
|
||||||
{"reasonTitle": "Запрещенные товары", "reasonId": 21},
|
|
||||||
{"reasonTitle": "Другое", "reasonId": 7},
|
|
||||||
]},
|
|
||||||
{"typeId": 7, "reasons": [
|
|
||||||
{"reasonTitle": "Мошенничество", "reasonId": 8},
|
|
||||||
{"reasonTitle": "Спам", "reasonId": 9},
|
|
||||||
{"reasonTitle": "Порнографический контент", "reasonId": 23},
|
|
||||||
{"reasonTitle": "Насилие", "reasonId": 18},
|
|
||||||
{"reasonTitle": "Оскорбления", "reasonId": 11},
|
|
||||||
{"reasonTitle": "Экстремизм", "reasonId": 20},
|
|
||||||
{"reasonTitle": "Запрещенные товары", "reasonId": 21},
|
|
||||||
{"reasonTitle": "Другое", "reasonId": 7},
|
|
||||||
]},
|
|
||||||
{"typeId": 8, "reasons": [
|
|
||||||
{"reasonTitle": "Спам", "reasonId": 9},
|
|
||||||
{"reasonTitle": "Шантаж", "reasonId": 10},
|
|
||||||
{"reasonTitle": "Оскорбления", "reasonId": 11},
|
|
||||||
{"reasonTitle": "Другое", "reasonId": 7},
|
|
||||||
]},
|
|
||||||
{"typeId": 2, "reasons": [
|
|
||||||
{"reasonTitle": "Мошенничество", "reasonId": 8},
|
|
||||||
{"reasonTitle": "Спам", "reasonId": 9},
|
|
||||||
{"reasonTitle": "Порнографический контент", "reasonId": 23},
|
|
||||||
{"reasonTitle": "Насилие", "reasonId": 18},
|
|
||||||
{"reasonTitle": "Оскорбления", "reasonId": 11},
|
|
||||||
{"reasonTitle": "Экстремизм", "reasonId": 20},
|
|
||||||
{"reasonTitle": "Запрещенные товары", "reasonId": 21},
|
|
||||||
{"reasonTitle": "Мне не нравится", "reasonId": 22},
|
|
||||||
{"reasonTitle": "Другое", "reasonId": 7},
|
|
||||||
]},
|
|
||||||
{"typeId": 6, "reasons": [
|
|
||||||
{"reasonTitle": "Мошенничество", "reasonId": 8},
|
|
||||||
{"reasonTitle": "Спам", "reasonId": 9},
|
|
||||||
{"reasonTitle": "Порнографический контент", "reasonId": 23},
|
|
||||||
{"reasonTitle": "Насилие", "reasonId": 18},
|
|
||||||
{"reasonTitle": "Оскорбления", "reasonId": 11},
|
|
||||||
{"reasonTitle": "Экстремизм", "reasonId": 20},
|
|
||||||
{"reasonTitle": "Запрещенные товары", "reasonId": 21},
|
|
||||||
{"reasonTitle": "Другое", "reasonId": 7},
|
|
||||||
]},
|
|
||||||
{"typeId": 1, "reasons": [
|
|
||||||
{"reasonTitle": "Мошенничество", "reasonId": 8},
|
|
||||||
{"reasonTitle": "Спам", "reasonId": 9},
|
|
||||||
{"reasonTitle": "Порнографический контент", "reasonId": 23},
|
|
||||||
{"reasonTitle": "Насилие", "reasonId": 18},
|
|
||||||
{"reasonTitle": "Оскорбления", "reasonId": 11},
|
|
||||||
{"reasonTitle": "Экстремизм", "reasonId": 20},
|
|
||||||
{"reasonTitle": "Запрещенные товары", "reasonId": 21},
|
|
||||||
{"reasonTitle": "Другое", "reasonId": 7},
|
|
||||||
]},
|
|
||||||
]
|
]
|
||||||
|
|
||||||
|
### Заглушка для папок
|
||||||
|
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 = {
|
USER_SETTINGS = {
|
||||||
"CHATS_PUSH_NOTIFICATION": "ON",
|
"CHATS_PUSH_NOTIFICATION": "ON",
|
||||||
"PUSH_DETAILS": True,
|
"PUSH_DETAILS": True,
|
||||||
|
|||||||
@@ -1,63 +1,28 @@
|
|||||||
|
import hashlib
|
||||||
import json
|
import json
|
||||||
import random
|
import random
|
||||||
import secrets
|
|
||||||
import time
|
import time
|
||||||
|
|
||||||
import geoip2.database
|
|
||||||
|
|
||||||
|
|
||||||
class Tools:
|
class Tools:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def build_message_dict(self, row, protocol_type="mobile"):
|
|
||||||
"""Сборка тела сообщения"""
|
|
||||||
try:
|
|
||||||
attaches = json.loads(row.get("attaches") or "[]")
|
|
||||||
except (TypeError, ValueError):
|
|
||||||
attaches = []
|
|
||||||
try:
|
|
||||||
elements = json.loads(row.get("elements") or "[]")
|
|
||||||
except (TypeError, ValueError):
|
|
||||||
elements = []
|
|
||||||
|
|
||||||
message = {
|
|
||||||
"id": row.get("id") if protocol_type == "mobile" else str(row.get("id")),
|
|
||||||
"cid": int(row.get("cid") or 0),
|
|
||||||
"chatId": int(row.get("chat_id") or 0),
|
|
||||||
"time": int(row.get("time")),
|
|
||||||
"type": row.get("type") or "USER",
|
|
||||||
"sender": row.get("sender"),
|
|
||||||
"text": row.get("text") or "",
|
|
||||||
"attaches": attaches,
|
|
||||||
"elements": elements,
|
|
||||||
"reactionInfo": {},
|
|
||||||
"link": {}
|
|
||||||
}
|
|
||||||
|
|
||||||
return message
|
|
||||||
|
|
||||||
def generate_profile(
|
def generate_profile(
|
||||||
self,
|
self,
|
||||||
id=None,
|
id=1,
|
||||||
phone=None,
|
phone=70000000000,
|
||||||
avatarUrl=None,
|
avatarUrl=None,
|
||||||
photoId=None,
|
photoId=None,
|
||||||
updateTime=None,
|
updateTime=0,
|
||||||
firstName=None,
|
firstName="Test",
|
||||||
lastName=None,
|
lastName="Account",
|
||||||
options=None,
|
options=[],
|
||||||
description=None,
|
description=None,
|
||||||
accountStatus=None,
|
accountStatus=0,
|
||||||
profileOptions=None,
|
profileOptions=[],
|
||||||
includeProfileOptions=True,
|
includeProfileOptions=True,
|
||||||
username=None,
|
username=None,
|
||||||
|
|
||||||
# для контактов, собственно
|
|
||||||
custom_firstname=None,
|
|
||||||
custom_lastname=None,
|
|
||||||
|
|
||||||
blocked=False
|
|
||||||
):
|
):
|
||||||
contact = {
|
contact = {
|
||||||
"id": id,
|
"id": id,
|
||||||
@@ -73,8 +38,6 @@ class Tools:
|
|||||||
],
|
],
|
||||||
"options": options,
|
"options": options,
|
||||||
"accountStatus": accountStatus,
|
"accountStatus": accountStatus,
|
||||||
"location": "RU",
|
|
||||||
"registrationTime": int(time.time() * 1000)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if avatarUrl:
|
if avatarUrl:
|
||||||
@@ -88,19 +51,6 @@ class Tools:
|
|||||||
if username:
|
if username:
|
||||||
contact["link"] = "https://max.ru/" + username
|
contact["link"] = "https://max.ru/" + username
|
||||||
|
|
||||||
if custom_firstname:
|
|
||||||
contact["names"].append(
|
|
||||||
{
|
|
||||||
"name": custom_firstname,
|
|
||||||
"firstName": custom_firstname,
|
|
||||||
"lastName": custom_lastname,
|
|
||||||
"type": "CUSTOM"
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
if blocked:
|
|
||||||
contact["status"] = "BLOCKED"
|
|
||||||
|
|
||||||
if includeProfileOptions:
|
if includeProfileOptions:
|
||||||
return {"contact": contact, "profileOptions": profileOptions}
|
return {"contact": contact, "profileOptions": profileOptions}
|
||||||
else:
|
else:
|
||||||
@@ -108,40 +58,22 @@ class Tools:
|
|||||||
|
|
||||||
def generate_profile_tt(
|
def generate_profile_tt(
|
||||||
self,
|
self,
|
||||||
id=None,
|
id=1,
|
||||||
phone=None,
|
phone=70000000000,
|
||||||
avatarUrl=None,
|
avatarUrl=None,
|
||||||
photoId=None,
|
photoId=None,
|
||||||
updateTime=None,
|
updateTime=0,
|
||||||
firstName=None,
|
firstName="Test",
|
||||||
lastName=None,
|
lastName="Account",
|
||||||
options=None,
|
options=[],
|
||||||
description=None,
|
description=None,
|
||||||
username=None,
|
username=None,
|
||||||
custom_firstname=None,
|
|
||||||
custom_lastname=None,
|
|
||||||
blocked=None
|
|
||||||
):
|
):
|
||||||
# Так как TT не поддерживает фамилию, и если нам ее не передали в функцию
|
|
||||||
# то используем только имя, чтобы избежать None в фамилии
|
|
||||||
if firstName and lastName:
|
|
||||||
name = f"{firstName} {lastName}"
|
|
||||||
else:
|
|
||||||
name = firstName
|
|
||||||
|
|
||||||
# Используем такой же костыль, как и выше
|
|
||||||
if custom_firstname and custom_lastname:
|
|
||||||
custom_name = f"{custom_firstname} {custom_lastname}"
|
|
||||||
elif custom_firstname:
|
|
||||||
custom_name = custom_firstname
|
|
||||||
else:
|
|
||||||
custom_name = None
|
|
||||||
|
|
||||||
contact = {
|
contact = {
|
||||||
"id": id,
|
"id": id,
|
||||||
"updateTime": updateTime,
|
"updateTime": updateTime,
|
||||||
"phone": phone,
|
"phone": phone,
|
||||||
"names": [{"name": name, "type": "TT"}],
|
"names": [{"name": f"{firstName} {lastName}", "type": "TT"}],
|
||||||
"options": options,
|
"options": options,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -153,19 +85,8 @@ class Tools:
|
|||||||
if description:
|
if description:
|
||||||
contact["description"] = description
|
contact["description"] = description
|
||||||
|
|
||||||
# NOTE: официальный сервер вроде как отдавал tt.me, но клиент примет любую ссылку
|
|
||||||
# можно потом как нибудь сделать возможность редактирования этого момента, но это
|
|
||||||
# позже, так как по юзернейму искать пока нельзя
|
|
||||||
if username:
|
if username:
|
||||||
contact["link"] = "https://tt.me/" + username
|
contact["link"] = "https://tamtam.chat/" + username
|
||||||
|
|
||||||
if custom_firstname:
|
|
||||||
contact["names"].append(
|
|
||||||
{"name": custom_name, "type": "CUSTOM"}
|
|
||||||
)
|
|
||||||
|
|
||||||
if blocked:
|
|
||||||
contact["status"] = "BLOCKED"
|
|
||||||
|
|
||||||
return contact
|
return contact
|
||||||
|
|
||||||
@@ -175,12 +96,10 @@ class Tools:
|
|||||||
"""Генерация чата"""
|
"""Генерация чата"""
|
||||||
# Генерируем список участников
|
# Генерируем список участников
|
||||||
if isinstance(participants, dict):
|
if isinstance(participants, dict):
|
||||||
result_participants = {
|
result_participants = {str(k): v for k, v in participants.items()}
|
||||||
int(k): int(v) if v is not None else 0 for k, v in participants.items()
|
|
||||||
}
|
|
||||||
else:
|
else:
|
||||||
# assume list
|
# assume list
|
||||||
result_participants = {int(participant): 0 for participant in participants}
|
result_participants = {str(participant): 0 for participant in participants}
|
||||||
|
|
||||||
result = None
|
result = None
|
||||||
|
|
||||||
@@ -206,14 +125,7 @@ class Tools:
|
|||||||
# Возвращаем
|
# Возвращаем
|
||||||
return result
|
return result
|
||||||
|
|
||||||
async def generate_chats(
|
async def generate_chats(self, chatIds, db_pool, senderId, include_favourites=True, protocol_type='mobile'):
|
||||||
self,
|
|
||||||
chatIds,
|
|
||||||
db_pool,
|
|
||||||
senderId,
|
|
||||||
include_favourites=True,
|
|
||||||
protocol_type="mobile",
|
|
||||||
):
|
|
||||||
"""Генерирует чаты для отдачи клиенту"""
|
"""Генерирует чаты для отдачи клиенту"""
|
||||||
# Готовый список с чатами
|
# Готовый список с чатами
|
||||||
chats = []
|
chats = []
|
||||||
@@ -263,28 +175,25 @@ class Tools:
|
|||||||
|
|
||||||
if include_favourites:
|
if include_favourites:
|
||||||
# Получаем последнее сообщение из избранного
|
# Получаем последнее сообщение из избранного
|
||||||
favouriteChatId = -senderId
|
|
||||||
message, messageTime = await self.get_last_message(
|
message, messageTime = await self.get_last_message(
|
||||||
favouriteChatId, db_pool, protocol_type=protocol_type
|
senderId, db_pool, protocol_type=protocol_type
|
||||||
)
|
)
|
||||||
|
|
||||||
# ID избранного для клиента
|
# ID избранного
|
||||||
chatId = senderId ^ senderId
|
chatId = senderId ^ senderId
|
||||||
|
|
||||||
# Получаем последнюю активность в избранном
|
# Получаем последнюю активность участника (отправителя) в избранном
|
||||||
participants = await self.get_participant_last_activity(
|
participants = await self.get_participant_last_activity(
|
||||||
favouriteChatId, [senderId], db_pool
|
senderId, [senderId], db_pool
|
||||||
)
|
)
|
||||||
|
|
||||||
# Получаем ID предыдущего сообщения для избранного
|
# Получаем ID предыдущего сообщения для избранного (чат ID = senderId)
|
||||||
prevMessageId = await self.get_previous_message_id(
|
prevMessageId = await self.get_previous_message_id(senderId, db_pool, protocol_type=protocol_type)
|
||||||
favouriteChatId, db_pool, protocol_type=protocol_type
|
|
||||||
)
|
|
||||||
|
|
||||||
# Хардкодим в лист чатов избранное
|
# Хардкодим в лист чатов избранное
|
||||||
chats.append(
|
chats.append(
|
||||||
self.generate_chat(
|
self.generate_chat(
|
||||||
chatId,
|
chatId if protocol_type == 'mobile' else str(chatId),
|
||||||
senderId,
|
senderId,
|
||||||
"DIALOG",
|
"DIALOG",
|
||||||
participants,
|
participants,
|
||||||
@@ -296,124 +205,6 @@ class Tools:
|
|||||||
|
|
||||||
return chats
|
return chats
|
||||||
|
|
||||||
async def generate_contacts(
|
|
||||||
self,
|
|
||||||
contacts,
|
|
||||||
db_pool,
|
|
||||||
avatar_base_url="",
|
|
||||||
):
|
|
||||||
"""
|
|
||||||
Генерация контакт-листа для отдачи клиенту
|
|
||||||
|
|
||||||
[notes]
|
|
||||||
В contacts должен поступать список вида
|
|
||||||
|
|
||||||
[
|
|
||||||
{
|
|
||||||
"firstname": "test",
|
|
||||||
"lastname": "testovich",
|
|
||||||
"id": 4323
|
|
||||||
}
|
|
||||||
]
|
|
||||||
|
|
||||||
А формировать мы должны его до вызова функции,
|
|
||||||
ибо я хочу вынести контакты в отдельную таблицу,
|
|
||||||
по моему мнению так будет намного практичнее и лучше
|
|
||||||
"""
|
|
||||||
# Готовый список с контакт-листом
|
|
||||||
contact_list = []
|
|
||||||
|
|
||||||
# Формируем список контактов
|
|
||||||
for contact in contacts:
|
|
||||||
# ID контакта
|
|
||||||
contact_id = contact.get("id")
|
|
||||||
|
|
||||||
# Имя и фамилия которые указал юзер для контакта
|
|
||||||
firstname = contact.get("firstname")
|
|
||||||
lastname = contact.get("lastname")
|
|
||||||
blocked = contact.get("blocked", False)
|
|
||||||
|
|
||||||
async with db_pool.acquire() as db_connection:
|
|
||||||
async with db_connection.cursor() as cursor:
|
|
||||||
# Получаем контакт по id
|
|
||||||
await cursor.execute(
|
|
||||||
"SELECT * FROM `users` WHERE id = %s", (contact_id,)
|
|
||||||
)
|
|
||||||
user = await cursor.fetchone()
|
|
||||||
|
|
||||||
if user:
|
|
||||||
# Аватарка с биографией
|
|
||||||
photoId = (
|
|
||||||
None
|
|
||||||
if not user.get("avatar_id")
|
|
||||||
else int(user.get("avatar_id"))
|
|
||||||
)
|
|
||||||
avatar_url = (
|
|
||||||
None
|
|
||||||
if not photoId
|
|
||||||
else avatar_base_url + str(photoId)
|
|
||||||
)
|
|
||||||
description = (
|
|
||||||
None
|
|
||||||
if not user.get("description")
|
|
||||||
else user.get("description")
|
|
||||||
)
|
|
||||||
|
|
||||||
# Создаем профиль
|
|
||||||
contact = self.generate_profile(
|
|
||||||
id=user.get("id"),
|
|
||||||
phone=int(user.get("phone")),
|
|
||||||
avatarUrl=avatar_url,
|
|
||||||
photoId=photoId,
|
|
||||||
updateTime=int(user.get("updatetime")),
|
|
||||||
firstName=user.get("firstname"),
|
|
||||||
lastName=user.get("lastname"),
|
|
||||||
options=json.loads(user.get("options")),
|
|
||||||
description=description,
|
|
||||||
accountStatus=int(user.get("accountstatus")),
|
|
||||||
includeProfileOptions=False,
|
|
||||||
username=user.get("username"),
|
|
||||||
custom_firstname=firstname,
|
|
||||||
custom_lastname=lastname,
|
|
||||||
blocked=blocked,
|
|
||||||
)
|
|
||||||
|
|
||||||
# Выносим результат в лист
|
|
||||||
contact_list.append(contact)
|
|
||||||
|
|
||||||
return contact_list
|
|
||||||
|
|
||||||
async def collect_user_contacts(
|
|
||||||
self,
|
|
||||||
owner_id,
|
|
||||||
db_pool,
|
|
||||||
avatar_base_url="",
|
|
||||||
):
|
|
||||||
"""Собирает все контакты пользователя и возвращает готовый контакт-лист"""
|
|
||||||
contacts = []
|
|
||||||
|
|
||||||
async with db_pool.acquire() as db_connection:
|
|
||||||
async with db_connection.cursor() as cursor:
|
|
||||||
await cursor.execute(
|
|
||||||
"SELECT * FROM `contacts` WHERE owner_id = %s AND is_blocked = FALSE",
|
|
||||||
(owner_id,),
|
|
||||||
)
|
|
||||||
rows = await cursor.fetchall()
|
|
||||||
|
|
||||||
for row in rows:
|
|
||||||
contacts.append(
|
|
||||||
{
|
|
||||||
"id": int(row.get("contact_id")),
|
|
||||||
"firstname": row.get("custom_firstname"),
|
|
||||||
"lastname": row.get("custom_lastname"),
|
|
||||||
"blocked": bool(row.get("is_blocked")),
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
return await self.generate_contacts(
|
|
||||||
contacts, db_pool, avatar_base_url=avatar_base_url
|
|
||||||
)
|
|
||||||
|
|
||||||
async def insert_message(
|
async def insert_message(
|
||||||
self, chatId, senderId, text, attaches, elements, cid, type, db_pool
|
self, chatId, senderId, text, attaches, elements, cid, type, db_pool
|
||||||
):
|
):
|
||||||
@@ -428,11 +219,9 @@ class Tools:
|
|||||||
|
|
||||||
row = await cursor.fetchone() or {}
|
row = await cursor.fetchone() or {}
|
||||||
last_message_id = row.get("id") or 0 # последнее id сообщения в чате
|
last_message_id = row.get("id") or 0 # последнее id сообщения в чате
|
||||||
|
message_id = self.generate_id()
|
||||||
message_time = int(time.time() * 1000) # время отправки сообщения
|
message_time = int(time.time() * 1000) # время отправки сообщения
|
||||||
|
|
||||||
# Генерируем ID сообщения
|
|
||||||
message_id = int(time.time() * 1000000) * 1000 + random.randint(100, 999)
|
|
||||||
|
|
||||||
# Вносим новое сообщение в таблицу
|
# Вносим новое сообщение в таблицу
|
||||||
await cursor.execute(
|
await cursor.execute(
|
||||||
"INSERT INTO `messages` (id, chat_id, sender, time, text, attaches, cid, elements, type) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)",
|
"INSERT INTO `messages` (id, chat_id, sender, time, text, attaches, cid, elements, type) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)",
|
||||||
@@ -452,7 +241,7 @@ class Tools:
|
|||||||
# Возвращаем айдишки
|
# Возвращаем айдишки
|
||||||
return int(message_id), int(last_message_id), message_time
|
return int(message_id), int(last_message_id), message_time
|
||||||
|
|
||||||
async def get_last_message(self, chatId, db_pool, protocol_type="mobile"):
|
async def get_last_message(self, chatId, db_pool, protocol_type='mobile'):
|
||||||
"""Получение последнего сообщения в чате"""
|
"""Получение последнего сообщения в чате"""
|
||||||
async with db_pool.acquire() as db_connection:
|
async with db_pool.acquire() as db_connection:
|
||||||
async with db_connection.cursor() as cursor:
|
async with db_connection.cursor() as cursor:
|
||||||
@@ -468,10 +257,23 @@ class Tools:
|
|||||||
if not row:
|
if not row:
|
||||||
return None, None
|
return None, None
|
||||||
|
|
||||||
# Возвращаем
|
# Собираем сообщение
|
||||||
return self.build_message_dict(row, protocol_type), int(row.get("time"))
|
message = {
|
||||||
|
"id": row.get("id") if protocol_type == 'mobile' else str(row.get('id')),
|
||||||
|
"time": int(row.get("time")),
|
||||||
|
"type": row.get("type"),
|
||||||
|
"sender": row.get("sender"),
|
||||||
|
"cid": int(row.get("cid")),
|
||||||
|
"text": row.get("text"),
|
||||||
|
"attaches": json.loads(row.get("attaches")),
|
||||||
|
"elements": json.loads(row.get("elements")),
|
||||||
|
"reactionInfo": {},
|
||||||
|
}
|
||||||
|
|
||||||
async def get_previous_message_id(self, chatId, db_pool, protocol_type="mobile"):
|
# Возвращаем
|
||||||
|
return message, int(row.get("time"))
|
||||||
|
|
||||||
|
async def get_previous_message_id(self, chatId, db_pool, protocol_type='mobile'):
|
||||||
"""Получение ID предыдущего сообщения (второго с конца) в чате."""
|
"""Получение ID предыдущего сообщения (второго с конца) в чате."""
|
||||||
async with db_pool.acquire() as db_connection:
|
async with db_pool.acquire() as db_connection:
|
||||||
async with db_connection.cursor() as cursor:
|
async with db_connection.cursor() as cursor:
|
||||||
@@ -483,14 +285,10 @@ class Tools:
|
|||||||
|
|
||||||
# Если результат есть, возвращаем его
|
# Если результат есть, возвращаем его
|
||||||
if row:
|
if row:
|
||||||
return (
|
return row.get("id") if protocol_type == 'mobile' else str(row.get('id'))
|
||||||
row.get("id")
|
|
||||||
if protocol_type == "mobile"
|
|
||||||
else str(row.get("id"))
|
|
||||||
)
|
|
||||||
|
|
||||||
# В ином случае возвращаем 0
|
# В ином случае возвращаем 0
|
||||||
return 0 if protocol_type == "mobile" else "0"
|
return 0 if protocol_type == 'mobile' else "0"
|
||||||
|
|
||||||
async def get_participant_last_activity(self, chatId, participant_ids, db_pool):
|
async def get_participant_last_activity(self, chatId, participant_ids, db_pool):
|
||||||
"""Возвращает словарь {participant_id: last_activity_time} для участников чата."""
|
"""Возвращает словарь {participant_id: last_activity_time} для участников чата."""
|
||||||
@@ -512,11 +310,11 @@ class Tools:
|
|||||||
rows = await cursor.fetchall()
|
rows = await cursor.fetchall()
|
||||||
|
|
||||||
# Собираем список участников без времени последней активности в чате
|
# Собираем список участников без времени последней активности в чате
|
||||||
result = {int(pid): 0 for pid in participant_ids}
|
result = {str(pid): 0 for pid in participant_ids}
|
||||||
|
|
||||||
# Обновляем для каждого участника время последней активности в чате
|
# Обновляем для каждого участника время последней активности в чате
|
||||||
for row in rows:
|
for row in rows:
|
||||||
sender = int(row["sender"])
|
sender = str(row["sender"])
|
||||||
last_time = row["last_time"]
|
last_time = row["last_time"]
|
||||||
if last_time is not None:
|
if last_time is not None:
|
||||||
result[sender] = int(last_time)
|
result[sender] = int(last_time)
|
||||||
@@ -532,109 +330,22 @@ class Tools:
|
|||||||
(chatId,),
|
(chatId,),
|
||||||
)
|
)
|
||||||
rows = await cursor.fetchall()
|
rows = await cursor.fetchall()
|
||||||
return [int(row["user_id"]) for row in rows]
|
return [row["user_id"] for row in rows]
|
||||||
|
|
||||||
async def auth_required(self, userPhone, coro, *args):
|
async def auth_required(self, userPhone, coro, *args):
|
||||||
if userPhone:
|
if userPhone:
|
||||||
await coro(*args)
|
await coro(*args)
|
||||||
|
|
||||||
async def update_user_config(self, cursor, phone, user_settings, default_settings):
|
def generate_id(self):
|
||||||
"""Функция для обновления юзер конфига из бд в случае его изменения"""
|
# Получаем время в юниксе
|
||||||
|
timestamp = int(time.time())
|
||||||
|
|
||||||
user_config = json.loads(user_settings)
|
# Генерируем дополнительно рандомное число
|
||||||
updated_config = {**default_settings, **user_config}
|
random_number = random.randint(0, 9999)
|
||||||
|
|
||||||
if updated_config != user_config:
|
# Собираем их вместе и вычисляем хеш
|
||||||
await cursor.execute(
|
combined = f"{timestamp}{random_number}".encode()
|
||||||
"UPDATE user_data SET user_config = %s WHERE phone = %s",
|
unique_id = int(hashlib.md5(combined).hexdigest(), 16) % 1000000000
|
||||||
(json.dumps(updated_config), phone),
|
|
||||||
)
|
|
||||||
|
|
||||||
return updated_config
|
# Возвращаем
|
||||||
|
return unique_id
|
||||||
async def collect_presence(self, contact_ids, clients, db_pool):
|
|
||||||
"""Собирает статусы пользователей"""
|
|
||||||
now = int(time.time())
|
|
||||||
presence = {}
|
|
||||||
|
|
||||||
# Список тех, кого нужно поискать в базе данных
|
|
||||||
db_lookup_ids = []
|
|
||||||
|
|
||||||
# Проходимся по всем айдишникам,
|
|
||||||
# которые передал нам клиент
|
|
||||||
for contact_id in contact_ids:
|
|
||||||
contact_id = int(contact_id)
|
|
||||||
|
|
||||||
client = clients.get(contact_id)
|
|
||||||
|
|
||||||
# Если пользователь онлайн
|
|
||||||
if client and client.get("status") == 2:
|
|
||||||
presence[str(contact_id)] = {"seen": now, "status": 2}
|
|
||||||
# Если пользователь подключен,
|
|
||||||
# но не взаимодействует с клиентом
|
|
||||||
elif client and client.get("last_seen"):
|
|
||||||
presence[str(contact_id)] = {"seen": client.get("last_seen")}
|
|
||||||
# А если никакое условие не подошло, то добавляем его в лист,
|
|
||||||
# а позже посмотрим в базе данных
|
|
||||||
else:
|
|
||||||
db_lookup_ids.append(contact_id)
|
|
||||||
|
|
||||||
# Проходимся по листу и добавляем недостающих,
|
|
||||||
# если такие существуют конечно
|
|
||||||
async with db_pool.acquire() as conn:
|
|
||||||
async with conn.cursor() as cursor:
|
|
||||||
for contact_id in db_lookup_ids:
|
|
||||||
await cursor.execute(
|
|
||||||
"SELECT lastseen FROM users WHERE id = %s",
|
|
||||||
(contact_id,)
|
|
||||||
)
|
|
||||||
|
|
||||||
row = await cursor.fetchone()
|
|
||||||
|
|
||||||
if row:
|
|
||||||
lastseen = row.get("lastseen")
|
|
||||||
presence[int(contact_id)] = {"seen": int(lastseen)}
|
|
||||||
|
|
||||||
return presence
|
|
||||||
|
|
||||||
def get_geo(self, ip, db_path):
|
|
||||||
"""
|
|
||||||
Получение страны пользователя по его айпи адресу
|
|
||||||
Используется во время запуска сессии
|
|
||||||
"""
|
|
||||||
try:
|
|
||||||
with geoip2.database.Reader(db_path) as reader:
|
|
||||||
response = reader.country(ip)
|
|
||||||
return response.country.name or "Localhost Federation"
|
|
||||||
except Exception:
|
|
||||||
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
|
|
||||||
|
|
||||||
async def contact_is_blocked(self, owner_id, contact_id, db_pool):
|
|
||||||
"""
|
|
||||||
По изначальной задумке, данная функция должна проверять, заблокирован ли контакт
|
|
||||||
На сервере долгое время не был доделан черный список, хотя управление им было реализовано
|
|
||||||
(на деле, это я поленился)
|
|
||||||
|
|
||||||
Вернёт вам true, если контакт заблокирован, иначе false
|
|
||||||
"""
|
|
||||||
# Проверяем наличие контакта
|
|
||||||
async with db_pool.acquire() as conn:
|
|
||||||
async with conn.cursor() as cursor:
|
|
||||||
await cursor.execute("SELECT * FROM contacts WHERE owner_id = %s AND contact_id = %s AND is_blocked = %s", (owner_id, contact_id, True))
|
|
||||||
row = await cursor.fetchone()
|
|
||||||
|
|
||||||
# Есди контакт существует и заблокирован, возвращаем true,
|
|
||||||
if row:
|
|
||||||
return True
|
|
||||||
else: # в ином случае false
|
|
||||||
return False
|
|
||||||
|
|||||||
71
src/main.py
71
src/main.py
@@ -1,14 +1,9 @@
|
|||||||
# Импортирование библиотек
|
# Импортирование библиотек
|
||||||
import asyncio
|
import asyncio
|
||||||
import logging
|
import logging
|
||||||
import signal
|
|
||||||
import ssl
|
import ssl
|
||||||
import sys
|
|
||||||
import traceback
|
|
||||||
|
|
||||||
from common.config import ServerConfig
|
from common.config import ServerConfig
|
||||||
from common.push import PushService
|
|
||||||
from common.sqlite import SQLitePoolCompat
|
|
||||||
from oneme.controller import OnemeController
|
from oneme.controller import OnemeController
|
||||||
from tamtam.controller import TTController
|
from tamtam.controller import TTController
|
||||||
from telegrambot.controller import TelegramBotController
|
from telegrambot.controller import TelegramBotController
|
||||||
@@ -16,6 +11,7 @@ from telegrambot.controller import TelegramBotController
|
|||||||
# Конфиг сервера
|
# Конфиг сервера
|
||||||
server_config = ServerConfig()
|
server_config = ServerConfig()
|
||||||
|
|
||||||
|
|
||||||
async def init_db():
|
async def init_db():
|
||||||
"""Инициализация базы данных"""
|
"""Инициализация базы данных"""
|
||||||
|
|
||||||
@@ -36,9 +32,8 @@ async def init_db():
|
|||||||
elif server_config.db_type == "sqlite":
|
elif server_config.db_type == "sqlite":
|
||||||
import aiosqlite
|
import aiosqlite
|
||||||
|
|
||||||
raw_db = await aiosqlite.connect(server_config.db_file, isolation_level=None)
|
raw_db = await aiosqlite.connect(server_config.db_file)
|
||||||
raw_db.row_factory = aiosqlite.Row
|
db["acquire"] = lambda: raw_db
|
||||||
db = SQLitePoolCompat(raw_db)
|
|
||||||
|
|
||||||
# Возвращаем
|
# Возвращаем
|
||||||
return db
|
return db
|
||||||
@@ -53,6 +48,7 @@ def init_ssl():
|
|||||||
# Возвращаем
|
# Возвращаем
|
||||||
return ssl_context
|
return ssl_context
|
||||||
|
|
||||||
|
|
||||||
def set_logging():
|
def set_logging():
|
||||||
"""Настройка уровня логирования"""
|
"""Настройка уровня логирования"""
|
||||||
# Настройка уровня логирования
|
# Настройка уровня логирования
|
||||||
@@ -69,33 +65,14 @@ def set_logging():
|
|||||||
async def main():
|
async def main():
|
||||||
"""Запуск сервера"""
|
"""Запуск сервера"""
|
||||||
|
|
||||||
|
async def api_event(target, eventData):
|
||||||
|
for client in api.get("clients", {}).get(target, {}).get("clients", {}):
|
||||||
|
await controllers[client["protocol"]].event(target, client, eventData)
|
||||||
|
|
||||||
set_logging()
|
set_logging()
|
||||||
db = await init_db()
|
db = await init_db()
|
||||||
ssl_context = init_ssl()
|
ssl_context = init_ssl()
|
||||||
clients = {}
|
clients = {}
|
||||||
push_service = PushService(server_config.firebase_credentials_path)
|
|
||||||
|
|
||||||
async def api_event(target, eventData):
|
|
||||||
target_clients = api.get("clients", {}).get(target, {}).get("clients", [])
|
|
||||||
|
|
||||||
for client in target_clients:
|
|
||||||
await controllers[client["protocol"]].event(target, client, eventData)
|
|
||||||
|
|
||||||
# Если у пользователя нет активных подключений
|
|
||||||
# и это новое сообщение - отсылаем пуш
|
|
||||||
if not target_clients and eventData.get("eventType") == "new_msg":
|
|
||||||
message = eventData.get("message", {})
|
|
||||||
sender_id = message.get("sender")
|
|
||||||
text = message.get("text", "")
|
|
||||||
chat_id = eventData.get("chatId", "")
|
|
||||||
msg_id = message.get("id", 0)
|
|
||||||
await push_service.send_to_user(
|
|
||||||
db, target,
|
|
||||||
sender_id=sender_id,
|
|
||||||
msg_id=msg_id,
|
|
||||||
chat_id=chat_id,
|
|
||||||
text=text,
|
|
||||||
)
|
|
||||||
|
|
||||||
api = {
|
api = {
|
||||||
"db": db,
|
"db": db,
|
||||||
@@ -113,39 +90,11 @@ async def main():
|
|||||||
|
|
||||||
api["telegram_bot"] = controllers["telegrambot"]
|
api["telegram_bot"] = controllers["telegrambot"]
|
||||||
|
|
||||||
loop = asyncio.get_running_loop()
|
tasks = [controller.launch(api) for controller in controllers.values()]
|
||||||
running_tasks = []
|
|
||||||
|
|
||||||
def _shutdown(sig):
|
|
||||||
logging.info(f"Получен сигнал {sig}, завершаем все задачи...")
|
|
||||||
for task in running_tasks:
|
|
||||||
task.cancel()
|
|
||||||
|
|
||||||
if sys.platform != "win32":
|
|
||||||
for sig in (signal.SIGTERM, signal.SIGINT):
|
|
||||||
loop.add_signal_handler(sig, _shutdown, sig)
|
|
||||||
|
|
||||||
coros = [controller.launch(api) for controller in controllers.values()]
|
|
||||||
running_tasks.extend(asyncio.create_task(coro) for coro in coros)
|
|
||||||
|
|
||||||
# Запускаем контроллеры
|
# Запускаем контроллеры
|
||||||
try:
|
await asyncio.gather(*tasks)
|
||||||
await asyncio.gather(*running_tasks)
|
|
||||||
except asyncio.CancelledError:
|
|
||||||
logging.info("Все задачи завершены")
|
|
||||||
except Exception as e:
|
|
||||||
logging.error(
|
|
||||||
f"Произошла неизвестная ошибка: {e}"
|
|
||||||
)
|
|
||||||
traceback.print_exc()
|
|
||||||
finally:
|
|
||||||
if hasattr(db, 'close'):
|
|
||||||
db.close()
|
|
||||||
await db.wait_closed()
|
|
||||||
elif hasattr(db, 'connection') and hasattr(db.connection, 'close'):
|
|
||||||
await db.connection.close()
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
asyncio.run(main())
|
asyncio.run(main())
|
||||||
|
|
||||||
|
|||||||
@@ -3,334 +3,120 @@ class OnemeConfig:
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
SERVER_CONFIG = {
|
SERVER_CONFIG = {
|
||||||
"account-nickname-enabled": False,
|
"async-tracer": 0,
|
||||||
"account-removal-enabled": False,
|
"presence-ttl": 300,
|
||||||
"anr-config": {
|
"non-contact-sync-time": 86400,
|
||||||
"enabled": True,
|
"contact-batching-variant": 0,
|
||||||
"timeout": {
|
"account-nickname-enabled": True,
|
||||||
"low": 5000,
|
"web-ad-banner": {
|
||||||
"avg": 5000,
|
"enabled": False
|
||||||
"high": 5000
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
"appearance-multi-theme-screen-enabled": True,
|
"edit-timeout": 0,
|
||||||
"audio-transcription-locales": [],
|
"reactions-menu": [],
|
||||||
"available-complaints": [
|
|
||||||
"FAKE",
|
|
||||||
"SPAM",
|
|
||||||
"PORNO",
|
|
||||||
"EXTREMISM",
|
|
||||||
"THREAT",
|
|
||||||
"OTHER"
|
|
||||||
],
|
|
||||||
"avatars-screen-enabled": True,
|
|
||||||
"bad-networ-indicator-config": {
|
|
||||||
"signalingConfig": {
|
|
||||||
"dcReportNetworkStatEnabled": False
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"bots-channel-adding": True,
|
|
||||||
"cache-msg-preprocess": True,
|
|
||||||
"call-incoming-ab": 2,
|
|
||||||
"call-permissions-interval": 259200,
|
|
||||||
"call-pinch-to-zoom": True,
|
|
||||||
"call-rate": {
|
|
||||||
"limit": 3,
|
|
||||||
"sdk-limit": 2,
|
|
||||||
"duration": 10,
|
|
||||||
"delay": 86400
|
|
||||||
},
|
|
||||||
"callDontUseVpnForRtp": False,
|
|
||||||
"callEnableIceRenomination": False,
|
|
||||||
"calls-endpoint": "",
|
|
||||||
"calls-sdk-am-speaker-fix": True,
|
|
||||||
"calls-sdk-audio-dynamic-redundancy": {
|
|
||||||
"mab": 16,
|
|
||||||
"dsb": 64,
|
|
||||||
"nl": True,
|
|
||||||
"df": True,
|
|
||||||
"dlb": True
|
|
||||||
},
|
|
||||||
"calls-sdk-enable-nohost": True,
|
|
||||||
"calls-sdk-incall-stat": False,
|
|
||||||
"calls-sdk-linear-opus-bwe": True,
|
|
||||||
"calls-sdk-mapping": {
|
|
||||||
"off": True
|
|
||||||
},
|
|
||||||
"calls-sdk-remove-nonopus-audiocodecs": True,
|
|
||||||
"calls-use-call-end-reason-fix": True,
|
|
||||||
"calls-use-ws-url-validation": True,
|
|
||||||
"cfs": True,
|
|
||||||
"channels-complaint-enabled": True,
|
|
||||||
"channels-enabled": True,
|
|
||||||
"channels-search-subscribers-visible": True,
|
|
||||||
"chat-complaint-enabled": False,
|
|
||||||
"chat-gif-autoplay-enabled": True,
|
|
||||||
"chat-history-notif-msg-strategy": 1,
|
|
||||||
"chat-history-persist": False,
|
|
||||||
"chat-history-warm-opts": 0,
|
|
||||||
"chat-invite-link-permissions-enabled": True,
|
|
||||||
"chat-media-scrollable-caption-enabled": True,
|
|
||||||
"chat-video-autoplay-enabled": True,
|
|
||||||
"chat-video-call-button": True,
|
|
||||||
"chatlist-subtitle-ver": 1,
|
|
||||||
"chats-folder-enabled": True,
|
|
||||||
"chats-page-size": 50,
|
|
||||||
"chats-preload-period": 15,
|
|
||||||
"cis-enabled": True,
|
|
||||||
"contact-add-bottom-sheet": True,
|
|
||||||
"creation-2fa-config": {
|
|
||||||
"pass_min_len": 6,
|
|
||||||
"pass_max_len": 64,
|
|
||||||
"hint_max_len": 30,
|
|
||||||
"enabled": True
|
|
||||||
},
|
|
||||||
"debug-profile-info": False,
|
|
||||||
"default-reactions-settings": {
|
|
||||||
"isActive": True,
|
|
||||||
"count": 8,
|
|
||||||
"included": False,
|
|
||||||
"reactionIds": []
|
|
||||||
},
|
|
||||||
"delete-msg-fys-large-chat-disabled": True,
|
|
||||||
"devnull": {
|
|
||||||
"opcode": True,
|
|
||||||
"upload_hang": True
|
|
||||||
},
|
|
||||||
"disconnect-timeout": 300,
|
|
||||||
"double-tap-reaction": "👍",
|
|
||||||
"double-tap-reaction-enabled": True,
|
|
||||||
"drafts-sync-enabled": False,
|
|
||||||
"edit-chat-type-screen-enabled": False,
|
|
||||||
"edit-timeout": 604800,
|
|
||||||
"enable-filters-for-folders": True,
|
|
||||||
"enable-unknown-contact-bottom-sheet": 2,
|
|
||||||
"fake-chats": True,
|
|
||||||
"family-protection-botid": 67804175,
|
|
||||||
"february-23-26-theme": True,
|
|
||||||
"file-preview": True,
|
|
||||||
"file-upload-enabled": True,
|
|
||||||
"file-upload-max-size": 4294967296,
|
|
||||||
"file-upload-unsupported-types": [
|
|
||||||
"exe"
|
|
||||||
],
|
|
||||||
"force-play-embed": True,
|
|
||||||
"gc-from-p2p": True,
|
|
||||||
"gce": False,
|
|
||||||
"group-call-part-limit": 100,
|
|
||||||
"grse": False,
|
|
||||||
"gsse": True,
|
|
||||||
"hide-incoming-call-notif": True,
|
|
||||||
"host-reachability": True,
|
|
||||||
"image-height": 1920,
|
|
||||||
"image-quality": 0.800000011920929,
|
|
||||||
"image-size": 40000000,
|
|
||||||
"image-width": 1920,
|
|
||||||
"in-app-review-triggers": 255,
|
|
||||||
"informer-enabled": True,
|
|
||||||
"inline-ev-player": True,
|
|
||||||
"invalidate-db-msg-exception": True,
|
|
||||||
"invite-friends-sheet-frequency": [
|
|
||||||
2,
|
|
||||||
7
|
|
||||||
],
|
|
||||||
"invite-link": "",
|
|
||||||
"invite-long": "",
|
"invite-long": "",
|
||||||
"invite-short": "",
|
"calls-endpoint": "",
|
||||||
"join-requests": True,
|
"calls-test-domain": "",
|
||||||
"js-download-delegate": False,
|
|
||||||
"keep-connection": 2,
|
|
||||||
"lebedev-theme-enabled": True,
|
|
||||||
"lgce": True,
|
|
||||||
"markdown-enabled": True,
|
|
||||||
"markdown-menu": 0,
|
|
||||||
"max-audio-length": 3600,
|
|
||||||
"max-description-length": 400,
|
|
||||||
"max-favorite-chats": 5,
|
|
||||||
"max-favorite-sticker-sets": 100,
|
|
||||||
"max-favorite-stickers": 100,
|
|
||||||
"max-msg-length": 4000,
|
|
||||||
"max-participants": 20000,
|
|
||||||
"max-readmarks": 100,
|
"max-readmarks": 100,
|
||||||
"max-theme-length": 200,
|
"max-cname-length": 200,
|
||||||
"max-video-duration-download": 1200,
|
"max-description-length": 400,
|
||||||
"max-video-message-length": 60,
|
"new-avatar-gradient-colors-enabled": True,
|
||||||
"media-order": 1,
|
"max-msg-length": 4000,
|
||||||
"media-playlist-enabled": True,
|
"file-upload-unsupported-types": [],
|
||||||
"media-transform": {
|
"file-upload-max-size": 4294967296,
|
||||||
"enabled": True,
|
"image-quality": 0.8,
|
||||||
"hdr_enabled": False,
|
"image-width": 1920,
|
||||||
"hevc_enabled": True,
|
"image-height": 1920,
|
||||||
"max_enc_frames": {
|
"image-size": 10000000,
|
||||||
"low": 1,
|
"max-favorite-chats": 5,
|
||||||
"avg": 1,
|
"bot-complaint-enabled": True,
|
||||||
"high": 2
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"media-viewer-rotation-enabled": True,
|
|
||||||
"media-viewer-video-collage-enabled": True,
|
|
||||||
"mentions-enabled": True,
|
|
||||||
"mentions_entity_names_limit": 3,
|
|
||||||
"migrate-unsafe-warn": True,
|
|
||||||
"min-image-side-size": 64,
|
|
||||||
"miui-menu-enabled": True,
|
|
||||||
"money-transfer-botid": 1134691,
|
|
||||||
"moscow-theme-enabled": True,
|
|
||||||
"msg-get-reactions-page-size": 40,
|
|
||||||
"music-files-enabled": False,
|
|
||||||
"mytracker-enabled": True,
|
|
||||||
"net-client-dns-enabled": True,
|
|
||||||
"net-session-suppress-bad-disconnected-state": True,
|
|
||||||
"net-stat-config": [
|
|
||||||
64,
|
|
||||||
48,
|
|
||||||
128,
|
|
||||||
135
|
|
||||||
],
|
|
||||||
"new-admin-permissions": True,
|
|
||||||
"new-logout-logic": False,
|
|
||||||
"new-media-upload-ui": True,
|
|
||||||
"new-media-viewer-enabled": True,
|
|
||||||
"new-settings-storage-screen-enabled": False,
|
|
||||||
"new-width-text-bubbles-mob": True,
|
|
||||||
"new-year-theme-2026": False,
|
|
||||||
"nick-max-length": 60,
|
|
||||||
"nick-min-length": 7,
|
|
||||||
"official-org": True,
|
|
||||||
"one-video-failover": True,
|
|
||||||
"one-video-player": True,
|
|
||||||
"one-video-uploader": True,
|
|
||||||
"one-video-uploader-audio": True,
|
|
||||||
"one-video-uploader-progress-fix": True,
|
|
||||||
"perf-events": {
|
|
||||||
"startup_report": 2,
|
|
||||||
"web_app": 2
|
|
||||||
},
|
|
||||||
"player-load-control": {
|
|
||||||
"mp_autoplay_enabled": False,
|
|
||||||
"time_over_size": False,
|
|
||||||
"buffer_after_rebuffer_ms": 3000,
|
|
||||||
"buffer_ms": 500,
|
|
||||||
"max_buffer_ms": 13000,
|
|
||||||
"min_buffer_ms": 5000,
|
|
||||||
"use_min_size_lc": True,
|
|
||||||
"min_size_lc_fmt_mis_sf": 4
|
|
||||||
},
|
|
||||||
"progress-diff-for-notify": 1,
|
|
||||||
"push-delivery": True,
|
|
||||||
"qr-auth-enabled": True,
|
|
||||||
"quotes-enabled": True,
|
|
||||||
"react-errors": [
|
|
||||||
"error.comment.chat.access",
|
|
||||||
"error.comment.invalid",
|
|
||||||
"error.message.invalid",
|
|
||||||
"error.message.chat.access",
|
|
||||||
"error.message.like.unknown.like",
|
|
||||||
"error.message.like.unknown.reaction",
|
|
||||||
"error.too-many-unlikes-dialog",
|
|
||||||
"error.too-many-unlikes-chat",
|
|
||||||
"error.too-many-likes",
|
|
||||||
"error.reactions.not.allowed"
|
|
||||||
],
|
|
||||||
"react-permission": 2,
|
|
||||||
"reactions-enabled": True,
|
|
||||||
"reactions-max": 8,
|
"reactions-max": 8,
|
||||||
"reactions-menu": [
|
"welcome-sticker-ids": [],
|
||||||
"👍",
|
"edit-chat-type-screen-enabled": True,
|
||||||
"❤️",
|
"edit-channel-type-screen-enabled": True,
|
||||||
"🤣",
|
"esia-verify-botId": 0,
|
||||||
"🔥",
|
"official-org": False,
|
||||||
"😭",
|
"esia-enabled": False,
|
||||||
"💯",
|
"calls-debug-mode": False,
|
||||||
"💩",
|
"channels-suggests-folder": True,
|
||||||
"😡"
|
"delete-msg-fys-large-chat-disabled": False,
|
||||||
],
|
"calls-web-download-logs": False,
|
||||||
|
"calls-web-upload-logs": False,
|
||||||
|
"calls-video-zoom": False,
|
||||||
|
"calls-fullscreen-mode": False,
|
||||||
|
"group-call-part-limit": 100,
|
||||||
|
"call-chat-members-load-config": {},
|
||||||
|
"cfs": False,
|
||||||
|
"cse": False,
|
||||||
|
"calls-hotkeys": True,
|
||||||
|
"gc-link-pre-settings": False,
|
||||||
|
"gc-from-p2p": False,
|
||||||
|
"call-rate": {},
|
||||||
|
"channels-enabled": True,
|
||||||
|
"max-participants": 20000,
|
||||||
|
"max-added-participants": 100,
|
||||||
|
"saved-messages-aliases": [],
|
||||||
|
"author-visibility-forward-enabled": False,
|
||||||
|
"official-bot-naming-enabled": False,
|
||||||
|
"search-webapps-showcase": {
|
||||||
|
"items": []
|
||||||
|
},
|
||||||
|
"settings-entry-banners": [],
|
||||||
|
"settings-business": "https://telegram.org/blog/telegram-business",
|
||||||
|
"appearance-multi-theme-screen-enabled": True,
|
||||||
|
"moscow-theme-enabled": True,
|
||||||
|
"creation-2fa-config": {
|
||||||
|
"enabled": False,
|
||||||
|
"pass_min_len": 6,
|
||||||
|
"pass_max_len": 64,
|
||||||
|
"hint_max_len": 30
|
||||||
|
},
|
||||||
|
"lebedev-theme-enabled": True,
|
||||||
|
"quotes-enabled": True,
|
||||||
|
"channels-complaint-enabled": True,
|
||||||
"reactions-settings-enabled": True,
|
"reactions-settings-enabled": True,
|
||||||
"reconnect-call-ringtone": True,
|
"channel-statistics-botid": 0,
|
||||||
"ringtone-am-mode": True,
|
"enable-unknown-contact-bottom-sheet": 0,
|
||||||
"saved-messages-aliases": [
|
"informer-enabled": True,
|
||||||
"избранное",
|
"family-protection-botid": 0,
|
||||||
"saved",
|
"new-year-theme-2026": True,
|
||||||
"favourite",
|
|
||||||
"favorite",
|
|
||||||
"личное",
|
|
||||||
"моё",
|
|
||||||
"мои",
|
|
||||||
"мой",
|
|
||||||
"моя",
|
|
||||||
"любимое",
|
|
||||||
"сохраненные",
|
|
||||||
"сохраненное",
|
|
||||||
"заметки",
|
|
||||||
"закладки"
|
|
||||||
],
|
|
||||||
"scheduled-messages-enabled": True,
|
"scheduled-messages-enabled": True,
|
||||||
"scheduled-posts-enabled": True,
|
"scheduled-posts-enabled": True,
|
||||||
"search-webapps-showcase": {
|
"scheduled-faves-enabled": True,
|
||||||
"items": []
|
"non-contact-complaints-enabled": True,
|
||||||
},
|
"join-requests": True,
|
||||||
"send-location-enabled": True,
|
"web-persistent-cache": False,
|
||||||
"send-logs-interval-sec": 900,
|
"create-channel-type-screen": True,
|
||||||
"server-side-complains-enabled": True,
|
|
||||||
"set-audio-device": False,
|
|
||||||
"set-unread-timeout": 31536000,
|
|
||||||
"settings-entry-banners": [],
|
|
||||||
"show-reactions-on-multiselect": True,
|
|
||||||
"show-warning-links": True,
|
"show-warning-links": True,
|
||||||
"speedy-upload": True,
|
|
||||||
"speedy-voice-messages": True,
|
|
||||||
"sse": True,
|
|
||||||
"stat-session-background-threshold": 60000,
|
|
||||||
"sticker-suggestion": [
|
|
||||||
"RECENT",
|
|
||||||
"NEW",
|
|
||||||
"TOP"
|
|
||||||
],
|
|
||||||
"stickers-controller-suspend": True,
|
|
||||||
"stickers-db-batch": True,
|
|
||||||
"streamable-mp4": True,
|
|
||||||
"stub": "stub2",
|
|
||||||
"suspend-video-converter": True,
|
|
||||||
"system-default-ringtone-opt": True,
|
|
||||||
"transfer-botid": 1134691,
|
|
||||||
"typing-enabled-FILE": True,
|
|
||||||
"unique-favorites": True,
|
|
||||||
"unsafe-files-alert": True,
|
|
||||||
"upload-reusability": True,
|
|
||||||
"upload-rx-no-blocking": True,
|
|
||||||
"user-debug-report": 2340932,
|
|
||||||
"video-msg-channels-enabled": True,
|
|
||||||
"video-msg-config": {
|
|
||||||
"duration": 60,
|
|
||||||
"quality": 480,
|
|
||||||
"min_frame_rate": 30,
|
|
||||||
"max_frame_rate": 30
|
|
||||||
},
|
|
||||||
"video-msg-enabled": True,
|
|
||||||
"video-transcoding-class": [
|
|
||||||
2,
|
|
||||||
3
|
|
||||||
],
|
|
||||||
"views-count-enabled": True,
|
|
||||||
"watchdog-config": {
|
|
||||||
"enabled": True,
|
|
||||||
"stuck": 10,
|
|
||||||
"hang": 60
|
|
||||||
},
|
|
||||||
"webapp-exc": [],
|
|
||||||
"webapp-push-open": True,
|
|
||||||
"webview-cache-enabled": False,
|
|
||||||
"welcome-sticker-ids": [],
|
|
||||||
"white-list-links": [],
|
"white-list-links": [],
|
||||||
"wm-analytics-enabled": True,
|
"february-23-26-theme": True,
|
||||||
"wm-workers-limit": 80,
|
"march-8-26-theme": True,
|
||||||
"wud": False,
|
"audio-play-cmd": False,
|
||||||
|
"audio-play-opus": False,
|
||||||
|
"bots-channel-adding": True,
|
||||||
|
"stickers-botid": 0,
|
||||||
|
"sticker-set-edit-enabled": True,
|
||||||
|
"calls-new-history-enabled": True,
|
||||||
"y-map": {
|
"y-map": {
|
||||||
"tile": "34c7fd82-723d-4b23-8abb-33376729a893",
|
"tile": "",
|
||||||
"geocoder": "34c7fd82-723d-4b23-8abb-33376729a893",
|
"geocoder": "",
|
||||||
"static": "34c7fd82-723d-4b23-8abb-33376729a893",
|
"static": ""
|
||||||
"logoLight": "https://st.max.ru/icons/ya_maps_logo_light.webp",
|
|
||||||
"logoDark": "https://st.max.ru/icons/ya_maps_logo_dark.webp"
|
|
||||||
},
|
},
|
||||||
"has-phone": True
|
"enable-audio-messages-transcription": True,
|
||||||
}
|
"enable-video-messages-transcription": True,
|
||||||
|
"retry-transcribe-attempt": 5,
|
||||||
|
"retry-transcribe-timeout": 2000,
|
||||||
|
"org-profile": False,
|
||||||
|
"media-not-ready-retry-delay": 2000,
|
||||||
|
"polls-in-chats": True,
|
||||||
|
"polls-in-channels": True,
|
||||||
|
"render-polls": True,
|
||||||
|
"poll-ttl": {
|
||||||
|
"chat": 5000,
|
||||||
|
"bigchat": 15000,
|
||||||
|
"channel": 25000
|
||||||
|
},
|
||||||
|
"new-collage": False,
|
||||||
|
"channel-profile-invite-link": False,
|
||||||
|
"rename-profile-to-settings": True,
|
||||||
|
"live-streams": True
|
||||||
|
}
|
||||||
@@ -10,23 +10,13 @@ from common.opcodes import Opcodes
|
|||||||
class OnemeController(ControllerBase):
|
class OnemeController(ControllerBase):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.config = ServerConfig()
|
self.config = ServerConfig()
|
||||||
self.proto_tcp = MobileProto()
|
self.proto = MobileProto()
|
||||||
self.proto_web = WebProto()
|
|
||||||
self.opcodes = Opcodes()
|
self.opcodes = Opcodes()
|
||||||
|
|
||||||
async def event(self, target, client, eventData):
|
async def event(self, target, client, eventData):
|
||||||
# Извлекаем тип события и врайтер
|
# Извлекаем тип события и врайтер
|
||||||
eventType = eventData.get("eventType")
|
eventType = eventData.get("eventType")
|
||||||
writer = client.get("writer")
|
writer = client.get("writer")
|
||||||
is_web = client.get("type") == "web"
|
|
||||||
|
|
||||||
# Выбираем протокол в зависимости от типа подключения
|
|
||||||
proto = self.proto_web if is_web else self.proto_tcp
|
|
||||||
packet = None
|
|
||||||
|
|
||||||
# Не отправляем событие самому себе
|
|
||||||
if writer == eventData.get("writer"):
|
|
||||||
return
|
|
||||||
|
|
||||||
# Обрабатываем событие
|
# Обрабатываем событие
|
||||||
if eventType == "new_msg":
|
if eventType == "new_msg":
|
||||||
@@ -47,7 +37,7 @@ class OnemeController(ControllerBase):
|
|||||||
}
|
}
|
||||||
|
|
||||||
# Создаем пакет
|
# Создаем пакет
|
||||||
packet = proto.pack_packet(
|
packet = self.proto.pack_packet(
|
||||||
cmd=0, seq=1, opcode=self.opcodes.NOTIF_MESSAGE, payload=payload
|
cmd=0, seq=1, opcode=self.opcodes.NOTIF_MESSAGE, payload=payload
|
||||||
)
|
)
|
||||||
elif eventType == "typing":
|
elif eventType == "typing":
|
||||||
@@ -64,7 +54,7 @@ class OnemeController(ControllerBase):
|
|||||||
}
|
}
|
||||||
|
|
||||||
# Создаем пакет
|
# Создаем пакет
|
||||||
packet = proto.pack_packet(
|
packet = self.proto.pack_packet(
|
||||||
cmd=0, seq=1, opcode=self.opcodes.NOTIF_TYPING, payload=payload
|
cmd=0, seq=1, opcode=self.opcodes.NOTIF_TYPING, payload=payload
|
||||||
)
|
)
|
||||||
elif eventType == "profile_updated":
|
elif eventType == "profile_updated":
|
||||||
@@ -77,30 +67,12 @@ class OnemeController(ControllerBase):
|
|||||||
}
|
}
|
||||||
|
|
||||||
# Создаем пакет
|
# Создаем пакет
|
||||||
packet = proto.pack_packet(
|
packet = self.proto.pack_packet(
|
||||||
cmd=0, seq=1, opcode=self.opcodes.NOTIF_PROFILE, payload=payload
|
cmd=0, seq=1, opcode=self.opcodes.NOTIF_PROFILE, payload=payload
|
||||||
)
|
)
|
||||||
elif eventType == "presence":
|
|
||||||
userId = eventData.get("userId")
|
|
||||||
presence = eventData.get("presence")
|
|
||||||
event_time = eventData.get("time")
|
|
||||||
|
|
||||||
payload = {
|
# Отправляем пакет
|
||||||
"userId": userId,
|
if writer != eventData.get("writer"):
|
||||||
"presence": presence,
|
|
||||||
"time": event_time
|
|
||||||
}
|
|
||||||
|
|
||||||
packet = proto.pack_packet(
|
|
||||||
cmd=0, seq=1, opcode=self.opcodes.NOTIF_PRESENCE, payload=payload
|
|
||||||
)
|
|
||||||
|
|
||||||
if not packet:
|
|
||||||
return
|
|
||||||
|
|
||||||
if is_web:
|
|
||||||
await writer.send(packet)
|
|
||||||
else:
|
|
||||||
writer.write(packet)
|
writer.write(packet)
|
||||||
await writer.drain()
|
await writer.drain()
|
||||||
|
|
||||||
@@ -119,11 +91,10 @@ class OnemeController(ControllerBase):
|
|||||||
OnemeWS(
|
OnemeWS(
|
||||||
host=self.config.host,
|
host=self.config.host,
|
||||||
port=self.config.oneme_ws_port,
|
port=self.config.oneme_ws_port,
|
||||||
|
clients=api['clients'],
|
||||||
ssl_context=api['ssl'],
|
ssl_context=api['ssl'],
|
||||||
db_pool=api['db'],
|
db_pool=api['db'],
|
||||||
clients=api['clients'],
|
send_event=api['event']
|
||||||
send_event=api['event'],
|
|
||||||
telegram_bot=api.get('telegram_bot'),
|
|
||||||
).start()
|
).start()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -54,32 +54,6 @@ class AssetsPayloadModel(pydantic.BaseModel):
|
|||||||
sync: int
|
sync: int
|
||||||
type: str
|
type: str
|
||||||
|
|
||||||
class AssetsGetPayloadModel(pydantic.BaseModel):
|
|
||||||
type: str
|
|
||||||
count: int = 100
|
|
||||||
query: str = None
|
|
||||||
|
|
||||||
class AssetsGetByIdsPayloadModel(pydantic.BaseModel):
|
|
||||||
type: str
|
|
||||||
ids: list
|
|
||||||
|
|
||||||
class AssetsAddPayloadModel(pydantic.BaseModel):
|
|
||||||
type: str
|
|
||||||
id: int = None
|
|
||||||
|
|
||||||
class AssetsRemovePayloadModel(pydantic.BaseModel):
|
|
||||||
type: str
|
|
||||||
ids: list
|
|
||||||
|
|
||||||
class AssetsMovePayloadModel(pydantic.BaseModel):
|
|
||||||
type: str
|
|
||||||
id: int
|
|
||||||
position: int
|
|
||||||
|
|
||||||
class AssetsListModifyPayloadModel(pydantic.BaseModel):
|
|
||||||
type: str
|
|
||||||
ids: list
|
|
||||||
|
|
||||||
class GetCallHistoryPayloadModel(pydantic.BaseModel):
|
class GetCallHistoryPayloadModel(pydantic.BaseModel):
|
||||||
forward: bool
|
forward: bool
|
||||||
count: int
|
count: int
|
||||||
@@ -101,12 +75,6 @@ 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
|
||||||
|
|
||||||
@@ -162,21 +130,4 @@ class ChatHistoryPayloadModel(pydantic.BaseModel):
|
|||||||
|
|
||||||
class ChatSubscribePayloadModel(pydantic.BaseModel):
|
class ChatSubscribePayloadModel(pydantic.BaseModel):
|
||||||
chatId: int
|
chatId: int
|
||||||
subscribe: bool
|
subscribe: bool
|
||||||
|
|
||||||
class ContactListPayloadModel(pydantic.BaseModel):
|
|
||||||
status: str
|
|
||||||
count: int = None
|
|
||||||
|
|
||||||
class ContactPresencePayloadModel(pydantic.BaseModel):
|
|
||||||
contactIds: list
|
|
||||||
|
|
||||||
class ContactAddByPhonePayloadModel(pydantic.BaseModel):
|
|
||||||
phone: str
|
|
||||||
firstName: str
|
|
||||||
|
|
||||||
class ContactUpdatePayloadModel(pydantic.BaseModel):
|
|
||||||
action: str
|
|
||||||
contactId: int
|
|
||||||
firstName: str = None
|
|
||||||
lastName: str = None
|
|
||||||
@@ -2,8 +2,7 @@ from .assets import AssetsProcessors
|
|||||||
from .auth import AuthProcessors
|
from .auth import AuthProcessors
|
||||||
from .calls import CallsProcessors
|
from .calls import CallsProcessors
|
||||||
from .chats import ChatsProcessors
|
from .chats import ChatsProcessors
|
||||||
from .complaints import ComplaintsProcessors
|
from .complains import ComplainsProcessors
|
||||||
from .contacts import ContactsProcessors
|
|
||||||
from .folders import FoldersProcessors
|
from .folders import FoldersProcessors
|
||||||
from .history import HistoryProcessors
|
from .history import HistoryProcessors
|
||||||
from .main import MainProcessors
|
from .main import MainProcessors
|
||||||
@@ -16,8 +15,7 @@ class Processors(
|
|||||||
AuthProcessors,
|
AuthProcessors,
|
||||||
CallsProcessors,
|
CallsProcessors,
|
||||||
ChatsProcessors,
|
ChatsProcessors,
|
||||||
ComplaintsProcessors,
|
ComplainsProcessors,
|
||||||
ContactsProcessors,
|
|
||||||
FoldersProcessors,
|
FoldersProcessors,
|
||||||
HistoryProcessors,
|
HistoryProcessors,
|
||||||
MainProcessors,
|
MainProcessors,
|
||||||
|
|||||||
@@ -1,167 +1,31 @@
|
|||||||
import pydantic
|
import pydantic
|
||||||
import time
|
import time
|
||||||
from classes.baseprocessor import BaseProcessor
|
from classes.baseprocessor import BaseProcessor
|
||||||
from oneme.models import (
|
from oneme.models import AssetsPayloadModel
|
||||||
AssetsPayloadModel,
|
|
||||||
AssetsGetPayloadModel,
|
|
||||||
AssetsGetByIdsPayloadModel,
|
|
||||||
AssetsAddPayloadModel,
|
|
||||||
AssetsRemovePayloadModel,
|
|
||||||
AssetsMovePayloadModel,
|
|
||||||
AssetsListModifyPayloadModel,
|
|
||||||
)
|
|
||||||
|
|
||||||
class AssetsProcessors(BaseProcessor):
|
class AssetsProcessors(BaseProcessor):
|
||||||
async def assets_update(self, payload, seq, writer):
|
async def assets_update(self, payload, seq, writer):
|
||||||
|
"""Обработчик запроса ассетов клиента на сервере"""
|
||||||
|
# Валидируем данные пакета
|
||||||
try:
|
try:
|
||||||
AssetsPayloadModel.model_validate(payload)
|
AssetsPayloadModel.model_validate(payload)
|
||||||
except pydantic.ValidationError as error:
|
except pydantic.ValidationError as error:
|
||||||
self.logger.error(f"Возникли ошибки при валидации пакета: {error}")
|
self.logger.error(f"Возникли ошибки при валидации пакета: {error}")
|
||||||
await self._send_error(seq, self.opcodes.ASSETS_UPDATE, self.error_types.INVALID_PAYLOAD, writer)
|
await self._send_error(seq, self.opcodes.ASSETS_UPDATE, self.error_types.INVALID_PAYLOAD, writer)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
# TODO: сейчас это заглушка, а попозже нужно сделать полноценную реализацию
|
||||||
|
|
||||||
response = {
|
# Данные пакета
|
||||||
"sync": int(time.time() * 1000),
|
payload = {
|
||||||
"stickerSetsUpdates": {},
|
"sections": [],
|
||||||
"stickersUpdates": {},
|
"sync": int(time.time() * 1000)
|
||||||
"stickersOrder": [
|
|
||||||
"RECENT",
|
|
||||||
"FAVORITE_STICKERS",
|
|
||||||
"FAVORITE_STICKER_SETS",
|
|
||||||
"TOP",
|
|
||||||
"NEW",
|
|
||||||
"NEW_STICKER_SETS",
|
|
||||||
],
|
|
||||||
"sections": [
|
|
||||||
{
|
|
||||||
"id": "RECENT",
|
|
||||||
"type": "RECENTS",
|
|
||||||
"recentsList": [],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"id": "FAVORITE_STICKERS",
|
|
||||||
"type": "STICKERS",
|
|
||||||
"stickers": [],
|
|
||||||
"marker": None,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"id": "FAVORITE_STICKER_SETS",
|
|
||||||
"type": "STICKER_SETS",
|
|
||||||
"stickerSets": [],
|
|
||||||
"marker": None,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"id": "TOP",
|
|
||||||
"type": "STICKERS",
|
|
||||||
"stickers": [],
|
|
||||||
"marker": None,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"id": "NEW",
|
|
||||||
"type": "STICKERS",
|
|
||||||
"stickers": [],
|
|
||||||
"marker": None,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"id": "NEW_STICKER_SETS",
|
|
||||||
"type": "STICKER_SETS",
|
|
||||||
"stickerSets": [],
|
|
||||||
"marker": None,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Собираем пакет
|
||||||
packet = self.proto.pack_packet(
|
packet = self.proto.pack_packet(
|
||||||
cmd=self.proto.CMD_OK, seq=seq, opcode=self.opcodes.ASSETS_UPDATE, payload=response
|
cmd=self.proto.CMD_OK, seq=seq, opcode=self.opcodes.ASSETS_UPDATE, payload=payload
|
||||||
)
|
)
|
||||||
await self._send(writer, packet)
|
|
||||||
|
|
||||||
async def assets_get(self, payload, seq, writer):
|
# Отправляем
|
||||||
try:
|
await self._send(writer, packet)
|
||||||
data = AssetsGetPayloadModel.model_validate(payload)
|
|
||||||
except pydantic.ValidationError as error:
|
|
||||||
self.logger.error(f"Возникли ошибки при валидации пакета: {error}")
|
|
||||||
await self._send_error(seq, self.opcodes.ASSETS_GET, self.error_types.INVALID_PAYLOAD, writer)
|
|
||||||
return
|
|
||||||
|
|
||||||
asset_type = data.type
|
|
||||||
if asset_type == "STICKER_SET":
|
|
||||||
response = {"stickerSets": [], "marker": None}
|
|
||||||
else:
|
|
||||||
response = {"stickers": [], "marker": None}
|
|
||||||
|
|
||||||
packet = self.proto.pack_packet(
|
|
||||||
cmd=self.proto.CMD_OK, seq=seq, opcode=self.opcodes.ASSETS_GET, payload=response
|
|
||||||
)
|
|
||||||
await self._send(writer, packet)
|
|
||||||
|
|
||||||
async def assets_get_by_ids(self, payload, seq, writer):
|
|
||||||
try:
|
|
||||||
data = AssetsGetByIdsPayloadModel.model_validate(payload)
|
|
||||||
except pydantic.ValidationError as error:
|
|
||||||
self.logger.error(f"Возникли ошибки при валидации пакета: {error}")
|
|
||||||
await self._send_error(seq, self.opcodes.ASSETS_GET_BY_IDS, self.error_types.INVALID_PAYLOAD, writer)
|
|
||||||
return
|
|
||||||
|
|
||||||
asset_type = data.type
|
|
||||||
if asset_type == "STICKER_SET":
|
|
||||||
response = {"stickerSets": []}
|
|
||||||
else:
|
|
||||||
response = {"stickers": []}
|
|
||||||
|
|
||||||
packet = self.proto.pack_packet(
|
|
||||||
cmd=self.proto.CMD_OK, seq=seq, opcode=self.opcodes.ASSETS_GET_BY_IDS, payload=response
|
|
||||||
)
|
|
||||||
await self._send(writer, packet)
|
|
||||||
|
|
||||||
async def assets_add(self, payload, seq, writer):
|
|
||||||
try:
|
|
||||||
AssetsAddPayloadModel.model_validate(payload)
|
|
||||||
except pydantic.ValidationError as error:
|
|
||||||
self.logger.error(f"Возникли ошибки при валидации пакета: {error}")
|
|
||||||
await self._send_error(seq, self.opcodes.ASSETS_ADD, self.error_types.INVALID_PAYLOAD, writer)
|
|
||||||
return
|
|
||||||
|
|
||||||
packet = self.proto.pack_packet(
|
|
||||||
cmd=self.proto.CMD_OK, seq=seq, opcode=self.opcodes.ASSETS_ADD, payload={}
|
|
||||||
)
|
|
||||||
await self._send(writer, packet)
|
|
||||||
|
|
||||||
async def assets_remove(self, payload, seq, writer):
|
|
||||||
try:
|
|
||||||
AssetsRemovePayloadModel.model_validate(payload)
|
|
||||||
except pydantic.ValidationError as error:
|
|
||||||
self.logger.error(f"Возникли ошибки при валидации пакета: {error}")
|
|
||||||
await self._send_error(seq, self.opcodes.ASSETS_REMOVE, self.error_types.INVALID_PAYLOAD, writer)
|
|
||||||
return
|
|
||||||
|
|
||||||
packet = self.proto.pack_packet(
|
|
||||||
cmd=self.proto.CMD_OK, seq=seq, opcode=self.opcodes.ASSETS_REMOVE, payload={}
|
|
||||||
)
|
|
||||||
await self._send(writer, packet)
|
|
||||||
|
|
||||||
async def assets_move(self, payload, seq, writer):
|
|
||||||
try:
|
|
||||||
AssetsMovePayloadModel.model_validate(payload)
|
|
||||||
except pydantic.ValidationError as error:
|
|
||||||
self.logger.error(f"Возникли ошибки при валидации пакета: {error}")
|
|
||||||
await self._send_error(seq, self.opcodes.ASSETS_MOVE, self.error_types.INVALID_PAYLOAD, writer)
|
|
||||||
return
|
|
||||||
|
|
||||||
packet = self.proto.pack_packet(
|
|
||||||
cmd=self.proto.CMD_OK, seq=seq, opcode=self.opcodes.ASSETS_MOVE, payload={}
|
|
||||||
)
|
|
||||||
await self._send(writer, packet)
|
|
||||||
|
|
||||||
async def assets_list_modify(self, payload, seq, writer):
|
|
||||||
try:
|
|
||||||
AssetsListModifyPayloadModel.model_validate(payload)
|
|
||||||
except pydantic.ValidationError as error:
|
|
||||||
self.logger.error(f"Возникли ошибки при валидации пакета: {error}")
|
|
||||||
await self._send_error(seq, self.opcodes.ASSETS_LIST_MODIFY, self.error_types.INVALID_PAYLOAD, writer)
|
|
||||||
return
|
|
||||||
|
|
||||||
packet = self.proto.pack_packet(
|
|
||||||
cmd=self.proto.CMD_OK, seq=seq, opcode=self.opcodes.ASSETS_LIST_MODIFY, payload={}
|
|
||||||
)
|
|
||||||
await self._send(writer, packet)
|
|
||||||
@@ -1,107 +1,33 @@
|
|||||||
import hashlib
|
|
||||||
import json
|
import json
|
||||||
import logging
|
|
||||||
import secrets
|
import secrets
|
||||||
|
import hashlib
|
||||||
import time
|
import time
|
||||||
|
import logging
|
||||||
import pydantic
|
import pydantic
|
||||||
|
|
||||||
from classes.baseprocessor import BaseProcessor
|
from classes.baseprocessor import BaseProcessor
|
||||||
from common.sms import send_sms_code
|
|
||||||
from oneme.config import OnemeConfig
|
|
||||||
from oneme.models import (
|
from oneme.models import (
|
||||||
AuthConfirmRegisterPayloadModel,
|
|
||||||
LoginPayloadModel,
|
|
||||||
RequestCodePayloadModel,
|
RequestCodePayloadModel,
|
||||||
VerifyCodePayloadModel,
|
VerifyCodePayloadModel,
|
||||||
|
AuthConfirmRegisterPayloadModel,
|
||||||
|
LoginPayloadModel,
|
||||||
)
|
)
|
||||||
|
from oneme.config import OnemeConfig
|
||||||
|
from common.sms import send_sms_code
|
||||||
|
|
||||||
|
|
||||||
class AuthProcessors(BaseProcessor):
|
class AuthProcessors(BaseProcessor):
|
||||||
def __init__(
|
def __init__(self, db_pool=None, clients=None, send_event=None, telegram_bot=None, type="socket"):
|
||||||
self,
|
|
||||||
db_pool=None,
|
|
||||||
clients=None,
|
|
||||||
send_event=None,
|
|
||||||
telegram_bot=None,
|
|
||||||
type="socket",
|
|
||||||
):
|
|
||||||
super().__init__(db_pool, clients, send_event, type)
|
super().__init__(db_pool, clients, send_event, type)
|
||||||
self.server_config = OnemeConfig().SERVER_CONFIG
|
self.server_config = OnemeConfig().SERVER_CONFIG
|
||||||
self.telegram_bot = telegram_bot
|
self.telegram_bot = telegram_bot
|
||||||
|
|
||||||
def _check_legacy_version(self, app_version):
|
|
||||||
"""
|
|
||||||
Функция определения легаси версий клиентов,
|
|
||||||
для которых потребуются некоторые корректировки ответов сервера
|
|
||||||
|
|
||||||
Сейчас данная функция используется для форматирования ответов
|
|
||||||
под версии ниже 25.8.0
|
|
||||||
|
|
||||||
Функция вернет True, если версия слишком старая,
|
|
||||||
или False в противном случае
|
|
||||||
|
|
||||||
[fun fact] С 25.8.0, похоже, начали корректировать протокол, поскольку это
|
|
||||||
самая старая версия, которая работала без всяких корректировок сервера
|
|
||||||
"""
|
|
||||||
|
|
||||||
return tuple(int(v) for v in app_version.split(".")) < (25, 8, 0)
|
|
||||||
|
|
||||||
async def _send_banners(self, writer):
|
|
||||||
"""Функция отправки баннеров клиенту"""
|
|
||||||
# Итоговый список баннеров для отдачи клиенту
|
|
||||||
banners = []
|
|
||||||
|
|
||||||
async with self.db_pool.acquire() as conn:
|
|
||||||
async with conn.cursor() as cursor:
|
|
||||||
# Собираем все баннеры, которые есть в бд
|
|
||||||
await cursor.execute(
|
|
||||||
"SELECT * FROM banners WHERE enabled = TRUE"
|
|
||||||
)
|
|
||||||
rows = await cursor.fetchall()
|
|
||||||
|
|
||||||
# Добавляем каждый баннер в лист
|
|
||||||
for row in rows:
|
|
||||||
banner = {
|
|
||||||
"description": row.get("description"),
|
|
||||||
"title": row.get("title"),
|
|
||||||
"priority": row.get("priority"),
|
|
||||||
"type": row.get("type"),
|
|
||||||
"hideCloseButton": bool(row.get("hide_close_button")),
|
|
||||||
"rerun": row.get("rerun"),
|
|
||||||
"url": row.get("url"),
|
|
||||||
"animojiId": row.get("animoji_id"),
|
|
||||||
"repeat": row.get("repeat"),
|
|
||||||
"hideOnClick": bool(row.get("hide_on_click")),
|
|
||||||
"id": row.get("id"),
|
|
||||||
"isTitleAnimated": bool(row.get("is_title_animated")),
|
|
||||||
}
|
|
||||||
|
|
||||||
banners.append(banner)
|
|
||||||
|
|
||||||
# Собираем данные пакета
|
|
||||||
payload = {
|
|
||||||
"showTime": 86400000,
|
|
||||||
"updateTime": int(time.time() * 1000),
|
|
||||||
"banners": banners
|
|
||||||
}
|
|
||||||
|
|
||||||
# Собираем пакет
|
|
||||||
packet = self.proto.pack_packet(
|
|
||||||
cmd=0, opcode=self.opcodes.NOTIF_BANNERS, payload=payload
|
|
||||||
)
|
|
||||||
|
|
||||||
# Отправляет
|
|
||||||
await self._send(writer, packet)
|
|
||||||
|
|
||||||
async def auth_request(self, payload, seq, writer):
|
async def auth_request(self, payload, seq, writer):
|
||||||
"""Обработчик запроса кода"""
|
"""Обработчик запроса кода"""
|
||||||
try:
|
try:
|
||||||
RequestCodePayloadModel.model_validate(payload)
|
RequestCodePayloadModel.model_validate(payload)
|
||||||
except pydantic.ValidationError as error:
|
except pydantic.ValidationError as error:
|
||||||
self.logger.error(f"Возникли ошибки при валидации пакета: {error}")
|
self.logger.error(f"Возникли ошибки при валидации пакета: {error}")
|
||||||
await self._send_error(
|
await self._send_error(seq, self.opcodes.AUTH_REQUEST, self.error_types.INVALID_PAYLOAD, writer)
|
||||||
seq, self.opcodes.AUTH_REQUEST, self.error_types.INVALID_PAYLOAD, writer
|
|
||||||
)
|
|
||||||
return
|
return
|
||||||
|
|
||||||
# Извлекаем телефон из пакета
|
# Извлекаем телефон из пакета
|
||||||
@@ -123,16 +49,13 @@ class AuthProcessors(BaseProcessor):
|
|||||||
user = await cursor.fetchone()
|
user = await cursor.fetchone()
|
||||||
|
|
||||||
# Получаем код через SMS шлюз или генерируем локально (безопасность прежде всего)
|
# Получаем код через SMS шлюз или генерируем локально (безопасность прежде всего)
|
||||||
local_fallback_code = False
|
|
||||||
if self.config.sms_gateway_url:
|
if self.config.sms_gateway_url:
|
||||||
code = await send_sms_code(self.config.sms_gateway_url, phone)
|
code = await send_sms_code(self.config.sms_gateway_url, phone)
|
||||||
|
|
||||||
if code is None:
|
if code is None:
|
||||||
code = str(secrets.randbelow(900000) + 100000)
|
code = str(secrets.randbelow(900000) + 100000)
|
||||||
local_fallback_code = True
|
|
||||||
else:
|
else:
|
||||||
code = str(secrets.randbelow(900000) + 100000)
|
code = str(secrets.randbelow(900000) + 100000)
|
||||||
local_fallback_code = True
|
|
||||||
|
|
||||||
# Хешируем
|
# Хешируем
|
||||||
code_hash = hashlib.sha256(code.encode()).hexdigest()
|
code_hash = hashlib.sha256(code.encode()).hexdigest()
|
||||||
@@ -145,35 +68,18 @@ class AuthProcessors(BaseProcessor):
|
|||||||
# Сохраняем токен
|
# Сохраняем токен
|
||||||
await cursor.execute(
|
await cursor.execute(
|
||||||
"INSERT INTO auth_tokens (phone, token_hash, code_hash, expires) VALUES (%s, %s, %s, %s)",
|
"INSERT INTO auth_tokens (phone, token_hash, code_hash, expires) VALUES (%s, %s, %s, %s)",
|
||||||
(
|
(phone, token_hash, code_hash, expires,)
|
||||||
phone,
|
|
||||||
token_hash,
|
|
||||||
code_hash,
|
|
||||||
expires,
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# Если код был сгенерирован локально, а тг привязан к аккаунту - отправляем туда сообщение
|
# Если тг бот включен, и тг привязан к аккаунту - отправляем туда сообщение
|
||||||
if (
|
if not self.config.sms_gateway_url and self.telegram_bot and user.get("telegram_id"):
|
||||||
local_fallback_code
|
await self.telegram_bot.send_code(chat_id=int(user.get("telegram_id")), phone=phone, code=code)
|
||||||
and self.telegram_bot
|
|
||||||
and user.get("telegram_id")
|
|
||||||
):
|
|
||||||
await self.telegram_bot.send_code(
|
|
||||||
chat_id=int(user.get("telegram_id")), phone=phone, code=code
|
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
# Пользователь не найден - сохраняем токен со state='register'
|
# Пользователь не найден - сохраняем токен со state='register'
|
||||||
# чтобы после верификации кода направить на экран регистрации
|
# чтобы после верификации кода направить на экран регистрации
|
||||||
await cursor.execute(
|
await cursor.execute(
|
||||||
"INSERT INTO auth_tokens (phone, token_hash, code_hash, expires, state) VALUES (%s, %s, %s, %s, %s)",
|
"INSERT INTO auth_tokens (phone, token_hash, code_hash, expires, state) VALUES (%s, %s, %s, %s, %s)",
|
||||||
(
|
(phone, token_hash, code_hash, expires, "register",)
|
||||||
phone,
|
|
||||||
token_hash,
|
|
||||||
code_hash,
|
|
||||||
expires,
|
|
||||||
"register",
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# Данные пакета
|
# Данные пакета
|
||||||
@@ -182,30 +88,25 @@ class AuthProcessors(BaseProcessor):
|
|||||||
"codeLength": 6,
|
"codeLength": 6,
|
||||||
"requestMaxDuration": 60000,
|
"requestMaxDuration": 60000,
|
||||||
"requestCountLeft": 10,
|
"requestCountLeft": 10,
|
||||||
"altActionDuration": 60000,
|
"altActionDuration": 60000
|
||||||
}
|
}
|
||||||
|
|
||||||
# Собираем пакет
|
# Собираем пакет
|
||||||
packet = self.proto.pack_packet(
|
packet = self.proto.pack_packet(
|
||||||
cmd=self.proto.CMD_OK,
|
cmd=self.proto.CMD_OK, seq=seq, opcode=self.opcodes.AUTH_REQUEST, payload=payload
|
||||||
seq=seq,
|
|
||||||
opcode=self.opcodes.AUTH_REQUEST,
|
|
||||||
payload=payload,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# Отправляем
|
# Отправляем
|
||||||
await self._send(writer, packet)
|
await self._send(writer, packet)
|
||||||
self.logger.debug(f"Код для {phone}: {code} (существующий={user_exists})")
|
self.logger.debug(f"Код для {phone}: {code} (существующий={user_exists})")
|
||||||
|
|
||||||
async def auth(self, payload, seq, writer, deviceType, deviceName, appVersion, ip):
|
async def auth(self, payload, seq, writer, deviceType, deviceName):
|
||||||
"""Обработчик проверки кода"""
|
"""Обработчик проверки кода"""
|
||||||
try:
|
try:
|
||||||
VerifyCodePayloadModel.model_validate(payload)
|
VerifyCodePayloadModel.model_validate(payload)
|
||||||
except pydantic.ValidationError as error:
|
except pydantic.ValidationError as error:
|
||||||
self.logger.error(f"Возникли ошибки при валидации пакета: {error}")
|
self.logger.error(f"Возникли ошибки при валидации пакета: {error}")
|
||||||
await self._send_error(
|
await self._send_error(seq, self.opcodes.AUTH, self.error_types.INVALID_PAYLOAD, writer)
|
||||||
seq, self.opcodes.AUTH, self.error_types.INVALID_PAYLOAD, writer
|
|
||||||
)
|
|
||||||
return
|
return
|
||||||
|
|
||||||
# Извлекаем данные из пакета
|
# Извлекаем данные из пакета
|
||||||
@@ -226,22 +127,18 @@ class AuthProcessors(BaseProcessor):
|
|||||||
# Ищем токен
|
# Ищем токен
|
||||||
await cursor.execute(
|
await cursor.execute(
|
||||||
"SELECT * FROM auth_tokens WHERE token_hash = %s AND expires > UNIX_TIMESTAMP()",
|
"SELECT * FROM auth_tokens WHERE token_hash = %s AND expires > UNIX_TIMESTAMP()",
|
||||||
(hashed_token,),
|
(hashed_token,)
|
||||||
)
|
)
|
||||||
stored_token = await cursor.fetchone()
|
stored_token = await cursor.fetchone()
|
||||||
|
|
||||||
# Если токен просрочен, или его нет - отправляем ошибку
|
# Если токен просрочен, или его нет - отправляем ошибку
|
||||||
if stored_token is None:
|
if stored_token is None:
|
||||||
await self._send_error(
|
await self._send_error(seq, self.opcodes.AUTH, self.error_types.CODE_EXPIRED, writer)
|
||||||
seq, self.opcodes.AUTH, self.error_types.CODE_EXPIRED, writer
|
|
||||||
)
|
|
||||||
return
|
return
|
||||||
|
|
||||||
# Проверяем код
|
# Проверяем код
|
||||||
if stored_token.get("code_hash") != hashed_code:
|
if stored_token.get("code_hash") != hashed_code:
|
||||||
await self._send_error(
|
await self._send_error(seq, self.opcodes.AUTH, self.error_types.INVALID_CODE, writer)
|
||||||
seq, self.opcodes.AUTH, self.error_types.INVALID_CODE, writer
|
|
||||||
)
|
|
||||||
return
|
return
|
||||||
|
|
||||||
# Если это новый пользователь - переводим токен в state='verified'
|
# Если это новый пользователь - переводим токен в state='verified'
|
||||||
@@ -249,67 +146,48 @@ class AuthProcessors(BaseProcessor):
|
|||||||
if stored_token.get("state") == "register":
|
if stored_token.get("state") == "register":
|
||||||
await cursor.execute(
|
await cursor.execute(
|
||||||
"UPDATE auth_tokens SET state = %s WHERE token_hash = %s",
|
"UPDATE auth_tokens SET state = %s WHERE token_hash = %s",
|
||||||
(
|
("verified", hashed_token,)
|
||||||
"verified",
|
|
||||||
hashed_token,
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
packet = self.proto.pack_packet(
|
packet = self.proto.pack_packet(
|
||||||
cmd=self.proto.CMD_OK,
|
cmd=self.proto.CMD_OK, seq=seq, opcode=self.opcodes.AUTH,
|
||||||
seq=seq,
|
|
||||||
opcode=self.opcodes.AUTH,
|
|
||||||
payload={
|
payload={
|
||||||
"tokenAttrs": {"REGISTER": {"token": token}},
|
"tokenAttrs": {
|
||||||
"presetAvatars": [],
|
"REGISTER": {
|
||||||
},
|
"token": token
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"presetAvatars": []
|
||||||
|
}
|
||||||
)
|
)
|
||||||
await self._send(writer, packet)
|
await self._send(writer, packet)
|
||||||
return
|
return
|
||||||
|
|
||||||
# Ищем аккаунт
|
# Ищем аккаунт
|
||||||
await cursor.execute(
|
await cursor.execute("SELECT * FROM users WHERE phone = %s", (stored_token.get("phone"),))
|
||||||
"SELECT * FROM users WHERE phone = %s", (stored_token.get("phone"),)
|
|
||||||
)
|
|
||||||
account = await cursor.fetchone()
|
account = await cursor.fetchone()
|
||||||
|
|
||||||
# Удаляем токен
|
# Удаляем токен
|
||||||
await cursor.execute(
|
await cursor.execute("DELETE FROM auth_tokens WHERE token_hash = %s", (hashed_token,))
|
||||||
"DELETE FROM auth_tokens WHERE token_hash = %s", (hashed_token,)
|
|
||||||
)
|
|
||||||
|
|
||||||
# Создаем сессию
|
# Создаем сессию
|
||||||
await cursor.execute(
|
await cursor.execute(
|
||||||
"INSERT INTO tokens (phone, token_hash, device_type, device_name, location, time) VALUES (%s, %s, %s, %s, %s, %s)",
|
"INSERT INTO tokens (phone, token_hash, device_type, device_name, location, time) VALUES (%s, %s, %s, %s, %s, %s)",
|
||||||
(
|
(stored_token.get("phone"), hashed_login, deviceType, deviceName, "Little Saint James Island", int(time.time()),) # весь покрытый зеленью, абсолютно весь, остров невезения в океане есть
|
||||||
stored_token.get("phone"),
|
|
||||||
hashed_login,
|
|
||||||
deviceType,
|
|
||||||
deviceName,
|
|
||||||
self.tools.get_geo(
|
|
||||||
ip=ip, db_path=self.config.geo_db_path
|
|
||||||
),
|
|
||||||
int(time.time() * 1000),
|
|
||||||
), # весь покрытый зеленью, абсолютно весь, остров невезения в океане есть
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# Генерируем профиль
|
# Генерируем профиль
|
||||||
# Аватарка с биографией
|
# Аватарка с биографией
|
||||||
photoId = (
|
photoId = None if not account.get("avatar_id") else int(account.get("avatar_id"))
|
||||||
None if not account.get("avatar_id") else int(account.get("avatar_id"))
|
avatar_url = None if not photoId else self.config.avatar_base_url + photoId
|
||||||
)
|
description = None if not account.get("description") else account.get("description")
|
||||||
avatar_url = None if not photoId else self.config.avatar_base_url + str(photoId)
|
|
||||||
description = (
|
|
||||||
None if not account.get("description") else account.get("description")
|
|
||||||
)
|
|
||||||
|
|
||||||
if self._check_legacy_version(appVersion):
|
|
||||||
include_profile_options = False
|
|
||||||
else:
|
|
||||||
include_profile_options = True
|
|
||||||
|
|
||||||
# Собираем данные пакета
|
# Собираем данные пакета
|
||||||
payload = {
|
payload = {
|
||||||
"tokenAttrs": {"LOGIN": {"token": login}},
|
"tokenAttrs": {
|
||||||
|
"LOGIN": {
|
||||||
|
"token": login
|
||||||
|
}
|
||||||
|
},
|
||||||
"profile": self.tools.generate_profile(
|
"profile": self.tools.generate_profile(
|
||||||
id=account.get("id"),
|
id=account.get("id"),
|
||||||
phone=int(account.get("phone")),
|
phone=int(account.get("phone")),
|
||||||
@@ -322,9 +200,9 @@ class AuthProcessors(BaseProcessor):
|
|||||||
description=description,
|
description=description,
|
||||||
accountStatus=int(account.get("accountstatus")),
|
accountStatus=int(account.get("accountstatus")),
|
||||||
profileOptions=json.loads(account.get("profileoptions")),
|
profileOptions=json.loads(account.get("profileoptions")),
|
||||||
includeProfileOptions=include_profile_options,
|
includeProfileOptions=True,
|
||||||
username=account.get("username"),
|
username=account.get("username")
|
||||||
),
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
# Создаем пакет
|
# Создаем пакет
|
||||||
@@ -335,16 +213,14 @@ class AuthProcessors(BaseProcessor):
|
|||||||
# Отправляем
|
# Отправляем
|
||||||
await self._send(writer, packet)
|
await self._send(writer, packet)
|
||||||
|
|
||||||
async def auth_confirm(self, payload, seq, writer, deviceType, deviceName, appVersion, ip):
|
async def auth_confirm(self, payload, seq, writer, deviceType, deviceName):
|
||||||
"""Обработчик подтверждения регистрации нового пользователя"""
|
"""Обработчик подтверждения регистрации нового пользователя"""
|
||||||
# Валидируем данные пакета
|
# Валидируем данные пакета
|
||||||
try:
|
try:
|
||||||
AuthConfirmRegisterPayloadModel.model_validate(payload)
|
AuthConfirmRegisterPayloadModel.model_validate(payload)
|
||||||
except pydantic.ValidationError as error:
|
except pydantic.ValidationError as error:
|
||||||
self.logger.error(f"Возникли ошибки при валидации пакета: {error}")
|
self.logger.error(f"Возникли ошибки при валидации пакета: {error}")
|
||||||
await self._send_error(
|
await self._send_error(seq, self.opcodes.AUTH_CONFIRM, self.error_types.INVALID_PAYLOAD, writer)
|
||||||
seq, self.opcodes.AUTH_CONFIRM, self.error_types.INVALID_PAYLOAD, writer
|
|
||||||
)
|
|
||||||
return
|
return
|
||||||
|
|
||||||
# Извлекаем данные из пакета
|
# Извлекаем данные из пакета
|
||||||
@@ -364,21 +240,13 @@ class AuthProcessors(BaseProcessor):
|
|||||||
# Ищем токен - он должен быть в state='verified'
|
# Ищем токен - он должен быть в state='verified'
|
||||||
await cursor.execute(
|
await cursor.execute(
|
||||||
"SELECT * FROM auth_tokens WHERE token_hash = %s AND expires > UNIX_TIMESTAMP() AND state = %s",
|
"SELECT * FROM auth_tokens WHERE token_hash = %s AND expires > UNIX_TIMESTAMP() AND state = %s",
|
||||||
(
|
(hashed_token, "verified",)
|
||||||
hashed_token,
|
|
||||||
"verified",
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
stored_token = await cursor.fetchone()
|
stored_token = await cursor.fetchone()
|
||||||
|
|
||||||
# Если токен не найден или просрочен - отправляем ошибку
|
# Если токен не найден или просрочен - отправляем ошибку
|
||||||
if stored_token is None:
|
if stored_token is None:
|
||||||
await self._send_error(
|
await self._send_error(seq, self.opcodes.AUTH_CONFIRM, self.error_types.CODE_EXPIRED, writer)
|
||||||
seq,
|
|
||||||
self.opcodes.AUTH_CONFIRM,
|
|
||||||
self.error_types.CODE_EXPIRED,
|
|
||||||
writer,
|
|
||||||
)
|
|
||||||
return
|
return
|
||||||
|
|
||||||
phone = stored_token.get("phone")
|
phone = stored_token.get("phone")
|
||||||
@@ -386,20 +254,12 @@ class AuthProcessors(BaseProcessor):
|
|||||||
# Проверяем что пользователь с таким телефоном ещё не существует
|
# Проверяем что пользователь с таким телефоном ещё не существует
|
||||||
await cursor.execute("SELECT id FROM users WHERE phone = %s", (phone,))
|
await cursor.execute("SELECT id FROM users WHERE phone = %s", (phone,))
|
||||||
if await cursor.fetchone():
|
if await cursor.fetchone():
|
||||||
await self._send_error(
|
await self._send_error(seq, self.opcodes.AUTH_CONFIRM, self.error_types.INVALID_PAYLOAD, writer)
|
||||||
seq,
|
|
||||||
self.opcodes.AUTH_CONFIRM,
|
|
||||||
self.error_types.INVALID_PAYLOAD,
|
|
||||||
writer,
|
|
||||||
)
|
|
||||||
return
|
return
|
||||||
|
|
||||||
now_ms = int(time.time() * 1000)
|
now_ms = int(time.time() * 1000)
|
||||||
now_s = int(time.time())
|
now_s = int(time.time())
|
||||||
|
|
||||||
# Генерируем ID пользователя
|
|
||||||
user_id = await self.tools.generate_user_id(self.db_pool)
|
|
||||||
|
|
||||||
# Создаем пользователя
|
# Создаем пользователя
|
||||||
await cursor.execute(
|
await cursor.execute(
|
||||||
"""
|
"""
|
||||||
@@ -409,69 +269,39 @@ class AuthProcessors(BaseProcessor):
|
|||||||
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
|
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
|
||||||
""",
|
""",
|
||||||
(
|
(
|
||||||
user_id,
|
self.tools.generate_id(), phone, None, first_name, last_name, None,
|
||||||
phone,
|
json.dumps([]), json.dumps(["TT", "ONEME"]),
|
||||||
None,
|
0, str(now_ms), str(now_s),
|
||||||
first_name,
|
)
|
||||||
last_name,
|
|
||||||
None,
|
|
||||||
json.dumps([]),
|
|
||||||
json.dumps(["TT", "ONEME"]),
|
|
||||||
0,
|
|
||||||
str(now_ms),
|
|
||||||
str(now_s),
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
user_id = cursor.lastrowid
|
||||||
|
|
||||||
# Добавляем данные аккаунта
|
# Добавляем данные аккаунта
|
||||||
await cursor.execute(
|
await cursor.execute(
|
||||||
"""
|
"""
|
||||||
INSERT INTO user_data
|
INSERT INTO user_data
|
||||||
(phone, user_config, chat_config)
|
(phone, contacts, folders, user_config, chat_config)
|
||||||
VALUES (%s, %s, %s)
|
VALUES (%s, %s, %s, %s, %s)
|
||||||
""",
|
""",
|
||||||
(
|
(
|
||||||
phone,
|
phone,
|
||||||
|
json.dumps([]),
|
||||||
|
json.dumps(self.static.USER_FOLDERS),
|
||||||
json.dumps(self.static.USER_SETTINGS),
|
json.dumps(self.static.USER_SETTINGS),
|
||||||
json.dumps({}),
|
json.dumps({}),
|
||||||
),
|
)
|
||||||
)
|
|
||||||
|
|
||||||
# Добавляем дефолтную папку
|
|
||||||
await cursor.execute(
|
|
||||||
"""
|
|
||||||
INSERT INTO user_folders
|
|
||||||
(id, phone, title, sort_order)
|
|
||||||
VALUES ('all.chat.folder', %s, 'Все', 0)
|
|
||||||
""",
|
|
||||||
(phone,),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# Удаляем токен
|
# Удаляем токен
|
||||||
await cursor.execute(
|
await cursor.execute("DELETE FROM auth_tokens WHERE token_hash = %s", (hashed_token,))
|
||||||
"DELETE FROM auth_tokens WHERE token_hash = %s", (hashed_token,)
|
|
||||||
)
|
|
||||||
|
|
||||||
# Создаем сессию
|
# Создаем сессию
|
||||||
await cursor.execute(
|
await cursor.execute(
|
||||||
"INSERT INTO tokens (phone, token_hash, device_type, device_name, location, time) VALUES (%s, %s, %s, %s, %s, %s)",
|
"INSERT INTO tokens (phone, token_hash, device_type, device_name, location, time) VALUES (%s, %s, %s, %s, %s, %s)",
|
||||||
(
|
(phone, hashed_login, deviceType or "ANDROID", deviceName or "Unknown", "Little Saint James Island", now_s,)
|
||||||
phone,
|
|
||||||
hashed_login,
|
|
||||||
deviceType or "ANDROID",
|
|
||||||
deviceName or "Unknown",
|
|
||||||
self.tools.get_geo(
|
|
||||||
ip=ip, db_path=self.config.geo_db_path
|
|
||||||
),
|
|
||||||
now_ms,
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if self._check_legacy_version(appVersion):
|
|
||||||
include_profile_options = False
|
|
||||||
else:
|
|
||||||
include_profile_options = True
|
|
||||||
|
|
||||||
# Генерируем профиль
|
# Генерируем профиль
|
||||||
profile = self.tools.generate_profile(
|
profile = self.tools.generate_profile(
|
||||||
id=user_id,
|
id=user_id,
|
||||||
@@ -485,8 +315,8 @@ class AuthProcessors(BaseProcessor):
|
|||||||
description=None,
|
description=None,
|
||||||
accountStatus=0,
|
accountStatus=0,
|
||||||
profileOptions=[],
|
profileOptions=[],
|
||||||
includeProfileOptions=include_profile_options,
|
includeProfileOptions=True,
|
||||||
username=None,
|
username=None
|
||||||
)
|
)
|
||||||
|
|
||||||
# Собираем данные пакета
|
# Собираем данные пакета
|
||||||
@@ -494,35 +324,28 @@ class AuthProcessors(BaseProcessor):
|
|||||||
"userToken": "0",
|
"userToken": "0",
|
||||||
"profile": profile,
|
"profile": profile,
|
||||||
"tokenType": "LOGIN",
|
"tokenType": "LOGIN",
|
||||||
"token": login,
|
"token": login
|
||||||
}
|
}
|
||||||
|
|
||||||
# Создаем пакет
|
# Создаем пакет
|
||||||
packet = self.proto.pack_packet(
|
packet = self.proto.pack_packet(
|
||||||
cmd=self.proto.CMD_OK,
|
cmd=self.proto.CMD_OK, seq=seq, opcode=self.opcodes.AUTH_CONFIRM, payload=payload
|
||||||
seq=seq,
|
|
||||||
opcode=self.opcodes.AUTH_CONFIRM,
|
|
||||||
payload=payload,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# Отправляем
|
# Отправляем
|
||||||
await self._send(writer, packet)
|
await self._send(writer, packet)
|
||||||
self.logger.info(
|
self.logger.info(f"Новый пользователь зарегистрирован: phone={phone} id={user_id} name={first_name} {last_name}")
|
||||||
f"Новый пользователь зарегистрирован: phone={phone} id={user_id} name={first_name} {last_name}"
|
|
||||||
)
|
|
||||||
|
|
||||||
async def login(self, payload, seq, writer, appVersion):
|
async def login(self, payload, seq, writer):
|
||||||
"""Обработчик авторизации клиента на сервере"""
|
"""Обработчик авторизации клиента на сервере"""
|
||||||
# Валидируем данные пакета
|
# Валидируем данные пакета
|
||||||
try:
|
try:
|
||||||
LoginPayloadModel.model_validate(payload)
|
LoginPayloadModel.model_validate(payload)
|
||||||
except pydantic.ValidationError as error:
|
except pydantic.ValidationError as error:
|
||||||
self.logger.error(f"Возникли ошибки при валидации пакета: {error}")
|
self.logger.error(f"Возникли ошибки при валидации пакета: {error}")
|
||||||
await self._send_error(
|
await self._send_error(seq, self.opcodes.LOGIN, self.error_types.INVALID_PAYLOAD, writer)
|
||||||
seq, self.opcodes.LOGIN, self.error_types.INVALID_PAYLOAD, writer
|
return
|
||||||
)
|
|
||||||
return None, None, None
|
|
||||||
|
|
||||||
# Чаты, где состоит пользователь
|
# Чаты, где состоит пользователь
|
||||||
chats = []
|
chats = []
|
||||||
|
|
||||||
@@ -535,57 +358,39 @@ class AuthProcessors(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 * FROM tokens WHERE token_hash = %s", (hashed_token,))
|
||||||
"SELECT * FROM tokens WHERE token_hash = %s", (hashed_token,)
|
|
||||||
)
|
|
||||||
token_data = await cursor.fetchone()
|
token_data = await cursor.fetchone()
|
||||||
|
|
||||||
# Если токен не найден, отправляем ошибку
|
# Если токен не найден, отправляем ошибку
|
||||||
if token_data is None:
|
if token_data is None:
|
||||||
await self._send_error(
|
await self._send_error(seq, self.opcodes.LOGIN, self.error_types.INVALID_TOKEN, writer)
|
||||||
seq, self.opcodes.LOGIN, self.error_types.INVALID_TOKEN, writer
|
return
|
||||||
)
|
|
||||||
return None, None, None
|
|
||||||
|
|
||||||
# Ищем аккаунт пользователя в бд
|
# Ищем аккаунт пользователя в бд
|
||||||
await cursor.execute(
|
await cursor.execute("SELECT * FROM users WHERE phone = %s", (token_data.get("phone"),))
|
||||||
"SELECT * FROM users WHERE phone = %s", (token_data.get("phone"),)
|
|
||||||
)
|
|
||||||
user = await cursor.fetchone()
|
user = await cursor.fetchone()
|
||||||
|
|
||||||
# Ищем данные пользователя в бд
|
# Ищем данные пользователя в бд
|
||||||
await cursor.execute(
|
await cursor.execute("SELECT * FROM user_data WHERE phone = %s", (token_data.get("phone"),))
|
||||||
"SELECT * FROM user_data WHERE phone = %s",
|
|
||||||
(token_data.get("phone"),),
|
|
||||||
)
|
|
||||||
user_data = await cursor.fetchone()
|
user_data = await cursor.fetchone()
|
||||||
|
|
||||||
# Ищем все чаты, где состоит пользователь
|
# Ищем все чаты, где состоит пользователь
|
||||||
await cursor.execute(
|
await cursor.execute(
|
||||||
"SELECT * FROM chat_participants WHERE user_id = %s",
|
"SELECT * FROM chat_participants WHERE user_id = %s",
|
||||||
(user.get("id"),),
|
(user.get('id'))
|
||||||
)
|
)
|
||||||
user_chats = await cursor.fetchall()
|
user_chats = await cursor.fetchall()
|
||||||
|
|
||||||
for chat in user_chats:
|
for chat in user_chats:
|
||||||
chats.append(chat.get("chat_id"))
|
chats.append(
|
||||||
|
chat.get("chat_id")
|
||||||
# Обновляем юзер конфиг
|
)
|
||||||
updated_user_config = await self.tools.update_user_config(
|
|
||||||
cursor, token_data.get("phone"),
|
|
||||||
user_data.get("user_config"), self.static.USER_SETTINGS
|
|
||||||
)
|
|
||||||
|
|
||||||
# Аватарка с биографией
|
# Аватарка с биографией
|
||||||
photoId = None if not user.get("avatar_id") else int(user.get("avatar_id"))
|
photoId = None if not user.get("avatar_id") else int(user.get("avatar_id"))
|
||||||
avatar_url = None if not photoId else self.config.avatar_base_url + str(photoId)
|
avatar_url = None if not photoId else self.config.avatar_base_url + photoId
|
||||||
description = None if not user.get("description") else user.get("description")
|
description = None if not user.get("description") else user.get("description")
|
||||||
|
|
||||||
if self._check_legacy_version(appVersion):
|
|
||||||
include_profile_options = False
|
|
||||||
else:
|
|
||||||
include_profile_options = True
|
|
||||||
|
|
||||||
# Генерируем профиль
|
# Генерируем профиль
|
||||||
profile = self.tools.generate_profile(
|
profile = self.tools.generate_profile(
|
||||||
id=user.get("id"),
|
id=user.get("id"),
|
||||||
@@ -599,42 +404,31 @@ class AuthProcessors(BaseProcessor):
|
|||||||
description=description,
|
description=description,
|
||||||
accountStatus=int(user.get("accountstatus")),
|
accountStatus=int(user.get("accountstatus")),
|
||||||
profileOptions=json.loads(user.get("profileoptions")),
|
profileOptions=json.loads(user.get("profileoptions")),
|
||||||
includeProfileOptions=include_profile_options,
|
includeProfileOptions=True,
|
||||||
username=user.get("username"),
|
username=user.get("username")
|
||||||
)
|
)
|
||||||
|
|
||||||
# Генерируем список чатов
|
|
||||||
chats = await self.tools.generate_chats(
|
chats = await self.tools.generate_chats(
|
||||||
chats, self.db_pool, user.get("id"), protocol_type=self.type
|
chats, self.db_pool, user.get("id"), protocol_type=self.type
|
||||||
)
|
)
|
||||||
|
|
||||||
# Генерируем список контактов
|
|
||||||
contacts = await self.tools.collect_user_contacts(
|
|
||||||
user.get("id"), self.db_pool, self.config.avatar_base_url
|
|
||||||
)
|
|
||||||
|
|
||||||
# Собираем статусы контактов
|
|
||||||
contact_ids = [c.get("id") for c in contacts if c.get("id") is not None]
|
|
||||||
presence = await self.tools.collect_presence(contact_ids, self.clients, self.db_pool)
|
|
||||||
|
|
||||||
# Формируем данные пакета
|
# Формируем данные пакета
|
||||||
payload = {
|
payload = {
|
||||||
"profile": profile,
|
"profile": profile,
|
||||||
"chats": chats,
|
"chats": chats,
|
||||||
"chatMarker": 0,
|
"chatMarker": 0,
|
||||||
"messages": {},
|
"messages": {},
|
||||||
"contacts": contacts,
|
"contacts": [],
|
||||||
"presence": presence,
|
"presence": {},
|
||||||
"config": {
|
"config": {
|
||||||
"hash": "0",
|
|
||||||
"server": self.server_config,
|
"server": self.server_config,
|
||||||
"user": updated_user_config,
|
"user": json.loads(user_data.get("user_config"))
|
||||||
},
|
},
|
||||||
"token": token,
|
"token": token,
|
||||||
"videoChatHistory": False,
|
"videoChatHistory": False,
|
||||||
"time": int(time.time() * 1000),
|
"time": int(time.time() * 1000)
|
||||||
}
|
}
|
||||||
|
|
||||||
# Собираем пакет
|
# Собираем пакет
|
||||||
packet = self.proto.pack_packet(
|
packet = self.proto.pack_packet(
|
||||||
cmd=self.proto.CMD_OK, seq=seq, opcode=self.opcodes.LOGIN, payload=payload
|
cmd=self.proto.CMD_OK, seq=seq, opcode=self.opcodes.LOGIN, payload=payload
|
||||||
@@ -642,10 +436,6 @@ class AuthProcessors(BaseProcessor):
|
|||||||
|
|
||||||
# Отправляем
|
# Отправляем
|
||||||
await self._send(writer, packet)
|
await self._send(writer, packet)
|
||||||
|
|
||||||
# Отправляем баннеры
|
|
||||||
await self._send_banners(writer)
|
|
||||||
|
|
||||||
return int(user.get("phone")), int(user.get("id")), hashed_token
|
return int(user.get("phone")), int(user.get("id")), hashed_token
|
||||||
|
|
||||||
async def logout(self, seq, writer, hashedToken):
|
async def logout(self, seq, writer, hashedToken):
|
||||||
@@ -653,14 +443,12 @@ class AuthProcessors(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("DELETE FROM tokens WHERE token_hash = %s", (hashedToken,))
|
||||||
"DELETE FROM tokens WHERE token_hash = %s", (hashedToken,)
|
|
||||||
)
|
|
||||||
|
|
||||||
# Создаем пакет
|
# Создаем пакет
|
||||||
response = self.proto.pack_packet(
|
response = self.proto.pack_packet(
|
||||||
cmd=self.proto.CMD_OK, seq=seq, opcode=self.opcodes.LOGOUT, payload=None
|
cmd=self.proto.CMD_OK, seq=seq, opcode=self.opcodes.LOGOUT, payload=None
|
||||||
)
|
)
|
||||||
|
|
||||||
# Отправляем
|
# Отправляем
|
||||||
await self._send(writer, response)
|
await self._send(writer, response)
|
||||||
@@ -3,7 +3,7 @@ import time
|
|||||||
from classes.baseprocessor import BaseProcessor
|
from classes.baseprocessor import BaseProcessor
|
||||||
from oneme.models import ComplainReasonsGetPayloadModel
|
from oneme.models import ComplainReasonsGetPayloadModel
|
||||||
|
|
||||||
class ComplaintsProcessors(BaseProcessor):
|
class ComplainsProcessors(BaseProcessor):
|
||||||
async def complain_reasons_get(self, payload, seq, writer):
|
async def complain_reasons_get(self, payload, seq, writer):
|
||||||
"""Обработчик получения причин жалоб"""
|
"""Обработчик получения причин жалоб"""
|
||||||
# Валидируем данные пакета
|
# Валидируем данные пакета
|
||||||
@@ -1,412 +0,0 @@
|
|||||||
import pydantic
|
|
||||||
import json
|
|
||||||
import time
|
|
||||||
from classes.baseprocessor import BaseProcessor
|
|
||||||
from oneme.models import ContactAddByPhonePayloadModel, ContactListPayloadModel, ContactPresencePayloadModel, ContactUpdatePayloadModel
|
|
||||||
|
|
||||||
class ContactsProcessors(BaseProcessor):
|
|
||||||
async def contact_list(self, payload, seq, writer, userId):
|
|
||||||
"""Обработчик получения контактов"""
|
|
||||||
# Валидируем данные пакета
|
|
||||||
try:
|
|
||||||
ContactListPayloadModel.model_validate(payload)
|
|
||||||
except pydantic.ValidationError as error:
|
|
||||||
self.logger.error(f"Возникли ошибки при валидации пакета: {error}")
|
|
||||||
await self._send_error(seq, self.opcodes.CONTACT_LIST, self.error_types.INVALID_PAYLOAD, writer)
|
|
||||||
return
|
|
||||||
|
|
||||||
status = payload.get("status")
|
|
||||||
count = payload.get("count")
|
|
||||||
|
|
||||||
# Итоговый контакт-лист
|
|
||||||
contact_list = []
|
|
||||||
|
|
||||||
if status == "BLOCKED":
|
|
||||||
# Собираем контакты, которые в черном списке
|
|
||||||
blocked = []
|
|
||||||
|
|
||||||
async with self.db_pool.acquire() as conn:
|
|
||||||
async with conn.cursor() as cursor:
|
|
||||||
if count:
|
|
||||||
await cursor.execute(
|
|
||||||
"SELECT * FROM contacts WHERE owner_id = %s AND is_blocked = TRUE LIMIT %s",
|
|
||||||
(userId, count),
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
await cursor.execute(
|
|
||||||
"SELECT * FROM contacts WHERE owner_id = %s AND is_blocked = TRUE",
|
|
||||||
(userId,),
|
|
||||||
)
|
|
||||||
rows = await cursor.fetchall()
|
|
||||||
|
|
||||||
for row in rows:
|
|
||||||
blocked.append(
|
|
||||||
{
|
|
||||||
"id": int(row.get("contact_id")),
|
|
||||||
"firstname": row.get("custom_firstname"),
|
|
||||||
"lastname": row.get("custom_lastname"),
|
|
||||||
"blocked": True,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
# Генерируем контакт-лист
|
|
||||||
contact_list = await self.tools.generate_contacts(
|
|
||||||
blocked, self.db_pool, avatar_base_url=self.config.avatar_base_url
|
|
||||||
)
|
|
||||||
|
|
||||||
# Собираем данные пакета
|
|
||||||
response_payload = {
|
|
||||||
"contacts": contact_list
|
|
||||||
}
|
|
||||||
|
|
||||||
# Создаем пакет
|
|
||||||
packet = self.proto.pack_packet(
|
|
||||||
seq=seq, opcode=self.opcodes.CONTACT_LIST, payload=response_payload
|
|
||||||
)
|
|
||||||
|
|
||||||
# Отправляем пакет
|
|
||||||
await self._send(writer, packet)
|
|
||||||
|
|
||||||
async def contact_update(self, payload, seq, writer, userId):
|
|
||||||
"""
|
|
||||||
Обработчик опкода какого-то там
|
|
||||||
(их хуй запомнишь, даже в мриме команды помню, бля)
|
|
||||||
|
|
||||||
Отвечает за добавку, удаление, блокировку и разблокировку контакта
|
|
||||||
"""
|
|
||||||
# Валидируем данные пакета
|
|
||||||
try:
|
|
||||||
ContactUpdatePayloadModel.model_validate(payload)
|
|
||||||
except pydantic.ValidationError as error:
|
|
||||||
self.logger.error(f"Возникли ошибки при валидации пакета: {error}")
|
|
||||||
await self._send_error(seq, self.opcodes.CONTACT_UPDATE, self.error_types.INVALID_PAYLOAD, writer)
|
|
||||||
return
|
|
||||||
|
|
||||||
action = payload.get("action")
|
|
||||||
contactId = payload.get("contactId")
|
|
||||||
firstName = payload.get("firstName")
|
|
||||||
lastName = payload.get("lastName", "")
|
|
||||||
|
|
||||||
if action == "ADD":
|
|
||||||
# Проверяем, существует ли пользователь с таким ID
|
|
||||||
async with self.db_pool.acquire() as conn:
|
|
||||||
async with conn.cursor() as cursor:
|
|
||||||
await cursor.execute("SELECT * FROM users WHERE id = %s", (contactId,))
|
|
||||||
user = await cursor.fetchone()
|
|
||||||
|
|
||||||
if not user:
|
|
||||||
await self._send_error(seq, self.opcodes.CONTACT_UPDATE, self.error_types.CONTACT_NOT_FOUND, writer)
|
|
||||||
return
|
|
||||||
|
|
||||||
# Проверяем, не добавлен ли уже контакт
|
|
||||||
await cursor.execute(
|
|
||||||
"SELECT * FROM contacts WHERE owner_id = %s AND contact_id = %s",
|
|
||||||
(userId, contactId)
|
|
||||||
)
|
|
||||||
row = await cursor.fetchone()
|
|
||||||
|
|
||||||
# Если контакта не существует, то можем продолжать,
|
|
||||||
if not row:
|
|
||||||
# Добавляем контакт
|
|
||||||
await cursor.execute(
|
|
||||||
"INSERT INTO contacts (owner_id, contact_id, custom_firstname, custom_lastname, is_blocked) VALUES (%s, %s, %s, %s, FALSE)",
|
|
||||||
(userId, contactId, firstName, lastName)
|
|
||||||
)
|
|
||||||
|
|
||||||
# Создаем диалог, если его нет
|
|
||||||
chatId = userId ^ contactId
|
|
||||||
await cursor.execute("SELECT * FROM chats WHERE id = %s", (chatId,))
|
|
||||||
chat = await cursor.fetchone()
|
|
||||||
|
|
||||||
if not chat:
|
|
||||||
await cursor.execute(
|
|
||||||
"INSERT INTO chats (id, owner, type) VALUES (%s, %s, %s)",
|
|
||||||
(chatId, userId, "DIALOG")
|
|
||||||
)
|
|
||||||
|
|
||||||
for uid in [int(userId), int(contactId)]:
|
|
||||||
await cursor.execute(
|
|
||||||
"INSERT INTO chat_participants (chat_id, user_id) VALUES (%s, %s)",
|
|
||||||
(chatId, uid)
|
|
||||||
)
|
|
||||||
# а если уже существует, отправляем ошибку
|
|
||||||
else:
|
|
||||||
await self._send_error(seq, self.opcodes.CONTACT_UPDATE, self.error_types.CONTACT_ALREADY_ADDED, writer)
|
|
||||||
return
|
|
||||||
|
|
||||||
# Генерируем профиль
|
|
||||||
photoId = None if not user.get("avatar_id") else int(user.get("avatar_id"))
|
|
||||||
avatar_url = None if not photoId else self.config.avatar_base_url + str(photoId)
|
|
||||||
|
|
||||||
contact = self.tools.generate_profile(
|
|
||||||
id=user.get("id"),
|
|
||||||
phone=int(user.get("phone")),
|
|
||||||
avatarUrl=avatar_url,
|
|
||||||
photoId=photoId,
|
|
||||||
updateTime=int(user.get("updatetime")),
|
|
||||||
firstName=user.get("firstname"),
|
|
||||||
lastName=user.get("lastname"),
|
|
||||||
options=json.loads(user.get("options")),
|
|
||||||
accountStatus=int(user.get("accountstatus")),
|
|
||||||
includeProfileOptions=False,
|
|
||||||
custom_firstname=firstName,
|
|
||||||
custom_lastname=lastName,
|
|
||||||
)
|
|
||||||
|
|
||||||
response_payload = {
|
|
||||||
"contact": contact
|
|
||||||
}
|
|
||||||
|
|
||||||
packet = self.proto.pack_packet(
|
|
||||||
cmd=self.proto.CMD_OK, seq=seq, opcode=self.opcodes.CONTACT_UPDATE, payload=response_payload
|
|
||||||
)
|
|
||||||
|
|
||||||
await self._send(writer, packet)
|
|
||||||
|
|
||||||
elif action == "REMOVE":
|
|
||||||
# Удаляем контакт
|
|
||||||
async with self.db_pool.acquire() as conn:
|
|
||||||
async with conn.cursor() as cursor:
|
|
||||||
await cursor.execute(
|
|
||||||
"DELETE FROM contacts WHERE owner_id = %s AND contact_id = %s",
|
|
||||||
(userId, contactId)
|
|
||||||
)
|
|
||||||
|
|
||||||
packet = self.proto.pack_packet(
|
|
||||||
cmd=self.proto.CMD_OK, seq=seq, opcode=self.opcodes.CONTACT_UPDATE, payload=None
|
|
||||||
)
|
|
||||||
|
|
||||||
await self._send(writer, packet)
|
|
||||||
|
|
||||||
elif action == "BLOCK":
|
|
||||||
async with self.db_pool.acquire() as conn:
|
|
||||||
async with conn.cursor() as cursor:
|
|
||||||
# Проверяем, существует ли контакт
|
|
||||||
await cursor.execute(
|
|
||||||
"SELECT * FROM contacts WHERE owner_id = %s AND contact_id = %s",
|
|
||||||
(userId, contactId)
|
|
||||||
)
|
|
||||||
row = await cursor.fetchone()
|
|
||||||
|
|
||||||
# Обновляем существующий контакт, если такой есть
|
|
||||||
if row:
|
|
||||||
await cursor.execute(
|
|
||||||
"UPDATE contacts SET is_blocked = TRUE WHERE owner_id = %s AND contact_id = %s",
|
|
||||||
(userId, contactId)
|
|
||||||
)
|
|
||||||
else: # В ином случае добавляем новую запись в бд
|
|
||||||
await cursor.execute("SELECT * FROM users WHERE id = %s", (contactId,))
|
|
||||||
user = await cursor.fetchone()
|
|
||||||
if not user:
|
|
||||||
await self._send_error(seq, self.opcodes.CONTACT_UPDATE, self.error_types.USER_NOT_FOUND, writer)
|
|
||||||
return
|
|
||||||
|
|
||||||
await cursor.execute(
|
|
||||||
"INSERT INTO contacts (owner_id, contact_id, custom_firstname, custom_lastname, is_blocked) VALUES (%s, %s, %s, %s, TRUE)",
|
|
||||||
(userId, contactId, firstName, lastName)
|
|
||||||
)
|
|
||||||
|
|
||||||
packet = self.proto.pack_packet(
|
|
||||||
cmd=self.proto.CMD_OK, seq=seq, opcode=self.opcodes.CONTACT_UPDATE, payload=None
|
|
||||||
)
|
|
||||||
|
|
||||||
await self._send(writer, packet)
|
|
||||||
|
|
||||||
elif action == "UNBLOCK":
|
|
||||||
# Разблокируем контакт
|
|
||||||
async with self.db_pool.acquire() as conn:
|
|
||||||
async with conn.cursor() as cursor:
|
|
||||||
# Проверяем, существует ли контакт
|
|
||||||
await cursor.execute(
|
|
||||||
"SELECT * FROM contacts WHERE owner_id = %s AND contact_id = %s",
|
|
||||||
(userId, contactId)
|
|
||||||
)
|
|
||||||
row = await cursor.fetchone()
|
|
||||||
|
|
||||||
# Обновляем контакт, если он есть
|
|
||||||
if row:
|
|
||||||
await cursor.execute(
|
|
||||||
"UPDATE contacts SET is_blocked = FALSE WHERE owner_id = %s AND contact_id = %s",
|
|
||||||
(userId, contactId)
|
|
||||||
)
|
|
||||||
else: # В ином случае отправляем ошибку
|
|
||||||
await self._send_error(seq, self.opcodes.CONTACT_UPDATE, self.error_types.CONTACT_NOT_FOUND, writer)
|
|
||||||
return
|
|
||||||
|
|
||||||
packet = self.proto.pack_packet(
|
|
||||||
cmd=self.proto.CMD_OK, seq=seq, opcode=self.opcodes.CONTACT_UPDATE, payload=None
|
|
||||||
)
|
|
||||||
|
|
||||||
await self._send(writer, packet)
|
|
||||||
|
|
||||||
elif action == "UPDATE":
|
|
||||||
async with self.db_pool.acquire() as conn:
|
|
||||||
async with conn.cursor() as cursor:
|
|
||||||
# Проверяем, существует ли контакт
|
|
||||||
await cursor.execute(
|
|
||||||
"SELECT * FROM contacts WHERE owner_id = %s AND contact_id = %s",
|
|
||||||
(userId, contactId)
|
|
||||||
)
|
|
||||||
row = await cursor.fetchone()
|
|
||||||
|
|
||||||
# Если контакта нет, отдаем ошибку
|
|
||||||
if not row:
|
|
||||||
await self._send_error(seq, self.opcodes.CONTACT_UPDATE, self.error_types.CONTACT_NOT_FOUND, writer)
|
|
||||||
return
|
|
||||||
|
|
||||||
# Обновляем контакт
|
|
||||||
await cursor.execute(
|
|
||||||
"UPDATE contacts SET custom_firstname = %s, custom_lastname = %s WHERE owner_id = %s AND contact_id = %s",
|
|
||||||
(firstName, lastName, userId, contactId)
|
|
||||||
)
|
|
||||||
|
|
||||||
# Получаем данные пользователя
|
|
||||||
await cursor.execute("SELECT * FROM users WHERE id = %s", (contactId,))
|
|
||||||
user = await cursor.fetchone()
|
|
||||||
|
|
||||||
# Генерируем профиль
|
|
||||||
photoId = None if not user.get("avatar_id") else int(user.get("avatar_id"))
|
|
||||||
avatar_url = None if not photoId else self.config.avatar_base_url + str(photoId)
|
|
||||||
|
|
||||||
contact = self.tools.generate_profile(
|
|
||||||
id=user.get("id"),
|
|
||||||
phone=int(user.get("phone")),
|
|
||||||
avatarUrl=avatar_url,
|
|
||||||
photoId=photoId,
|
|
||||||
updateTime=int(user.get("updatetime")),
|
|
||||||
firstName=user.get("firstname"),
|
|
||||||
lastName=user.get("lastname"),
|
|
||||||
options=json.loads(user.get("options")),
|
|
||||||
accountStatus=int(user.get("accountstatus")),
|
|
||||||
description=user.get("description"),
|
|
||||||
includeProfileOptions=False,
|
|
||||||
custom_firstname=firstName,
|
|
||||||
custom_lastname=lastName,
|
|
||||||
)
|
|
||||||
|
|
||||||
response_payload = {
|
|
||||||
"contact": contact
|
|
||||||
}
|
|
||||||
|
|
||||||
packet = self.proto.pack_packet(
|
|
||||||
cmd=self.proto.CMD_OK, seq=seq, opcode=self.opcodes.CONTACT_UPDATE, payload=response_payload
|
|
||||||
)
|
|
||||||
|
|
||||||
await self._send(writer, packet)
|
|
||||||
|
|
||||||
async def contact_add_by_phone(self, payload, seq, writer, userId):
|
|
||||||
"""Добавление контакта по номеру телефона"""
|
|
||||||
# Валидируем данные пакета
|
|
||||||
try:
|
|
||||||
ContactAddByPhonePayloadModel.model_validate(payload)
|
|
||||||
except pydantic.ValidationError as error:
|
|
||||||
self.logger.error(f"Возникли ошибки при валидации пакета: {error}")
|
|
||||||
await self._send_error(seq, self.opcodes.CONTACT_ADD_BY_PHONE, self.error_types.INVALID_PAYLOAD, writer)
|
|
||||||
return
|
|
||||||
|
|
||||||
phone = payload.get("phone")
|
|
||||||
firstName = payload.get("firstName")
|
|
||||||
lastName = payload.get("lastName")
|
|
||||||
|
|
||||||
# Ищем пользователя по номеру телефона
|
|
||||||
async with self.db_pool.acquire() as conn:
|
|
||||||
async with conn.cursor() as cursor:
|
|
||||||
await cursor.execute("SELECT * FROM users WHERE phone = %s", (int(phone),))
|
|
||||||
user = await cursor.fetchone()
|
|
||||||
|
|
||||||
if not user:
|
|
||||||
await self._send_error(seq, self.opcodes.CONTACT_ADD_BY_PHONE, self.error_types.CONTACT_NOT_FOUND, writer)
|
|
||||||
return
|
|
||||||
|
|
||||||
contactId = user.get("id")
|
|
||||||
|
|
||||||
# Проверяем, не добавлен ли уже контакт
|
|
||||||
await cursor.execute(
|
|
||||||
"SELECT * FROM contacts WHERE owner_id = %s AND contact_id = %s",
|
|
||||||
(userId, contactId)
|
|
||||||
)
|
|
||||||
existing_contact = await cursor.fetchone()
|
|
||||||
|
|
||||||
is_new = existing_contact is None
|
|
||||||
|
|
||||||
if is_new:
|
|
||||||
# Добавляем контакт
|
|
||||||
await cursor.execute(
|
|
||||||
"INSERT INTO contacts (owner_id, contact_id, custom_firstname, custom_lastname) VALUES (%s, %s, %s, %s)",
|
|
||||||
(userId, contactId, firstName, lastName)
|
|
||||||
)
|
|
||||||
|
|
||||||
# Создаем диалог, если его нет
|
|
||||||
chatId = userId ^ contactId
|
|
||||||
await cursor.execute("SELECT * FROM chats WHERE id = %s", (chatId,))
|
|
||||||
chat = await cursor.fetchone()
|
|
||||||
|
|
||||||
if not chat:
|
|
||||||
await cursor.execute(
|
|
||||||
"INSERT INTO chats (id, owner, type) VALUES (%s, %s, %s)",
|
|
||||||
(chatId, userId, "DIALOG")
|
|
||||||
)
|
|
||||||
|
|
||||||
for uid in [int(userId), int(contactId)]:
|
|
||||||
await cursor.execute(
|
|
||||||
"INSERT INTO chat_participants (chat_id, user_id) VALUES (%s, %s)",
|
|
||||||
(chatId, uid)
|
|
||||||
)
|
|
||||||
|
|
||||||
# Генерируем профиль
|
|
||||||
photoId = None if not user.get("avatar_id") else int(user.get("avatar_id"))
|
|
||||||
avatar_url = None if not photoId else self.config.avatar_base_url + str(photoId)
|
|
||||||
|
|
||||||
contact = self.tools.generate_profile(
|
|
||||||
id=user.get("id"),
|
|
||||||
phone=int(user.get("phone")),
|
|
||||||
avatarUrl=avatar_url,
|
|
||||||
photoId=photoId,
|
|
||||||
updateTime=int(user.get("updatetime")),
|
|
||||||
firstName=user.get("firstname"),
|
|
||||||
lastName=user.get("lastname"),
|
|
||||||
options=json.loads(user.get("options")),
|
|
||||||
accountStatus=int(user.get("accountstatus")),
|
|
||||||
description=user.get("description"),
|
|
||||||
includeProfileOptions=False,
|
|
||||||
custom_firstname=firstName,
|
|
||||||
custom_lastname=lastName,
|
|
||||||
username=user.get("username"),
|
|
||||||
)
|
|
||||||
|
|
||||||
response_payload = {
|
|
||||||
"new": is_new,
|
|
||||||
"contact": contact
|
|
||||||
}
|
|
||||||
|
|
||||||
packet = self.proto.pack_packet(
|
|
||||||
cmd=self.proto.CMD_OK, seq=seq, opcode=self.opcodes.CONTACT_ADD_BY_PHONE, payload=response_payload
|
|
||||||
)
|
|
||||||
|
|
||||||
await self._send(writer, packet)
|
|
||||||
|
|
||||||
async def contact_presence(self, payload, seq, writer):
|
|
||||||
"""Обработчик получения статуса контактов"""
|
|
||||||
# Валидируем данные пакета
|
|
||||||
try:
|
|
||||||
ContactPresencePayloadModel.model_validate(payload)
|
|
||||||
except pydantic.ValidationError as error:
|
|
||||||
self.logger.error(f"Возникли ошибки при валидации пакета: {error}")
|
|
||||||
await self._send_error(seq, self.opcodes.CONTACT_PRESENCE, self.error_types.INVALID_PAYLOAD, writer)
|
|
||||||
return
|
|
||||||
|
|
||||||
contact_ids = payload.get("contactIds", [])
|
|
||||||
now_ms = int(time.time() * 1000)
|
|
||||||
|
|
||||||
presence = await self.tools.collect_presence(contact_ids, self.clients, self.db_pool)
|
|
||||||
|
|
||||||
response_payload = {
|
|
||||||
"presence": presence,
|
|
||||||
"time": now_ms
|
|
||||||
}
|
|
||||||
|
|
||||||
packet = self.proto.pack_packet(
|
|
||||||
cmd=self.proto.CMD_OK, seq=seq, opcode=self.opcodes.CONTACT_PRESENCE, payload=response_payload
|
|
||||||
)
|
|
||||||
|
|
||||||
await self._send(writer, packet)
|
|
||||||
@@ -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, CreateFolderPayloadModel
|
from oneme.models import SyncFoldersPayloadModel
|
||||||
|
|
||||||
class FoldersProcessors(BaseProcessor):
|
class FoldersProcessors(BaseProcessor):
|
||||||
async def folders_get(self, payload, seq, writer, senderPhone):
|
async def folders_get(self, payload, seq, writer, senderPhone):
|
||||||
@@ -18,32 +18,16 @@ 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 folders FROM user_data WHERE phone = %s", (int(senderPhone),))
|
||||||
"SELECT id, title, filters, `include`, options, update_time, source_id "
|
result_folders = await cursor.fetchone()
|
||||||
"FROM user_folders WHERE phone = %s ORDER BY sort_order",
|
user_folders = json.loads(result_folders.get("folders"))
|
||||||
(int(senderPhone),)
|
|
||||||
)
|
|
||||||
result_folders = await cursor.fetchall()
|
|
||||||
|
|
||||||
folders = [
|
|
||||||
{
|
|
||||||
"id": folder["id"],
|
|
||||||
"title": folder["title"],
|
|
||||||
"filters": json.loads(folder["filters"]),
|
|
||||||
"include": json.loads(folder["include"]),
|
|
||||||
"updateTime": folder["update_time"],
|
|
||||||
"options": json.loads(folder["options"]),
|
|
||||||
"sourceId": folder["source_id"]
|
|
||||||
}
|
|
||||||
for folder in result_folders
|
|
||||||
]
|
|
||||||
|
|
||||||
# Создаем данные пакета
|
# Создаем данные пакета
|
||||||
payload = {
|
payload = {
|
||||||
"folderSync": int(time.time() * 1000),
|
"folderSync": int(time.time() * 1000),
|
||||||
"folders": folders,
|
"folders": self.static.ALL_CHAT_FOLDER + user_folders.get("folders"),
|
||||||
"foldersOrder": [folder["id"] for folder in result_folders],
|
"foldersOrder": self.static.ALL_CHAT_FOLDER_ORDER + user_folders.get("foldersOrder"),
|
||||||
"allFilterExcludeFolders": []
|
"allFilterExcludeFolders": user_folders.get("allFilterExcludeFolders")
|
||||||
}
|
}
|
||||||
|
|
||||||
# Собираем пакет
|
# Собираем пакет
|
||||||
@@ -52,82 +36,4 @@ 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)
|
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
import pydantic
|
import pydantic
|
||||||
import json
|
import json
|
||||||
import time
|
|
||||||
from classes.baseprocessor import BaseProcessor
|
from classes.baseprocessor import BaseProcessor
|
||||||
from oneme.models import ChatHistoryPayloadModel
|
from oneme.models import ChatHistoryPayloadModel
|
||||||
|
|
||||||
@@ -21,21 +20,19 @@ class HistoryProcessors(BaseProcessor):
|
|||||||
backward = payload.get("backward", 0)
|
backward = payload.get("backward", 0)
|
||||||
from_time = payload.get("from", 0)
|
from_time = payload.get("from", 0)
|
||||||
getMessages = payload.get("getMessages", True)
|
getMessages = payload.get("getMessages", True)
|
||||||
getChat = payload.get("getChat", False)
|
|
||||||
messages = []
|
messages = []
|
||||||
|
|
||||||
# Если пользователь хочет получить историю из избранного,
|
# Если пользователь хочет получить историю из избранного,
|
||||||
# то выставляем в качестве ID чата отрицательный ID отправителя
|
# то выставляем в качестве ID чата его ID
|
||||||
isFavourite = chatId == (senderId ^ senderId)
|
if chatId == 0:
|
||||||
if isFavourite:
|
chatId = senderId
|
||||||
chatId = -senderId
|
|
||||||
|
|
||||||
# Проверяем, существует ли чат
|
# Проверяем, существует ли чат
|
||||||
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:
|
||||||
# Проверяем состоит ли пользователь в чате,
|
# Проверяем состоит ли пользователь в чате,
|
||||||
# только в случае того, если это не избранное
|
# только в случае того, если это не избранное
|
||||||
if not isFavourite:
|
if chatId != senderId:
|
||||||
await cursor.execute("SELECT * FROM chats WHERE id = %s", (chatId,))
|
await cursor.execute("SELECT * FROM chats WHERE id = %s", (chatId,))
|
||||||
chat = await cursor.fetchone()
|
chat = await cursor.fetchone()
|
||||||
|
|
||||||
@@ -61,8 +58,18 @@ class HistoryProcessors(BaseProcessor):
|
|||||||
result = await cursor.fetchall()
|
result = await cursor.fetchall()
|
||||||
|
|
||||||
for row in result:
|
for row in result:
|
||||||
messages.append(self.tools.build_message_dict(row, self.type))
|
# TODO: Сборку тела сообщения нужно вынести в отдельную функцию
|
||||||
backward_count = len(result)
|
messages.append({
|
||||||
|
"id": row.get("id") if self.type == 'mobile' else str(row.get('id')),
|
||||||
|
"time": int(row.get("time")),
|
||||||
|
"type": row.get("type"),
|
||||||
|
"sender": row.get("sender"),
|
||||||
|
"text": row.get("text"),
|
||||||
|
"attaches": json.loads(row.get("attaches")),
|
||||||
|
"elements": json.loads(row.get("elements")),
|
||||||
|
"reactionInfo": {}
|
||||||
|
})
|
||||||
|
|
||||||
if forward > 0:
|
if forward > 0:
|
||||||
await cursor.execute(
|
await cursor.execute(
|
||||||
"SELECT * FROM messages WHERE chat_id = %s AND time > %s ORDER BY time ASC LIMIT %s",
|
"SELECT * FROM messages WHERE chat_id = %s AND time > %s ORDER BY time ASC LIMIT %s",
|
||||||
@@ -72,19 +79,25 @@ class HistoryProcessors(BaseProcessor):
|
|||||||
result = await cursor.fetchall()
|
result = await cursor.fetchall()
|
||||||
|
|
||||||
for row in result:
|
for row in result:
|
||||||
messages.append(self.tools.build_message_dict(row, self.type))
|
messages.append({
|
||||||
forward_count = len(result)
|
"id": row.get("id"),
|
||||||
|
"time": int(row.get("time")),
|
||||||
|
"type": row.get("type"),
|
||||||
|
"sender": row.get("sender"),
|
||||||
|
"text": row.get("text"),
|
||||||
|
"attaches": json.loads(row.get("attaches")),
|
||||||
|
"elements": json.loads(row.get("elements")),
|
||||||
|
"reactionInfo": {}
|
||||||
|
})
|
||||||
|
|
||||||
# Сортируем сообщения по времени
|
# Сортируем сообщения по времени
|
||||||
messages.sort(key=lambda x: x["time"])
|
messages.sort(key=lambda x: x["time"])
|
||||||
|
|
||||||
|
# Формируем ответ
|
||||||
payload = {
|
payload = {
|
||||||
"messages": messages
|
"messages": messages
|
||||||
}
|
}
|
||||||
|
|
||||||
if getChat:
|
|
||||||
payload["chat"] = {}
|
|
||||||
|
|
||||||
# Собираем пакет
|
# Собираем пакет
|
||||||
packet = self.proto.pack_packet(
|
packet = self.proto.pack_packet(
|
||||||
cmd=self.proto.CMD_OK, seq=seq, opcode=self.opcodes.CHAT_HISTORY, payload=payload
|
cmd=self.proto.CMD_OK, seq=seq, opcode=self.opcodes.CHAT_HISTORY, payload=payload
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
import pydantic
|
import pydantic
|
||||||
import json
|
import json
|
||||||
import time
|
|
||||||
from classes.baseprocessor import BaseProcessor
|
from classes.baseprocessor import BaseProcessor
|
||||||
from oneme.models import (
|
from oneme.models import (
|
||||||
HelloPayloadModel,
|
HelloPayloadModel,
|
||||||
PingPayloadModel,
|
PingPayloadModel,
|
||||||
UpdateProfilePayloadModel
|
UpdateProfilePayloadModel
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -21,17 +20,14 @@ class MainProcessors(BaseProcessor):
|
|||||||
except pydantic.ValidationError as error:
|
except pydantic.ValidationError as error:
|
||||||
self.logger.error(f"Возникли ошибки при валидации пакета: {error}")
|
self.logger.error(f"Возникли ошибки при валидации пакета: {error}")
|
||||||
await self._send_error(seq, self.opcodes.SESSION_INIT, self.error_types.INVALID_PAYLOAD, writer)
|
await self._send_error(seq, self.opcodes.SESSION_INIT, self.error_types.INVALID_PAYLOAD, writer)
|
||||||
return None, None, None
|
return None, None
|
||||||
|
|
||||||
# Получаем данные из пакета
|
# Получаем данные из пакета
|
||||||
userAgent = payload.get("userAgent")
|
deviceType = payload.get("userAgent").get("deviceType")
|
||||||
deviceType = userAgent.get("deviceType")
|
deviceName = payload.get("userAgent").get("deviceName")
|
||||||
deviceName = userAgent.get("deviceName")
|
|
||||||
appVersion = userAgent.get("appVersion")
|
|
||||||
|
|
||||||
# Данные пакета
|
# Данные пакета
|
||||||
payload = {
|
payload = {
|
||||||
"callsSeed": int(time.time() * 1000),
|
|
||||||
"location": "RU",
|
"location": "RU",
|
||||||
"app-update-type": 0, # 1 = принудительное обновление
|
"app-update-type": 0, # 1 = принудительное обновление
|
||||||
"reg-country-code": self.static.REG_COUNTRY_CODES,
|
"reg-country-code": self.static.REG_COUNTRY_CODES,
|
||||||
@@ -47,9 +43,9 @@ class MainProcessors(BaseProcessor):
|
|||||||
|
|
||||||
# Отправляем
|
# Отправляем
|
||||||
await self._send(writer, packet)
|
await self._send(writer, packet)
|
||||||
return deviceType, deviceName, appVersion
|
return deviceType, deviceName
|
||||||
|
|
||||||
async def ping(self, payload, seq, writer, userId=None):
|
async def ping(self, payload, seq, writer):
|
||||||
"""Обработчик пинга"""
|
"""Обработчик пинга"""
|
||||||
# Валидируем данные пакета
|
# Валидируем данные пакета
|
||||||
try:
|
try:
|
||||||
@@ -59,58 +55,6 @@ class MainProcessors(BaseProcessor):
|
|||||||
await self._send_error(seq, self.opcodes.PING, self.error_types.INVALID_PAYLOAD, writer)
|
await self._send_error(seq, self.opcodes.PING, self.error_types.INVALID_PAYLOAD, writer)
|
||||||
return
|
return
|
||||||
|
|
||||||
# Обновляем статус пользователя, если он авторизован
|
|
||||||
# и в пакете отправлен интерактив
|
|
||||||
interactive = payload.get("interactive") if payload else None
|
|
||||||
if userId and interactive is not None:
|
|
||||||
now = int(time.time())
|
|
||||||
user = self.clients.get(userId)
|
|
||||||
if user:
|
|
||||||
if interactive:
|
|
||||||
user["status"] = 2
|
|
||||||
user["last_seen"] = now
|
|
||||||
else:
|
|
||||||
user["status"] = 0
|
|
||||||
user["last_seen"] = now
|
|
||||||
|
|
||||||
# Сохраняем последнее время посещения
|
|
||||||
async with self.db_pool.acquire() as conn:
|
|
||||||
async with conn.cursor() as cursor:
|
|
||||||
await cursor.execute(
|
|
||||||
"UPDATE users SET lastseen = %s WHERE id = %s",
|
|
||||||
(str(now), userId)
|
|
||||||
)
|
|
||||||
|
|
||||||
# Рассылаем статус контактам пользователя
|
|
||||||
now_ms = int(time.time() * 1000)
|
|
||||||
if interactive:
|
|
||||||
presence_data = {"on": "ON", "seen": now, "status": 1}
|
|
||||||
else:
|
|
||||||
presence_data = {"seen": now}
|
|
||||||
|
|
||||||
# Находим всех, у кого этот пользователь в контактах
|
|
||||||
async with self.db_pool.acquire() as conn2:
|
|
||||||
async with conn2.cursor() as cursor2:
|
|
||||||
await cursor2.execute(
|
|
||||||
"SELECT owner_id FROM contacts WHERE contact_id = %s",
|
|
||||||
(userId,)
|
|
||||||
)
|
|
||||||
contact_owners = await cursor2.fetchall()
|
|
||||||
|
|
||||||
# Рассылаем
|
|
||||||
for row in contact_owners:
|
|
||||||
owner_id = int(row.get("owner_id"))
|
|
||||||
if owner_id in self.clients:
|
|
||||||
await self.event(
|
|
||||||
owner_id,
|
|
||||||
{
|
|
||||||
"eventType": "presence",
|
|
||||||
"userId": userId,
|
|
||||||
"presence": presence_data,
|
|
||||||
"time": now_ms,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
# Собираем пакет
|
# Собираем пакет
|
||||||
response = self.proto.pack_packet(
|
response = self.proto.pack_packet(
|
||||||
cmd=self.proto.CMD_OK, seq=seq, opcode=self.opcodes.PING, payload=None
|
cmd=self.proto.CMD_OK, seq=seq, opcode=self.opcodes.PING, payload=None
|
||||||
@@ -153,7 +97,7 @@ class MainProcessors(BaseProcessor):
|
|||||||
|
|
||||||
# Аватарка с биографией
|
# Аватарка с биографией
|
||||||
photoId = None if not user.get("avatar_id") else int(user.get("avatar_id"))
|
photoId = None if not user.get("avatar_id") else int(user.get("avatar_id"))
|
||||||
avatar_url = None if not photoId else self.config.avatar_base_url + str(photoId)
|
avatar_url = None if not photoId else self.config.avatar_base_url + photoId
|
||||||
description = None if not user.get("description") else user.get("description")
|
description = None if not user.get("description") else user.get("description")
|
||||||
|
|
||||||
# Генерируем профиль
|
# Генерируем профиль
|
||||||
@@ -183,62 +127,5 @@ class MainProcessors(BaseProcessor):
|
|||||||
cmd=self.proto.CMD_OK, seq=seq, opcode=self.opcodes.PROFILE, payload=payload
|
cmd=self.proto.CMD_OK, seq=seq, opcode=self.opcodes.PROFILE, payload=payload
|
||||||
)
|
)
|
||||||
|
|
||||||
# Отправляем
|
|
||||||
await self._send(writer, response)
|
|
||||||
|
|
||||||
async def update_config(self, payload, seq, writer, userPhone, hashedToken=None):
|
|
||||||
"""
|
|
||||||
Обработчик 22 опкода (config)
|
|
||||||
Он отвечает за обновление настроек приватности
|
|
||||||
и пуш токена для пушей
|
|
||||||
"""
|
|
||||||
# Пейлоад, который отдадим клиенту
|
|
||||||
# а отдавать его нужно только при изменении настроек приватности
|
|
||||||
result_payload = None
|
|
||||||
|
|
||||||
if payload.get("pushToken"):
|
|
||||||
push_token = payload.get("pushToken")
|
|
||||||
async with self.db_pool.acquire() as conn:
|
|
||||||
async with conn.cursor() as cursor:
|
|
||||||
await cursor.execute(
|
|
||||||
"UPDATE tokens SET push_token = %s WHERE phone = %s AND token_hash = %s",
|
|
||||||
(push_token, str(userPhone), hashedToken)
|
|
||||||
)
|
|
||||||
elif payload.get("settings") and payload.get("settings").get("user"):
|
|
||||||
"""Обновление настроек приватности"""
|
|
||||||
new_settings = payload.get("settings").get("user")
|
|
||||||
|
|
||||||
async with self.db_pool.acquire() as conn:
|
|
||||||
async with conn.cursor() as cursor:
|
|
||||||
# Получаем текущий конфиг
|
|
||||||
await cursor.execute(
|
|
||||||
"SELECT user_config FROM user_data WHERE phone = %s", (userPhone,)
|
|
||||||
)
|
|
||||||
row = await cursor.fetchone()
|
|
||||||
|
|
||||||
if row:
|
|
||||||
current_config = json.loads(row.get("user_config"))
|
|
||||||
|
|
||||||
# Обновляем настройки
|
|
||||||
for key, value in new_settings.items():
|
|
||||||
if key in current_config:
|
|
||||||
current_config[key] = value
|
|
||||||
|
|
||||||
# Сохраняем обновлённый конфиг
|
|
||||||
await cursor.execute(
|
|
||||||
"UPDATE user_data SET user_config = %s WHERE phone = %s",
|
|
||||||
(json.dumps(current_config), userPhone)
|
|
||||||
)
|
|
||||||
|
|
||||||
result_payload = {
|
|
||||||
"user": current_config,
|
|
||||||
"hash": "0"
|
|
||||||
}
|
|
||||||
|
|
||||||
# Собираем пакет
|
|
||||||
response = self.proto.pack_packet(
|
|
||||||
cmd=self.proto.CMD_OK, seq=seq, opcode=self.opcodes.CONFIG, payload=result_payload
|
|
||||||
)
|
|
||||||
|
|
||||||
# Отправляем
|
# Отправляем
|
||||||
await self._send(writer, response)
|
await self._send(writer, response)
|
||||||
@@ -49,8 +49,7 @@ class MessagesProcessors(BaseProcessor):
|
|||||||
"eventType": "typing",
|
"eventType": "typing",
|
||||||
"chatId": chatId,
|
"chatId": chatId,
|
||||||
"type": type,
|
"type": type,
|
||||||
"userId": senderId,
|
"userId": senderId
|
||||||
"writer": writer,
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -88,9 +87,10 @@ class MessagesProcessors(BaseProcessor):
|
|||||||
chatId = userId ^ senderId
|
chatId = userId ^ senderId
|
||||||
|
|
||||||
# Если клиент хочет отправить сообщение в избранное,
|
# Если клиент хочет отправить сообщение в избранное,
|
||||||
# то выставляем в качестве ID чата отрицательный ID отправителя
|
# то выставляем в качестве ID чата ID отправителя
|
||||||
if chatId == (senderId ^ senderId):
|
# (А ещё используем это, если клиент вообще ничего не указал)
|
||||||
chatId = -senderId
|
if chatId == 0 or not chatId:
|
||||||
|
chatId = senderId
|
||||||
participants = [senderId]
|
participants = [senderId]
|
||||||
else:
|
else:
|
||||||
# Если все таки клиент хочет отправить сообщение в нормальный чат,
|
# Если все таки клиент хочет отправить сообщение в нормальный чат,
|
||||||
@@ -113,14 +113,6 @@ class MessagesProcessors(BaseProcessor):
|
|||||||
await self._send_error(seq, self.opcodes.MSG_SEND, self.error_types.CHAT_NOT_ACCESS, writer)
|
await self._send_error(seq, self.opcodes.MSG_SEND, self.error_types.CHAT_NOT_ACCESS, writer)
|
||||||
return
|
return
|
||||||
|
|
||||||
# Проверяем блокировку собеседника
|
|
||||||
if chat.get("type") == "DIALOG":
|
|
||||||
contactid = [p for p in participants if p != int(senderId)][0]
|
|
||||||
# Проверяем, заблокировал ли отправитель собеседника
|
|
||||||
if await self.tools.contact_is_blocked(contactid, senderId, db_pool):
|
|
||||||
await self._send_error(seq, self.opcodes.MSG_SEND, self.error_types.CONTACT_BLOCKED, writer)
|
|
||||||
return
|
|
||||||
|
|
||||||
# Добавляем сообщение в историю
|
# Добавляем сообщение в историю
|
||||||
messageId, lastMessageId, messageTime = await self.tools.insert_message(
|
messageId, lastMessageId, messageTime = await self.tools.insert_message(
|
||||||
chatId=chatId,
|
chatId=chatId,
|
||||||
@@ -133,21 +125,16 @@ class MessagesProcessors(BaseProcessor):
|
|||||||
db_pool=self.db_pool
|
db_pool=self.db_pool
|
||||||
)
|
)
|
||||||
|
|
||||||
# Готовое тело сообщения. Поля cid / elements / reactionInfo / link
|
# Готовое тело сообщения
|
||||||
# должны присутствовать ВСЕГДА (даже пустые) — десктопный MAX
|
|
||||||
# ожидает фиксированную msgpack-схему и обрывает соединение
|
|
||||||
# при отсутствии любого из них (см. регрессию из 87cfc19).
|
|
||||||
bodyMessage = {
|
bodyMessage = {
|
||||||
"id": messageId if self.type == "mobile" else str(messageId),
|
"id": messageId,
|
||||||
"cid": int(cid or 0),
|
|
||||||
"time": messageTime,
|
"time": messageTime,
|
||||||
"type": "USER",
|
"type": "USER",
|
||||||
"sender": senderId,
|
"sender": senderId,
|
||||||
|
"cid": cid,
|
||||||
"text": text,
|
"text": text,
|
||||||
"attaches": attaches if isinstance(attaches, list) else [],
|
"attaches": attaches,
|
||||||
"elements": elements if isinstance(elements, list) else [],
|
"elements": elements
|
||||||
"reactionInfo": {},
|
|
||||||
"link": {},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Отправляем событие всем участникам чата
|
# Отправляем событие всем участникам чата
|
||||||
@@ -156,7 +143,7 @@ class MessagesProcessors(BaseProcessor):
|
|||||||
participant,
|
participant,
|
||||||
{
|
{
|
||||||
"eventType": "new_msg",
|
"eventType": "new_msg",
|
||||||
"chatId": 0 if chatId == -senderId else chatId,
|
"chatId": 0 if chatId == senderId else chatId,
|
||||||
"message": bodyMessage,
|
"message": bodyMessage,
|
||||||
"prevMessageId": lastMessageId,
|
"prevMessageId": lastMessageId,
|
||||||
"time": messageTime,
|
"time": messageTime,
|
||||||
@@ -166,7 +153,7 @@ class MessagesProcessors(BaseProcessor):
|
|||||||
|
|
||||||
# Данные пакета
|
# Данные пакета
|
||||||
payload = {
|
payload = {
|
||||||
"chatId": 0 if chatId == -senderId else chatId,
|
"chatId": 0 if chatId == senderId else chatId,
|
||||||
"message": bodyMessage,
|
"message": bodyMessage,
|
||||||
"unread": 0,
|
"unread": 0,
|
||||||
"mark": messageTime
|
"mark": messageTime
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ from oneme.models import (
|
|||||||
|
|
||||||
|
|
||||||
class SearchProcessors(BaseProcessor):
|
class SearchProcessors(BaseProcessor):
|
||||||
async def contact_info(self, payload, seq, writer, senderId):
|
async def contact_info(self, payload, seq, writer):
|
||||||
"""Поиск пользователей по ID"""
|
"""Поиск пользователей по ID"""
|
||||||
# Валидируем данные пакета
|
# Валидируем данные пакета
|
||||||
try:
|
try:
|
||||||
@@ -36,19 +36,9 @@ class SearchProcessors(BaseProcessor):
|
|||||||
if user:
|
if user:
|
||||||
# Аватарка с биографией
|
# Аватарка с биографией
|
||||||
photoId = None if not user.get("avatar_id") else int(user.get("avatar_id"))
|
photoId = None if not user.get("avatar_id") else int(user.get("avatar_id"))
|
||||||
avatar_url = None if not photoId else self.config.avatar_base_url + str(photoId)
|
avatar_url = None if not photoId else self.config.avatar_base_url + photoId
|
||||||
description = None if not user.get("description") else user.get("description")
|
description = None if not user.get("description") else user.get("description")
|
||||||
|
|
||||||
# Получаем данные контакта
|
|
||||||
await cursor.execute(
|
|
||||||
"SELECT * FROM contacts WHERE owner_id = %s AND contact_id = %s",
|
|
||||||
(senderId, contactId),
|
|
||||||
)
|
|
||||||
contact_row = await cursor.fetchone()
|
|
||||||
custom_firstname = contact_row.get("custom_firstname") if contact_row else None
|
|
||||||
custom_lastname = contact_row.get("custom_lastname") if contact_row else None
|
|
||||||
blocked = bool(contact_row.get("is_blocked")) if contact_row else False
|
|
||||||
|
|
||||||
# Генерируем профиль
|
# Генерируем профиль
|
||||||
users.append(
|
users.append(
|
||||||
self.tools.generate_profile(
|
self.tools.generate_profile(
|
||||||
@@ -64,10 +54,7 @@ class SearchProcessors(BaseProcessor):
|
|||||||
accountStatus=int(user.get("accountstatus")),
|
accountStatus=int(user.get("accountstatus")),
|
||||||
profileOptions=json.loads(user.get("profileoptions")),
|
profileOptions=json.loads(user.get("profileoptions")),
|
||||||
includeProfileOptions=False,
|
includeProfileOptions=False,
|
||||||
username=user.get("username"),
|
username=user.get("username")
|
||||||
custom_firstname=custom_firstname,
|
|
||||||
custom_lastname=custom_lastname,
|
|
||||||
blocked=blocked,
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -129,21 +116,9 @@ class SearchProcessors(BaseProcessor):
|
|||||||
|
|
||||||
# Аватарка с биографией
|
# Аватарка с биографией
|
||||||
photoId = None if not user.get("avatar_id") else int(user.get("avatar_id"))
|
photoId = None if not user.get("avatar_id") else int(user.get("avatar_id"))
|
||||||
avatar_url = None if not photoId else self.config.avatar_base_url + str(photoId)
|
avatar_url = None if not photoId else self.config.avatar_base_url + photoId
|
||||||
description = None if not user.get("description") else user.get("description")
|
description = None if not user.get("description") else user.get("description")
|
||||||
|
|
||||||
# Получаем данные контакта
|
|
||||||
async with self.db_pool.acquire() as conn:
|
|
||||||
async with conn.cursor() as cursor:
|
|
||||||
await cursor.execute(
|
|
||||||
"SELECT * FROM contacts WHERE owner_id = %s AND contact_id = %s",
|
|
||||||
(senderId, user.get("id")),
|
|
||||||
)
|
|
||||||
contact_row = await cursor.fetchone()
|
|
||||||
custom_firstname = contact_row.get("custom_firstname") if contact_row else None
|
|
||||||
custom_lastname = contact_row.get("custom_lastname") if contact_row else None
|
|
||||||
blocked = bool(contact_row.get("is_blocked")) if contact_row else False
|
|
||||||
|
|
||||||
# Генерируем профиль
|
# Генерируем профиль
|
||||||
profile = self.tools.generate_profile(
|
profile = self.tools.generate_profile(
|
||||||
id=user.get("id"),
|
id=user.get("id"),
|
||||||
@@ -158,10 +133,7 @@ class SearchProcessors(BaseProcessor):
|
|||||||
accountStatus=int(user.get("accountstatus")),
|
accountStatus=int(user.get("accountstatus")),
|
||||||
profileOptions=json.loads(user.get("profileoptions")),
|
profileOptions=json.loads(user.get("profileoptions")),
|
||||||
includeProfileOptions=False,
|
includeProfileOptions=False,
|
||||||
username=user.get("username"),
|
username=user.get("username")
|
||||||
custom_firstname=custom_firstname,
|
|
||||||
custom_lastname=custom_lastname,
|
|
||||||
blocked=blocked,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# Создаем данные пакета
|
# Создаем данные пакета
|
||||||
@@ -223,12 +195,12 @@ class SearchProcessors(BaseProcessor):
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
# Получаем последнее сообщение из избранного
|
# Получаем последнее сообщение из чата
|
||||||
message, messageTime = await self.tools.get_last_message(
|
message, messageTime = await self.tools.get_last_message(
|
||||||
-senderId, self.db_pool, protocol_type=self.type
|
senderId, self.db_pool, protocol_type=self.type
|
||||||
)
|
)
|
||||||
|
|
||||||
# ID избранного для клиента
|
# ID избранного
|
||||||
chatId = senderId ^ senderId
|
chatId = senderId ^ senderId
|
||||||
|
|
||||||
# Добавляем чат в список
|
# Добавляем чат в список
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
import logging
|
import logging
|
||||||
import time
|
|
||||||
import traceback
|
import traceback
|
||||||
|
|
||||||
from common.opcodes import Opcodes
|
from common.opcodes import Opcodes
|
||||||
@@ -33,7 +32,7 @@ class OnemeMobile:
|
|||||||
self.opcodes = Opcodes()
|
self.opcodes = Opcodes()
|
||||||
|
|
||||||
# rate limiter anti ddos brute force protection
|
# rate limiter anti ddos brute force protection
|
||||||
self.auth_rate_limiter = RateLimiter(max_attempts=15, window_seconds=60)
|
self.auth_rate_limiter = RateLimiter(max_attempts=5, window_seconds=60)
|
||||||
|
|
||||||
self.read_timeout = 300 # Таймаут чтения из сокета (секунды)
|
self.read_timeout = 300 # Таймаут чтения из сокета (секунды)
|
||||||
self.max_read_size = 65536 # Максимальный размер данных из сокета
|
self.max_read_size = 65536 # Максимальный размер данных из сокета
|
||||||
@@ -46,7 +45,6 @@ class OnemeMobile:
|
|||||||
|
|
||||||
deviceType = None
|
deviceType = None
|
||||||
deviceName = None
|
deviceName = None
|
||||||
appVersion = None
|
|
||||||
|
|
||||||
userPhone = None
|
userPhone = None
|
||||||
userId = None
|
userId = None
|
||||||
@@ -91,7 +89,7 @@ class OnemeMobile:
|
|||||||
|
|
||||||
match opcode:
|
match opcode:
|
||||||
case self.opcodes.SESSION_INIT:
|
case self.opcodes.SESSION_INIT:
|
||||||
deviceType, deviceName, appVersion = await self.processors.session_init(
|
deviceType, deviceName = await self.processors.session_init(
|
||||||
payload, seq, writer
|
payload, seq, writer
|
||||||
)
|
)
|
||||||
case self.opcodes.AUTH_REQUEST:
|
case self.opcodes.AUTH_REQUEST:
|
||||||
@@ -114,7 +112,7 @@ class OnemeMobile:
|
|||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
await self.processors.auth(
|
await self.processors.auth(
|
||||||
payload, seq, writer, deviceType, deviceName, appVersion, address[0]
|
payload, seq, writer, deviceType, deviceName
|
||||||
)
|
)
|
||||||
case self.opcodes.AUTH_CONFIRM:
|
case self.opcodes.AUTH_CONFIRM:
|
||||||
if not self.auth_rate_limiter.is_allowed(address[0]):
|
if not self.auth_rate_limiter.is_allowed(address[0]):
|
||||||
@@ -126,7 +124,7 @@ class OnemeMobile:
|
|||||||
)
|
)
|
||||||
elif payload and payload.get("tokenType") == "REGISTER":
|
elif payload and payload.get("tokenType") == "REGISTER":
|
||||||
await self.processors.auth_confirm(
|
await self.processors.auth_confirm(
|
||||||
payload, seq, writer, deviceType, deviceName, appVersion, address[0]
|
payload, seq, writer, deviceType, deviceName
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
self.logger.warning(
|
self.logger.warning(
|
||||||
@@ -145,7 +143,7 @@ class OnemeMobile:
|
|||||||
userPhone,
|
userPhone,
|
||||||
userId,
|
userId,
|
||||||
hashedToken,
|
hashedToken,
|
||||||
) = await self.processors.login(payload, seq, writer, appVersion)
|
) = await self.processors.login(payload, seq, writer)
|
||||||
|
|
||||||
if userPhone:
|
if userPhone:
|
||||||
await self._finish_auth(
|
await self._finish_auth(
|
||||||
@@ -157,7 +155,7 @@ class OnemeMobile:
|
|||||||
)
|
)
|
||||||
break
|
break
|
||||||
case self.opcodes.PING:
|
case self.opcodes.PING:
|
||||||
await self.processors.ping(payload, seq, writer, userId)
|
await self.processors.ping(payload, seq, writer)
|
||||||
case self.opcodes.LOG:
|
case self.opcodes.LOG:
|
||||||
await self.processors.log(payload, seq, writer)
|
await self.processors.log(payload, seq, writer)
|
||||||
case self.opcodes.ASSETS_UPDATE:
|
case self.opcodes.ASSETS_UPDATE:
|
||||||
@@ -168,54 +166,6 @@ class OnemeMobile:
|
|||||||
seq,
|
seq,
|
||||||
writer,
|
writer,
|
||||||
)
|
)
|
||||||
case self.opcodes.ASSETS_GET:
|
|
||||||
await self.auth_required(
|
|
||||||
userPhone,
|
|
||||||
self.processors.assets_get,
|
|
||||||
payload,
|
|
||||||
seq,
|
|
||||||
writer,
|
|
||||||
)
|
|
||||||
case self.opcodes.ASSETS_GET_BY_IDS:
|
|
||||||
await self.auth_required(
|
|
||||||
userPhone,
|
|
||||||
self.processors.assets_get_by_ids,
|
|
||||||
payload,
|
|
||||||
seq,
|
|
||||||
writer,
|
|
||||||
)
|
|
||||||
case self.opcodes.ASSETS_ADD:
|
|
||||||
await self.auth_required(
|
|
||||||
userPhone,
|
|
||||||
self.processors.assets_add,
|
|
||||||
payload,
|
|
||||||
seq,
|
|
||||||
writer,
|
|
||||||
)
|
|
||||||
case self.opcodes.ASSETS_REMOVE:
|
|
||||||
await self.auth_required(
|
|
||||||
userPhone,
|
|
||||||
self.processors.assets_remove,
|
|
||||||
payload,
|
|
||||||
seq,
|
|
||||||
writer,
|
|
||||||
)
|
|
||||||
case self.opcodes.ASSETS_MOVE:
|
|
||||||
await self.auth_required(
|
|
||||||
userPhone,
|
|
||||||
self.processors.assets_move,
|
|
||||||
payload,
|
|
||||||
seq,
|
|
||||||
writer,
|
|
||||||
)
|
|
||||||
case self.opcodes.ASSETS_LIST_MODIFY:
|
|
||||||
await self.auth_required(
|
|
||||||
userPhone,
|
|
||||||
self.processors.assets_list_modify,
|
|
||||||
payload,
|
|
||||||
seq,
|
|
||||||
writer,
|
|
||||||
)
|
|
||||||
case self.opcodes.VIDEO_CHAT_HISTORY:
|
case self.opcodes.VIDEO_CHAT_HISTORY:
|
||||||
await self.auth_required(
|
await self.auth_required(
|
||||||
userPhone,
|
userPhone,
|
||||||
@@ -243,15 +193,6 @@ 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,
|
||||||
@@ -309,16 +250,6 @@ class OnemeMobile:
|
|||||||
payload,
|
payload,
|
||||||
seq,
|
seq,
|
||||||
writer,
|
writer,
|
||||||
userId,
|
|
||||||
)
|
|
||||||
case self.opcodes.CONTACT_LIST:
|
|
||||||
await self.auth_required(
|
|
||||||
userPhone,
|
|
||||||
self.processors.contact_list,
|
|
||||||
payload,
|
|
||||||
seq,
|
|
||||||
writer,
|
|
||||||
userId,
|
|
||||||
)
|
)
|
||||||
case self.opcodes.COMPLAIN_REASONS_GET:
|
case self.opcodes.COMPLAIN_REASONS_GET:
|
||||||
await self.auth_required(
|
await self.auth_required(
|
||||||
@@ -340,42 +271,6 @@ class OnemeMobile:
|
|||||||
seq,
|
seq,
|
||||||
writer,
|
writer,
|
||||||
)
|
)
|
||||||
case self.opcodes.CONFIG:
|
|
||||||
await self.auth_required(
|
|
||||||
userPhone,
|
|
||||||
self.processors.update_config,
|
|
||||||
payload,
|
|
||||||
seq,
|
|
||||||
writer,
|
|
||||||
userPhone,
|
|
||||||
hashedToken,
|
|
||||||
)
|
|
||||||
case self.opcodes.CONTACT_UPDATE:
|
|
||||||
await self.auth_required(
|
|
||||||
userPhone,
|
|
||||||
self.processors.contact_update,
|
|
||||||
payload,
|
|
||||||
seq,
|
|
||||||
writer,
|
|
||||||
userId,
|
|
||||||
)
|
|
||||||
case self.opcodes.CONTACT_ADD_BY_PHONE:
|
|
||||||
await self.auth_required(
|
|
||||||
userPhone,
|
|
||||||
self.processors.contact_add_by_phone,
|
|
||||||
payload,
|
|
||||||
seq,
|
|
||||||
writer,
|
|
||||||
userId,
|
|
||||||
)
|
|
||||||
case self.opcodes.CONTACT_PRESENCE:
|
|
||||||
await self.auth_required(
|
|
||||||
userPhone,
|
|
||||||
self.processors.contact_presence,
|
|
||||||
payload,
|
|
||||||
seq,
|
|
||||||
writer
|
|
||||||
)
|
|
||||||
case _:
|
case _:
|
||||||
self.logger.warning(f"Неизвестный опкод {opcode}")
|
self.logger.warning(f"Неизвестный опкод {opcode}")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@@ -407,8 +302,6 @@ class OnemeMobile:
|
|||||||
self.clients[id] = {
|
self.clients[id] = {
|
||||||
"phone": phone,
|
"phone": phone,
|
||||||
"id": id,
|
"id": id,
|
||||||
"status": 2,
|
|
||||||
"last_seen": 0,
|
|
||||||
"clients": [
|
"clients": [
|
||||||
{
|
{
|
||||||
"writer": writer,
|
"writer": writer,
|
||||||
@@ -419,38 +312,6 @@ class OnemeMobile:
|
|||||||
],
|
],
|
||||||
}
|
}
|
||||||
|
|
||||||
await self._broadcast_presence(id, True)
|
|
||||||
|
|
||||||
async def _broadcast_presence(self, userId, online):
|
|
||||||
now = int(time.time())
|
|
||||||
now_ms = int(time.time() * 1000)
|
|
||||||
|
|
||||||
if online:
|
|
||||||
presence_data = {"on": "ON", "seen": now, "status": 1}
|
|
||||||
else:
|
|
||||||
presence_data = {"seen": now}
|
|
||||||
|
|
||||||
async with self.db_pool.acquire() as conn:
|
|
||||||
async with conn.cursor() as cursor:
|
|
||||||
await cursor.execute(
|
|
||||||
"SELECT owner_id FROM contacts WHERE contact_id = %s",
|
|
||||||
(userId,)
|
|
||||||
)
|
|
||||||
contact_owners = await cursor.fetchall()
|
|
||||||
|
|
||||||
for row in contact_owners:
|
|
||||||
owner_id = int(row.get("owner_id"))
|
|
||||||
if owner_id in self.clients:
|
|
||||||
await self.processors.event(
|
|
||||||
owner_id,
|
|
||||||
{
|
|
||||||
"eventType": "presence",
|
|
||||||
"userId": userId,
|
|
||||||
"presence": presence_data,
|
|
||||||
"time": now_ms,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
async def _end_session(self, id, ip, port):
|
async def _end_session(self, id, ip, port):
|
||||||
"""Завершение сессии"""
|
"""Завершение сессии"""
|
||||||
# Получаем пользователя в списке
|
# Получаем пользователя в списке
|
||||||
@@ -466,20 +327,6 @@ class OnemeMobile:
|
|||||||
if (client.get("ip"), client.get("port")) == (ip, port):
|
if (client.get("ip"), client.get("port")) == (ip, port):
|
||||||
clients.pop(i)
|
clients.pop(i)
|
||||||
|
|
||||||
if not clients:
|
|
||||||
now = int(time.time())
|
|
||||||
user["status"] = 0
|
|
||||||
user["last_seen"] = now
|
|
||||||
|
|
||||||
async with self.db_pool.acquire() as conn:
|
|
||||||
async with conn.cursor() as cursor:
|
|
||||||
await cursor.execute(
|
|
||||||
"UPDATE users SET lastseen = %s WHERE id = %s",
|
|
||||||
(str(now), id)
|
|
||||||
)
|
|
||||||
|
|
||||||
await self._broadcast_presence(id, False)
|
|
||||||
|
|
||||||
async def start(self):
|
async def start(self):
|
||||||
"""Функция для запуска сервера"""
|
"""Функция для запуска сервера"""
|
||||||
self.server = await asyncio.start_server(
|
self.server = await asyncio.start_server(
|
||||||
@@ -488,9 +335,5 @@ class OnemeMobile:
|
|||||||
|
|
||||||
self.logger.info(f"Сокет запущен на порту {self.port}")
|
self.logger.info(f"Сокет запущен на порту {self.port}")
|
||||||
|
|
||||||
try:
|
async with self.server:
|
||||||
async with self.server:
|
await self.server.serve_forever()
|
||||||
await self.server.serve_forever()
|
|
||||||
except asyncio.CancelledError:
|
|
||||||
self.server.close()
|
|
||||||
await self.server.wait_closed()
|
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
import logging
|
import logging
|
||||||
import time
|
|
||||||
import traceback
|
import traceback
|
||||||
import websockets
|
import websockets
|
||||||
import asyncio
|
|
||||||
from common.proto_web import WebProto
|
from common.proto_web import WebProto
|
||||||
from oneme.processors import Processors
|
from oneme.processors import Processors
|
||||||
from common.rate_limiter import RateLimiter
|
from common.rate_limiter import RateLimiter
|
||||||
@@ -10,9 +8,7 @@ from common.opcodes import Opcodes
|
|||||||
from common.tools import Tools
|
from common.tools import Tools
|
||||||
|
|
||||||
class OnemeWS:
|
class OnemeWS:
|
||||||
def __init__(
|
def __init__(self, host, port, clients, ssl_context, db_pool, send_event):
|
||||||
self, host, port, ssl_context, db_pool, clients, send_event, telegram_bot
|
|
||||||
):
|
|
||||||
self.host = host
|
self.host = host
|
||||||
self.port = port
|
self.port = port
|
||||||
self.ssl_context = ssl_context
|
self.ssl_context = ssl_context
|
||||||
@@ -24,13 +20,7 @@ class OnemeWS:
|
|||||||
self.opcodes = Opcodes()
|
self.opcodes = Opcodes()
|
||||||
|
|
||||||
self.proto = WebProto()
|
self.proto = WebProto()
|
||||||
self.processors = Processors(
|
self.processors = Processors(db_pool=db_pool, clients=clients, send_event=send_event, type="web")
|
||||||
db_pool=db_pool,
|
|
||||||
clients=clients,
|
|
||||||
send_event=send_event,
|
|
||||||
telegram_bot=telegram_bot,
|
|
||||||
type="web"
|
|
||||||
)
|
|
||||||
self.auth_required = Tools().auth_required
|
self.auth_required = Tools().auth_required
|
||||||
|
|
||||||
# rate limiter
|
# rate limiter
|
||||||
@@ -47,7 +37,6 @@ class OnemeWS:
|
|||||||
|
|
||||||
deviceType = None
|
deviceType = None
|
||||||
deviceName = None
|
deviceName = None
|
||||||
appVersion = None
|
|
||||||
|
|
||||||
userPhone = None
|
userPhone = None
|
||||||
userId = None
|
userId = None
|
||||||
@@ -74,7 +63,7 @@ class OnemeWS:
|
|||||||
|
|
||||||
match opcode:
|
match opcode:
|
||||||
case self.opcodes.SESSION_INIT:
|
case self.opcodes.SESSION_INIT:
|
||||||
deviceType, deviceName, appVersion = await self.processors.session_init(
|
deviceType, deviceName = await self.processors.session_init(
|
||||||
payload, seq, websocket
|
payload, seq, websocket
|
||||||
)
|
)
|
||||||
case self.opcodes.AUTH_REQUEST:
|
case self.opcodes.AUTH_REQUEST:
|
||||||
@@ -97,7 +86,7 @@ class OnemeWS:
|
|||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
await self.processors.auth(
|
await self.processors.auth(
|
||||||
payload, seq, websocket, deviceType, deviceName, appVersion, address[0]
|
payload, seq, websocket, deviceType, deviceName
|
||||||
)
|
)
|
||||||
case self.opcodes.AUTH_CONFIRM:
|
case self.opcodes.AUTH_CONFIRM:
|
||||||
if not self.auth_rate_limiter.is_allowed(address[0]):
|
if not self.auth_rate_limiter.is_allowed(address[0]):
|
||||||
@@ -109,7 +98,7 @@ class OnemeWS:
|
|||||||
)
|
)
|
||||||
elif payload and payload.get("tokenType") == "REGISTER":
|
elif payload and payload.get("tokenType") == "REGISTER":
|
||||||
await self.processors.auth_confirm(
|
await self.processors.auth_confirm(
|
||||||
payload, seq, websocket, deviceType, deviceName, appVersion, address[0]
|
payload, seq, websocket, deviceType, deviceName
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
self.logger.warning(
|
self.logger.warning(
|
||||||
@@ -128,7 +117,7 @@ class OnemeWS:
|
|||||||
userPhone,
|
userPhone,
|
||||||
userId,
|
userId,
|
||||||
hashedToken,
|
hashedToken,
|
||||||
) = await self.processors.login(payload, seq, websocket, appVersion)
|
) = await self.processors.login(payload, seq, websocket)
|
||||||
|
|
||||||
if userPhone:
|
if userPhone:
|
||||||
await self._finish_auth(
|
await self._finish_auth(
|
||||||
@@ -140,7 +129,7 @@ class OnemeWS:
|
|||||||
)
|
)
|
||||||
break
|
break
|
||||||
case self.opcodes.PING:
|
case self.opcodes.PING:
|
||||||
await self.processors.ping(payload, seq, websocket, userId)
|
await self.processors.ping(payload, seq, websocket)
|
||||||
case self.opcodes.LOG:
|
case self.opcodes.LOG:
|
||||||
await self.processors.log(payload, seq, websocket)
|
await self.processors.log(payload, seq, websocket)
|
||||||
case self.opcodes.ASSETS_UPDATE:
|
case self.opcodes.ASSETS_UPDATE:
|
||||||
@@ -151,54 +140,6 @@ class OnemeWS:
|
|||||||
seq,
|
seq,
|
||||||
websocket,
|
websocket,
|
||||||
)
|
)
|
||||||
case self.opcodes.ASSETS_GET:
|
|
||||||
await self.auth_required(
|
|
||||||
userPhone,
|
|
||||||
self.processors.assets_get,
|
|
||||||
payload,
|
|
||||||
seq,
|
|
||||||
websocket,
|
|
||||||
)
|
|
||||||
case self.opcodes.ASSETS_GET_BY_IDS:
|
|
||||||
await self.auth_required(
|
|
||||||
userPhone,
|
|
||||||
self.processors.assets_get_by_ids,
|
|
||||||
payload,
|
|
||||||
seq,
|
|
||||||
websocket,
|
|
||||||
)
|
|
||||||
case self.opcodes.ASSETS_ADD:
|
|
||||||
await self.auth_required(
|
|
||||||
userPhone,
|
|
||||||
self.processors.assets_add,
|
|
||||||
payload,
|
|
||||||
seq,
|
|
||||||
websocket,
|
|
||||||
)
|
|
||||||
case self.opcodes.ASSETS_REMOVE:
|
|
||||||
await self.auth_required(
|
|
||||||
userPhone,
|
|
||||||
self.processors.assets_remove,
|
|
||||||
payload,
|
|
||||||
seq,
|
|
||||||
websocket,
|
|
||||||
)
|
|
||||||
case self.opcodes.ASSETS_MOVE:
|
|
||||||
await self.auth_required(
|
|
||||||
userPhone,
|
|
||||||
self.processors.assets_move,
|
|
||||||
payload,
|
|
||||||
seq,
|
|
||||||
websocket,
|
|
||||||
)
|
|
||||||
case self.opcodes.ASSETS_LIST_MODIFY:
|
|
||||||
await self.auth_required(
|
|
||||||
userPhone,
|
|
||||||
self.processors.assets_list_modify,
|
|
||||||
payload,
|
|
||||||
seq,
|
|
||||||
websocket,
|
|
||||||
)
|
|
||||||
case self.opcodes.VIDEO_CHAT_HISTORY:
|
case self.opcodes.VIDEO_CHAT_HISTORY:
|
||||||
await self.auth_required(
|
await self.auth_required(
|
||||||
userPhone,
|
userPhone,
|
||||||
@@ -226,15 +167,6 @@ 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,
|
||||||
@@ -292,16 +224,6 @@ class OnemeWS:
|
|||||||
payload,
|
payload,
|
||||||
seq,
|
seq,
|
||||||
websocket,
|
websocket,
|
||||||
userId,
|
|
||||||
)
|
|
||||||
case self.opcodes.CONTACT_LIST:
|
|
||||||
await self.auth_required(
|
|
||||||
userPhone,
|
|
||||||
self.processors.contact_list,
|
|
||||||
payload,
|
|
||||||
seq,
|
|
||||||
websocket,
|
|
||||||
userId,
|
|
||||||
)
|
)
|
||||||
case self.opcodes.COMPLAIN_REASONS_GET:
|
case self.opcodes.COMPLAIN_REASONS_GET:
|
||||||
await self.auth_required(
|
await self.auth_required(
|
||||||
@@ -323,42 +245,6 @@ class OnemeWS:
|
|||||||
seq,
|
seq,
|
||||||
websocket,
|
websocket,
|
||||||
)
|
)
|
||||||
case self.opcodes.CONFIG:
|
|
||||||
await self.auth_required(
|
|
||||||
userPhone,
|
|
||||||
self.processors.update_config,
|
|
||||||
payload,
|
|
||||||
seq,
|
|
||||||
websocket,
|
|
||||||
userPhone,
|
|
||||||
hashedToken,
|
|
||||||
)
|
|
||||||
case self.opcodes.CONTACT_UPDATE:
|
|
||||||
await self.auth_required(
|
|
||||||
userPhone,
|
|
||||||
self.processors.contact_update,
|
|
||||||
payload,
|
|
||||||
seq,
|
|
||||||
websocket,
|
|
||||||
userId,
|
|
||||||
)
|
|
||||||
case self.opcodes.CONTACT_ADD_BY_PHONE:
|
|
||||||
await self.auth_required(
|
|
||||||
userPhone,
|
|
||||||
self.processors.contact_add_by_phone,
|
|
||||||
payload,
|
|
||||||
seq,
|
|
||||||
websocket,
|
|
||||||
userId,
|
|
||||||
)
|
|
||||||
case self.opcodes.CONTACT_PRESENCE:
|
|
||||||
await self.auth_required(
|
|
||||||
userPhone,
|
|
||||||
self.processors.contact_presence,
|
|
||||||
payload,
|
|
||||||
seq,
|
|
||||||
websocket
|
|
||||||
)
|
|
||||||
case _:
|
case _:
|
||||||
self.logger.warning(f"Неизвестный опкод {opcode}")
|
self.logger.warning(f"Неизвестный опкод {opcode}")
|
||||||
except websockets.exceptions.ConnectionClosed:
|
except websockets.exceptions.ConnectionClosed:
|
||||||
@@ -385,59 +271,23 @@ class OnemeWS:
|
|||||||
"writer": websocket,
|
"writer": websocket,
|
||||||
"ip": addr[0],
|
"ip": addr[0],
|
||||||
"port": addr[1],
|
"port": addr[1],
|
||||||
"protocol": "oneme",
|
"protocol": "oneme"
|
||||||
"type": "web"
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
self.clients[id] = {
|
self.clients[id] = {
|
||||||
"phone": phone,
|
"phone": phone,
|
||||||
"id": id,
|
"id": id,
|
||||||
"status": 2,
|
|
||||||
"last_seen": 0,
|
|
||||||
"clients": [
|
"clients": [
|
||||||
{
|
{
|
||||||
"writer": websocket,
|
"writer": websocket,
|
||||||
"ip": addr[0],
|
"ip": addr[0],
|
||||||
"port": addr[1],
|
"port": addr[1],
|
||||||
"protocol": "oneme",
|
"protocol": "oneme"
|
||||||
"type": "web"
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
await self._broadcast_presence(id, True)
|
|
||||||
|
|
||||||
async def _broadcast_presence(self, userId, online):
|
|
||||||
now = int(time.time())
|
|
||||||
now_ms = int(time.time() * 1000)
|
|
||||||
|
|
||||||
if online:
|
|
||||||
presence_data = {"on": "ON", "seen": now, "status": 1}
|
|
||||||
else:
|
|
||||||
presence_data = {"seen": now}
|
|
||||||
|
|
||||||
async with self.db_pool.acquire() as conn:
|
|
||||||
async with conn.cursor() as cursor:
|
|
||||||
await cursor.execute(
|
|
||||||
"SELECT owner_id FROM contacts WHERE contact_id = %s",
|
|
||||||
(userId,)
|
|
||||||
)
|
|
||||||
contact_owners = await cursor.fetchall()
|
|
||||||
|
|
||||||
for row in contact_owners:
|
|
||||||
owner_id = int(row.get("owner_id"))
|
|
||||||
if owner_id in self.clients:
|
|
||||||
await self.processors.event(
|
|
||||||
owner_id,
|
|
||||||
{
|
|
||||||
"eventType": "presence",
|
|
||||||
"userId": userId,
|
|
||||||
"presence": presence_data,
|
|
||||||
"time": now_ms,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
async def _end_session(self, id, ip, port):
|
async def _end_session(self, id, ip, port):
|
||||||
"""Завершение сессии"""
|
"""Завершение сессии"""
|
||||||
# Получаем пользователя в списке
|
# Получаем пользователя в списке
|
||||||
@@ -453,20 +303,6 @@ class OnemeWS:
|
|||||||
if (client.get("ip"), client.get("port")) == (ip, port):
|
if (client.get("ip"), client.get("port")) == (ip, port):
|
||||||
clients.pop(i)
|
clients.pop(i)
|
||||||
|
|
||||||
if not clients:
|
|
||||||
now = int(time.time())
|
|
||||||
user["status"] = 0
|
|
||||||
user["last_seen"] = now
|
|
||||||
|
|
||||||
async with self.db_pool.acquire() as conn:
|
|
||||||
async with conn.cursor() as cursor:
|
|
||||||
await cursor.execute(
|
|
||||||
"UPDATE users SET lastseen = %s WHERE id = %s",
|
|
||||||
(str(now), id)
|
|
||||||
)
|
|
||||||
|
|
||||||
await self._broadcast_presence(id, False)
|
|
||||||
|
|
||||||
async def start(self):
|
async def start(self):
|
||||||
"""Функция для запуска WebSocket сервера"""
|
"""Функция для запуска WebSocket сервера"""
|
||||||
self.server = await websockets.serve(
|
self.server = await websockets.serve(
|
||||||
@@ -478,9 +314,4 @@ class OnemeWS:
|
|||||||
|
|
||||||
self.logger.info(f"WebSocket запущен на порту {self.port}")
|
self.logger.info(f"WebSocket запущен на порту {self.port}")
|
||||||
|
|
||||||
try:
|
await self.server.wait_closed()
|
||||||
await self.server.wait_closed()
|
|
||||||
except asyncio.CancelledError:
|
|
||||||
self.server.close()
|
|
||||||
await self.server.wait_closed()
|
|
||||||
raise
|
|
||||||
@@ -9,12 +9,319 @@ class TTConfig:
|
|||||||
"googNoiseSuppression": "true",
|
"googNoiseSuppression": "true",
|
||||||
"googHighpassFilter": "false",
|
"googHighpassFilter": "false",
|
||||||
"googTypingNoiseDetection": "false",
|
"googTypingNoiseDetection": "false",
|
||||||
"googAudioNetworkAdaptorConfig": ""
|
"googAudioNetworkAdaptorConfig": "ChyyARkNCtcjPBUK1yM8GKjDASCw6gEomHUwoJwBCgfKAQQIABAACgvCAQgIqMMBELiRAgosqgEpChEIuBcVzcxMPhjogQIlCtejOxIRCOgHFc3MTD4YsOoBJQrXozsYyAEKC7oBCAiw6gEQoJwB"
|
||||||
},
|
},
|
||||||
"a-lte": 24,
|
"a-lte": 24,
|
||||||
"a-wifi": 34,
|
"a-wifi": 34,
|
||||||
"account-removal-enabled": False,
|
"account-removal-enabled": False,
|
||||||
"animated-emojis": {},
|
"animated-emojis": {
|
||||||
|
"❤️": {
|
||||||
|
"reactionAction": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2022-12-28lottie/r/03.json"
|
||||||
|
},
|
||||||
|
"emoji": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2022-12-28lottie/e/04.json"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"👍": {
|
||||||
|
"emoji": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2022-12-29lottie/e/16.json"
|
||||||
|
},
|
||||||
|
"reactionAction": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2022-12-28lottie/r/01_m.json"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"👎": {
|
||||||
|
"emoji": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2022-12-28lottie/e/17.json"
|
||||||
|
},
|
||||||
|
"reactionAction": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2022-12-28lottie/r/02.json"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"🙏": {
|
||||||
|
"reactionAction": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2022-12-28lottie/r/04.json"
|
||||||
|
},
|
||||||
|
"emoji": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2022-12-29lottie/e/30_ng.json"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"😘": {
|
||||||
|
"reactionAction": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2022-12-28lottie/r/05.json"
|
||||||
|
},
|
||||||
|
"emoji": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2022-12-28lottie/e/03.json"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"🔥": {
|
||||||
|
"reactionAction": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2022-12-28lottie/r/06.json"
|
||||||
|
},
|
||||||
|
"emoji": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2022-12-28lottie/e/10.json"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"😂": {
|
||||||
|
"reactionAction": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2022-12-28lottie/r/07.json"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"👏": {
|
||||||
|
"emoji": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2023-08-14animoji/56.json"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"😮": {
|
||||||
|
"reactionAction": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2022-12-28lottie/r/09.json"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"💋": {
|
||||||
|
"emoji": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2022-12-28lottie/e/13_v02.json"
|
||||||
|
},
|
||||||
|
"reactionAction": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2023-01-18lottie/r/kissing2.json"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"🥂": {
|
||||||
|
"emoji": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2022-12-28lottie/e/20.json"
|
||||||
|
},
|
||||||
|
"reactionAction": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2022-12-28lottie/r/20.json"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"😳": {
|
||||||
|
"reactionAction": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2022-12-28lottie/r/09.json"
|
||||||
|
},
|
||||||
|
"emoji": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2022-12-28lottie/e/02.json"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"😔": {
|
||||||
|
"reactionAction": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2022-12-28lottie/r/11.json"
|
||||||
|
},
|
||||||
|
"emoji": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2022-12-28lottie/e/05.json"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"😍": {
|
||||||
|
"emoji": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2022-12-28lottie/e/07.json"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"😯": {
|
||||||
|
"emoji": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2022-12-28lottie/e/08.json"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"😉": {
|
||||||
|
"emoji": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2022-12-28lottie/e/09.json"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"🌺": {
|
||||||
|
"reactionAction": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2023-03-06lottie/flower.json"
|
||||||
|
},
|
||||||
|
"emoji": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2022-12-28lottie/e/11.json"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"🎂": {
|
||||||
|
"emoji": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2022-12-28lottie/e/14.json"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"💩": {
|
||||||
|
"emoji": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2022-12-28lottie/e/15.json"
|
||||||
|
},
|
||||||
|
"reactionAction": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2023-01-18lottie/r/shit_1.json"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"🐰": {
|
||||||
|
"emoji": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2022-12-28lottie/e/19.json"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"🎅": {
|
||||||
|
"emoji": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2022-12-28lottie/e/21.json"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"🎄": {
|
||||||
|
"emoji": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2022-12-28lottie/e/23.json"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"🎆": {
|
||||||
|
"emoji": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2022-12-28lottie/e/22.json"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"❄️": {
|
||||||
|
"emoji": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2022-12-28lottie/e/25.json"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"🎉": {
|
||||||
|
"emoji": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2022-12-28lottie/e/12.json"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"🥗": {
|
||||||
|
"emoji": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2022-12-29lottie/e/28.json"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"🧡": {
|
||||||
|
"emoji": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2023-07-20animojie/31.json"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"💔": {
|
||||||
|
"emoji": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2023-07-20animojie/32.json"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"🎁": {
|
||||||
|
"emoji": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2023-07-20animojie/34.json"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"🌹": {
|
||||||
|
"emoji": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2023-07-20animojie/35.json"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"🌸": {
|
||||||
|
"emoji": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2023-07-20animojie/36.json"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"🍒": {
|
||||||
|
"emoji": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2023-07-20animojie/37.json"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"🥕": {
|
||||||
|
"emoji": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2023-07-20animojie/39.json"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"🍑": {
|
||||||
|
"emoji": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2023-07-20animojie/40.json"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"🍋": {
|
||||||
|
"emoji": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2023-07-20animojie/41.json"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"🍃": {
|
||||||
|
"emoji": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2023-07-20animojie/42.json"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"😺": {
|
||||||
|
"emoji": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2023-07-20animojie/43.json"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"🐶": {
|
||||||
|
"emoji": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2023-07-20animojie/44.json"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"🐽": {
|
||||||
|
"emoji": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2023-07-20animojie/45.json"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"💐": {
|
||||||
|
"emoji": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2023-08-14animoji/46.json"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"🎈": {
|
||||||
|
"emoji": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2023-08-14animoji/47.json"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"🍾": {
|
||||||
|
"emoji": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2023-08-14animoji/48.json"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"⚡": {
|
||||||
|
"emoji": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2023-08-14animoji/49.json"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"⭐": {
|
||||||
|
"emoji": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2023-08-14animoji/50.json"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"✨": {
|
||||||
|
"emoji": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2023-08-14animoji/51.json"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"💃": {
|
||||||
|
"emoji": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2023-08-14animoji/52.json"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"☀️": {
|
||||||
|
"emoji": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2023-08-14animoji/53.json"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"👋": {
|
||||||
|
"emoji": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2023-08-14animoji/54.json"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"☕": {
|
||||||
|
"emoji": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2023-08-14animoji/57.json"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"🙂": {
|
||||||
|
"emoji": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2023-08-14animoji/58.json"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"🤩": {
|
||||||
|
"emoji": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2023-08-15animoji/59.json"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"😇": {
|
||||||
|
"emoji": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2023-08-14animoji/60.json"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"😎": {
|
||||||
|
"emoji": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2023-08-14animoji/61.json"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"🍎": {
|
||||||
|
"emoji": {
|
||||||
|
"url": "https://st.okcdn.ru/static/messages/2023-08-14animoji/62.json"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"animated-emojis-limits": {
|
"animated-emojis-limits": {
|
||||||
"low": 5,
|
"low": 5,
|
||||||
"average": 10,
|
"average": 10,
|
||||||
@@ -76,10 +383,10 @@ class TTConfig:
|
|||||||
"image-quality": 0.800000011920929,
|
"image-quality": 0.800000011920929,
|
||||||
"image-size": 40000000,
|
"image-size": 40000000,
|
||||||
"image-width": 1680,
|
"image-width": 1680,
|
||||||
"invite-header": "",
|
"invite-header": "Приглашение в ТамТам",
|
||||||
"invite-link": "",
|
"invite-link": "https://tt.me/starwear",
|
||||||
"invite-long": "",
|
"invite-long": "Я общаюсь в ТамТам, присоединяйся https://tt.me/starwear",
|
||||||
"invite-short": "",
|
"invite-short": "Привет! Ставь ТамТам! Жду ответа! https://tt.me/starwear",
|
||||||
"keep-connection": 2,
|
"keep-connection": 2,
|
||||||
"l10n": False,
|
"l10n": False,
|
||||||
"live-location-enabled": True,
|
"live-location-enabled": True,
|
||||||
@@ -146,12 +453,25 @@ class TTConfig:
|
|||||||
"profiling-enabled": False,
|
"profiling-enabled": False,
|
||||||
"progress-diff-for-notify": 1,
|
"progress-diff-for-notify": 1,
|
||||||
"promo-contact-id": 0,
|
"promo-contact-id": 0,
|
||||||
"promo-recent-contacts": False,
|
"promo-recent-contacts": True,
|
||||||
"promo_contact_label": "",
|
"promo_contact_label": "Белый Маг",
|
||||||
"proxy": "",
|
"proxy": "msgproxy.okcdn.ru",
|
||||||
"proxy-domains": [],
|
"proxy-domains": [
|
||||||
"proxy-exclude": [],
|
"okcdn.ru",
|
||||||
"proxy-rotation": False,
|
"mycdn.me",
|
||||||
|
"ok.ru",
|
||||||
|
"odnoklassniki.ru",
|
||||||
|
"odkl.ru",
|
||||||
|
"vk.com",
|
||||||
|
"userapi.com",
|
||||||
|
"vkuser.net",
|
||||||
|
"vkusercdn.ru"
|
||||||
|
],
|
||||||
|
"proxy-exclude": [
|
||||||
|
"r.mradx.net",
|
||||||
|
"ad.mail.ru"
|
||||||
|
],
|
||||||
|
"proxy-rotation": True,
|
||||||
"push-alert-timeout": 604800,
|
"push-alert-timeout": 604800,
|
||||||
"push-tracking-enabled": True,
|
"push-tracking-enabled": True,
|
||||||
"quick-forward-cases": [],
|
"quick-forward-cases": [],
|
||||||
@@ -199,24 +519,24 @@ class TTConfig:
|
|||||||
"TOP"
|
"TOP"
|
||||||
],
|
],
|
||||||
"stickers-suggestion-keywords-inline": False,
|
"stickers-suggestion-keywords-inline": False,
|
||||||
"support-account": "",
|
"support-account": "tt.me/support",
|
||||||
"support-button-enable": False,
|
"support-button-enable": False,
|
||||||
"t-ice-reconnect": 15,
|
"t-ice-reconnect": 15,
|
||||||
"t-incoming-call": 40,
|
"t-incoming-call": 40,
|
||||||
"t-start-connect": 20,
|
"t-start-connect": 20,
|
||||||
"tam-emoji-font-url": "",
|
"tam-emoji-font-url": "https://st.okcdn.ru/static/messages/2022-08-25noto/TamNotoColorEmojiCompat.ttf",
|
||||||
"tcp-candidates": False,
|
"tcp-candidates": False,
|
||||||
"tracer-crash-report-enabled": False,
|
"tracer-crash-report-enabled": True,
|
||||||
"tracer-crash-report-host": "",
|
"tracer-crash-report-host": "https://api-hprof.odkl.ru",
|
||||||
"tracer-crash-send-asap-enabled": False,
|
"tracer-crash-send-asap-enabled": True,
|
||||||
"tracer-crash-send-logs-enabled": False,
|
"tracer-crash-send-logs-enabled": True,
|
||||||
"tracer-crash-send-threads-dump-enabled": False,
|
"tracer-crash-send-threads-dump-enabled": True,
|
||||||
"tracer-disk-overflow-report-threshold": 3000000000,
|
"tracer-disk-overflow-report-threshold": 3000000000,
|
||||||
"tracer-disk-usage-probability": 500,
|
"tracer-disk-usage-probability": 500,
|
||||||
"tracer-enabled": False,
|
"tracer-enabled": True,
|
||||||
"tracer-host": "",
|
"tracer-host": "https://api-hprof.odkl.ru",
|
||||||
"tracer-hprof-probability": -1,
|
"tracer-hprof-probability": -1,
|
||||||
"tracer-sampled-conditions": "",
|
"tracer-sampled-conditions": "tag=app_start_ui_freeze_2k;probability=100000;startEvent=app_first_activity_created;interestingEvent=app_freeze;interestingDuration=2000",
|
||||||
"tracer-sampled-duration": 20000,
|
"tracer-sampled-duration": 20000,
|
||||||
"tracer-systrace-duration": 20000,
|
"tracer-systrace-duration": 20000,
|
||||||
"tracer-systrace-interesting-duration": 10000,
|
"tracer-systrace-interesting-duration": 10000,
|
||||||
@@ -249,4 +569,4 @@ class TTConfig:
|
|||||||
"iceServers": [],
|
"iceServers": [],
|
||||||
"has-phone": True,
|
"has-phone": True,
|
||||||
"promo-constructors": []
|
"promo-constructors": []
|
||||||
}
|
}
|
||||||
@@ -3,106 +3,10 @@ from tamtam.socket import TamTamMobile
|
|||||||
from tamtam.websocket import TamTamWS
|
from tamtam.websocket import TamTamWS
|
||||||
from classes.controllerbase import ControllerBase
|
from classes.controllerbase import ControllerBase
|
||||||
from common.config import ServerConfig
|
from common.config import ServerConfig
|
||||||
from common.proto_tcp import MobileProto
|
|
||||||
from common.proto_web import WebProto
|
|
||||||
from common.opcodes import Opcodes
|
|
||||||
|
|
||||||
class TTController(ControllerBase):
|
class TTController(ControllerBase):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.config = ServerConfig()
|
self.config = ServerConfig()
|
||||||
self.proto_tcp = MobileProto()
|
|
||||||
self.proto_web = WebProto()
|
|
||||||
self.opcodes = Opcodes()
|
|
||||||
|
|
||||||
async def event(self, target, client, eventData):
|
|
||||||
# Извлекаем тип события и врайтер
|
|
||||||
eventType = eventData.get("eventType")
|
|
||||||
writer = client.get("writer")
|
|
||||||
is_web = client.get("type") == "web"
|
|
||||||
|
|
||||||
# Выбираем протокол в зависимости от типа подключения
|
|
||||||
proto = self.proto_web if is_web else self.proto_tcp
|
|
||||||
packet = None
|
|
||||||
|
|
||||||
# Не отправляем событие самому себе
|
|
||||||
if writer == eventData.get("writer"):
|
|
||||||
return
|
|
||||||
|
|
||||||
# Обрабатываем событие
|
|
||||||
if eventType == "new_msg":
|
|
||||||
# Данные сообщения
|
|
||||||
chatId = eventData.get("chatId")
|
|
||||||
message = eventData.get("message")
|
|
||||||
prevMessageId = eventData.get("prevMessageId")
|
|
||||||
time = eventData.get("time")
|
|
||||||
|
|
||||||
# Данные пакета
|
|
||||||
payload = {
|
|
||||||
"chatId": chatId,
|
|
||||||
"message": message,
|
|
||||||
"prevMessageId": prevMessageId,
|
|
||||||
"ttl": False,
|
|
||||||
"unread": 0,
|
|
||||||
"mark": time
|
|
||||||
}
|
|
||||||
|
|
||||||
# Создаем пакет
|
|
||||||
packet = proto.pack_packet(
|
|
||||||
cmd=0, seq=1, opcode=self.opcodes.NOTIF_MESSAGE, payload=payload
|
|
||||||
)
|
|
||||||
elif eventType == "typing":
|
|
||||||
# Данные события
|
|
||||||
chatId = eventData.get("chatId")
|
|
||||||
userId = eventData.get("userId")
|
|
||||||
type = eventData.get("type")
|
|
||||||
|
|
||||||
# Данные пакета
|
|
||||||
payload = {
|
|
||||||
"chatId": chatId,
|
|
||||||
"userId": userId,
|
|
||||||
"type": type
|
|
||||||
}
|
|
||||||
|
|
||||||
# Создаем пакет
|
|
||||||
packet = proto.pack_packet(
|
|
||||||
cmd=0, seq=1, opcode=self.opcodes.NOTIF_TYPING, payload=payload
|
|
||||||
)
|
|
||||||
elif eventType == "profile_updated":
|
|
||||||
# Данные события
|
|
||||||
profile = eventData.get("profile")
|
|
||||||
|
|
||||||
# Данные пакета
|
|
||||||
payload = {
|
|
||||||
"profile": profile
|
|
||||||
}
|
|
||||||
|
|
||||||
# Создаем пакет
|
|
||||||
packet = proto.pack_packet(
|
|
||||||
cmd=0, seq=1, opcode=self.opcodes.NOTIF_PROFILE, payload=payload
|
|
||||||
)
|
|
||||||
elif eventType == "presence":
|
|
||||||
userId = eventData.get("userId")
|
|
||||||
presence = eventData.get("presence")
|
|
||||||
event_time = eventData.get("time")
|
|
||||||
|
|
||||||
payload = {
|
|
||||||
"userId": userId,
|
|
||||||
"presence": presence,
|
|
||||||
"time": event_time
|
|
||||||
}
|
|
||||||
|
|
||||||
packet = proto.pack_packet(
|
|
||||||
cmd=0, seq=1, opcode=self.opcodes.NOTIF_PRESENCE, payload=payload
|
|
||||||
)
|
|
||||||
|
|
||||||
if not packet:
|
|
||||||
return
|
|
||||||
|
|
||||||
if is_web:
|
|
||||||
await writer.send(packet)
|
|
||||||
else:
|
|
||||||
writer.write(packet)
|
|
||||||
await writer.drain()
|
|
||||||
|
|
||||||
def launch(self, api):
|
def launch(self, api):
|
||||||
async def _start_all():
|
async def _start_all():
|
||||||
|
|||||||
@@ -3,24 +3,24 @@ import pydantic
|
|||||||
class UserAgentModel(pydantic.BaseModel):
|
class UserAgentModel(pydantic.BaseModel):
|
||||||
deviceType: str
|
deviceType: str
|
||||||
appVersion: str
|
appVersion: str
|
||||||
osVersion: str = None
|
osVersion: str
|
||||||
timezone: str = None
|
timezone: str
|
||||||
screen: str = None
|
screen: str
|
||||||
pushDeviceType: str = None
|
pushDeviceType: str = None
|
||||||
locale: str = None
|
locale: str
|
||||||
deviceName: str
|
deviceName: str
|
||||||
deviceLocale: str = None
|
deviceLocale: str
|
||||||
|
|
||||||
class HelloPayloadModel(pydantic.BaseModel):
|
class HelloPayloadModel(pydantic.BaseModel):
|
||||||
userAgent: UserAgentModel
|
userAgent: UserAgentModel
|
||||||
deviceId: str = None
|
deviceId: str
|
||||||
|
|
||||||
class RequestCodePayloadModel(pydantic.BaseModel):
|
class RequestCodePayloadModel(pydantic.BaseModel):
|
||||||
phone: str
|
phone: str
|
||||||
|
|
||||||
class VerifyCodePayloadModel(pydantic.BaseModel):
|
class VerifyCodePayloadModel(pydantic.BaseModel):
|
||||||
verifyCode: str
|
verifyCode: str
|
||||||
authTokenType: str = None
|
authTokenType: str
|
||||||
token: str
|
token: str
|
||||||
|
|
||||||
class FinalAuthPayloadModel(pydantic.BaseModel):
|
class FinalAuthPayloadModel(pydantic.BaseModel):
|
||||||
@@ -30,7 +30,7 @@ class FinalAuthPayloadModel(pydantic.BaseModel):
|
|||||||
token: str
|
token: str
|
||||||
|
|
||||||
class LoginPayloadModel(pydantic.BaseModel):
|
class LoginPayloadModel(pydantic.BaseModel):
|
||||||
interactive: bool = None
|
interactive: bool
|
||||||
token: str
|
token: str
|
||||||
|
|
||||||
class SearchUsersPayloadModel(pydantic.BaseModel):
|
class SearchUsersPayloadModel(pydantic.BaseModel):
|
||||||
@@ -41,99 +41,4 @@ class PingPayloadModel(pydantic.BaseModel):
|
|||||||
|
|
||||||
class ChatHistoryPayloadModel(pydantic.BaseModel):
|
class ChatHistoryPayloadModel(pydantic.BaseModel):
|
||||||
chatId: int
|
chatId: int
|
||||||
backward: int
|
backward: int
|
||||||
|
|
||||||
class UpdateProfilePayloadModel(pydantic.BaseModel):
|
|
||||||
pass
|
|
||||||
|
|
||||||
class SearchChatsPayloadModel(pydantic.BaseModel):
|
|
||||||
chatIds: list
|
|
||||||
|
|
||||||
class AssetsPayloadModel(pydantic.BaseModel):
|
|
||||||
sync: int
|
|
||||||
type: str = None
|
|
||||||
userId: int = None
|
|
||||||
|
|
||||||
class AssetsGetPayloadModel(pydantic.BaseModel):
|
|
||||||
type: str
|
|
||||||
count: int = 100
|
|
||||||
query: str = None
|
|
||||||
|
|
||||||
class AssetsGetByIdsPayloadModel(pydantic.BaseModel):
|
|
||||||
type: str
|
|
||||||
ids: list
|
|
||||||
|
|
||||||
class AssetsAddPayloadModel(pydantic.BaseModel):
|
|
||||||
type: str
|
|
||||||
id: int = None
|
|
||||||
|
|
||||||
class AssetsRemovePayloadModel(pydantic.BaseModel):
|
|
||||||
type: str
|
|
||||||
ids: list
|
|
||||||
|
|
||||||
class AssetsMovePayloadModel(pydantic.BaseModel):
|
|
||||||
type: str
|
|
||||||
id: int
|
|
||||||
position: int
|
|
||||||
|
|
||||||
class AssetsListModifyPayloadModel(pydantic.BaseModel):
|
|
||||||
type: str
|
|
||||||
ids: list
|
|
||||||
|
|
||||||
class GetCallTokenPayloadModel(pydantic.BaseModel):
|
|
||||||
userId: int
|
|
||||||
value: str
|
|
||||||
|
|
||||||
class GetCallHistoryPayloadModel(pydantic.BaseModel):
|
|
||||||
forward: bool
|
|
||||||
count: int
|
|
||||||
|
|
||||||
class ChatSubscribePayloadModel(pydantic.BaseModel):
|
|
||||||
chatId: int
|
|
||||||
subscribe: bool
|
|
||||||
|
|
||||||
class ContactListPayloadModel(pydantic.BaseModel):
|
|
||||||
status: str
|
|
||||||
count: int = None
|
|
||||||
|
|
||||||
class ContactPresencePayloadModel(pydantic.BaseModel):
|
|
||||||
contactIds: list
|
|
||||||
|
|
||||||
class ContactUpdatePayloadModel(pydantic.BaseModel):
|
|
||||||
action: str
|
|
||||||
contactId: int
|
|
||||||
firstName: str = None
|
|
||||||
lastName: str = None
|
|
||||||
|
|
||||||
class TypingPayloadModel(pydantic.BaseModel):
|
|
||||||
chatId: int
|
|
||||||
type: str = None
|
|
||||||
|
|
||||||
class MessageModel(pydantic.BaseModel):
|
|
||||||
isLive: bool = None
|
|
||||||
detectShare: bool = None
|
|
||||||
elements: list = None
|
|
||||||
attaches: list = None
|
|
||||||
cid: int = None
|
|
||||||
text: str = None
|
|
||||||
|
|
||||||
class SendMessagePayloadModel(pydantic.BaseModel):
|
|
||||||
userId: int = None
|
|
||||||
chatId: int = None
|
|
||||||
message: MessageModel
|
|
||||||
|
|
||||||
class AuthConfirmRegisterPayloadModel(pydantic.BaseModel):
|
|
||||||
token: str
|
|
||||||
name: str
|
|
||||||
tokenType: str
|
|
||||||
deviceType: str
|
|
||||||
deviceId: str = None
|
|
||||||
|
|
||||||
@pydantic.field_validator('name')
|
|
||||||
def validate_name(cls, v):
|
|
||||||
v = v.strip()
|
|
||||||
if not v:
|
|
||||||
raise ValueError('name must not be empty')
|
|
||||||
if len(v) > 59:
|
|
||||||
raise ValueError('name too long')
|
|
||||||
return v
|
|
||||||
@@ -2,19 +2,9 @@ from .main import MainProcessors
|
|||||||
from .auth import AuthProcessors
|
from .auth import AuthProcessors
|
||||||
from .search import SearchProcessors
|
from .search import SearchProcessors
|
||||||
from .history import HistoryProcessors
|
from .history import HistoryProcessors
|
||||||
from .assets import AssetsProcessors
|
|
||||||
from .chats import ChatsProcessors
|
|
||||||
from .contacts import ContactsProcessors
|
|
||||||
from .messages import MessagesProcessors
|
|
||||||
from .sessions import SessionsProcessors
|
|
||||||
|
|
||||||
class Processors(MainProcessors,
|
class Processors(MainProcessors,
|
||||||
AuthProcessors,
|
AuthProcessors,
|
||||||
SearchProcessors,
|
SearchProcessors,
|
||||||
HistoryProcessors,
|
HistoryProcessors):
|
||||||
AssetsProcessors,
|
pass
|
||||||
ChatsProcessors,
|
|
||||||
ContactsProcessors,
|
|
||||||
MessagesProcessors,
|
|
||||||
SessionsProcessors):
|
|
||||||
pass
|
|
||||||
@@ -1,167 +0,0 @@
|
|||||||
import pydantic
|
|
||||||
import time
|
|
||||||
from classes.baseprocessor import BaseProcessor
|
|
||||||
from tamtam.models import (
|
|
||||||
AssetsPayloadModel,
|
|
||||||
AssetsGetPayloadModel,
|
|
||||||
AssetsGetByIdsPayloadModel,
|
|
||||||
AssetsAddPayloadModel,
|
|
||||||
AssetsRemovePayloadModel,
|
|
||||||
AssetsMovePayloadModel,
|
|
||||||
AssetsListModifyPayloadModel,
|
|
||||||
)
|
|
||||||
|
|
||||||
class AssetsProcessors(BaseProcessor):
|
|
||||||
async def assets_update(self, payload, seq, writer):
|
|
||||||
try:
|
|
||||||
AssetsPayloadModel.model_validate(payload)
|
|
||||||
except pydantic.ValidationError as error:
|
|
||||||
self.logger.error(f"Возникли ошибки при валидации пакета: {error}")
|
|
||||||
await self._send_error(seq, self.opcodes.ASSETS_UPDATE, self.error_types.INVALID_PAYLOAD, writer)
|
|
||||||
return
|
|
||||||
|
|
||||||
response = {
|
|
||||||
"sync": int(time.time() * 1000),
|
|
||||||
"stickerSetsUpdates": {},
|
|
||||||
"stickersUpdates": {},
|
|
||||||
"stickersOrder": [
|
|
||||||
"RECENT",
|
|
||||||
"FAVORITE_STICKERS",
|
|
||||||
"FAVORITE_STICKER_SETS",
|
|
||||||
"TOP",
|
|
||||||
"NEW",
|
|
||||||
"NEW_STICKER_SETS",
|
|
||||||
],
|
|
||||||
"sections": [
|
|
||||||
{
|
|
||||||
"id": "RECENT",
|
|
||||||
"type": "RECENTS",
|
|
||||||
"recentsList": [],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"id": "FAVORITE_STICKERS",
|
|
||||||
"type": "STICKERS",
|
|
||||||
"stickers": [],
|
|
||||||
"marker": None,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"id": "FAVORITE_STICKER_SETS",
|
|
||||||
"type": "STICKER_SETS",
|
|
||||||
"stickerSets": [],
|
|
||||||
"marker": None,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"id": "TOP",
|
|
||||||
"type": "STICKERS",
|
|
||||||
"stickers": [],
|
|
||||||
"marker": None,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"id": "NEW",
|
|
||||||
"type": "STICKERS",
|
|
||||||
"stickers": [],
|
|
||||||
"marker": None,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"id": "NEW_STICKER_SETS",
|
|
||||||
"type": "STICKER_SETS",
|
|
||||||
"stickerSets": [],
|
|
||||||
"marker": None,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
}
|
|
||||||
|
|
||||||
packet = self.proto.pack_packet(
|
|
||||||
cmd=self.proto.CMD_OK, seq=seq, opcode=self.opcodes.ASSETS_UPDATE, payload=response
|
|
||||||
)
|
|
||||||
await self._send(writer, packet)
|
|
||||||
|
|
||||||
async def assets_get(self, payload, seq, writer):
|
|
||||||
try:
|
|
||||||
data = AssetsGetPayloadModel.model_validate(payload)
|
|
||||||
except pydantic.ValidationError as error:
|
|
||||||
self.logger.error(f"Возникли ошибки при валидации пакета: {error}")
|
|
||||||
await self._send_error(seq, self.opcodes.ASSETS_GET, self.error_types.INVALID_PAYLOAD, writer)
|
|
||||||
return
|
|
||||||
|
|
||||||
asset_type = data.type
|
|
||||||
if asset_type == "STICKER_SET":
|
|
||||||
response = {"stickerSets": [], "marker": None}
|
|
||||||
else:
|
|
||||||
response = {"stickers": [], "marker": None}
|
|
||||||
|
|
||||||
packet = self.proto.pack_packet(
|
|
||||||
cmd=self.proto.CMD_OK, seq=seq, opcode=self.opcodes.ASSETS_GET, payload=response
|
|
||||||
)
|
|
||||||
await self._send(writer, packet)
|
|
||||||
|
|
||||||
async def assets_get_by_ids(self, payload, seq, writer):
|
|
||||||
try:
|
|
||||||
data = AssetsGetByIdsPayloadModel.model_validate(payload)
|
|
||||||
except pydantic.ValidationError as error:
|
|
||||||
self.logger.error(f"Возникли ошибки при валидации пакета: {error}")
|
|
||||||
await self._send_error(seq, self.opcodes.ASSETS_GET_BY_IDS, self.error_types.INVALID_PAYLOAD, writer)
|
|
||||||
return
|
|
||||||
|
|
||||||
asset_type = data.type
|
|
||||||
if asset_type == "STICKER_SET":
|
|
||||||
response = {"stickerSets": []}
|
|
||||||
else:
|
|
||||||
response = {"stickers": []}
|
|
||||||
|
|
||||||
packet = self.proto.pack_packet(
|
|
||||||
cmd=self.proto.CMD_OK, seq=seq, opcode=self.opcodes.ASSETS_GET_BY_IDS, payload=response
|
|
||||||
)
|
|
||||||
await self._send(writer, packet)
|
|
||||||
|
|
||||||
async def assets_add(self, payload, seq, writer):
|
|
||||||
try:
|
|
||||||
AssetsAddPayloadModel.model_validate(payload)
|
|
||||||
except pydantic.ValidationError as error:
|
|
||||||
self.logger.error(f"Возникли ошибки при валидации пакета: {error}")
|
|
||||||
await self._send_error(seq, self.opcodes.ASSETS_ADD, self.error_types.INVALID_PAYLOAD, writer)
|
|
||||||
return
|
|
||||||
|
|
||||||
packet = self.proto.pack_packet(
|
|
||||||
cmd=self.proto.CMD_OK, seq=seq, opcode=self.opcodes.ASSETS_ADD, payload={}
|
|
||||||
)
|
|
||||||
await self._send(writer, packet)
|
|
||||||
|
|
||||||
async def assets_remove(self, payload, seq, writer):
|
|
||||||
try:
|
|
||||||
AssetsRemovePayloadModel.model_validate(payload)
|
|
||||||
except pydantic.ValidationError as error:
|
|
||||||
self.logger.error(f"Возникли ошибки при валидации пакета: {error}")
|
|
||||||
await self._send_error(seq, self.opcodes.ASSETS_REMOVE, self.error_types.INVALID_PAYLOAD, writer)
|
|
||||||
return
|
|
||||||
|
|
||||||
packet = self.proto.pack_packet(
|
|
||||||
cmd=self.proto.CMD_OK, seq=seq, opcode=self.opcodes.ASSETS_REMOVE, payload={}
|
|
||||||
)
|
|
||||||
await self._send(writer, packet)
|
|
||||||
|
|
||||||
async def assets_move(self, payload, seq, writer):
|
|
||||||
try:
|
|
||||||
AssetsMovePayloadModel.model_validate(payload)
|
|
||||||
except pydantic.ValidationError as error:
|
|
||||||
self.logger.error(f"Возникли ошибки при валидации пакета: {error}")
|
|
||||||
await self._send_error(seq, self.opcodes.ASSETS_MOVE, self.error_types.INVALID_PAYLOAD, writer)
|
|
||||||
return
|
|
||||||
|
|
||||||
packet = self.proto.pack_packet(
|
|
||||||
cmd=self.proto.CMD_OK, seq=seq, opcode=self.opcodes.ASSETS_MOVE, payload={}
|
|
||||||
)
|
|
||||||
await self._send(writer, packet)
|
|
||||||
|
|
||||||
async def assets_list_modify(self, payload, seq, writer):
|
|
||||||
try:
|
|
||||||
AssetsListModifyPayloadModel.model_validate(payload)
|
|
||||||
except pydantic.ValidationError as error:
|
|
||||||
self.logger.error(f"Возникли ошибки при валидации пакета: {error}")
|
|
||||||
await self._send_error(seq, self.opcodes.ASSETS_LIST_MODIFY, self.error_types.INVALID_PAYLOAD, writer)
|
|
||||||
return
|
|
||||||
|
|
||||||
packet = self.proto.pack_packet(
|
|
||||||
cmd=self.proto.CMD_OK, seq=seq, opcode=self.opcodes.ASSETS_LIST_MODIFY, payload={}
|
|
||||||
)
|
|
||||||
await self._send(writer, packet)
|
|
||||||
@@ -4,12 +4,10 @@ import time
|
|||||||
import json
|
import json
|
||||||
import re
|
import re
|
||||||
from classes.baseprocessor import BaseProcessor
|
from classes.baseprocessor import BaseProcessor
|
||||||
from common.sms import send_sms_code
|
|
||||||
from tamtam.models import (
|
from tamtam.models import (
|
||||||
RequestCodePayloadModel,
|
RequestCodePayloadModel,
|
||||||
VerifyCodePayloadModel,
|
VerifyCodePayloadModel,
|
||||||
FinalAuthPayloadModel,
|
FinalAuthPayloadModel,
|
||||||
AuthConfirmRegisterPayloadModel,
|
|
||||||
LoginPayloadModel,
|
LoginPayloadModel,
|
||||||
)
|
)
|
||||||
from tamtam.config import TTConfig
|
from tamtam.config import TTConfig
|
||||||
@@ -19,172 +17,6 @@ class AuthProcessors(BaseProcessor):
|
|||||||
super().__init__(db_pool, clients, send_event, type)
|
super().__init__(db_pool, clients, send_event, type)
|
||||||
self.server_config = TTConfig().SERVER_CONFIG
|
self.server_config = TTConfig().SERVER_CONFIG
|
||||||
|
|
||||||
async def _finish_auth(self, payload, seq, writer, cursor, phone, hashed_token, hashed_login, account, deviceType, deviceName, ip, login):
|
|
||||||
"""Завершение существующего пользователя"""
|
|
||||||
# Валидируем данные пакета
|
|
||||||
try:
|
|
||||||
FinalAuthPayloadModel.model_validate(payload)
|
|
||||||
except Exception as e:
|
|
||||||
await self._send_error(seq, self.opcodes.AUTH_CONFIRM,
|
|
||||||
self.error_types.INVALID_PAYLOAD, writer)
|
|
||||||
return None
|
|
||||||
|
|
||||||
# Удаляем токен
|
|
||||||
await cursor.execute("DELETE FROM auth_tokens WHERE token_hash = %s", (hashed_token,))
|
|
||||||
|
|
||||||
# Создаем сессию
|
|
||||||
await cursor.execute(
|
|
||||||
"INSERT INTO tokens (phone, token_hash, device_type, device_name, location, time) VALUES (%s, %s, %s, %s, %s, %s)",
|
|
||||||
(
|
|
||||||
phone,
|
|
||||||
hashed_login,
|
|
||||||
deviceType,
|
|
||||||
deviceName,
|
|
||||||
self.tools.get_geo(
|
|
||||||
ip=ip, db_path=self.config.geo_db_path
|
|
||||||
),
|
|
||||||
int(time.time() * 1000)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
# Аватарка с биографией
|
|
||||||
photo_id = None if not account.get("avatar_id") else int(account.get("avatar_id"))
|
|
||||||
avatar_url = None if not photo_id else self.config.avatar_base_url + str(photo_id)
|
|
||||||
description = None if not account.get("description") else account.get("description")
|
|
||||||
|
|
||||||
# Собираем данные пакета
|
|
||||||
return {
|
|
||||||
"userToken": str(account.get("id")),
|
|
||||||
"profile": self.tools.generate_profile_tt(
|
|
||||||
id=account.get("id"),
|
|
||||||
phone=int(account.get("phone")),
|
|
||||||
avatarUrl=avatar_url,
|
|
||||||
photoId=photo_id,
|
|
||||||
updateTime=int(account.get("updatetime")),
|
|
||||||
firstName=account.get("firstname"),
|
|
||||||
lastName=account.get("lastname"),
|
|
||||||
options=json.loads(account.get("options")),
|
|
||||||
description=description,
|
|
||||||
username=account.get("username")
|
|
||||||
),
|
|
||||||
"tokenType": "LOGIN",
|
|
||||||
"token": login
|
|
||||||
}
|
|
||||||
|
|
||||||
async def _finish_reg(self, payload, seq, writer, cursor, phone, hashed_token, hashed_login, deviceType, deviceName, ip, login):
|
|
||||||
"""Регистрация пользователя во время авторизации"""
|
|
||||||
# Валидируем данные пакета
|
|
||||||
try:
|
|
||||||
AuthConfirmRegisterPayloadModel.model_validate(payload)
|
|
||||||
except Exception as e:
|
|
||||||
await self._send_error(seq, self.opcodes.AUTH_CONFIRM,
|
|
||||||
self.error_types.INVALID_PAYLOAD, writer)
|
|
||||||
return None
|
|
||||||
|
|
||||||
name = payload.get("name", "").strip()
|
|
||||||
|
|
||||||
now_ms = int(time.time() * 1000)
|
|
||||||
now_s = int(time.time())
|
|
||||||
|
|
||||||
# Генерируем ID пользователя
|
|
||||||
user_id = await self.tools.generate_user_id(self.db_pool)
|
|
||||||
|
|
||||||
# Создаем пользователя
|
|
||||||
|
|
||||||
# NOTE: На бумаге у нас как бы полная поддержка ТТ (ну, все функции, в которые может макс),
|
|
||||||
# а клиенты тамтама не знают, что такое фамилия в аккаунтах тамтама (оно предназначено только для ОК)
|
|
||||||
# по этому просто не писать указывать фамилию в бд, ее клиент и так не отдаст
|
|
||||||
|
|
||||||
await cursor.execute(
|
|
||||||
"""
|
|
||||||
INSERT INTO users
|
|
||||||
(id, phone, telegram_id, firstname, lastname, username,
|
|
||||||
profileoptions, options, accountstatus, updatetime, lastseen)
|
|
||||||
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
|
|
||||||
""",
|
|
||||||
(
|
|
||||||
user_id,
|
|
||||||
phone,
|
|
||||||
None,
|
|
||||||
name,
|
|
||||||
None,
|
|
||||||
None,
|
|
||||||
json.dumps([]),
|
|
||||||
json.dumps(["TT", "ONEME"]),
|
|
||||||
0,
|
|
||||||
str(now_ms),
|
|
||||||
str(now_s),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
# Добавляем данные аккаунта
|
|
||||||
await cursor.execute(
|
|
||||||
"""
|
|
||||||
INSERT INTO user_data
|
|
||||||
(phone, user_config, chat_config)
|
|
||||||
VALUES (%s, %s, %s)
|
|
||||||
""",
|
|
||||||
(
|
|
||||||
phone,
|
|
||||||
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,))
|
|
||||||
|
|
||||||
# Создаем сессию
|
|
||||||
await cursor.execute(
|
|
||||||
"INSERT INTO tokens (phone, token_hash, device_type, device_name, location, time) VALUES (%s, %s, %s, %s, %s, %s)",
|
|
||||||
(
|
|
||||||
phone,
|
|
||||||
hashed_login,
|
|
||||||
deviceType or "ANDROID",
|
|
||||||
deviceName or "Unknown",
|
|
||||||
self.tools.get_geo(
|
|
||||||
ip=ip, db_path=self.config.geo_db_path
|
|
||||||
),
|
|
||||||
now_ms,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
# Генерируем профиль
|
|
||||||
profile = self.tools.generate_profile_tt(
|
|
||||||
id=user_id,
|
|
||||||
phone=int(phone),
|
|
||||||
avatarUrl=None,
|
|
||||||
photoId=None,
|
|
||||||
updateTime=now_ms,
|
|
||||||
firstName=name,
|
|
||||||
lastName="",
|
|
||||||
options=["TT", "ONEME"],
|
|
||||||
description=None,
|
|
||||||
username=None,
|
|
||||||
)
|
|
||||||
|
|
||||||
self.logger.info(
|
|
||||||
f"Новый пользователь зарегистрирован: phone={phone} id={user_id} name={name}"
|
|
||||||
)
|
|
||||||
|
|
||||||
# Собираем данные пакета
|
|
||||||
return {
|
|
||||||
"userToken": "0",
|
|
||||||
"profile": profile,
|
|
||||||
"tokenType": "LOGIN",
|
|
||||||
"token": login,
|
|
||||||
}
|
|
||||||
|
|
||||||
async def auth_request(self, payload, seq, writer):
|
async def auth_request(self, payload, seq, writer):
|
||||||
"""Обработчик запроса кода"""
|
"""Обработчик запроса кода"""
|
||||||
# Валидируем данные пакета
|
# Валидируем данные пакета
|
||||||
@@ -198,51 +30,29 @@ class AuthProcessors(BaseProcessor):
|
|||||||
# Извлекаем телефон из пакета
|
# Извлекаем телефон из пакета
|
||||||
phone = re.sub(r'\D', '', payload.get("phone", ""))
|
phone = re.sub(r'\D', '', payload.get("phone", ""))
|
||||||
|
|
||||||
# Генерируем токен
|
# Генерируем токен с кодом
|
||||||
|
code = f"{secrets.randbelow(1_000_000):06d}"
|
||||||
token = secrets.token_urlsafe(128)
|
token = secrets.token_urlsafe(128)
|
||||||
|
|
||||||
|
# Хешируем
|
||||||
|
code_hash = hashlib.sha256(code.encode()).hexdigest()
|
||||||
token_hash = hashlib.sha256(token.encode()).hexdigest()
|
token_hash = hashlib.sha256(token.encode()).hexdigest()
|
||||||
|
|
||||||
# Срок жизни токена (5 минут)
|
# Срок жизни токена (5 минут)
|
||||||
expires = int(time.time()) + 300
|
expires = int(time.time()) + 300
|
||||||
|
|
||||||
user_exists = False
|
# Ищем пользователя, и если он существует, сохраняем токен
|
||||||
|
|
||||||
# Ищем пользователя
|
|
||||||
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("SELECT * FROM users WHERE phone = %s", (phone,))
|
await cursor.execute("SELECT * FROM users WHERE phone = %s", (phone,))
|
||||||
user = await cursor.fetchone()
|
user = await cursor.fetchone()
|
||||||
|
|
||||||
# Получаем код через SMS шлюз или генерируем локально
|
# Если пользователь существует, сохраняем токен
|
||||||
local_fallback_code = False
|
|
||||||
if self.config.sms_gateway_url:
|
|
||||||
code = await send_sms_code(self.config.sms_gateway_url, phone)
|
|
||||||
|
|
||||||
if code is None:
|
|
||||||
code = f"{secrets.randbelow(1_000_000):06d}"
|
|
||||||
local_fallback_code = True
|
|
||||||
else:
|
|
||||||
code = f"{secrets.randbelow(1_000_000):06d}"
|
|
||||||
local_fallback_code = True
|
|
||||||
|
|
||||||
# Хешируем
|
|
||||||
code_hash = hashlib.sha256(code.encode()).hexdigest()
|
|
||||||
|
|
||||||
# Сохраняем токен
|
|
||||||
async with self.db_pool.acquire() as conn:
|
|
||||||
async with conn.cursor() as cursor:
|
|
||||||
if user:
|
if user:
|
||||||
user_exists = True
|
|
||||||
await cursor.execute(
|
await cursor.execute(
|
||||||
"INSERT INTO auth_tokens (phone, token_hash, code_hash, expires, state) VALUES (%s, %s, %s, %s, %s)",
|
"INSERT INTO auth_tokens (phone, token_hash, code_hash, expires, state) VALUES (%s, %s, %s, %s, %s)",
|
||||||
(phone, token_hash, code_hash, expires, "started")
|
(phone, token_hash, code_hash, expires, "started")
|
||||||
)
|
)
|
||||||
else:
|
|
||||||
# Пользователь не найден - сохраняем токен в register
|
|
||||||
await cursor.execute(
|
|
||||||
"INSERT INTO auth_tokens (phone, token_hash, code_hash, expires, state) VALUES (%s, %s, %s, %s, %s)",
|
|
||||||
(phone, token_hash, code_hash, expires, "register")
|
|
||||||
)
|
|
||||||
|
|
||||||
# Данные пакета
|
# Данные пакета
|
||||||
payload = {
|
payload = {
|
||||||
@@ -261,7 +71,7 @@ class AuthProcessors(BaseProcessor):
|
|||||||
|
|
||||||
# Отправляем
|
# Отправляем
|
||||||
await self._send(writer, packet)
|
await self._send(writer, packet)
|
||||||
self.logger.debug(f"Код для {phone}: {code} (существующий={user_exists})")
|
self.logger.debug(f"Код для {phone}: {code}")
|
||||||
|
|
||||||
async def auth(self, payload, seq, writer):
|
async def auth(self, payload, seq, writer):
|
||||||
"""Обработчик проверки кода"""
|
"""Обработчик проверки кода"""
|
||||||
@@ -302,32 +112,13 @@ class AuthProcessors(BaseProcessor):
|
|||||||
self.error_types.INVALID_CODE, writer)
|
self.error_types.INVALID_CODE, writer)
|
||||||
return
|
return
|
||||||
|
|
||||||
# Если это новый пользователь - переводим токен в verified
|
|
||||||
# и отдаём клиенту NEW токен, чтобы он показал экран ввода имени
|
|
||||||
if stored_token.get("state") == "register":
|
|
||||||
await cursor.execute(
|
|
||||||
"UPDATE auth_tokens SET state = %s WHERE token_hash = %s",
|
|
||||||
("verified", hashed_token)
|
|
||||||
)
|
|
||||||
packet = self.proto.pack_packet(
|
|
||||||
cmd=self.proto.CMD_OK,
|
|
||||||
seq=seq,
|
|
||||||
opcode=self.opcodes.AUTH,
|
|
||||||
payload={
|
|
||||||
"tokenAttrs": {"NEW": {"token": token}},
|
|
||||||
"tokenTypes": {"NEW": token},
|
|
||||||
},
|
|
||||||
)
|
|
||||||
await self._send(writer, packet)
|
|
||||||
return
|
|
||||||
|
|
||||||
# Ищем аккаунт
|
# Ищем аккаунт
|
||||||
await cursor.execute("SELECT * FROM users WHERE phone = %s", (stored_token.get("phone"),))
|
await cursor.execute("SELECT * FROM users WHERE phone = %s", (stored_token.get("phone"),))
|
||||||
account = await cursor.fetchone()
|
account = await cursor.fetchone()
|
||||||
|
|
||||||
# Обновляем состояние токена
|
# Обновляем состояние токена
|
||||||
await cursor.execute(
|
await cursor.execute(
|
||||||
"UPDATE auth_tokens SET state = %s WHERE token_hash = %s",
|
"UPDATE auth_tokens set state = %s WHERE token_hash = %s",
|
||||||
("verified", hashed_token)
|
("verified", hashed_token)
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -367,17 +158,22 @@ class AuthProcessors(BaseProcessor):
|
|||||||
|
|
||||||
await self._send(writer, packet)
|
await self._send(writer, packet)
|
||||||
|
|
||||||
async def auth_confirm(self, payload, seq, writer, deviceType, deviceName, ip):
|
async def auth_confirm(self, payload, seq, writer, deviceType, deviceName):
|
||||||
"""Обработчик финальной аутентификации / регистрации"""
|
"""Обработчик финальной аутентификации"""
|
||||||
|
# Валидируем данные пакета
|
||||||
|
try:
|
||||||
|
FinalAuthPayloadModel.model_validate(payload)
|
||||||
|
except Exception as e:
|
||||||
|
await self._send_error(seq, self.opcodes.AUTH_CONFIRM,
|
||||||
|
self.error_types.INVALID_PAYLOAD, writer)
|
||||||
|
return
|
||||||
|
|
||||||
# Извлекаем данные из пакета
|
# Извлекаем данные из пакета
|
||||||
token = payload.get("token")
|
token = payload.get("token")
|
||||||
|
|
||||||
if not deviceType:
|
if not deviceType:
|
||||||
deviceType = payload.get("deviceType")
|
deviceType = payload.get("deviceType")
|
||||||
|
|
||||||
if not deviceName:
|
|
||||||
deviceName = "Unknown device"
|
|
||||||
|
|
||||||
# Хешируем токен
|
# Хешируем токен
|
||||||
hashed_token = hashlib.sha256(token.encode()).hexdigest()
|
hashed_token = hashlib.sha256(token.encode()).hexdigest()
|
||||||
|
|
||||||
@@ -385,9 +181,10 @@ class AuthProcessors(BaseProcessor):
|
|||||||
login = secrets.token_urlsafe(128)
|
login = secrets.token_urlsafe(128)
|
||||||
hashed_login = hashlib.sha256(login.encode()).hexdigest()
|
hashed_login = hashlib.sha256(login.encode()).hexdigest()
|
||||||
|
|
||||||
# Ищем токен
|
# Ищем токен с кодом
|
||||||
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 * FROM auth_tokens WHERE token_hash = %s AND expires > UNIX_TIMESTAMP()",
|
"SELECT * FROM auth_tokens WHERE token_hash = %s AND expires > UNIX_TIMESTAMP()",
|
||||||
(hashed_token,)
|
(hashed_token,)
|
||||||
@@ -399,36 +196,53 @@ class AuthProcessors(BaseProcessor):
|
|||||||
self.error_types.INVALID_TOKEN, writer)
|
self.error_types.INVALID_TOKEN, writer)
|
||||||
return
|
return
|
||||||
|
|
||||||
# Если авторизация только началась (код ещё не проверен) - отдаем ошибку
|
# Если авторизация только началась - отдаем ошибку
|
||||||
if stored_token.get("state") == "started" or stored_token.get("state") == "register":
|
if stored_token.get("state") == "started":
|
||||||
await self._send_error(seq, self.opcodes.AUTH_CONFIRM,
|
await self._send_error(seq, self.opcodes.AUTH_CONFIRM,
|
||||||
self.error_types.INVALID_TOKEN, writer)
|
self.error_types.INVALID_TOKEN, writer)
|
||||||
return
|
return
|
||||||
|
|
||||||
phone = stored_token.get("phone")
|
# Ищем аккаунт
|
||||||
|
await cursor.execute("SELECT * FROM users WHERE phone = %s", (stored_token.get("phone"),))
|
||||||
# Проверяем, существует ли пользователь
|
|
||||||
await cursor.execute("SELECT * FROM users WHERE phone = %s", (phone,))
|
|
||||||
account = await cursor.fetchone()
|
account = await cursor.fetchone()
|
||||||
|
|
||||||
# Если пользователь есть, производим создание сессии
|
# Удаляем токен
|
||||||
if account:
|
await cursor.execute("DELETE FROM auth_tokens WHERE token_hash = %s", (hashed_token,))
|
||||||
resp_payload = await self._finish_auth(
|
|
||||||
payload, seq, writer, cursor, phone, hashed_token,
|
|
||||||
hashed_login, account, deviceType, deviceName, ip, login
|
|
||||||
)
|
|
||||||
else: # в ином случае производим регистрацию
|
|
||||||
resp_payload = await self._finish_reg(
|
|
||||||
payload, seq, writer, cursor, phone, hashed_token,
|
|
||||||
hashed_login, deviceType, deviceName, ip, login
|
|
||||||
)
|
|
||||||
|
|
||||||
if resp_payload is None:
|
# Создаем сессию
|
||||||
return
|
await cursor.execute(
|
||||||
|
"INSERT INTO tokens (phone, token_hash, device_type, device_name, location, time) VALUES (%s, %s, %s, %s, %s, %s)",
|
||||||
|
(stored_token.get("phone"), hashed_login, deviceType, deviceName,
|
||||||
|
"Epstein Island", int(time.time()))
|
||||||
|
)
|
||||||
|
|
||||||
|
# Аватарка с биографией
|
||||||
|
photo_id = None if not account.get("avatar_id") else int(account.get("avatar_id"))
|
||||||
|
avatar_url = None if not photo_id else self.config.avatar_base_url + str(photo_id)
|
||||||
|
description = None if not account.get("description") else account.get("description")
|
||||||
|
|
||||||
|
# Собираем данные пакета
|
||||||
|
payload = {
|
||||||
|
"userToken": "0", # Пока как заглушка
|
||||||
|
"profile": self.tools.generate_profile_tt(
|
||||||
|
id=account.get("id"),
|
||||||
|
phone=int(account.get("phone")),
|
||||||
|
avatarUrl=avatar_url,
|
||||||
|
photoId=photo_id,
|
||||||
|
updateTime=int(account.get("updatetime")),
|
||||||
|
firstName=account.get("firstname"),
|
||||||
|
lastName=account.get("lastname"),
|
||||||
|
options=json.loads(account.get("options")),
|
||||||
|
description=description,
|
||||||
|
username=account.get("username")
|
||||||
|
),
|
||||||
|
"tokenType": "LOGIN",
|
||||||
|
"token": login
|
||||||
|
}
|
||||||
|
|
||||||
# Создаем пакет
|
# Создаем пакет
|
||||||
packet = self.proto.pack_packet(
|
packet = self.proto.pack_packet(
|
||||||
cmd=self.proto.CMD_OK, seq=seq, opcode=self.opcodes.AUTH_CONFIRM, payload=resp_payload
|
cmd=self.proto.CMD_OK, seq=seq, opcode=self.opcodes.AUTH_CONFIRM, payload=payload
|
||||||
)
|
)
|
||||||
|
|
||||||
# Отправляем
|
# Отправляем
|
||||||
@@ -443,7 +257,7 @@ class AuthProcessors(BaseProcessor):
|
|||||||
self.logger.error(f"Возникли ошибки при валидации пакета: {e}")
|
self.logger.error(f"Возникли ошибки при валидации пакета: {e}")
|
||||||
await self._send_error(seq, self.opcodes.LOGIN,
|
await self._send_error(seq, self.opcodes.LOGIN,
|
||||||
self.error_types.INVALID_PAYLOAD, writer)
|
self.error_types.INVALID_PAYLOAD, writer)
|
||||||
return None, None, None
|
return
|
||||||
|
|
||||||
# Чаты, где состоит пользователь
|
# Чаты, где состоит пользователь
|
||||||
chats = []
|
chats = []
|
||||||
@@ -464,7 +278,7 @@ class AuthProcessors(BaseProcessor):
|
|||||||
if token_data is None:
|
if token_data is None:
|
||||||
await self._send_error(seq, self.opcodes.LOGIN,
|
await self._send_error(seq, self.opcodes.LOGIN,
|
||||||
self.error_types.INVALID_TOKEN, writer)
|
self.error_types.INVALID_TOKEN, writer)
|
||||||
return None, None, None
|
return
|
||||||
|
|
||||||
# Ищем аккаунт пользователя в бд
|
# Ищем аккаунт пользователя в бд
|
||||||
await cursor.execute("SELECT * FROM users WHERE phone = %s", (token_data.get("phone"),))
|
await cursor.execute("SELECT * FROM users WHERE phone = %s", (token_data.get("phone"),))
|
||||||
@@ -476,8 +290,8 @@ class AuthProcessors(BaseProcessor):
|
|||||||
|
|
||||||
# Ищем все чаты, где состоит пользователь
|
# Ищем все чаты, где состоит пользователь
|
||||||
await cursor.execute(
|
await cursor.execute(
|
||||||
"SELECT * FROM chat_participants WHERE user_id = %s",
|
"SELECT * FROM chat_participants WHERE user_id = %s",
|
||||||
(user.get('id'),)
|
(user.get('id'))
|
||||||
)
|
)
|
||||||
user_chats = await cursor.fetchall()
|
user_chats = await cursor.fetchall()
|
||||||
|
|
||||||
@@ -486,12 +300,6 @@ class AuthProcessors(BaseProcessor):
|
|||||||
chat.get("chat_id")
|
chat.get("chat_id")
|
||||||
)
|
)
|
||||||
|
|
||||||
# Обновляем юзер конфиг
|
|
||||||
updated_user_config = await self.tools.update_user_config(
|
|
||||||
cursor, token_data.get("phone"),
|
|
||||||
user_data.get("user_config"), self.static.USER_SETTINGS
|
|
||||||
)
|
|
||||||
|
|
||||||
# Аватарка с биографией
|
# Аватарка с биографией
|
||||||
photo_id = None if not user.get("avatar_id") else int(user.get("avatar_id"))
|
photo_id = None if not user.get("avatar_id") else int(user.get("avatar_id"))
|
||||||
avatar_url = None if not photo_id else self.config.avatar_base_url + str(photo_id)
|
avatar_url = None if not photo_id else self.config.avatar_base_url + str(photo_id)
|
||||||
@@ -516,27 +324,18 @@ class AuthProcessors(BaseProcessor):
|
|||||||
include_favourites=False
|
include_favourites=False
|
||||||
)
|
)
|
||||||
|
|
||||||
# Генерируем список контактов
|
|
||||||
contacts = await self.tools.collect_user_contacts(
|
|
||||||
user.get("id"), self.db_pool, self.config.avatar_base_url
|
|
||||||
)
|
|
||||||
|
|
||||||
# Собираем статусы контактов
|
|
||||||
contact_ids = [c.get("id") for c in contacts if c.get("id") is not None]
|
|
||||||
presence = await self.tools.collect_presence(contact_ids, self.clients, self.db_pool)
|
|
||||||
|
|
||||||
# Формируем данные пакета
|
# Формируем данные пакета
|
||||||
payload = {
|
payload = {
|
||||||
"profile": profile,
|
"profile": profile,
|
||||||
"chats": chats,
|
"chats": chats,
|
||||||
"chatMarker": 0,
|
"chatMarker": 0,
|
||||||
"messages": {},
|
"messages": {},
|
||||||
"contacts": contacts,
|
"contacts": [],
|
||||||
"presence": presence,
|
"presence": {},
|
||||||
"config": {
|
"config": {
|
||||||
"hash": "0",
|
"hash": "e5903aa8-0000000000000000-80000106-0000000000000001-00000001-0000000000000000-00000000-2-00000001-0000019c9559d057",
|
||||||
"server": self.server_config,
|
"server": self.server_config,
|
||||||
"user": updated_user_config,
|
"user": json.loads(user_data.get("user_config")),
|
||||||
"chatFolders": {
|
"chatFolders": {
|
||||||
"FOLDERS": [],
|
"FOLDERS": [],
|
||||||
"ALL_FILTER_EXCLUDE": []
|
"ALL_FILTER_EXCLUDE": []
|
||||||
@@ -558,10 +357,6 @@ class AuthProcessors(BaseProcessor):
|
|||||||
"time": int(time.time() * 1000)
|
"time": int(time.time() * 1000)
|
||||||
}
|
}
|
||||||
|
|
||||||
# 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.LOGIN, payload=payload
|
cmd=self.proto.CMD_OK, seq=seq, opcode=self.opcodes.LOGIN, payload=payload
|
||||||
@@ -569,39 +364,4 @@ class AuthProcessors(BaseProcessor):
|
|||||||
|
|
||||||
# Отправляем
|
# Отправляем
|
||||||
await self._send(writer, packet)
|
await self._send(writer, packet)
|
||||||
return int(user.get("phone")), int(user.get("id")), hashed_token
|
return int(user.get("phone")), int(user.get("id")), hashed_token
|
||||||
|
|
||||||
async def logout(self, seq, writer, hashedToken):
|
|
||||||
"""Обработчик завершения сессии"""
|
|
||||||
# Удаляем токен из бд
|
|
||||||
async with self.db_pool.acquire() as conn:
|
|
||||||
async with conn.cursor() as cursor:
|
|
||||||
await cursor.execute(
|
|
||||||
"DELETE FROM tokens WHERE token_hash = %s", (hashedToken,)
|
|
||||||
)
|
|
||||||
|
|
||||||
# ⣿⡇⣽⣿⣿⣿⣧⠘⣿⣿⠠⣤⣍⡛⢿⣿⣿⠏⣰⣿⣿⣿⣿⣿⡆⢿
|
|
||||||
# ⣿⢀⣿⣿⣿⣿⣿⣷⡈⢿⡄⢿⣿⣿⣦⡙⠏⣰⣿⣿⣿⣿⣿⣿⡇⣿
|
|
||||||
# ⣿⢸⣿⣿⣿⣿⣿⣿⡿⠄⣡⣤⣿⣿⣿⣿⣄⣿⣿⣿⣿⣿⣿⣿⡇⣿
|
|
||||||
# ⣿⢸⣿⣿⣿⣿⣿⣿⣤⣬⣭⣬⣬⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⣿
|
|
||||||
# ⣿⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠇⣿
|
|
||||||
# ⣿⡀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠿⠿⠿⠿⣿⣿⡿⢠⣿
|
|
||||||
# ⣿⣧⠸⣿⣧⠀⣴⡆⣤⠀⢸⣿⣿⣿⣿⠀⠀⢸⣿⡌⣶⣿⠟⢁⣾⣿
|
|
||||||
# ⠙⠛⠂⠹⡿⢸⣿⡇⢸⠁⢸⣿⣿⣿⣿⠀⠀⢈⣿⡇⢸⣯⣤⣤⠀⣿
|
|
||||||
# ⣆⠙⣿⣿⣇⢸⣿⣇⠀⢀⣾⡿⢿⣿⣿⣀⣀⣼⣿⡇⣸⣿⡿⢁⣾⣿
|
|
||||||
# ⣿⣷⢀⡟⡉⠞⢻⣿⣿⣿⣿⣶⣾⣿⣿⣿⣿⣿⠋⠘⣹⣿⡄⢻⣿⣿
|
|
||||||
# ⣿⡇⣼⣿⣧⣶⣿⣿⣿⣟⠻⢋⣍⣉⣋⣼⣿⣿⣿⣶⢿⣿⣿⡄⢻⣿
|
|
||||||
# ⣿⣧⣭⣭⣄⡙⠻⢿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠿⠛⣡⣤⣭⣤⣴⣾⣿
|
|
||||||
# ⣿⣿⣿⣿⣿⣿⣇⠠⣬⣭⣽⣿⣿⣿⣿⣿⣷⡈⢿⣿⣿⣿⣿⣿⣿⣿
|
|
||||||
# ⣿⣿⣿⣿⣿⣿⣿⣦⠙⣿⣿⣿⣿⣿⣿⣿⣿⣷⡈⣿⣿⣿⣿⣿⣿⣿
|
|
||||||
# ⣿⣿⣿⣿⣿⣿⣿⠃⠼⢿⣿⣿⣿⣿⣿⣿⣿⣿⣧⠹⣿⣿⣿⣿⣿⣿
|
|
||||||
# ⣿⣿⣿⣿⣿⣿⣿⣶⠆⣼⣿⣿⣿⣿⣿⣿⣿⣿⣿⡄⣿⣿⣿⣿⣿⣿
|
|
||||||
# ⣿⣿⣿⣿⣿⣿⣿⣿⢀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⣿⣿⣿⣿
|
|
||||||
|
|
||||||
# Создаем пакет
|
|
||||||
response = self.proto.pack_packet(
|
|
||||||
cmd=self.proto.CMD_OK, seq=seq, opcode=self.opcodes.LOGOUT, payload=None
|
|
||||||
)
|
|
||||||
|
|
||||||
# Отправляем
|
|
||||||
await self._send(writer, response)
|
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
import pydantic
|
|
||||||
from classes.baseprocessor import BaseProcessor
|
|
||||||
from tamtam.models import ChatSubscribePayloadModel
|
|
||||||
|
|
||||||
class ChatsProcessors(BaseProcessor):
|
|
||||||
async def chat_subscribe(self, payload, seq, writer):
|
|
||||||
# Валидируем входные данные
|
|
||||||
try:
|
|
||||||
ChatSubscribePayloadModel.model_validate(payload)
|
|
||||||
except Exception as e:
|
|
||||||
await self._send_error(seq, self.opcodes.CHAT_SUBSCRIBE, self.error_types.INVALID_PAYLOAD, writer)
|
|
||||||
return
|
|
||||||
|
|
||||||
# Созадаем пакет
|
|
||||||
packet = self.proto.pack_packet(
|
|
||||||
cmd=self.proto.CMD_OK, seq=seq, opcode=self.opcodes.CHAT_SUBSCRIBE, payload=None
|
|
||||||
)
|
|
||||||
|
|
||||||
# Отправялем
|
|
||||||
await self._send(writer, packet)
|
|
||||||
@@ -1,301 +0,0 @@
|
|||||||
import pydantic
|
|
||||||
import json
|
|
||||||
import time
|
|
||||||
from classes.baseprocessor import BaseProcessor
|
|
||||||
from tamtam.models import ContactListPayloadModel, ContactPresencePayloadModel, ContactUpdatePayloadModel
|
|
||||||
|
|
||||||
class ContactsProcessors(BaseProcessor):
|
|
||||||
async def contact_list(self, payload, seq, writer, userId):
|
|
||||||
"""Обработчик получения контактов"""
|
|
||||||
# Валидируем данные пакета
|
|
||||||
try:
|
|
||||||
ContactListPayloadModel.model_validate(payload)
|
|
||||||
except pydantic.ValidationError as error:
|
|
||||||
self.logger.error(f"Возникли ошибки при валидации пакета: {error}")
|
|
||||||
await self._send_error(seq, self.opcodes.CONTACT_LIST, self.error_types.INVALID_PAYLOAD, writer)
|
|
||||||
return
|
|
||||||
|
|
||||||
status = payload.get("status")
|
|
||||||
count = payload.get("count")
|
|
||||||
|
|
||||||
# Итоговый контакт-лист
|
|
||||||
contact_list = []
|
|
||||||
|
|
||||||
if status == "BLOCKED":
|
|
||||||
# Собираем контакты, которые в черном списке
|
|
||||||
blocked = []
|
|
||||||
|
|
||||||
async with self.db_pool.acquire() as conn:
|
|
||||||
async with conn.cursor() as cursor:
|
|
||||||
if count:
|
|
||||||
await cursor.execute(
|
|
||||||
"SELECT * FROM contacts WHERE owner_id = %s AND is_blocked = TRUE LIMIT %s",
|
|
||||||
(userId, count),
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
await cursor.execute(
|
|
||||||
"SELECT * FROM contacts WHERE owner_id = %s AND is_blocked = TRUE",
|
|
||||||
(userId,),
|
|
||||||
)
|
|
||||||
rows = await cursor.fetchall()
|
|
||||||
|
|
||||||
for row in rows:
|
|
||||||
blocked.append(
|
|
||||||
{
|
|
||||||
"id": int(row.get("contact_id")),
|
|
||||||
"firstname": row.get("custom_firstname"),
|
|
||||||
"lastname": row.get("custom_lastname"),
|
|
||||||
"blocked": True,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
# Генерируем контакт-лист
|
|
||||||
contact_list = await self.tools.generate_contacts(
|
|
||||||
blocked, self.db_pool, avatar_base_url=self.config.avatar_base_url
|
|
||||||
)
|
|
||||||
|
|
||||||
# Собираем данные пакета
|
|
||||||
response_payload = {
|
|
||||||
"contacts": contact_list
|
|
||||||
}
|
|
||||||
|
|
||||||
# Создаем пакет
|
|
||||||
packet = self.proto.pack_packet(
|
|
||||||
seq=seq, opcode=self.opcodes.CONTACT_LIST, payload=response_payload
|
|
||||||
)
|
|
||||||
|
|
||||||
# Отправляем пакет
|
|
||||||
await self._send(writer, packet)
|
|
||||||
|
|
||||||
async def contact_update(self, payload, seq, writer, userId):
|
|
||||||
"""
|
|
||||||
Обработчик опкода какого-то там
|
|
||||||
(их хуй запомнишь, даже в мриме команды помню, бля)
|
|
||||||
|
|
||||||
Отвечает за добавку, удаление, блокировку и разблокировку контакта
|
|
||||||
"""
|
|
||||||
# Валидируем данные пакета
|
|
||||||
try:
|
|
||||||
ContactUpdatePayloadModel.model_validate(payload)
|
|
||||||
except pydantic.ValidationError as error:
|
|
||||||
self.logger.error(f"Возникли ошибки при валидации пакета: {error}")
|
|
||||||
await self._send_error(seq, self.opcodes.CONTACT_UPDATE, self.error_types.INVALID_PAYLOAD, writer)
|
|
||||||
return
|
|
||||||
|
|
||||||
action = payload.get("action")
|
|
||||||
contactId = payload.get("contactId")
|
|
||||||
firstName = payload.get("firstName")
|
|
||||||
lastName = payload.get("lastName", "")
|
|
||||||
|
|
||||||
if action == "ADD":
|
|
||||||
# Проверяем, существует ли пользователь с таким ID
|
|
||||||
async with self.db_pool.acquire() as conn:
|
|
||||||
async with conn.cursor() as cursor:
|
|
||||||
await cursor.execute("SELECT * FROM users WHERE id = %s", (contactId,))
|
|
||||||
user = await cursor.fetchone()
|
|
||||||
|
|
||||||
if not user:
|
|
||||||
await self._send_error(seq, self.opcodes.CONTACT_UPDATE, self.error_types.USER_NOT_FOUND, writer)
|
|
||||||
return
|
|
||||||
|
|
||||||
# Проверяем, не добавлен ли уже контакт
|
|
||||||
await cursor.execute(
|
|
||||||
"SELECT * FROM contacts WHERE owner_id = %s AND contact_id = %s",
|
|
||||||
(userId, contactId)
|
|
||||||
)
|
|
||||||
row = await cursor.fetchone()
|
|
||||||
|
|
||||||
# Если контакта не существует, то можем продолжать,
|
|
||||||
if not row:
|
|
||||||
# Добавляем контакт
|
|
||||||
await cursor.execute(
|
|
||||||
"INSERT INTO contacts (owner_id, contact_id, custom_firstname, custom_lastname, is_blocked) VALUES (%s, %s, %s, %s, FALSE)",
|
|
||||||
(userId, contactId, firstName, lastName)
|
|
||||||
)
|
|
||||||
# а если уже существует, отправляем ошибку
|
|
||||||
else:
|
|
||||||
await self._send_error(seq, self.opcodes.CONTACT_UPDATE, self.error_types.CONTACT_ALREADY_ADDED, writer)
|
|
||||||
return
|
|
||||||
|
|
||||||
# Генерируем профиль
|
|
||||||
photoId = None if not user.get("avatar_id") else int(user.get("avatar_id"))
|
|
||||||
avatar_url = None if not photoId else self.config.avatar_base_url + str(photoId)
|
|
||||||
|
|
||||||
contact = self.tools.generate_profile(
|
|
||||||
id=user.get("id"),
|
|
||||||
phone=int(user.get("phone")),
|
|
||||||
avatarUrl=avatar_url,
|
|
||||||
photoId=photoId,
|
|
||||||
updateTime=int(user.get("updatetime")),
|
|
||||||
firstName=user.get("firstname"),
|
|
||||||
lastName=user.get("lastname"),
|
|
||||||
options=json.loads(user.get("options")),
|
|
||||||
accountStatus=int(user.get("accountstatus")),
|
|
||||||
includeProfileOptions=False,
|
|
||||||
custom_firstname=firstName,
|
|
||||||
custom_lastname=lastName,
|
|
||||||
)
|
|
||||||
|
|
||||||
response_payload = {
|
|
||||||
"contact": contact
|
|
||||||
}
|
|
||||||
|
|
||||||
packet = self.proto.pack_packet(
|
|
||||||
cmd=self.proto.CMD_OK, seq=seq, opcode=self.opcodes.CONTACT_UPDATE, payload=response_payload
|
|
||||||
)
|
|
||||||
|
|
||||||
await self._send(writer, packet)
|
|
||||||
|
|
||||||
elif action == "REMOVE":
|
|
||||||
# Удаляем контакт
|
|
||||||
async with self.db_pool.acquire() as conn:
|
|
||||||
async with conn.cursor() as cursor:
|
|
||||||
await cursor.execute(
|
|
||||||
"DELETE FROM contacts WHERE owner_id = %s AND contact_id = %s",
|
|
||||||
(userId, contactId)
|
|
||||||
)
|
|
||||||
|
|
||||||
packet = self.proto.pack_packet(
|
|
||||||
cmd=self.proto.CMD_OK, seq=seq, opcode=self.opcodes.CONTACT_UPDATE, payload=None
|
|
||||||
)
|
|
||||||
|
|
||||||
await self._send(writer, packet)
|
|
||||||
|
|
||||||
elif action == "BLOCK":
|
|
||||||
async with self.db_pool.acquire() as conn:
|
|
||||||
async with conn.cursor() as cursor:
|
|
||||||
# Проверяем, существует ли контакт
|
|
||||||
await cursor.execute(
|
|
||||||
"SELECT * FROM contacts WHERE owner_id = %s AND contact_id = %s",
|
|
||||||
(userId, contactId)
|
|
||||||
)
|
|
||||||
row = await cursor.fetchone()
|
|
||||||
|
|
||||||
# Обновляем существующий контакт, если такой есть
|
|
||||||
if row:
|
|
||||||
await cursor.execute(
|
|
||||||
"UPDATE contacts SET is_blocked = TRUE WHERE owner_id = %s AND contact_id = %s",
|
|
||||||
(userId, contactId)
|
|
||||||
)
|
|
||||||
else: # В ином случае добавляем новую запись в бд
|
|
||||||
await cursor.execute("SELECT * FROM users WHERE id = %s", (contactId,))
|
|
||||||
user = await cursor.fetchone()
|
|
||||||
if not user:
|
|
||||||
await self._send_error(seq, self.opcodes.CONTACT_UPDATE, self.error_types.USER_NOT_FOUND, writer)
|
|
||||||
return
|
|
||||||
|
|
||||||
await cursor.execute(
|
|
||||||
"INSERT INTO contacts (owner_id, contact_id, custom_firstname, custom_lastname, is_blocked) VALUES (%s, %s, %s, %s, TRUE)",
|
|
||||||
(userId, contactId, firstName, lastName)
|
|
||||||
)
|
|
||||||
|
|
||||||
packet = self.proto.pack_packet(
|
|
||||||
cmd=self.proto.CMD_OK, seq=seq, opcode=self.opcodes.CONTACT_UPDATE, payload=None
|
|
||||||
)
|
|
||||||
|
|
||||||
await self._send(writer, packet)
|
|
||||||
|
|
||||||
elif action == "UNBLOCK":
|
|
||||||
# Разблокируем контакт
|
|
||||||
async with self.db_pool.acquire() as conn:
|
|
||||||
async with conn.cursor() as cursor:
|
|
||||||
# Проверяем, существует ли контакт
|
|
||||||
await cursor.execute(
|
|
||||||
"SELECT * FROM contacts WHERE owner_id = %s AND contact_id = %s",
|
|
||||||
(userId, contactId)
|
|
||||||
)
|
|
||||||
row = await cursor.fetchone()
|
|
||||||
|
|
||||||
# Обновляем контакт, если он есть
|
|
||||||
if row:
|
|
||||||
await cursor.execute(
|
|
||||||
"UPDATE contacts SET is_blocked = FALSE WHERE owner_id = %s AND contact_id = %s",
|
|
||||||
(userId, contactId)
|
|
||||||
)
|
|
||||||
else: # В ином случае отправляем ошибку
|
|
||||||
await self._send_error(seq, self.opcodes.CONTACT_UPDATE, self.error_types.CONTACT_NOT_FOUND, writer)
|
|
||||||
return
|
|
||||||
|
|
||||||
packet = self.proto.pack_packet(
|
|
||||||
cmd=self.proto.CMD_OK, seq=seq, opcode=self.opcodes.CONTACT_UPDATE, payload=None
|
|
||||||
)
|
|
||||||
|
|
||||||
await self._send(writer, packet)
|
|
||||||
|
|
||||||
elif action == "UPDATE":
|
|
||||||
async with self.db_pool.acquire() as conn:
|
|
||||||
async with conn.cursor() as cursor:
|
|
||||||
# Проверяем, существует ли контакт
|
|
||||||
await cursor.execute(
|
|
||||||
"SELECT * FROM contacts WHERE owner_id = %s AND contact_id = %s",
|
|
||||||
(userId, contactId)
|
|
||||||
)
|
|
||||||
row = await cursor.fetchone()
|
|
||||||
|
|
||||||
# Если контакта нет, отдаем ошибку
|
|
||||||
if not row:
|
|
||||||
await self._send_error(seq, self.opcodes.CONTACT_UPDATE, self.error_types.CONTACT_NOT_FOUND, writer)
|
|
||||||
return
|
|
||||||
|
|
||||||
# Обновляем контакт
|
|
||||||
await cursor.execute(
|
|
||||||
"UPDATE contacts SET custom_firstname = %s, custom_lastname = %s WHERE owner_id = %s AND contact_id = %s",
|
|
||||||
(firstName, lastName, userId, contactId)
|
|
||||||
)
|
|
||||||
|
|
||||||
# Получаем данные пользователя
|
|
||||||
await cursor.execute("SELECT * FROM users WHERE id = %s", (contactId,))
|
|
||||||
user = await cursor.fetchone()
|
|
||||||
|
|
||||||
# Генерируем профиль
|
|
||||||
photo_id = None if not user.get("avatar_id") else int(user.get("avatar_id"))
|
|
||||||
avatar_url = None if not photo_id else self.config.avatar_base_url + str(photo_id)
|
|
||||||
|
|
||||||
contact = self.tools.generate_profile_tt(
|
|
||||||
id=user.get("id"),
|
|
||||||
phone=int(user.get("phone")),
|
|
||||||
avatarUrl=avatar_url,
|
|
||||||
photoId=photo_id,
|
|
||||||
updateTime=int(user.get("updatetime")),
|
|
||||||
firstName=user.get("firstname"),
|
|
||||||
lastName=user.get("lastname"),
|
|
||||||
options=json.loads(user.get("options")),
|
|
||||||
description=user.get("description"),
|
|
||||||
username=user.get("username")
|
|
||||||
),
|
|
||||||
|
|
||||||
response_payload = {
|
|
||||||
"contact": contact
|
|
||||||
}
|
|
||||||
|
|
||||||
packet = self.proto.pack_packet(
|
|
||||||
cmd=self.proto.CMD_OK, seq=seq, opcode=self.opcodes.CONTACT_UPDATE, payload=response_payload
|
|
||||||
)
|
|
||||||
|
|
||||||
await self._send(writer, packet)
|
|
||||||
|
|
||||||
async def contact_presence(self, payload, seq, writer):
|
|
||||||
"""Обработчик получения статуса контактов"""
|
|
||||||
# Валидируем данные пакета
|
|
||||||
try:
|
|
||||||
ContactPresencePayloadModel.model_validate(payload)
|
|
||||||
except pydantic.ValidationError as error:
|
|
||||||
self.logger.error(f"Возникли ошибки при валидации пакета: {error}")
|
|
||||||
await self._send_error(seq, self.opcodes.CONTACT_PRESENCE, self.error_types.INVALID_PAYLOAD, writer)
|
|
||||||
return
|
|
||||||
|
|
||||||
contact_ids = payload.get("contactIds", [])
|
|
||||||
now_ms = int(time.time() * 1000)
|
|
||||||
|
|
||||||
presence = await self.tools.collect_presence(contact_ids, self.clients, self.db_pool)
|
|
||||||
|
|
||||||
response_payload = {
|
|
||||||
"presence": presence,
|
|
||||||
"time": now_ms
|
|
||||||
}
|
|
||||||
|
|
||||||
packet = self.proto.pack_packet(
|
|
||||||
cmd=self.proto.CMD_OK, seq=seq, opcode=self.opcodes.CONTACT_PRESENCE, payload=response_payload
|
|
||||||
)
|
|
||||||
|
|
||||||
await self._send(writer, packet)
|
|
||||||
@@ -1,133 +0,0 @@
|
|||||||
import pydantic
|
|
||||||
import json
|
|
||||||
import time
|
|
||||||
from classes.baseprocessor import BaseProcessor
|
|
||||||
from tamtam.models import SyncFoldersPayloadModel, CreateFolderPayloadModel
|
|
||||||
|
|
||||||
class FoldersProcessors(BaseProcessor):
|
|
||||||
async def folders_get(self, payload, seq, writer, senderPhone):
|
|
||||||
"""Синхронизация папок с сервером"""
|
|
||||||
# Валидируем данные пакета
|
|
||||||
try:
|
|
||||||
SyncFoldersPayloadModel.model_validate(payload)
|
|
||||||
except pydantic.ValidationError as error:
|
|
||||||
self.logger.error(f"Возникли ошибки при валидации пакета: {error}")
|
|
||||||
await self._send_error(seq, self.opcodes.FOLDERS_GET, self.error_types.INVALID_PAYLOAD, writer)
|
|
||||||
return
|
|
||||||
|
|
||||||
# Ищем папки в бд
|
|
||||||
async with self.db_pool.acquire() as conn:
|
|
||||||
async with conn.cursor() as cursor:
|
|
||||||
await cursor.execute(
|
|
||||||
"SELECT id, title, filters, `include`, 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"]),
|
|
||||||
"include": json.loads(folder["include"]),
|
|
||||||
"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": folders,
|
|
||||||
"foldersOrder": [folder["id"] for folder in result_folders],
|
|
||||||
"allFilterExcludeFolders": []
|
|
||||||
}
|
|
||||||
|
|
||||||
# Собираем пакет
|
|
||||||
packet = self.proto.pack_packet(
|
|
||||||
cmd=self.proto.CMD_OK, seq=seq, opcode=self.opcodes.FOLDERS_GET, payload=payload
|
|
||||||
)
|
|
||||||
|
|
||||||
# Отправляем
|
|
||||||
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)
|
|
||||||
@@ -1,8 +1,6 @@
|
|||||||
import json
|
|
||||||
import pydantic
|
import pydantic
|
||||||
from classes.baseprocessor import BaseProcessor
|
from classes.baseprocessor import BaseProcessor
|
||||||
from tamtam.models import HelloPayloadModel, PingPayloadModel
|
from tamtam.models import HelloPayloadModel, PingPayloadModel
|
||||||
from tamtam.models import UpdateProfilePayloadModel
|
|
||||||
|
|
||||||
class MainProcessors(BaseProcessor):
|
class MainProcessors(BaseProcessor):
|
||||||
async def session_init(self, payload, seq, writer):
|
async def session_init(self, payload, seq, writer):
|
||||||
@@ -37,106 +35,6 @@ class MainProcessors(BaseProcessor):
|
|||||||
# Отправляем
|
# Отправляем
|
||||||
await self._send(writer, packet)
|
await self._send(writer, packet)
|
||||||
return device_type, device_name
|
return device_type, device_name
|
||||||
|
|
||||||
async def profile(self, payload, seq, writer, userId):
|
|
||||||
"""Обработчик получения/обновления профиля"""
|
|
||||||
# Валидируем входные данные
|
|
||||||
try:
|
|
||||||
UpdateProfilePayloadModel.model_validate(payload)
|
|
||||||
except pydantic.ValidationError as error:
|
|
||||||
self.logger.error(f"Возникли ошибки при валидации пакета: {error}")
|
|
||||||
await self._send_error(seq, self.opcodes.PROFILE, self.error_types.INVALID_PAYLOAD, writer)
|
|
||||||
return
|
|
||||||
|
|
||||||
# Ищем пользователя в бд
|
|
||||||
async with self.db_pool.acquire() as conn:
|
|
||||||
async with conn.cursor() as cursor:
|
|
||||||
await cursor.execute("SELECT * FROM users WHERE id = %s", (userId,))
|
|
||||||
user = await cursor.fetchone()
|
|
||||||
|
|
||||||
# Если пользователь не найден
|
|
||||||
if not user:
|
|
||||||
await self._send_error(seq, self.opcodes.PROFILE, self.error_types.USER_NOT_FOUND, writer)
|
|
||||||
return
|
|
||||||
|
|
||||||
# Аватарка с биографией
|
|
||||||
photo_id = int(user["avatar_id"]) if user.get("avatar_id") else None
|
|
||||||
avatar_url = f"{self.config.avatar_base_url}{photo_id}" if photo_id else None
|
|
||||||
description = user.get("description")
|
|
||||||
|
|
||||||
# Генерируем профиль
|
|
||||||
profile = self.tools.generate_profile_tt(
|
|
||||||
id=user.get("id"),
|
|
||||||
phone=int(user.get("phone")),
|
|
||||||
avatarUrl=avatar_url,
|
|
||||||
photoId=photo_id,
|
|
||||||
updateTime=int(user.get("updatetime")),
|
|
||||||
firstName=user.get("firstname"),
|
|
||||||
lastName=user.get("lastname"),
|
|
||||||
options=json.loads(user.get("options")),
|
|
||||||
description=description,
|
|
||||||
username=user.get("username")
|
|
||||||
)
|
|
||||||
|
|
||||||
# Создаем данные пакета
|
|
||||||
payload = {
|
|
||||||
"profile": profile
|
|
||||||
}
|
|
||||||
|
|
||||||
# Собираем пакет
|
|
||||||
response = self.proto.pack_packet(
|
|
||||||
cmd=self.proto.CMD_OK, seq=seq, opcode=self.opcodes.PROFILE, payload=payload
|
|
||||||
)
|
|
||||||
|
|
||||||
# Отправляем
|
|
||||||
await self._send(writer, response)
|
|
||||||
|
|
||||||
async def update_config(self, payload, seq, writer, userPhone, hashedToken=None):
|
|
||||||
"""Обработчик обновления настроек и пуш-токена"""
|
|
||||||
result_payload = None
|
|
||||||
|
|
||||||
if payload.get("pushToken"):
|
|
||||||
push_token = payload.get("pushToken")
|
|
||||||
async with self.db_pool.acquire() as conn:
|
|
||||||
async with conn.cursor() as cursor:
|
|
||||||
await cursor.execute(
|
|
||||||
"UPDATE tokens SET push_token = %s WHERE phone = %s AND token_hash = %s",
|
|
||||||
(push_token, str(userPhone), hashedToken)
|
|
||||||
)
|
|
||||||
elif payload.get("settings") and payload.get("settings").get("user"):
|
|
||||||
new_settings = payload.get("settings").get("user")
|
|
||||||
|
|
||||||
async with self.db_pool.acquire() as conn:
|
|
||||||
async with conn.cursor() as cursor:
|
|
||||||
await cursor.execute(
|
|
||||||
"SELECT user_config FROM user_data WHERE phone = %s", (userPhone,)
|
|
||||||
)
|
|
||||||
row = await cursor.fetchone()
|
|
||||||
|
|
||||||
if row:
|
|
||||||
current_config = json.loads(row.get("user_config"))
|
|
||||||
|
|
||||||
for key, value in new_settings.items():
|
|
||||||
if key in current_config:
|
|
||||||
current_config[key] = value
|
|
||||||
|
|
||||||
await cursor.execute(
|
|
||||||
"UPDATE user_data SET user_config = %s WHERE phone = %s",
|
|
||||||
(json.dumps(current_config), userPhone)
|
|
||||||
)
|
|
||||||
|
|
||||||
result_payload = {
|
|
||||||
"user": current_config,
|
|
||||||
"hash": "0"
|
|
||||||
}
|
|
||||||
|
|
||||||
# Собираем пакет
|
|
||||||
response = self.proto.pack_packet(
|
|
||||||
cmd=self.proto.CMD_OK, seq=seq, opcode=self.opcodes.CONFIG, payload=result_payload
|
|
||||||
)
|
|
||||||
|
|
||||||
# Отправляем
|
|
||||||
await self._send(writer, response)
|
|
||||||
|
|
||||||
async def ping(self, payload, seq, writer):
|
async def ping(self, payload, seq, writer):
|
||||||
"""Обработчик пинга"""
|
"""Обработчик пинга"""
|
||||||
|
|||||||
@@ -1,168 +0,0 @@
|
|||||||
import pydantic
|
|
||||||
from classes.baseprocessor import BaseProcessor
|
|
||||||
from tamtam.models import (
|
|
||||||
TypingPayloadModel,
|
|
||||||
SendMessagePayloadModel
|
|
||||||
)
|
|
||||||
|
|
||||||
class MessagesProcessors(BaseProcessor):
|
|
||||||
async def msg_typing(self, payload, seq, writer, senderId):
|
|
||||||
"""Обработчик события печатания"""
|
|
||||||
# Валидируем данные пакета
|
|
||||||
try:
|
|
||||||
TypingPayloadModel.model_validate(payload)
|
|
||||||
except pydantic.ValidationError as error:
|
|
||||||
self.logger.error(f"Возникли ошибки при валидации пакета: {error}")
|
|
||||||
await self._send_error(seq, self.opcodes.MSG_TYPING, self.error_types.INVALID_PAYLOAD, writer)
|
|
||||||
return
|
|
||||||
|
|
||||||
# Извлекаем данные из пакета
|
|
||||||
chatId = payload.get("chatId")
|
|
||||||
type = payload.get("type") or "TYPING"
|
|
||||||
|
|
||||||
# Ищем чат в базе данных
|
|
||||||
async with self.db_pool.acquire() as conn:
|
|
||||||
async with conn.cursor() as cursor:
|
|
||||||
await cursor.execute("SELECT * FROM chats WHERE id = %s", (chatId,))
|
|
||||||
chat = await cursor.fetchone()
|
|
||||||
|
|
||||||
# Если чат не найден, отправляем ошибку
|
|
||||||
if not chat:
|
|
||||||
await self._send_error(seq, self.opcodes.MSG_TYPING, self.error_types.CHAT_NOT_FOUND, writer)
|
|
||||||
return
|
|
||||||
|
|
||||||
# Участники чата
|
|
||||||
participants = await self.tools.get_chat_participants(chatId, self.db_pool)
|
|
||||||
|
|
||||||
# Проверяем, является ли отправитель участником чата
|
|
||||||
if int(senderId) not in participants:
|
|
||||||
await self._send_error(seq, self.opcodes.MSG_TYPING, self.error_types.CHAT_NOT_ACCESS, writer)
|
|
||||||
return
|
|
||||||
|
|
||||||
# Рассылаем событие участникам чата
|
|
||||||
for participant in participants:
|
|
||||||
if participant != senderId:
|
|
||||||
# Если участник не является отправителем, отправляем
|
|
||||||
await self.event(
|
|
||||||
participant,
|
|
||||||
{
|
|
||||||
"eventType": "typing",
|
|
||||||
"chatId": chatId,
|
|
||||||
"type": type,
|
|
||||||
"userId": senderId,
|
|
||||||
"writer": writer,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
# Создаем пакет
|
|
||||||
packet = self.proto.pack_packet(
|
|
||||||
seq=seq, opcode=self.opcodes.MSG_TYPING
|
|
||||||
)
|
|
||||||
|
|
||||||
# Отправляем пакет
|
|
||||||
await self._send(writer, packet)
|
|
||||||
|
|
||||||
async def msg_send(self, payload, seq, writer, senderId, db_pool):
|
|
||||||
"""Функция отправки сообщения"""
|
|
||||||
# Валидируем данные пакета
|
|
||||||
try:
|
|
||||||
SendMessagePayloadModel.model_validate(payload)
|
|
||||||
except pydantic.ValidationError as error:
|
|
||||||
self.logger.error(f"Возникли ошибки при валидации пакета: {error}")
|
|
||||||
await self._send_error(seq, self.opcodes.MSG_SEND, self.error_types.INVALID_PAYLOAD, writer)
|
|
||||||
return
|
|
||||||
|
|
||||||
# Извлекаем данные из пакета
|
|
||||||
userId = payload.get("userId")
|
|
||||||
chatId = payload.get("chatId")
|
|
||||||
message = payload.get("message")
|
|
||||||
|
|
||||||
elements = message.get("elements") or []
|
|
||||||
attaches = message.get("attaches") or []
|
|
||||||
cid = message.get("cid") or 0
|
|
||||||
text = message.get("text") or ""
|
|
||||||
|
|
||||||
# Вычисляем ID чата по ID пользователя и ID отправителя,
|
|
||||||
# в случае отсутствия ID чата
|
|
||||||
if chatId is None:
|
|
||||||
chatId = userId ^ senderId
|
|
||||||
|
|
||||||
async with db_pool.acquire() as db_connection:
|
|
||||||
async with db_connection.cursor() as cursor:
|
|
||||||
await cursor.execute("SELECT * FROM chats WHERE id = %s", (chatId,))
|
|
||||||
chat = await cursor.fetchone()
|
|
||||||
|
|
||||||
# Если нет такого чата - выбрасываем ошибку
|
|
||||||
if not chat:
|
|
||||||
await self._send_error(seq, self.opcodes.MSG_SEND, self.error_types.CHAT_NOT_FOUND, writer)
|
|
||||||
return
|
|
||||||
|
|
||||||
# Список участников
|
|
||||||
participants = await self.tools.get_chat_participants(chatId, db_pool)
|
|
||||||
|
|
||||||
# Проверяем, является ли отправитель участником чата
|
|
||||||
if int(senderId) not in participants:
|
|
||||||
await self._send_error(seq, self.opcodes.MSG_SEND, self.error_types.CHAT_NOT_ACCESS, writer)
|
|
||||||
return
|
|
||||||
|
|
||||||
# Проверяем блокировку собеседника
|
|
||||||
if chat.get("type") == "DIALOG":
|
|
||||||
contactid = [p for p in participants if p != int(senderId)][0]
|
|
||||||
# Проверяем, заблокировал ли отправитель собеседника
|
|
||||||
if await self.tools.contact_is_blocked(contactid, senderId, db_pool):
|
|
||||||
await self._send_error(seq, self.opcodes.MSG_SEND, self.error_types.CONTACT_BLOCKED, writer)
|
|
||||||
return
|
|
||||||
|
|
||||||
# Добавляем сообщение в историю
|
|
||||||
messageId, lastMessageId, messageTime = await self.tools.insert_message(
|
|
||||||
chatId=chatId,
|
|
||||||
senderId=senderId,
|
|
||||||
text=text,
|
|
||||||
attaches=attaches,
|
|
||||||
elements=elements,
|
|
||||||
cid=cid,
|
|
||||||
type="USER",
|
|
||||||
db_pool=self.db_pool
|
|
||||||
)
|
|
||||||
|
|
||||||
# Готовое тело сообщения
|
|
||||||
bodyMessage = {
|
|
||||||
"id": messageId,
|
|
||||||
"time": messageTime,
|
|
||||||
"type": "USER",
|
|
||||||
"sender": senderId,
|
|
||||||
"cid": cid,
|
|
||||||
"text": text,
|
|
||||||
"attaches": attaches,
|
|
||||||
"elements": elements
|
|
||||||
}
|
|
||||||
|
|
||||||
# Отправляем событие всем участникам чата
|
|
||||||
for participant in participants:
|
|
||||||
await self.event(
|
|
||||||
participant,
|
|
||||||
{
|
|
||||||
"eventType": "new_msg",
|
|
||||||
"chatId": chatId,
|
|
||||||
"message": bodyMessage,
|
|
||||||
"prevMessageId": lastMessageId,
|
|
||||||
"time": messageTime,
|
|
||||||
"writer": writer
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
# Данные пакета
|
|
||||||
payload = {
|
|
||||||
"chatId": chatId,
|
|
||||||
"message": bodyMessage,
|
|
||||||
"unread": 0,
|
|
||||||
"mark": messageTime
|
|
||||||
}
|
|
||||||
|
|
||||||
# Собираем пакет
|
|
||||||
packet = self.proto.pack_packet(
|
|
||||||
cmd=self.proto.CMD_OK, seq=seq, opcode=self.opcodes.MSG_SEND, payload=payload
|
|
||||||
)
|
|
||||||
|
|
||||||
# Отправляем
|
|
||||||
await self._send(writer, packet)
|
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
import json, pydantic
|
import json, pydantic
|
||||||
from classes.baseprocessor import BaseProcessor
|
from classes.baseprocessor import BaseProcessor
|
||||||
from tamtam.models import SearchUsersPayloadModel
|
from tamtam.models import SearchUsersPayloadModel
|
||||||
from tamtam.models import SearchChatsPayloadModel
|
|
||||||
|
|
||||||
class SearchProcessors(BaseProcessor):
|
class SearchProcessors(BaseProcessor):
|
||||||
async def contact_info(self, payload, seq, writer):
|
async def contact_info(self, payload, seq, writer):
|
||||||
@@ -60,61 +59,5 @@ class SearchProcessors(BaseProcessor):
|
|||||||
cmd=self.proto.CMD_OK, seq=seq, opcode=self.opcodes.CONTACT_INFO, payload=payload
|
cmd=self.proto.CMD_OK, seq=seq, opcode=self.opcodes.CONTACT_INFO, payload=payload
|
||||||
)
|
)
|
||||||
|
|
||||||
# Отправляем
|
|
||||||
await self._send(writer, response)
|
|
||||||
|
|
||||||
async def chat_info(self, payload, seq, writer, senderId):
|
|
||||||
"""Поиск чатов по ID"""
|
|
||||||
# Валидируем данные пакета
|
|
||||||
try:
|
|
||||||
SearchChatsPayloadModel.model_validate(payload)
|
|
||||||
except pydantic.ValidationError as error:
|
|
||||||
self.logger.error(f"Возникли ошибки при валидации пакета: {error}")
|
|
||||||
await self._send_error(seq, self.opcodes.CHAT_INFO, self.error_types.INVALID_PAYLOAD, writer)
|
|
||||||
return
|
|
||||||
|
|
||||||
# Итоговый список чатов
|
|
||||||
chats = []
|
|
||||||
|
|
||||||
# ID чатов, которые нам предстоит найти
|
|
||||||
chatIds = payload.get("chatIds")
|
|
||||||
|
|
||||||
# Ищем чаты в бд
|
|
||||||
async with self.db_pool.acquire() as conn:
|
|
||||||
async with conn.cursor() as cursor:
|
|
||||||
for chatId in chatIds:
|
|
||||||
await cursor.execute("SELECT * FROM chats WHERE id = %s", (chatId,))
|
|
||||||
chat = await cursor.fetchone()
|
|
||||||
|
|
||||||
if chat:
|
|
||||||
# Проверяем, является ли пользователь участником чата
|
|
||||||
participants = await self.tools.get_chat_participants(chatId, self.db_pool)
|
|
||||||
if int(senderId) not in participants:
|
|
||||||
continue
|
|
||||||
|
|
||||||
# Получаем последнее сообщение из чата
|
|
||||||
message, messageTime = await self.tools.get_last_message(
|
|
||||||
chatId, self.db_pool, protocol_type=self.type
|
|
||||||
)
|
|
||||||
|
|
||||||
# Добавляем чат в список
|
|
||||||
chats.append(
|
|
||||||
self.tools.generate_chat(
|
|
||||||
chatId, chat.get("owner"),
|
|
||||||
chat.get("type"), participants,
|
|
||||||
message, messageTime
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
# Создаем данные пакета
|
|
||||||
payload = {
|
|
||||||
"chats": chats
|
|
||||||
}
|
|
||||||
|
|
||||||
# Собираем пакет
|
|
||||||
response = self.proto.pack_packet(
|
|
||||||
cmd=self.proto.CMD_OK, seq=seq, opcode=self.opcodes.CHAT_INFO, payload=payload
|
|
||||||
)
|
|
||||||
|
|
||||||
# Отправляем
|
# Отправляем
|
||||||
await self._send(writer, response)
|
await self._send(writer, response)
|
||||||
@@ -1,38 +0,0 @@
|
|||||||
from classes.baseprocessor import BaseProcessor
|
|
||||||
|
|
||||||
class SessionsProcessors(BaseProcessor):
|
|
||||||
async def sessions_info(self, payload, seq, writer, senderPhone, hashedToken):
|
|
||||||
"""Получение активных сессий на аккаунте"""
|
|
||||||
# Готовый список сессий
|
|
||||||
sessions = []
|
|
||||||
|
|
||||||
# Ищем сессии в бд
|
|
||||||
async with self.db_pool.acquire() as conn:
|
|
||||||
async with conn.cursor() as cursor:
|
|
||||||
await cursor.execute("SELECT * FROM tokens WHERE phone = %s", (str(senderPhone),))
|
|
||||||
user_sessions = await cursor.fetchall()
|
|
||||||
|
|
||||||
# Собираем сессии в список
|
|
||||||
for session in user_sessions:
|
|
||||||
sessions.append(
|
|
||||||
{
|
|
||||||
"time": int(session.get("time")),
|
|
||||||
"client": f"TamTam {session.get('device_type')}",
|
|
||||||
"info": session.get("device_name"),
|
|
||||||
"location": session.get("location"),
|
|
||||||
"current": True if session.get("token_hash") == hashedToken else False
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
# Создаем данные пакета
|
|
||||||
payload = {
|
|
||||||
"sessions": sessions
|
|
||||||
}
|
|
||||||
|
|
||||||
# Создаем пакет
|
|
||||||
response = self.proto.pack_packet(
|
|
||||||
cmd=self.proto.CMD_OK, seq=seq, opcode=self.opcodes.SESSIONS_INFO, payload=payload
|
|
||||||
)
|
|
||||||
|
|
||||||
# Отправляем
|
|
||||||
await self._send(writer, response)
|
|
||||||
@@ -24,7 +24,7 @@ class TamTamMobile:
|
|||||||
self.auth_required = Tools().auth_required
|
self.auth_required = Tools().auth_required
|
||||||
|
|
||||||
# rate limiter
|
# rate limiter
|
||||||
self.auth_rate_limiter = RateLimiter(max_attempts=15, window_seconds=60)
|
self.auth_rate_limiter = RateLimiter(max_attempts=5, window_seconds=60)
|
||||||
|
|
||||||
self.read_timeout = 300 # Таймаут чтения из сокета (секунды)
|
self.read_timeout = 300 # Таймаут чтения из сокета (секунды)
|
||||||
self.max_read_size = 65536 # Максимальный размер данных из сокета
|
self.max_read_size = 65536 # Максимальный размер данных из сокета
|
||||||
@@ -96,7 +96,7 @@ class TamTamMobile:
|
|||||||
if not self.auth_rate_limiter.is_allowed(address[0]):
|
if not self.auth_rate_limiter.is_allowed(address[0]):
|
||||||
await self.processors._send_error(seq, self.opcodes.AUTH_CONFIRM, self.processors.error_types.RATE_LIMITED, writer)
|
await self.processors._send_error(seq, self.opcodes.AUTH_CONFIRM, self.processors.error_types.RATE_LIMITED, writer)
|
||||||
else:
|
else:
|
||||||
await self.processors.auth_confirm(payload, seq, writer, deviceType, deviceName, address[0])
|
await self.processors.auth_confirm(payload, seq, writer, deviceType, deviceName)
|
||||||
case self.opcodes.LOGIN:
|
case self.opcodes.LOGIN:
|
||||||
if not self.auth_rate_limiter.is_allowed(address[0]):
|
if not self.auth_rate_limiter.is_allowed(address[0]):
|
||||||
await self.processors._send_error(seq, self.opcodes.LOGIN, self.processors.error_types.RATE_LIMITED, writer)
|
await self.processors._send_error(seq, self.opcodes.LOGIN, self.processors.error_types.RATE_LIMITED, writer)
|
||||||
@@ -105,111 +105,18 @@ class TamTamMobile:
|
|||||||
|
|
||||||
if userPhone:
|
if userPhone:
|
||||||
await self._finish_auth(writer, address, userPhone, userId)
|
await self._finish_auth(writer, address, userPhone, userId)
|
||||||
case self.opcodes.LOGOUT:
|
|
||||||
await self.processors.logout(
|
|
||||||
seq, writer, hashedToken=hashedToken
|
|
||||||
)
|
|
||||||
break
|
|
||||||
case self.opcodes.CONTACT_INFO:
|
case self.opcodes.CONTACT_INFO:
|
||||||
await self.auth_required(
|
await self.auth_required(
|
||||||
userPhone, self.processors.contact_info, payload, seq, writer
|
userPhone, self.processors.contact_info, payload, seq, writer
|
||||||
)
|
)
|
||||||
case self.opcodes.CHAT_HISTORY:
|
|
||||||
await self.auth_required(
|
|
||||||
userPhone, self.processors.chat_history, payload, seq, writer, userId
|
|
||||||
)
|
|
||||||
case self.opcodes.ASSETS_UPDATE:
|
|
||||||
await self.auth_required(
|
|
||||||
userPhone, self.processors.assets_update, payload, seq, writer
|
|
||||||
)
|
|
||||||
case self.opcodes.ASSETS_GET:
|
|
||||||
await self.auth_required(
|
|
||||||
userPhone, self.processors.assets_get, payload, seq, writer
|
|
||||||
)
|
|
||||||
case self.opcodes.ASSETS_GET_BY_IDS:
|
|
||||||
await self.auth_required(
|
|
||||||
userPhone, self.processors.assets_get_by_ids, payload, seq, writer
|
|
||||||
)
|
|
||||||
case self.opcodes.ASSETS_ADD:
|
|
||||||
await self.auth_required(
|
|
||||||
userPhone, self.processors.assets_add, payload, seq, writer
|
|
||||||
)
|
|
||||||
case self.opcodes.ASSETS_REMOVE:
|
|
||||||
await self.auth_required(
|
|
||||||
userPhone, self.processors.assets_remove, payload, seq, writer
|
|
||||||
)
|
|
||||||
case self.opcodes.ASSETS_MOVE:
|
|
||||||
await self.auth_required(
|
|
||||||
userPhone, self.processors.assets_move, payload, seq, writer
|
|
||||||
)
|
|
||||||
case self.opcodes.ASSETS_LIST_MODIFY:
|
|
||||||
await self.auth_required(
|
|
||||||
userPhone, self.processors.assets_list_modify, payload, seq, writer
|
|
||||||
)
|
|
||||||
case self.opcodes.VIDEO_CHAT_HISTORY:
|
|
||||||
await self.auth_required(
|
|
||||||
userPhone, self.processors.video_chat_history, payload, seq, writer
|
|
||||||
)
|
|
||||||
case self.opcodes.MSG_SEND:
|
|
||||||
await self.auth_required(
|
|
||||||
userPhone, self.processors.msg_send, payload, seq, writer, userId, self.db_pool
|
|
||||||
)
|
|
||||||
case self.opcodes.MSG_TYPING:
|
|
||||||
await self.auth_required(
|
|
||||||
userPhone, self.processors.msg_typing, payload, seq, writer, userId
|
|
||||||
)
|
|
||||||
case self.opcodes.FOLDERS_GET:
|
|
||||||
await self.auth_required(
|
|
||||||
userPhone, self.processors.folders_get, payload, seq, writer, userPhone
|
|
||||||
)
|
|
||||||
case self.opcodes.FOLDERS_UPDATE:
|
|
||||||
await self.auth_required(
|
|
||||||
userPhone, self.processors.folders_update, payload, seq, writer, userPhone
|
|
||||||
)
|
|
||||||
case self.opcodes.SESSIONS_INFO:
|
|
||||||
await self.auth_required(
|
|
||||||
userPhone, self.processors.sessions_info, payload, seq, writer, userPhone, hashedToken
|
|
||||||
)
|
|
||||||
case self.opcodes.CHAT_INFO:
|
|
||||||
await self.auth_required(
|
|
||||||
userPhone, self.processors.chat_info, payload, seq, writer, userId
|
|
||||||
)
|
|
||||||
case self.opcodes.CONTACT_LIST:
|
|
||||||
await self.auth_required(
|
|
||||||
userPhone, self.processors.contact_list, payload, seq, writer, userId
|
|
||||||
)
|
|
||||||
case self.opcodes.PROFILE:
|
|
||||||
await self.processors.profile(
|
|
||||||
payload, seq, writer, userId=userId
|
|
||||||
)
|
|
||||||
case self.opcodes.CHAT_SUBSCRIBE:
|
|
||||||
await self.auth_required(
|
|
||||||
userPhone, self.processors.chat_subscribe, payload, seq, writer
|
|
||||||
)
|
|
||||||
case self.opcodes.CONFIG:
|
|
||||||
await self.auth_required(
|
|
||||||
userPhone, self.processors.update_config, payload, seq, writer, userPhone, hashedToken
|
|
||||||
)
|
|
||||||
case self.opcodes.CONTACT_UPDATE:
|
|
||||||
await self.auth_required(
|
|
||||||
userPhone, self.processors.contact_update, payload, seq, writer, userId
|
|
||||||
)
|
|
||||||
case self.opcodes.CONTACT_PRESENCE:
|
|
||||||
await self.auth_required(
|
|
||||||
userPhone, self.processors.contact_presence, payload, seq, writer
|
|
||||||
)
|
|
||||||
case _:
|
case _:
|
||||||
self.logger.warning(f"Неизвестный опкод {opcode}")
|
self.logger.warning(f"Неизвестный опкод {opcode}")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.logger.error(f"Произошла ошибка при работе с клиентом {address[0]}:{address[1]}: {e}")
|
self.logger.error(f"Произошла ошибка при работе с клиентом {address[0]}:{address[1]}: {e}")
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
|
|
||||||
# Удаляем клиента из словаря при отключении
|
|
||||||
if userId:
|
|
||||||
await self._end_session(userId, address[0], address[1])
|
|
||||||
|
|
||||||
writer.close()
|
writer.close()
|
||||||
self.logger.info(f"Прекратил работать с клиентом {address[0]}:{address[1]}")
|
self.logger.info(f"Прекратил работать работать с клиентом {address[0]}:{address[1]}")
|
||||||
|
|
||||||
async def _finish_auth(self, writer, addr, phone, id):
|
async def _finish_auth(self, writer, addr, phone, id):
|
||||||
"""Завершение открытия сессии"""
|
"""Завершение открытия сессии"""
|
||||||
@@ -223,8 +130,7 @@ class TamTamMobile:
|
|||||||
"writer": writer,
|
"writer": writer,
|
||||||
"ip": addr[0],
|
"ip": addr[0],
|
||||||
"port": addr[1],
|
"port": addr[1],
|
||||||
"protocol": "tamtam",
|
"protocol": "tamtam"
|
||||||
"type": "tcp"
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
@@ -236,8 +142,7 @@ class TamTamMobile:
|
|||||||
"writer": writer,
|
"writer": writer,
|
||||||
"ip": addr[0],
|
"ip": addr[0],
|
||||||
"port": addr[1],
|
"port": addr[1],
|
||||||
"protocol": "tamtam",
|
"protocol": "tamtam"
|
||||||
"type": "tcp"
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
@@ -265,9 +170,5 @@ class TamTamMobile:
|
|||||||
|
|
||||||
self.logger.info(f"Сокет запущен на порту {self.port}")
|
self.logger.info(f"Сокет запущен на порту {self.port}")
|
||||||
|
|
||||||
try:
|
async with self.server:
|
||||||
async with self.server:
|
await self.server.serve_forever()
|
||||||
await self.server.serve_forever()
|
|
||||||
except asyncio.CancelledError:
|
|
||||||
self.server.close()
|
|
||||||
await self.server.wait_closed()
|
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
import logging
|
import logging
|
||||||
import traceback
|
import traceback
|
||||||
import websockets
|
import websockets
|
||||||
import asyncio
|
|
||||||
from common.proto_web import WebProto
|
from common.proto_web import WebProto
|
||||||
from tamtam.processors import Processors
|
from tamtam.processors import Processors
|
||||||
from common.rate_limiter import RateLimiter
|
from common.rate_limiter import RateLimiter
|
||||||
@@ -30,7 +29,7 @@ class TamTamWS:
|
|||||||
self.read_timeout = 300 # Таймаут чтения из websocket (секунды)
|
self.read_timeout = 300 # Таймаут чтения из websocket (секунды)
|
||||||
self.max_read_size = 65536 # Максимальный размер данных
|
self.max_read_size = 65536 # Максимальный размер данных
|
||||||
|
|
||||||
async def handle_client(self, websocket):
|
async def handle_client(self, websocket, path):
|
||||||
"""Функция для обработки WebSocket подключений"""
|
"""Функция для обработки WebSocket подключений"""
|
||||||
# IP-адрес клиента
|
# IP-адрес клиента
|
||||||
address = websocket.remote_address
|
address = websocket.remote_address
|
||||||
@@ -83,7 +82,7 @@ class TamTamWS:
|
|||||||
if not self.auth_rate_limiter.is_allowed(address[0]):
|
if not self.auth_rate_limiter.is_allowed(address[0]):
|
||||||
await self.processors._send_error(seq, self.opcodes.AUTH_CONFIRM, self.processors.error_types.RATE_LIMITED, websocket)
|
await self.processors._send_error(seq, self.opcodes.AUTH_CONFIRM, self.processors.error_types.RATE_LIMITED, websocket)
|
||||||
else:
|
else:
|
||||||
await self.processors.auth_confirm(payload, seq, websocket, deviceType, deviceName, address[0])
|
await self.processors.auth_confirm(payload, seq, websocket, deviceType, deviceName)
|
||||||
case self.opcodes.LOGIN:
|
case self.opcodes.LOGIN:
|
||||||
if not self.auth_rate_limiter.is_allowed(address[0]):
|
if not self.auth_rate_limiter.is_allowed(address[0]):
|
||||||
await self.processors._send_error(seq, self.opcodes.LOGIN, self.processors.error_types.RATE_LIMITED, websocket)
|
await self.processors._send_error(seq, self.opcodes.LOGIN, self.processors.error_types.RATE_LIMITED, websocket)
|
||||||
@@ -92,11 +91,6 @@ class TamTamWS:
|
|||||||
|
|
||||||
if userPhone:
|
if userPhone:
|
||||||
await self._finish_auth(websocket, address, userPhone, userId)
|
await self._finish_auth(websocket, address, userPhone, userId)
|
||||||
case self.opcodes.LOGOUT:
|
|
||||||
await self.processors.logout(
|
|
||||||
seq, websocket, hashedToken=hashedToken
|
|
||||||
)
|
|
||||||
break
|
|
||||||
case self.opcodes.CONTACT_INFO:
|
case self.opcodes.CONTACT_INFO:
|
||||||
await self.auth_required(
|
await self.auth_required(
|
||||||
userPhone, self.processors.contact_info, payload, seq, websocket
|
userPhone, self.processors.contact_info, payload, seq, websocket
|
||||||
@@ -105,86 +99,6 @@ class TamTamWS:
|
|||||||
await self.auth_required(
|
await self.auth_required(
|
||||||
userPhone, self.processors.chat_history, payload, seq, websocket, userId
|
userPhone, self.processors.chat_history, payload, seq, websocket, userId
|
||||||
)
|
)
|
||||||
case self.opcodes.ASSETS_UPDATE:
|
|
||||||
await self.auth_required(
|
|
||||||
userPhone, self.processors.assets_update, payload, seq, websocket
|
|
||||||
)
|
|
||||||
case self.opcodes.ASSETS_GET:
|
|
||||||
await self.auth_required(
|
|
||||||
userPhone, self.processors.assets_get, payload, seq, websocket
|
|
||||||
)
|
|
||||||
case self.opcodes.ASSETS_GET_BY_IDS:
|
|
||||||
await self.auth_required(
|
|
||||||
userPhone, self.processors.assets_get_by_ids, payload, seq, websocket
|
|
||||||
)
|
|
||||||
case self.opcodes.ASSETS_ADD:
|
|
||||||
await self.auth_required(
|
|
||||||
userPhone, self.processors.assets_add, payload, seq, websocket
|
|
||||||
)
|
|
||||||
case self.opcodes.ASSETS_REMOVE:
|
|
||||||
await self.auth_required(
|
|
||||||
userPhone, self.processors.assets_remove, payload, seq, websocket
|
|
||||||
)
|
|
||||||
case self.opcodes.ASSETS_MOVE:
|
|
||||||
await self.auth_required(
|
|
||||||
userPhone, self.processors.assets_move, payload, seq, websocket
|
|
||||||
)
|
|
||||||
case self.opcodes.ASSETS_LIST_MODIFY:
|
|
||||||
await self.auth_required(
|
|
||||||
userPhone, self.processors.assets_list_modify, payload, seq, websocket
|
|
||||||
)
|
|
||||||
case self.opcodes.VIDEO_CHAT_HISTORY:
|
|
||||||
await self.auth_required(
|
|
||||||
userPhone, self.processors.video_chat_history, payload, seq, websocket
|
|
||||||
)
|
|
||||||
case self.opcodes.MSG_SEND:
|
|
||||||
await self.auth_required(
|
|
||||||
userPhone, self.processors.msg_send, payload, seq, websocket, userId, self.db_pool
|
|
||||||
)
|
|
||||||
case self.opcodes.MSG_TYPING:
|
|
||||||
await self.auth_required(
|
|
||||||
userPhone, self.processors.msg_typing, payload, seq, websocket, userId
|
|
||||||
)
|
|
||||||
case self.opcodes.FOLDERS_GET:
|
|
||||||
await self.auth_required(
|
|
||||||
userPhone, self.processors.folders_get, payload, seq, websocket, userPhone
|
|
||||||
)
|
|
||||||
case self.opcodes.FOLDERS_UPDATE:
|
|
||||||
await self.auth_required(
|
|
||||||
userPhone, self.processors.folders_update, payload, seq, websocket, userPhone
|
|
||||||
)
|
|
||||||
case self.opcodes.SESSIONS_INFO:
|
|
||||||
await self.auth_required(
|
|
||||||
userPhone, self.processors.sessions_info, payload, seq, websocket, userPhone, hashedToken
|
|
||||||
)
|
|
||||||
case self.opcodes.CHAT_INFO:
|
|
||||||
await self.auth_required(
|
|
||||||
userPhone, self.processors.chat_info, payload, seq, websocket, userId
|
|
||||||
)
|
|
||||||
case self.opcodes.CONTACT_LIST:
|
|
||||||
await self.auth_required(
|
|
||||||
userPhone, self.processors.contact_list, payload, seq, websocket, userId
|
|
||||||
)
|
|
||||||
case self.opcodes.PROFILE:
|
|
||||||
await self.processors.profile(
|
|
||||||
payload, seq, websocket, userId=userId
|
|
||||||
)
|
|
||||||
case self.opcodes.CHAT_SUBSCRIBE:
|
|
||||||
await self.auth_required(
|
|
||||||
userPhone, self.processors.chat_subscribe, payload, seq, websocket
|
|
||||||
)
|
|
||||||
case self.opcodes.CONFIG:
|
|
||||||
await self.auth_required(
|
|
||||||
userPhone, self.processors.update_config, payload, seq, websocket, userPhone, hashedToken
|
|
||||||
)
|
|
||||||
case self.opcodes.CONTACT_UPDATE:
|
|
||||||
await self.auth_required(
|
|
||||||
userPhone, self.processors.contact_update, payload, seq, websocket, userId
|
|
||||||
)
|
|
||||||
case self.opcodes.CONTACT_PRESENCE:
|
|
||||||
await self.auth_required(
|
|
||||||
userPhone, self.processors.contact_presence, payload, seq, websocket
|
|
||||||
)
|
|
||||||
case _:
|
case _:
|
||||||
self.logger.warning(f"Неизвестный опкод {opcode}")
|
self.logger.warning(f"Неизвестный опкод {opcode}")
|
||||||
except websockets.exceptions.ConnectionClosed:
|
except websockets.exceptions.ConnectionClosed:
|
||||||
@@ -211,8 +125,7 @@ class TamTamWS:
|
|||||||
"writer": websocket,
|
"writer": websocket,
|
||||||
"ip": addr[0],
|
"ip": addr[0],
|
||||||
"port": addr[1],
|
"port": addr[1],
|
||||||
"protocol": "tamtam",
|
"protocol": "tamtam"
|
||||||
"type": "web"
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
@@ -224,8 +137,7 @@ class TamTamWS:
|
|||||||
"writer": websocket,
|
"writer": websocket,
|
||||||
"ip": addr[0],
|
"ip": addr[0],
|
||||||
"port": addr[1],
|
"port": addr[1],
|
||||||
"protocol": "tamtam",
|
"protocol": "tamtam"
|
||||||
"type": "web"
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
@@ -256,9 +168,4 @@ class TamTamWS:
|
|||||||
|
|
||||||
self.logger.info(f"TT WebSocket запущен на порту {self.port}")
|
self.logger.info(f"TT WebSocket запущен на порту {self.port}")
|
||||||
|
|
||||||
try:
|
await self.server.wait_closed()
|
||||||
await self.server.wait_closed()
|
|
||||||
except asyncio.CancelledError:
|
|
||||||
self.server.close()
|
|
||||||
await self.server.wait_closed()
|
|
||||||
raise
|
|
||||||
@@ -14,7 +14,7 @@ from common.tools import Tools
|
|||||||
|
|
||||||
|
|
||||||
class TelegramBot:
|
class TelegramBot:
|
||||||
def __init__(self, token, enabled, db_pool, whitelist_ids=None, whitelist_enabled=False):
|
def __init__(self, token, enabled, db_pool, whitelist_ids=None):
|
||||||
self.bot = Bot(token=token)
|
self.bot = Bot(token=token)
|
||||||
self.dp = Dispatcher()
|
self.dp = Dispatcher()
|
||||||
self.router = Router()
|
self.router = Router()
|
||||||
@@ -23,7 +23,6 @@ class TelegramBot:
|
|||||||
self.enabled = enabled
|
self.enabled = enabled
|
||||||
self.db_pool = db_pool
|
self.db_pool = db_pool
|
||||||
self.whitelist_ids = whitelist_ids if whitelist_ids is not None else []
|
self.whitelist_ids = whitelist_ids if whitelist_ids is not None else []
|
||||||
self.whitelist_enabled = whitelist_enabled
|
|
||||||
self.logger = logging.getLogger(__name__)
|
self.logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
self.msg_types = Static().BotMessageTypes()
|
self.msg_types = Static().BotMessageTypes()
|
||||||
@@ -60,13 +59,12 @@ class TelegramBot:
|
|||||||
async def handle_register(self, message: Message):
|
async def handle_register(self, message: Message):
|
||||||
tg_id = str(message.from_user.id)
|
tg_id = str(message.from_user.id)
|
||||||
|
|
||||||
# Проверка ID на наличие в белом списке (если он включен, конечно)
|
# Проверка ID на наличие в белом списке
|
||||||
if self.whitelist_enabled:
|
if tg_id not in self.whitelist_ids:
|
||||||
if self.whitelist_ids and tg_id not in self.whitelist_ids:
|
await message.answer(
|
||||||
await message.answer(
|
self.get_bot_message(self.msg_types.ID_NOT_WHITELISTED)
|
||||||
self.get_bot_message(self.msg_types.ID_NOT_WHITELISTED)
|
)
|
||||||
)
|
return
|
||||||
return
|
|
||||||
|
|
||||||
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:
|
||||||
@@ -83,24 +81,17 @@ class TelegramBot:
|
|||||||
updatetime = str(int(time.time() * 1000))
|
updatetime = str(int(time.time() * 1000))
|
||||||
lastseen = str(int(time.time()))
|
lastseen = str(int(time.time()))
|
||||||
|
|
||||||
firstname = message.from_user.first_name[:59]
|
|
||||||
lastname = (message.from_user.last_name or "")[:59]
|
|
||||||
username = (message.from_user.username or f"user{int(time.time() * 1000)}")[:60]
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Генерируем ID пользователя
|
|
||||||
user_id = await self.tools.generate_user_id(self.db_pool)
|
|
||||||
|
|
||||||
# Создаем юзера
|
# Создаем юзера
|
||||||
await cursor.execute(
|
await cursor.execute(
|
||||||
self.sql_queries.INSERT_USER,
|
self.sql_queries.INSERT_USER,
|
||||||
(
|
(
|
||||||
user_id, # id
|
self.tools.generate_id(),
|
||||||
new_phone, # phone
|
new_phone, # phone
|
||||||
tg_id, # telegram_id
|
tg_id, # telegram_id
|
||||||
firstname, # firstname
|
message.from_user.first_name[:59], # firstname
|
||||||
lastname, # lastname
|
(message.from_user.last_name or "")[:59], # lastname
|
||||||
username, # username
|
(message.from_user.username or "")[:60], # username
|
||||||
json.dumps([]), # profileoptions
|
json.dumps([]), # profileoptions
|
||||||
json.dumps(["TT", "ONEME"]), # options
|
json.dumps(["TT", "ONEME"]), # options
|
||||||
0, # accountstatus
|
0, # accountstatus
|
||||||
@@ -114,17 +105,13 @@ class TelegramBot:
|
|||||||
self.sql_queries.INSERT_USER_DATA,
|
self.sql_queries.INSERT_USER_DATA,
|
||||||
(
|
(
|
||||||
new_phone, # phone
|
new_phone, # phone
|
||||||
|
json.dumps([]), # contacts
|
||||||
|
json.dumps(self.static.USER_FOLDERS), # folders
|
||||||
json.dumps(self.static.USER_SETTINGS), # user settings
|
json.dumps(self.static.USER_SETTINGS), # user settings
|
||||||
json.dumps({}), # chat_config
|
json.dumps({}), # chat_config
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
# Добавляем дефолтную папку
|
|
||||||
await cursor.execute(
|
|
||||||
self.sql_queries.INSERT_DEFAULT_FOLDER,
|
|
||||||
(new_phone,),
|
|
||||||
)
|
|
||||||
|
|
||||||
await message.answer(
|
await message.answer(
|
||||||
self.get_bot_message(
|
self.get_bot_message(
|
||||||
self.msg_types.REGISTRATION_SUCCESS
|
self.msg_types.REGISTRATION_SUCCESS
|
||||||
@@ -139,7 +126,7 @@ class TelegramBot:
|
|||||||
async def start(self):
|
async def start(self):
|
||||||
if self.enabled:
|
if self.enabled:
|
||||||
try:
|
try:
|
||||||
await self.dp.start_polling(self.bot, handle_signals=False)
|
await self.dp.start_polling(self.bot)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.logger.error(f"Ошибка запуска Telegram бота: {e}")
|
self.logger.error(f"Ошибка запуска Telegram бота: {e}")
|
||||||
else:
|
else:
|
||||||
|
|||||||
@@ -19,8 +19,7 @@ class TelegramBotController(ControllerBase):
|
|||||||
token=self.config.telegram_bot_token,
|
token=self.config.telegram_bot_token,
|
||||||
enabled=self.config.telegram_bot_enabled,
|
enabled=self.config.telegram_bot_enabled,
|
||||||
db_pool=api['db'],
|
db_pool=api['db'],
|
||||||
whitelist_ids=self.config.telegram_whitelist_ids,
|
whitelist_ids=self.config.telegram_whitelist_ids
|
||||||
whitelist_enabled=self.config.telegram_whitelist_enabled,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
return _start_all()
|
return _start_all()
|
||||||
|
|||||||
71
tables.sql
71
tables.sql
@@ -1,7 +1,7 @@
|
|||||||
CREATE TABLE `users` (
|
CREATE TABLE `users` (
|
||||||
`id` INT NOT NULL,
|
`id` INT PRIMARY KEY,
|
||||||
`phone` VARCHAR(20) UNIQUE,
|
`phone` VARCHAR(20) UNIQUE,
|
||||||
`telegram_id` VARCHAR(64),
|
`telegram_id` VARCHAR(64) UNIQUE,
|
||||||
`firstname` VARCHAR(59) NOT NULL,
|
`firstname` VARCHAR(59) NOT NULL,
|
||||||
`lastname` VARCHAR(59),
|
`lastname` VARCHAR(59),
|
||||||
`description` VARCHAR(400),
|
`description` VARCHAR(400),
|
||||||
@@ -12,8 +12,7 @@ CREATE TABLE `users` (
|
|||||||
`options` JSON NOT NULL,
|
`options` JSON NOT NULL,
|
||||||
`accountstatus` VARCHAR(16) NOT NULL,
|
`accountstatus` VARCHAR(16) NOT NULL,
|
||||||
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||||
`username` VARCHAR(60) UNIQUE,
|
`username` VARCHAR(60) UNIQUE
|
||||||
PRIMARY KEY (`id`)
|
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE `tokens` (
|
CREATE TABLE `tokens` (
|
||||||
@@ -22,9 +21,7 @@ CREATE TABLE `tokens` (
|
|||||||
`device_type` VARCHAR(256) NOT NULL,
|
`device_type` VARCHAR(256) NOT NULL,
|
||||||
`device_name` VARCHAR(256) NOT NULL,
|
`device_name` VARCHAR(256) NOT NULL,
|
||||||
`location` VARCHAR(256) NOT NULL,
|
`location` VARCHAR(256) NOT NULL,
|
||||||
`time` VARCHAR(16) NOT NULL,
|
`time` VARCHAR(16) NOT NULL
|
||||||
`push_token` VARCHAR(512) DEFAULT NULL,
|
|
||||||
PRIMARY KEY (`phone`, `token_hash`)
|
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE `auth_tokens` (
|
CREATE TABLE `auth_tokens` (
|
||||||
@@ -32,26 +29,25 @@ CREATE TABLE `auth_tokens` (
|
|||||||
`token_hash` VARCHAR(64) NOT NULL,
|
`token_hash` VARCHAR(64) NOT NULL,
|
||||||
`code_hash` VARCHAR(64) NOT NULL,
|
`code_hash` VARCHAR(64) NOT NULL,
|
||||||
`expires` VARCHAR(16) NOT NULL,
|
`expires` VARCHAR(16) NOT NULL,
|
||||||
`state` VARCHAR(16),
|
`state` VARCHAR(16)
|
||||||
PRIMARY KEY (`phone`, `token_hash`)
|
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE `user_data` (
|
CREATE TABLE `user_data` (
|
||||||
`phone` VARCHAR(20) NOT NULL UNIQUE,
|
`phone` VARCHAR(20) NOT NULL UNIQUE PRIMARY KEY,
|
||||||
|
`contacts` JSON NOT NULL,
|
||||||
|
`folders` JSON NOT NULL,
|
||||||
`user_config` JSON NOT NULL,
|
`user_config` JSON NOT NULL,
|
||||||
`chat_config` JSON NOT NULL,
|
`chat_config` JSON NOT NULL
|
||||||
PRIMARY KEY (`phone`)
|
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE `chats` (
|
CREATE TABLE `chats` (
|
||||||
`id` INT NOT NULL,
|
`id` INT NOT NULL PRIMARY KEY,
|
||||||
`owner` INT NOT NULL,
|
`owner` INT NOT NULL,
|
||||||
`type` VARCHAR(16) NOT NULL,
|
`type` VARCHAR(16) NOT NULL
|
||||||
PRIMARY KEY (`id`)
|
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE `messages` (
|
CREATE TABLE `messages` (
|
||||||
`id` BIGINT NOT NULL,
|
`id` INT NOT NULL PRIMARY KEY,
|
||||||
`chat_id` INT NOT NULL,
|
`chat_id` INT NOT NULL,
|
||||||
`sender` INT NOT NULL,
|
`sender` INT NOT NULL,
|
||||||
`time` VARCHAR(32) NOT NULL,
|
`time` VARCHAR(32) NOT NULL,
|
||||||
@@ -59,8 +55,7 @@ CREATE TABLE `messages` (
|
|||||||
`attaches` JSON NOT NULL,
|
`attaches` JSON NOT NULL,
|
||||||
`cid` VARCHAR(32) NOT NULL,
|
`cid` VARCHAR(32) NOT NULL,
|
||||||
`elements` JSON NOT NULL,
|
`elements` JSON NOT NULL,
|
||||||
`type` VARCHAR(16) NOT NULL,
|
`type` VARCHAR(16) NOT NULL
|
||||||
PRIMARY KEY (`id`)
|
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE `chat_participants` (
|
CREATE TABLE `chat_participants` (
|
||||||
@@ -69,43 +64,3 @@ CREATE TABLE `chat_participants` (
|
|||||||
`joined_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
`joined_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||||
PRIMARY KEY (`chat_id`, `user_id`)
|
PRIMARY KEY (`chat_id`, `user_id`)
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE `contacts` (
|
|
||||||
`owner_id` INT NOT NULL,
|
|
||||||
`contact_id` INT NOT NULL,
|
|
||||||
`custom_firstname` VARCHAR(64),
|
|
||||||
`custom_lastname` VARCHAR(64),
|
|
||||||
`is_blocked` BOOLEAN NOT NULL DEFAULT FALSE,
|
|
||||||
PRIMARY KEY (`owner_id`, `contact_id`)
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE TABLE `banners` (
|
|
||||||
`id` VARCHAR(64) NOT NULL,
|
|
||||||
`title` VARCHAR(256) NOT NULL,
|
|
||||||
`description` VARCHAR(512) NOT NULL,
|
|
||||||
`url` VARCHAR(512) NOT NULL,
|
|
||||||
`type` INT NOT NULL DEFAULT 1,
|
|
||||||
`priority` INT NOT NULL DEFAULT 0,
|
|
||||||
`animoji_id` INT NOT NULL DEFAULT 0,
|
|
||||||
`repeat` INT NOT NULL DEFAULT 1,
|
|
||||||
`rerun` BIGINT NOT NULL DEFAULT 0,
|
|
||||||
`hide_close_button` BOOLEAN NOT NULL DEFAULT FALSE,
|
|
||||||
`hide_on_click` BOOLEAN NOT NULL DEFAULT FALSE,
|
|
||||||
`is_title_animated` BOOLEAN NOT NULL DEFAULT FALSE,
|
|
||||||
`enabled` BOOLEAN NOT NULL DEFAULT TRUE,
|
|
||||||
`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 ('[]'),
|
|
||||||
`include` 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`)
|
|
||||||
);
|
|
||||||
|
|||||||
Reference in New Issue
Block a user