From ee650670ae03314856dac4e9db28f8d1338dd175 Mon Sep 17 00:00:00 2001 From: Ilya Smirnov Date: Thu, 24 Mar 2022 12:50:53 +0300 Subject: [PATCH 1/2] new: manager owner --- aiogram_dialog/context/intent_filter.py | 15 +++++++++------ aiogram_dialog/manager/manager.py | 3 ++- aiogram_dialog/manager/protocols.py | 12 ++++++++++++ 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/aiogram_dialog/context/intent_filter.py b/aiogram_dialog/context/intent_filter.py index 36853c75..30eccc98 100644 --- a/aiogram_dialog/context/intent_filter.py +++ b/aiogram_dialog/context/intent_filter.py @@ -13,7 +13,7 @@ from .events import DialogUpdateEvent from .storage import StorageProxy from ..exceptions import InvalidStackIdError, OutdatedIntent -from ..utils import remove_indent_id, get_chat +from ..utils import remove_indent_id, get_chat, get_owner_id STORAGE_KEY = "aiogd_storage_proxy" STACK_KEY = "aiogd_stack" @@ -51,9 +51,10 @@ async def on_pre_process_message(self, event: Union[Message, ChatMemberUpdated], data: dict): chat = get_chat(event) + owner = data['dialog_manager'].owner proxy = StorageProxy( storage=self.storage, - user_id=event.from_user.id, + user_id=get_owner_id(event, owner), chat_id=chat.id, state_groups=self.state_groups, ) @@ -74,9 +75,10 @@ async def on_post_process_message(self, _, result, data: dict): async def on_pre_process_aiogd_update(self, event: DialogUpdateEvent, data: dict): chat = get_chat(event) + owner = data['dialog_manager'].owner proxy = StorageProxy( storage=self.storage, - user_id=event.from_user.id, + user_id=get_owner_id(event, owner), chat_id=chat.id, state_groups=self.state_groups, ) @@ -112,9 +114,10 @@ async def on_pre_process_aiogd_update(self, event: DialogUpdateEvent, async def on_pre_process_callback_query(self, event: CallbackQuery, data: dict): chat = get_chat(event) + owner = data['dialog_manager'].owner proxy = StorageProxy( storage=self.storage, - user_id=event.from_user.id, + user_id=get_owner_id(event, owner), chat_id=chat.id, state_groups=self.state_groups, ) @@ -161,10 +164,10 @@ async def on_pre_process_error(self, update: Update, error: Exception, return chat = get_chat(event) - + owner = data['dialog_manager'].owner proxy = StorageProxy( storage=self.storage, - user_id=event.from_user.id, + user_id=get_owner_id(event, owner), chat_id=chat.id, state_groups=self.state_groups, ) diff --git a/aiogram_dialog/manager/manager.py b/aiogram_dialog/manager/manager.py index 5d3d006d..c8f8c39f 100644 --- a/aiogram_dialog/manager/manager.py +++ b/aiogram_dialog/manager/manager.py @@ -9,7 +9,7 @@ from .protocols import ( DialogManager, BaseDialogManager, ShowMode, LaunchMode, ManagedDialogAdapterProto, ManagedDialogProto, DialogRegistryProto, - NewMessage, + NewMessage, Owner, ) from ..context.context import Context from ..context.events import ChatEvent, StartMode, Data, FakeChat, FakeUser @@ -30,6 +30,7 @@ def __init__(self, event: ChatEvent, registry: DialogRegistryProto, self.event = event self.data = data self.show_mode: ShowMode = ShowMode.AUTO + self.owner: Owner = Owner.USER @property def registry(self) -> DialogRegistryProto: diff --git a/aiogram_dialog/manager/protocols.py b/aiogram_dialog/manager/protocols.py index 020d9aab..fc148b00 100644 --- a/aiogram_dialog/manager/protocols.py +++ b/aiogram_dialog/manager/protocols.py @@ -19,6 +19,17 @@ class ShowMode(Enum): SEND = "send" +class Owner(Enum): + """ + `GROUP` any users in a group can use this dialog + + `USER` only one user can use this dialog + """ + + GROUP = "group" + USER = "user" + + class LaunchMode(Enum): """ `ROOT` dialogs will be always a root dialog in stack. @@ -213,6 +224,7 @@ class DialogManager(BaseDialogManager): event: ChatEvent # current processing event data: Dict # data from middleware show_mode: ShowMode # mode used to show messages + owner: Owner # mode who can use dialogs def is_preview(self) -> bool: raise NotImplementedError From 9623adf3e02ee520009760e48b79ddf0f24e03c8 Mon Sep 17 00:00:00 2001 From: Ilya Smirnov Date: Thu, 24 Mar 2022 12:58:39 +0300 Subject: [PATCH 2/2] fix: add get owner function --- aiogram_dialog/utils.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/aiogram_dialog/utils.py b/aiogram_dialog/utils.py index f9babf58..1f8528bf 100644 --- a/aiogram_dialog/utils.py +++ b/aiogram_dialog/utils.py @@ -14,7 +14,7 @@ from .context.events import ( DialogUpdateEvent, ChatEvent ) -from .manager.protocols import MediaAttachment, NewMessage, ShowMode, MediaId +from .manager.protocols import MediaAttachment, NewMessage, ShowMode, MediaId, Owner logger = getLogger(__name__) @@ -38,6 +38,15 @@ def get_chat(event: ChatEvent) -> Chat: return event.message.chat +def get_owner_id(event: ChatEvent, owner: Owner) -> int: + if owner == Owner.GROUP: + return get_chat(event).id + elif owner == Owner.USER: + return event.from_user.id + else: + raise ValueError('Unknown owner type %s', owner) + + def is_chat_loaded(chat: Chat) -> bool: """ Checks if chat is correctly loaded from telegram.