Skip to content

Commit

Permalink
get user command
Browse files Browse the repository at this point in the history
  • Loading branch information
Zoom-Developer committed Nov 4, 2024
1 parent 4b4cfcb commit 5b71883
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 53 deletions.
1 change: 1 addition & 0 deletions backend/src/application/user/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ async def send_verify_request(self, user: User) -> None:
async def update_avatar(self, user: User, avatar: bytes) -> None:
if len(avatar) > MAX_AVATAR_SIZE:
raise FileSizeException
user.verify = False
await AttachmentService().delete(user.attachments[0])
await AttachmentService().upload(avatar, user)

Expand Down
25 changes: 13 additions & 12 deletions backend/src/domain/user/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,25 @@ class PatchUser(BaseUser):
class ReportUser(BaseModel):
reason: str = Field(min_length=3, max_length=64)

class UnbanUser(BaseModel):
class TelegramRequest(BaseModel):
msg_id: int

class UnbanUser(TelegramRequest):
user_id: int

class BanUser(UnbanUser):
msg_id: int
user_id: int
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: list[str]
37 changes: 26 additions & 11 deletions backend/src/interface/rabbit/admin.py
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
)
23 changes: 13 additions & 10 deletions bot/src/models/admin.py
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
31 changes: 21 additions & 10 deletions bot/src/rabbit/routers/admin.py
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
)
14 changes: 5 additions & 9 deletions bot/src/rabbit/routers/telegram.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from config import TG_ADMIN_CHAT
from models import SendAdminMediaMessage, SendAdminMessage, SendMessage, SendMediaMessage
from telegram.bot import bot
from telegram.utlls import send_media


router = RabbitRouter("tg_")
Expand All @@ -18,16 +19,11 @@ async def msg(msg: SendMessage | SendAdminMessage) -> None:

@router.subscriber("media")
async def msg(msg: SendMediaMessage | SendAdminMediaMessage) -> None:
await bot.send_media_group(
await send_media(
chat_id = msg.chat_id if msg.chat_id != -1 else TG_ADMIN_CHAT,
media = [
InputMediaPhoto(
media = URLInputFile(file),
caption = msg.text if i == 0 else None,
parse_mode = msg.parse_mode
)
for i, file in enumerate(msg.files)
]
text = msg.text,
parse_mode = msg.parse_mode,
files = msg.files
)


Expand Down
12 changes: 11 additions & 1 deletion bot/src/telegram/handlers/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from aiogram.types import Message

from config import TG_ADMIN_CHAT
from models.admin import BanUser, UnbanUser, VerifyUser
from models.admin import BanUser, GetUser, UnbanUser, VerifyUser
from rabbit.broker import broker

handler = Router()
Expand Down Expand Up @@ -47,4 +47,14 @@ async def ban(msg: Message):
await broker.publish(
VerifyUser(msg_id = msg.message_id, user_id = args[0], value = False),
"adm_verify"
)

@handler.message(Command("get"), F.chat.id == TG_ADMIN_CHAT)
async def ban(msg: Message):
args = msg.text.split()[1:]
if len(args) < 1 or not args[0].isdigit():
return msg.reply("❕ Use: /get [USER_ID]")
await broker.publish(
GetUser(msg_id = msg.message_id, user_id = args[0]),
"adm_user"
)
17 changes: 17 additions & 0 deletions bot/src/telegram/utlls.py
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)
]
)

0 comments on commit 5b71883

Please sign in to comment.