init
This commit is contained in:
3
middleware/telegram/__init__.py
Normal file
3
middleware/telegram/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from .auth import AuthMiddleware
|
||||
from .i18n import I18nMiddleware
|
||||
from .reset_state import ResetStateMiddleware
|
||||
BIN
middleware/telegram/__pycache__/__init__.cpython-313.pyc
Normal file
BIN
middleware/telegram/__pycache__/__init__.cpython-313.pyc
Normal file
Binary file not shown.
BIN
middleware/telegram/__pycache__/auth.cpython-313.pyc
Normal file
BIN
middleware/telegram/__pycache__/auth.cpython-313.pyc
Normal file
Binary file not shown.
BIN
middleware/telegram/__pycache__/i18n.cpython-313.pyc
Normal file
BIN
middleware/telegram/__pycache__/i18n.cpython-313.pyc
Normal file
Binary file not shown.
BIN
middleware/telegram/__pycache__/reset_state.cpython-313.pyc
Normal file
BIN
middleware/telegram/__pycache__/reset_state.cpython-313.pyc
Normal file
Binary file not shown.
40
middleware/telegram/auth.py
Normal file
40
middleware/telegram/auth.py
Normal file
@@ -0,0 +1,40 @@
|
||||
from aiogram import BaseMiddleware
|
||||
from aiogram.types import TelegramObject, Message, CallbackQuery
|
||||
from aiogram.utils.i18n import gettext as _
|
||||
from babel.support import LazyProxy
|
||||
from typing import Any, Awaitable, Callable, Dict
|
||||
|
||||
from ...model.user import UserBase
|
||||
|
||||
|
||||
class AuthMiddleware(BaseMiddleware):
|
||||
|
||||
def __init__[UserType: UserBase](self,
|
||||
user_class: type[UserType],
|
||||
not_authenticated_msg: LazyProxy | str = "not authenticated",
|
||||
not_active_msg: LazyProxy | str = "not active"):
|
||||
|
||||
self.user_class = user_class
|
||||
self.not_authenticated_msg = not_authenticated_msg
|
||||
self.not_active_msg = not_active_msg
|
||||
|
||||
|
||||
async def __call__(self,
|
||||
handler: Callable[[TelegramObject, Dict[str, Any]], Awaitable[Any]],
|
||||
event: TelegramObject,
|
||||
data: Dict[str, Any]) -> Any:
|
||||
|
||||
user = await self.user_class.get(id = event.from_user.id, session = data["db_session"])
|
||||
|
||||
if user and user.is_active:
|
||||
data["user"] = user
|
||||
return await handler(event, data)
|
||||
|
||||
if type(event) in [Message, CallbackQuery]:
|
||||
if user and not user.is_active:
|
||||
return await event.answer(self.not_active_msg)
|
||||
return await event.answer(self.not_authenticated_msg)
|
||||
|
||||
|
||||
|
||||
|
||||
28
middleware/telegram/i18n.py
Normal file
28
middleware/telegram/i18n.py
Normal file
@@ -0,0 +1,28 @@
|
||||
from typing import Optional, Dict, Any
|
||||
from aiogram.utils.i18n import I18n, SimpleI18nMiddleware
|
||||
from aiogram.types import TelegramObject
|
||||
from ...model.user import UserBase
|
||||
|
||||
|
||||
class I18nMiddleware(SimpleI18nMiddleware):
|
||||
"""
|
||||
This middleware stores locale in the FSM storage
|
||||
"""
|
||||
|
||||
def __init__[UserType: UserBase](
|
||||
self,
|
||||
user_class: type[UserType],
|
||||
i18n: I18n,
|
||||
i18n_key: Optional[str] = "i18n",
|
||||
middleware_key: str = "i18n_middleware",
|
||||
) -> None:
|
||||
self.user_class = user_class
|
||||
super().__init__(i18n=i18n, i18n_key=i18n_key, middleware_key=middleware_key)
|
||||
|
||||
async def get_locale(self, event: TelegramObject, data: Dict[str, Any]) -> str:
|
||||
db_session = data.get("db_session")
|
||||
if db_session and event.model_fields.get("from_user"):
|
||||
user = await self.user_class.get(id = event.from_user.id, session = db_session)
|
||||
if user and user.lang:
|
||||
return user.lang
|
||||
return await super().get_locale(event=event, data=data)
|
||||
30
middleware/telegram/reset_state.py
Normal file
30
middleware/telegram/reset_state.py
Normal file
@@ -0,0 +1,30 @@
|
||||
from typing import Any, Awaitable, Callable, Dict
|
||||
from aiogram import BaseMiddleware
|
||||
from aiogram.types import TelegramObject
|
||||
from aiogram.fsm.context import FSMContext
|
||||
from aiogram.utils.i18n import gettext as _
|
||||
|
||||
from ...bot.handlers.context import ContextData
|
||||
|
||||
|
||||
class ResetStateMiddleware(BaseMiddleware):
|
||||
async def __call__(self,
|
||||
handler: Callable[[TelegramObject, Dict[str, Any]], Awaitable[Any]],
|
||||
event: TelegramObject,
|
||||
data: Dict[str, Any]) -> Any:
|
||||
|
||||
save_state = False
|
||||
callback_data = data.get("callback_data")
|
||||
if isinstance(callback_data, ContextData):
|
||||
save_state = callback_data.save_state
|
||||
|
||||
if not save_state:
|
||||
state = data.get("state")
|
||||
if isinstance(state, FSMContext):
|
||||
await state.clear()
|
||||
|
||||
return await handler(event, data)
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user