Skip to content

Commit

Permalink
Add new FSM strategy CHAT_TOPIC strategy.py (#1344)
Browse files Browse the repository at this point in the history
* Create 1343.feature.rst

* Add new FSM strategy CHAT_TOPIC strategy.py

* ADD CHAT_TOPIC tests test_strategy.py

* Update 1343.feature.rst

* Update strategy.py

* add typing  user_id: Optional[int] = None, middleware.py

* add typing  user_id: Optional[int] = None  base.py

* Update strategy.py

* Update strategy.py

* Update middleware.py

* Update base.py

* Update test_strategy.py

* Update base.py

* Update strategy.py
  • Loading branch information
OnaZeroN committed Oct 21, 2023
1 parent 3b21262 commit d8e7801
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES/1343.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The new FSM strategy CHAT_TOPIC, which sets the state for the entire topic in the chat, also works in private messages and regular groups without topics.
4 changes: 4 additions & 0 deletions aiogram/fsm/strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class FSMStrategy(Enum):
CHAT = auto()
GLOBAL_USER = auto()
USER_IN_TOPIC = auto()
CHAT_TOPIC = auto()


def apply_strategy(
Expand All @@ -21,4 +22,7 @@ def apply_strategy(
return user_id, user_id, None
if strategy == FSMStrategy.USER_IN_TOPIC:
return chat_id, user_id, thread_id
if strategy == FSMStrategy.CHAT_TOPIC:
return chat_id, chat_id, thread_id

return chat_id, user_id, None
3 changes: 3 additions & 0 deletions tests/test_fsm/test_strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ class TestStrategy:
[FSMStrategy.USER_IN_TOPIC, CHAT, CHAT],
[FSMStrategy.USER_IN_TOPIC, PRIVATE, PRIVATE],
[FSMStrategy.USER_IN_TOPIC, THREAD, THREAD],
[FSMStrategy.CHAT_TOPIC, CHAT, (CHAT_ID, CHAT_ID, None)],
[FSMStrategy.CHAT_TOPIC, PRIVATE, PRIVATE],
[FSMStrategy.CHAT_TOPIC, THREAD, (CHAT_ID, CHAT_ID, THREAD_ID)],
],
)
def test_strategy(self, strategy, case, expected):
Expand Down

0 comments on commit d8e7801

Please sign in to comment.