mirror of
https://github.com/openmax-server/server.git
synced 2026-07-08 13:00:16 +03:00
Compare commits
74 Commits
master
..
ac40cc53c9
| Author | SHA1 | Date | |
|---|---|---|---|
| ac40cc53c9 | |||
| 756956d8a0 | |||
| 00071c80be | |||
| a045457128 | |||
| 4d51c70f8e | |||
| 2d3b9285bf | |||
| 6bb0d52419 | |||
| 911008c0a1 | |||
| b8472821eb | |||
| f1c1639d9f | |||
| 7426e83914 | |||
| 8dc3ef1731 | |||
| f1ff4fd062 | |||
| 0b6eda6178 | |||
| 02df98cdbd | |||
| 49d73200b0 | |||
| 389a08ebce | |||
| 613e1b96cd | |||
| 0f2d946b98 | |||
| 1ff974dfce | |||
| bcd94b3a57 | |||
| 89f1fefa31 | |||
| c716520ca4 | |||
| ff46e417f4 | |||
| bd95755db4 | |||
| cb058f6ab6 | |||
| cd9ba981ae | |||
| 810d480dbd | |||
| 227f90c3c3 | |||
| 56133416e3 | |||
| 35a4101608 | |||
| 9fcba1af86 | |||
| 4abe6de885 | |||
| 4121bd0e1d | |||
| d9798a6fc6 | |||
| d9cbafc4e3 | |||
| 448b854bdb | |||
| dcbcb0a600 | |||
| 0ffc649dd9 | |||
| 52949602af | |||
| 0f5c06f80c | |||
| e5607adb9b | |||
| 4014bab5c9 | |||
| 0b7282b284 | |||
| 7a2e5a20d6 | |||
| ac76015d08 | |||
| f4ea147a5f | |||
| 5fcc56950f | |||
| a8732b5d7f | |||
| aa6411b75d | |||
| f2e7e37592 | |||
| 8d7a1dacb5 | |||
| ef512b060f | |||
| 2cf18b878a | |||
| bbee49d2d8 | |||
| 9bc6c15d82 | |||
| 11b2e2748d | |||
| 6c05b5f1b5 | |||
| 2dab853569 | |||
| c7eace4648 | |||
| 09c1f8b6f6 | |||
| 9c3038bc84 | |||
| 91453d7173 | |||
| d5ea45cb96 | |||
| 2d09f52c2e | |||
| d4d5dd5530 | |||
| 81f5fb762f | |||
| 301e55be05 | |||
| db3b7323d9 | |||
| 66fb40a1fd | |||
| 9004566652 | |||
| 07dd71b0ad | |||
| 1ec1d49424 | |||
| 9034485408 |
@@ -1,8 +0,0 @@
|
|||||||
# Патч MAX для IOS
|
|
||||||
|
|
||||||
1) Распаковываете IPA
|
|
||||||
2) Находите файл MAX в Payload/Max.app
|
|
||||||
3) Открываете hex-редактор и находите api.oneme.ru и меняете на свой
|
|
||||||
При желании можете поменять другие URL
|
|
||||||
URL не должен привышать количество символов которое было у изначального URL!
|
|
||||||
4) Открываете IPA как архив и добавляете патченный файл MAX в Payload/Max.app
|
|
||||||
+2
-3
@@ -1,8 +1,7 @@
|
|||||||
# Навигация по faq
|
# Навигация по faq
|
||||||
|
|
||||||
## Работа с сервером
|
## Работа с сервером
|
||||||
- [Установка сервера](install.md)
|
[Установка сервера](install.md)
|
||||||
|
|
||||||
## Патчинг клиентов
|
## Патчинг клиентов
|
||||||
- [Патч мобильного клиента для Android](patch_apk.md)
|
[Патч apk](patch_apk.md)
|
||||||
- [Патч мобильного клиента для IOS](patch_ipa.md)
|
|
||||||
|
|||||||
@@ -1,11 +0,0 @@
|
|||||||
> [!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,6 +22,4 @@ 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)
|
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -6,9 +6,9 @@ class SQLQueries:
|
|||||||
|
|
||||||
INSERT_USER = """
|
INSERT_USER = """
|
||||||
INSERT INTO users
|
INSERT INTO users
|
||||||
(id, phone, telegram_id, firstname, lastname, username,
|
(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)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
INSERT_USER_DATA = """
|
INSERT_USER_DATA = """
|
||||||
|
|||||||
@@ -49,9 +49,6 @@ class SQLiteConnectionCompat:
|
|||||||
async def __aexit__(self, exc_type, exc, tb):
|
async def __aexit__(self, exc_type, exc, tb):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
async def commit(self):
|
|
||||||
await self.connection.commit()
|
|
||||||
|
|
||||||
def cursor(self):
|
def cursor(self):
|
||||||
return SQLiteCursorCompat(self.connection)
|
return SQLiteCursorCompat(self.connection)
|
||||||
|
|
||||||
|
|||||||
@@ -15,7 +15,6 @@ class Static:
|
|||||||
RATE_LIMITED = "rate_limited"
|
RATE_LIMITED = "rate_limited"
|
||||||
CONTACT_NOT_FOUND = "contact_not_found"
|
CONTACT_NOT_FOUND = "contact_not_found"
|
||||||
CONTACT_ALREADY_ADDED = "contact_already_added"
|
CONTACT_ALREADY_ADDED = "contact_already_added"
|
||||||
CONTACT_BLOCKED = "contact_blocked"
|
|
||||||
|
|
||||||
class ChatTypes:
|
class ChatTypes:
|
||||||
DIALOG = "DIALOG"
|
DIALOG = "DIALOG"
|
||||||
@@ -96,12 +95,6 @@ class Static:
|
|||||||
"message": "Contact already added",
|
"message": "Contact already added",
|
||||||
"title": "Контакт уже добавлен"
|
"title": "Контакт уже добавлен"
|
||||||
},
|
},
|
||||||
"contact_blocked": {
|
|
||||||
"localizedMessage": "Вы не можете написать этому пользователю",
|
|
||||||
"error": "contact.blocked",
|
|
||||||
"message": "Contact is blocked",
|
|
||||||
"title": "Вы не можете написать этому пользователю"
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
### Сообщения бота
|
### Сообщения бота
|
||||||
|
|||||||
+44
-120
@@ -1,7 +1,6 @@
|
|||||||
import json
|
import json
|
||||||
import random
|
|
||||||
import secrets
|
|
||||||
import time
|
import time
|
||||||
|
import os
|
||||||
|
|
||||||
import geoip2.database
|
import geoip2.database
|
||||||
|
|
||||||
@@ -10,46 +9,19 @@ 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,
|
||||||
|
|
||||||
@@ -73,8 +45,6 @@ class Tools:
|
|||||||
],
|
],
|
||||||
"options": options,
|
"options": options,
|
||||||
"accountStatus": accountStatus,
|
"accountStatus": accountStatus,
|
||||||
"location": "RU",
|
|
||||||
"registrationTime": int(time.time() * 1000)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if avatarUrl:
|
if avatarUrl:
|
||||||
@@ -108,40 +78,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 +105,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
|
||||||
|
|
||||||
@@ -263,28 +204,27 @@ 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(
|
||||||
favouriteChatId, db_pool, protocol_type=protocol_type
|
senderId, 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,
|
||||||
@@ -430,14 +370,10 @@ class Tools:
|
|||||||
last_message_id = row.get("id") or 0 # последнее id сообщения в чате
|
last_message_id = row.get("id") or 0 # последнее 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` (chat_id, sender, time, text, attaches, cid, elements, type) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)",
|
||||||
(
|
(
|
||||||
message_id,
|
|
||||||
chatId,
|
chatId,
|
||||||
senderId,
|
senderId,
|
||||||
message_time,
|
message_time,
|
||||||
@@ -449,6 +385,8 @@ class Tools:
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
message_id = cursor.lastrowid
|
||||||
|
|
||||||
# Возвращаем айдишки
|
# Возвращаем айдишки
|
||||||
return int(message_id), int(last_message_id), message_time
|
return int(message_id), int(last_message_id), message_time
|
||||||
|
|
||||||
@@ -468,8 +406,24 @@ class Tools:
|
|||||||
if not row:
|
if not row:
|
||||||
return None, None
|
return None, None
|
||||||
|
|
||||||
|
# Собираем сообщение
|
||||||
|
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": {},
|
||||||
|
"link": {}
|
||||||
|
}
|
||||||
|
|
||||||
# Возвращаем
|
# Возвращаем
|
||||||
return self.build_message_dict(row, protocol_type), int(row.get("time"))
|
return message, int(row.get("time"))
|
||||||
|
|
||||||
async def get_previous_message_id(self, chatId, db_pool, protocol_type="mobile"):
|
async def get_previous_message_id(self, chatId, db_pool, protocol_type="mobile"):
|
||||||
"""Получение ID предыдущего сообщения (второго с конца) в чате."""
|
"""Получение ID предыдущего сообщения (второго с конца) в чате."""
|
||||||
@@ -608,33 +562,3 @@ class Tools:
|
|||||||
return response.country.name or "Localhost Federation"
|
return response.country.name or "Localhost Federation"
|
||||||
except Exception:
|
except Exception:
|
||||||
return "Localhost Federation"
|
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
|
|
||||||
@@ -22,7 +22,6 @@ class OnemeController(ControllerBase):
|
|||||||
|
|
||||||
# Выбираем протокол в зависимости от типа подключения
|
# Выбираем протокол в зависимости от типа подключения
|
||||||
proto = self.proto_web if is_web else self.proto_tcp
|
proto = self.proto_web if is_web else self.proto_tcp
|
||||||
packet = None
|
|
||||||
|
|
||||||
# Не отправляем событие самому себе
|
# Не отправляем событие самому себе
|
||||||
if writer == eventData.get("writer"):
|
if writer == eventData.get("writer"):
|
||||||
@@ -95,9 +94,7 @@ class OnemeController(ControllerBase):
|
|||||||
cmd=0, seq=1, opcode=self.opcodes.NOTIF_PRESENCE, payload=payload
|
cmd=0, seq=1, opcode=self.opcodes.NOTIF_PRESENCE, payload=payload
|
||||||
)
|
)
|
||||||
|
|
||||||
if not packet:
|
# Отправляем пакет
|
||||||
return
|
|
||||||
|
|
||||||
if is_web:
|
if is_web:
|
||||||
await writer.send(packet)
|
await writer.send(packet)
|
||||||
else:
|
else:
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -171,10 +145,6 @@ class ContactListPayloadModel(pydantic.BaseModel):
|
|||||||
class ContactPresencePayloadModel(pydantic.BaseModel):
|
class ContactPresencePayloadModel(pydantic.BaseModel):
|
||||||
contactIds: list
|
contactIds: list
|
||||||
|
|
||||||
class ContactAddByPhonePayloadModel(pydantic.BaseModel):
|
|
||||||
phone: str
|
|
||||||
firstName: str
|
|
||||||
|
|
||||||
class ContactUpdatePayloadModel(pydantic.BaseModel):
|
class ContactUpdatePayloadModel(pydantic.BaseModel):
|
||||||
action: str
|
action: str
|
||||||
contactId: int
|
contactId: int
|
||||||
|
|||||||
@@ -2,7 +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 .contacts import ContactsProcessors
|
||||||
from .folders import FoldersProcessors
|
from .folders import FoldersProcessors
|
||||||
from .history import HistoryProcessors
|
from .history import HistoryProcessors
|
||||||
@@ -16,7 +16,7 @@ class Processors(
|
|||||||
AuthProcessors,
|
AuthProcessors,
|
||||||
CallsProcessors,
|
CallsProcessors,
|
||||||
ChatsProcessors,
|
ChatsProcessors,
|
||||||
ComplaintsProcessors,
|
ComplainsProcessors,
|
||||||
ContactsProcessors,
|
ContactsProcessors,
|
||||||
FoldersProcessors,
|
FoldersProcessors,
|
||||||
HistoryProcessors,
|
HistoryProcessors,
|
||||||
|
|||||||
+13
-149
@@ -1,18 +1,12 @@
|
|||||||
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:
|
||||||
@@ -20,148 +14,18 @@ class AssetsProcessors(BaseProcessor):
|
|||||||
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
|
||||||
|
|
||||||
response = {
|
# TODO: сейчас это заглушка, а попозже нужно сделать полноценную реализацию
|
||||||
"sync": int(time.time() * 1000),
|
|
||||||
"stickerSetsUpdates": {},
|
# Данные пакета
|
||||||
"stickersUpdates": {},
|
payload = {
|
||||||
"stickersOrder": [
|
"sections": [],
|
||||||
"RECENT",
|
"sync": int(time.time() * 1000)
|
||||||
"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:
|
|
||||||
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)
|
await self._send(writer, packet)
|
||||||
@@ -297,7 +297,7 @@ class AuthProcessors(BaseProcessor):
|
|||||||
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 + str(photoId)
|
avatar_url = None if not photoId else self.config.avatar_base_url + photoId
|
||||||
description = (
|
description = (
|
||||||
None if not account.get("description") else account.get("description")
|
None if not account.get("description") else account.get("description")
|
||||||
)
|
)
|
||||||
@@ -397,19 +397,15 @@ class AuthProcessors(BaseProcessor):
|
|||||||
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(
|
||||||
"""
|
"""
|
||||||
INSERT INTO users
|
INSERT INTO users
|
||||||
(id, phone, telegram_id, firstname, lastname, username,
|
(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)
|
||||||
""",
|
""",
|
||||||
(
|
(
|
||||||
user_id,
|
|
||||||
phone,
|
phone,
|
||||||
None,
|
None,
|
||||||
first_name,
|
first_name,
|
||||||
@@ -423,6 +419,8 @@ class AuthProcessors(BaseProcessor):
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
user_id = cursor.lastrowid
|
||||||
|
|
||||||
# Добавляем данные аккаунта
|
# Добавляем данные аккаунта
|
||||||
await cursor.execute(
|
await cursor.execute(
|
||||||
"""
|
"""
|
||||||
@@ -521,7 +519,7 @@ class AuthProcessors(BaseProcessor):
|
|||||||
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 None, None, None
|
return
|
||||||
|
|
||||||
# Чаты, где состоит пользователь
|
# Чаты, где состоит пользователь
|
||||||
chats = []
|
chats = []
|
||||||
@@ -545,7 +543,7 @@ class AuthProcessors(BaseProcessor):
|
|||||||
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 None, None, None
|
return
|
||||||
|
|
||||||
# Ищем аккаунт пользователя в бд
|
# Ищем аккаунт пользователя в бд
|
||||||
await cursor.execute(
|
await cursor.execute(
|
||||||
@@ -563,7 +561,7 @@ 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()
|
||||||
|
|
||||||
@@ -578,7 +576,7 @@ class AuthProcessors(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")
|
||||||
|
|
||||||
if self._check_legacy_version(appVersion):
|
if self._check_legacy_version(appVersion):
|
||||||
|
|||||||
@@ -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):
|
||||||
"""Обработчик получения причин жалоб"""
|
"""Обработчик получения причин жалоб"""
|
||||||
# Валидируем данные пакета
|
# Валидируем данные пакета
|
||||||
@@ -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 ContactAddByPhonePayloadModel, ContactListPayloadModel, ContactPresencePayloadModel, ContactUpdatePayloadModel
|
from oneme.models import ContactListPayloadModel, ContactPresencePayloadModel, ContactUpdatePayloadModel
|
||||||
|
|
||||||
class ContactsProcessors(BaseProcessor):
|
class ContactsProcessors(BaseProcessor):
|
||||||
async def contact_list(self, payload, seq, writer, userId):
|
async def contact_list(self, payload, seq, writer, userId):
|
||||||
@@ -112,23 +112,6 @@ class ContactsProcessors(BaseProcessor):
|
|||||||
"INSERT INTO contacts (owner_id, contact_id, custom_firstname, custom_lastname, is_blocked) VALUES (%s, %s, %s, %s, FALSE)",
|
"INSERT INTO contacts (owner_id, contact_id, custom_firstname, custom_lastname, is_blocked) VALUES (%s, %s, %s, %s, FALSE)",
|
||||||
(userId, contactId, firstName, lastName)
|
(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:
|
else:
|
||||||
await self._send_error(seq, self.opcodes.CONTACT_UPDATE, self.error_types.CONTACT_ALREADY_ADDED, writer)
|
await self._send_error(seq, self.opcodes.CONTACT_UPDATE, self.error_types.CONTACT_ALREADY_ADDED, writer)
|
||||||
@@ -294,97 +277,6 @@ class ContactsProcessors(BaseProcessor):
|
|||||||
|
|
||||||
await self._send(writer, packet)
|
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):
|
async def contact_presence(self, payload, seq, writer):
|
||||||
"""Обработчик получения статуса контактов"""
|
"""Обработчик получения статуса контактов"""
|
||||||
# Валидируем данные пакета
|
# Валидируем данные пакета
|
||||||
|
|||||||
@@ -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 == (senderId ^ senderId):
|
||||||
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,21 @@ 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"),
|
||||||
|
"cid": int(row.get("cid")),
|
||||||
|
"text": row.get("text"),
|
||||||
|
"attaches": json.loads(row.get("attaches")),
|
||||||
|
"elements": json.loads(row.get("elements")),
|
||||||
|
"reactionInfo": {},
|
||||||
|
"link": {},
|
||||||
|
#"options": 1,
|
||||||
|
})
|
||||||
|
|
||||||
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 +82,28 @@ 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") if self.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": {},
|
||||||
|
"link": {}
|
||||||
|
#"options": 1,
|
||||||
|
})
|
||||||
|
|
||||||
# Сортируем сообщения по времени
|
# Сортируем сообщения по времени
|
||||||
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
|
||||||
|
|||||||
@@ -153,7 +153,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")
|
||||||
|
|
||||||
# Генерируем профиль
|
# Генерируем профиль
|
||||||
|
|||||||
@@ -88,9 +88,9 @@ class MessagesProcessors(BaseProcessor):
|
|||||||
chatId = userId ^ senderId
|
chatId = userId ^ senderId
|
||||||
|
|
||||||
# Если клиент хочет отправить сообщение в избранное,
|
# Если клиент хочет отправить сообщение в избранное,
|
||||||
# то выставляем в качестве ID чата отрицательный ID отправителя
|
# то выставляем в качестве ID чата ID отправителя
|
||||||
if chatId == (senderId ^ senderId):
|
if chatId == (senderId ^ senderId):
|
||||||
chatId = -senderId
|
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 ^ 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
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ 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")
|
||||||
|
|
||||||
# Получаем данные контакта
|
# Получаем данные контакта
|
||||||
@@ -129,7 +129,7 @@ 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")
|
||||||
|
|
||||||
# Получаем данные контакта
|
# Получаем данные контакта
|
||||||
@@ -223,12 +223,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
|
||||||
|
|
||||||
# Добавляем чат в список
|
# Добавляем чат в список
|
||||||
|
|||||||
@@ -168,54 +168,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,
|
||||||
@@ -359,15 +311,6 @@ class OnemeMobile:
|
|||||||
writer,
|
writer,
|
||||||
userId,
|
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:
|
case self.opcodes.CONTACT_PRESENCE:
|
||||||
await self.auth_required(
|
await self.auth_required(
|
||||||
userPhone,
|
userPhone,
|
||||||
|
|||||||
@@ -151,54 +151,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,
|
||||||
@@ -342,15 +294,6 @@ class OnemeWS:
|
|||||||
websocket,
|
websocket,
|
||||||
userId,
|
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:
|
case self.opcodes.CONTACT_PRESENCE:
|
||||||
await self.auth_required(
|
await self.auth_required(
|
||||||
userPhone,
|
userPhone,
|
||||||
|
|||||||
@@ -22,7 +22,6 @@ class TTController(ControllerBase):
|
|||||||
|
|
||||||
# Выбираем протокол в зависимости от типа подключения
|
# Выбираем протокол в зависимости от типа подключения
|
||||||
proto = self.proto_web if is_web else self.proto_tcp
|
proto = self.proto_web if is_web else self.proto_tcp
|
||||||
packet = None
|
|
||||||
|
|
||||||
# Не отправляем событие самому себе
|
# Не отправляем событие самому себе
|
||||||
if writer == eventData.get("writer"):
|
if writer == eventData.get("writer"):
|
||||||
@@ -95,9 +94,7 @@ class TTController(ControllerBase):
|
|||||||
cmd=0, seq=1, opcode=self.opcodes.NOTIF_PRESENCE, payload=payload
|
cmd=0, seq=1, opcode=self.opcodes.NOTIF_PRESENCE, payload=payload
|
||||||
)
|
)
|
||||||
|
|
||||||
if not packet:
|
# Отправляем пакет
|
||||||
return
|
|
||||||
|
|
||||||
if is_web:
|
if is_web:
|
||||||
await writer.send(packet)
|
await writer.send(packet)
|
||||||
else:
|
else:
|
||||||
|
|||||||
+1
-43
@@ -54,32 +54,6 @@ class AssetsPayloadModel(pydantic.BaseModel):
|
|||||||
type: str = None
|
type: str = None
|
||||||
userId: int = 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):
|
class GetCallTokenPayloadModel(pydantic.BaseModel):
|
||||||
userId: int
|
userId: int
|
||||||
value: str
|
value: str
|
||||||
@@ -102,7 +76,7 @@ class ContactPresencePayloadModel(pydantic.BaseModel):
|
|||||||
class ContactUpdatePayloadModel(pydantic.BaseModel):
|
class ContactUpdatePayloadModel(pydantic.BaseModel):
|
||||||
action: str
|
action: str
|
||||||
contactId: int
|
contactId: int
|
||||||
firstName: str = None
|
firstName: str
|
||||||
lastName: str = None
|
lastName: str = None
|
||||||
|
|
||||||
class TypingPayloadModel(pydantic.BaseModel):
|
class TypingPayloadModel(pydantic.BaseModel):
|
||||||
@@ -121,19 +95,3 @@ class SendMessagePayloadModel(pydantic.BaseModel):
|
|||||||
userId: int = None
|
userId: int = None
|
||||||
chatId: int = None
|
chatId: int = None
|
||||||
message: MessageModel
|
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
|
|
||||||
|
|||||||
+13
-146
@@ -1,18 +1,12 @@
|
|||||||
import pydantic
|
import pydantic
|
||||||
import time
|
import time
|
||||||
from classes.baseprocessor import BaseProcessor
|
from classes.baseprocessor import BaseProcessor
|
||||||
from tamtam.models import (
|
from tamtam.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:
|
||||||
@@ -20,148 +14,21 @@ class AssetsProcessors(BaseProcessor):
|
|||||||
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
|
||||||
|
|
||||||
response = {
|
# TODO: сейчас это заглушка, а попозже нужно сделать полноценную реализацию
|
||||||
|
|
||||||
|
# Данные пакета
|
||||||
|
payload = {
|
||||||
"sync": int(time.time() * 1000),
|
"sync": int(time.time() * 1000),
|
||||||
"stickerSetsUpdates": {},
|
"stickerSetsUpdates": {},
|
||||||
"stickersUpdates": {},
|
"stickersUpdates": {},
|
||||||
"stickersOrder": [
|
"sections": [],
|
||||||
"RECENT",
|
"stickersOrder": []
|
||||||
"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:
|
|
||||||
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)
|
await self._send(writer, packet)
|
||||||
+68
-241
@@ -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)
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -368,7 +159,15 @@ 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, ip):
|
||||||
"""Обработчик финальной аутентификации / регистрации"""
|
"""Обработчик финальной аутентификации"""
|
||||||
|
# Валидируем данные пакета
|
||||||
|
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")
|
||||||
|
|
||||||
@@ -385,9 +184,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 +199,63 @@ 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
|
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,
|
||||||
|
self.tools.get_geo(
|
||||||
|
ip=ip, db_path=self.config.geo_db_path
|
||||||
|
),
|
||||||
|
int(time.time() * 1000)
|
||||||
)
|
)
|
||||||
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
|
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": 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
|
||||||
|
}
|
||||||
|
|
||||||
# Создаем пакет
|
# Создаем пакет
|
||||||
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 +270,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 +291,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"),))
|
||||||
@@ -477,7 +304,7 @@ 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()
|
||||||
|
|
||||||
|
|||||||
@@ -105,14 +105,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,
|
||||||
@@ -143,7 +135,7 @@ class MessagesProcessors(BaseProcessor):
|
|||||||
participant,
|
participant,
|
||||||
{
|
{
|
||||||
"eventType": "new_msg",
|
"eventType": "new_msg",
|
||||||
"chatId": chatId,
|
"chatId": 0 if chatId == senderId else chatId,
|
||||||
"message": bodyMessage,
|
"message": bodyMessage,
|
||||||
"prevMessageId": lastMessageId,
|
"prevMessageId": lastMessageId,
|
||||||
"time": messageTime,
|
"time": messageTime,
|
||||||
@@ -153,7 +145,7 @@ class MessagesProcessors(BaseProcessor):
|
|||||||
|
|
||||||
# Данные пакета
|
# Данные пакета
|
||||||
payload = {
|
payload = {
|
||||||
"chatId": chatId,
|
"chatId": 0 if chatId == senderId else chatId,
|
||||||
"message": bodyMessage,
|
"message": bodyMessage,
|
||||||
"unread": 0,
|
"unread": 0,
|
||||||
"mark": messageTime
|
"mark": messageTime
|
||||||
|
|||||||
@@ -122,30 +122,6 @@ class TamTamMobile:
|
|||||||
await self.auth_required(
|
await self.auth_required(
|
||||||
userPhone, self.processors.assets_update, payload, seq, writer
|
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:
|
case self.opcodes.VIDEO_CHAT_HISTORY:
|
||||||
await self.auth_required(
|
await self.auth_required(
|
||||||
userPhone, self.processors.video_chat_history, payload, seq, writer
|
userPhone, self.processors.video_chat_history, payload, seq, writer
|
||||||
|
|||||||
@@ -109,30 +109,6 @@ class TamTamWS:
|
|||||||
await self.auth_required(
|
await self.auth_required(
|
||||||
userPhone, self.processors.assets_update, payload, seq, websocket
|
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:
|
case self.opcodes.VIDEO_CHAT_HISTORY:
|
||||||
await self.auth_required(
|
await self.auth_required(
|
||||||
userPhone, self.processors.video_chat_history, payload, seq, websocket
|
userPhone, self.processors.video_chat_history, payload, seq, websocket
|
||||||
|
|||||||
@@ -88,14 +88,10 @@ class TelegramBot:
|
|||||||
username = (message.from_user.username or f"user{int(time.time() * 1000)}")[:60]
|
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
|
|
||||||
new_phone, # phone
|
new_phone, # phone
|
||||||
tg_id, # telegram_id
|
tg_id, # telegram_id
|
||||||
firstname, # firstname
|
firstname, # firstname
|
||||||
|
|||||||
+3
-3
@@ -1,7 +1,7 @@
|
|||||||
CREATE TABLE `users` (
|
CREATE TABLE `users` (
|
||||||
`id` INT NOT NULL,
|
`id` INT NOT NULL AUTO_INCREMENT,
|
||||||
`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),
|
||||||
@@ -51,7 +51,7 @@ CREATE TABLE `chats` (
|
|||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE `messages` (
|
CREATE TABLE `messages` (
|
||||||
`id` BIGINT NOT NULL,
|
`id` INT NOT NULL AUTO_INCREMENT,
|
||||||
`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,
|
||||||
|
|||||||
Reference in New Issue
Block a user