Content from global filters (#1267)

* Move global filters check placement into router to add chance to pass context from global filters into handlers in the same way as it possible in other places

* Added changelog
This commit is contained in:
Alex Root Junior 2023-08-14 22:18:11 +03:00 committed by GitHub
parent 577b44cdc1
commit 8ff992bf1d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 23 additions and 17 deletions

View file

@ -101,17 +101,14 @@ class TelegramEventObserver:
)
return wrapped_outer(event, data)
def check_root_filters(self, event: TelegramObject, **kwargs: Any) -> Any:
return self._handler.check(event, **kwargs)
async def trigger(self, event: TelegramObject, **kwargs: Any) -> Any:
"""
Propagate event to handlers and stops propagation on first match.
Handler will be called when all its filters is pass.
Handler will be called when all its filters are pass.
"""
# Check globally defined filters before any other handler will be checked
result, data = await self._handler.check(event, **kwargs)
if not result:
return REJECTED
kwargs.update(data)
for handler in self.handlers:
result, data = await handler.check(event, **kwargs)
if result:

View file

@ -125,8 +125,17 @@ class Router:
) -> Any:
response = UNHANDLED
if observer:
# Check globally defined filters before any other handler will be checked.
# This check is placed here instead of `trigger` method to add possibility
# to pass context to handlers from global filters.
result, data = await observer.check_root_filters(event, **kwargs)
if not result:
return UNHANDLED
kwargs.update(data)
response = await observer.trigger(event, **kwargs)
if response is REJECTED:
if response is REJECTED: # pragma: no cover
# Possible only if some handler returns REJECTED
return UNHANDLED
if response is not UNHANDLED:
return response