-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4b4cfcb
commit 5b71883
Showing
8 changed files
with
107 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,54 @@ | ||
from faststream.rabbit import RabbitRouter | ||
|
||
from application.user import UserService | ||
from domain.user import BanUser, BannedUser, VerifyUser, VerifiedUser, UnbanUser, UserRepository | ||
from domain.user import BanUser, VerifyUser, UnbanUser, GetUser, TelegramRequestResponse, GetUserResponse, UserRepository | ||
from infrastructure.db import CTX_SESSION | ||
|
||
|
||
router = RabbitRouter(prefix="adm_") | ||
|
||
@router.subscriber("ban") | ||
@router.publisher("banned") | ||
async def ban_user(data: BanUser) -> BannedUser: | ||
async def ban_user(data: BanUser) -> TelegramRequestResponse: | ||
user = await UserRepository().get(data.user_id) | ||
if not user: | ||
return BannedUser(msg_id = data.msg_id, success = False) | ||
return TelegramRequestResponse(msg_id = data.msg_id, success = False) | ||
await UserService().ban(user, data.reason) | ||
await CTX_SESSION.get().commit() | ||
return BannedUser(msg_id = data.msg_id, success = True) | ||
return TelegramRequestResponse(msg_id = data.msg_id, success = True) | ||
|
||
@router.subscriber("unban") | ||
@router.publisher("unbanned") | ||
async def ban_user(data: UnbanUser) -> BannedUser: | ||
async def ban_user(data: UnbanUser) -> TelegramRequestResponse: | ||
user = await UserRepository().get(data.user_id) | ||
if not user: | ||
return BannedUser(msg_id = data.msg_id, success = False) | ||
return TelegramRequestResponse(msg_id = data.msg_id, success = False) | ||
await UserService().unban(user) | ||
await CTX_SESSION.get().commit() | ||
return BannedUser(msg_id = data.msg_id, success = True) | ||
|
||
return TelegramRequestResponse(msg_id = data.msg_id, success = True) | ||
|
||
@router.subscriber("verify") | ||
@router.publisher("verified") | ||
async def ban_user(data: VerifyUser) -> VerifiedUser: | ||
async def ban_user(data: VerifyUser) -> TelegramRequestResponse: | ||
user = await UserRepository().get(data.user_id) | ||
if not user: | ||
return VerifiedUser(msg_id = data.msg_id, success = False) | ||
return TelegramRequestResponse(msg_id = data.msg_id, success = False) | ||
user.verify = data.value | ||
await CTX_SESSION.get().commit() | ||
return VerifiedUser(msg_id = data.msg_id, success = True) | ||
return TelegramRequestResponse(msg_id = data.msg_id, success = True) | ||
|
||
@router.subscriber("user") | ||
@router.publisher("user-res") | ||
async def ban_user(data: GetUser) -> TelegramRequestResponse: | ||
user = await UserRepository().get(data.user_id) | ||
if not user: | ||
return TelegramRequestResponse(msg_id = data.msg_id, success = False) | ||
return GetUserResponse( | ||
text = | ||
f"\n<b>Имя:</b> {user.mention} <b>(<code>{user.id}</code>)</b>" | ||
f"\n<b>Класс:</b> {user.literal}" | ||
f"\n<b>Пол:</b> {'Мужской' if user.male else 'Женский'}" | ||
f"\n<b>Описание:</b> <i>{user.desc}</i>", | ||
attachments = [attachment.url for attachment in user.attachments], | ||
success = True | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,25 @@ | ||
from pydantic import BaseModel | ||
|
||
|
||
class UnbanUser(BaseModel): | ||
class TelegramRequest(BaseModel): | ||
msg_id: int | ||
|
||
class UnbanUser(TelegramRequest): | ||
user_id: int | ||
|
||
class BanUser(UnbanUser): | ||
reason: str | ||
|
||
class BannedUser(BaseModel): | ||
msg_id: int | ||
success: bool | ||
|
||
class VerifyUser(BaseModel): | ||
msg_id: int | ||
class VerifyUser(TelegramRequest): | ||
user_id: int | ||
value: bool | ||
|
||
class VerifiedUser(BaseModel): | ||
msg_id: int | ||
success: bool | ||
class GetUser(TelegramRequest): | ||
user_id: int | ||
|
||
class TelegramRequestResponse(TelegramRequest): | ||
success: bool | ||
|
||
class GetUserResponse(GetUser, TelegramRequestResponse): | ||
text: str | ||
attachments: str |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,43 @@ | ||
from faststream.rabbit import RabbitRouter | ||
|
||
from config import TG_ADMIN_CHAT | ||
from models import BannedUser, VerifiedUser | ||
from models import TelegramRequestResponse | ||
from models.admin import GetUserResponse | ||
from telegram.bot import bot | ||
from telegram.utlls import send_media | ||
|
||
|
||
router = RabbitRouter("adm_") | ||
|
||
@router.subscriber("banned") | ||
async def msg(ban: BannedUser) -> None: | ||
async def msg(res: TelegramRequestResponse) -> None: | ||
await bot.send_message( | ||
chat_id = TG_ADMIN_CHAT, | ||
reply_to_message_id = ban.msg_id, | ||
text = "🚫 Пользователь заблокирован" if ban.success else "❕ Неверный пользователь" | ||
reply_to_message_id = res.msg_id, | ||
text = "🚫 Пользователь заблокирован" if res.success else "❕ Неверный пользователь" | ||
) | ||
|
||
@router.subscriber("unbanned") | ||
async def msg(ban: BannedUser) -> None: | ||
async def msg(res: TelegramRequestResponse) -> None: | ||
await bot.send_message( | ||
chat_id = TG_ADMIN_CHAT, | ||
reply_to_message_id = ban.msg_id, | ||
text = "⛓️💥 Пользователь разблокирован" if ban.success else "❕ Неверный пользователь" | ||
reply_to_message_id = res.msg_id, | ||
text = "⛓️💥 Пользователь разблокирован" if res.success else "❕ Неверный пользователь" | ||
) | ||
|
||
@router.subscriber("verified") | ||
async def msg(ban: VerifiedUser) -> None: | ||
async def msg(res: TelegramRequestResponse) -> None: | ||
await bot.send_message( | ||
chat_id = TG_ADMIN_CHAT, | ||
reply_to_message_id = ban.msg_id, | ||
text = "✅ Статус верификации пользователя обновлён" if ban.success else "❕ Неверный пользователь" | ||
reply_to_message_id = res.msg_id, | ||
text = "✅ Статус верификации пользователя обновлён" if res.success else "❕ Неверный пользователь" | ||
) | ||
|
||
@router.subscriber("user-res") | ||
async def msg(res: TelegramRequestResponse | GetUserResponse) -> None: | ||
await send_media( | ||
chat_id = TG_ADMIN_CHAT, | ||
text = res.text, | ||
parse_mode = "markdown", | ||
files = res.attachments | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from aiogram.types import InputMediaPhoto, URLInputFile | ||
|
||
from .bot import bot | ||
|
||
|
||
async def send_media(chat_id: int, text: str, files: list[str], parse_mode: str = "markdown") -> None: | ||
await bot.send_media_group( | ||
chat_id = chat_id, | ||
media = [ | ||
InputMediaPhoto( | ||
media = URLInputFile(file), | ||
caption = text if i == 0 else None, | ||
parse_mode = parse_mode | ||
) | ||
for i, file in enumerate(files) | ||
] | ||
) |