mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
Added GLOBAL_USER FSM strategy
This commit is contained in:
parent
2165bf3b70
commit
e0b03dc2fe
4 changed files with 22 additions and 10 deletions
|
|
@ -31,7 +31,7 @@ class Dispatcher(Router):
|
|||
def __init__(
|
||||
self,
|
||||
storage: Optional[BaseStorage] = None,
|
||||
fsm_strategy: FSMStrategy = FSMStrategy.USER,
|
||||
fsm_strategy: FSMStrategy = FSMStrategy.USER_IN_CHAT,
|
||||
isolate_events: bool = True,
|
||||
**kwargs: Any,
|
||||
) -> None:
|
||||
|
|
|
|||
|
|
@ -1,6 +0,0 @@
|
|||
from enum import Enum, auto
|
||||
|
||||
|
||||
class FSMStrategy(Enum):
|
||||
CHAT = auto()
|
||||
USER = auto()
|
||||
|
|
@ -3,6 +3,7 @@ from typing import Any, Awaitable, Callable, Dict, Optional
|
|||
from aiogram.dispatcher.fsm.context import FSMContext
|
||||
from aiogram.dispatcher.fsm.engine import FSMStrategy
|
||||
from aiogram.dispatcher.fsm.storage.base import BaseStorage
|
||||
from aiogram.dispatcher.fsm.strategy import apply_strategy
|
||||
from aiogram.dispatcher.middlewares.base import BaseMiddleware
|
||||
from aiogram.types import Update
|
||||
|
||||
|
|
@ -11,7 +12,7 @@ class FSMContextMiddleware(BaseMiddleware[Update]):
|
|||
def __init__(
|
||||
self,
|
||||
storage: BaseStorage,
|
||||
strategy: FSMStrategy = FSMStrategy.USER,
|
||||
strategy: FSMStrategy = FSMStrategy.USER_IN_CHAT,
|
||||
isolate_events: bool = True,
|
||||
) -> None:
|
||||
self.storage = storage
|
||||
|
|
@ -41,10 +42,11 @@ class FSMContextMiddleware(BaseMiddleware[Update]):
|
|||
|
||||
if chat_id is None:
|
||||
chat_id = user_id
|
||||
if self.strategy == FSMStrategy.CHAT:
|
||||
user_id = chat_id
|
||||
|
||||
if chat_id is not None and user_id is not None:
|
||||
chat_id, user_id = apply_strategy(
|
||||
chat_id=chat_id, user_id=user_id, strategy=self.strategy
|
||||
)
|
||||
return self.get_context(chat_id=chat_id, user_id=user_id)
|
||||
return None
|
||||
|
||||
|
|
|
|||
16
aiogram/dispatcher/fsm/strategy.py
Normal file
16
aiogram/dispatcher/fsm/strategy.py
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
from enum import Enum, auto
|
||||
from typing import Tuple
|
||||
|
||||
|
||||
class FSMStrategy(Enum):
|
||||
USER_IN_CHAT = auto()
|
||||
CHAT = auto()
|
||||
GLOBAL_USER = auto()
|
||||
|
||||
|
||||
def apply_strategy(chat_id: int, user_id: int, strategy: FSMStrategy) -> Tuple[int, int]:
|
||||
if strategy == FSMStrategy.CHAT:
|
||||
return chat_id, chat_id
|
||||
if strategy == FSMStrategy.GLOBAL_USER:
|
||||
return user_id, user_id
|
||||
return chat_id, user_id
|
||||
Loading…
Add table
Add a link
Reference in a new issue