Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TBD: Manager owner #137

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions aiogram_dialog/context/intent_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
)
Expand All @@ -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,
)
Expand Down Expand Up @@ -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,
)
Expand Down Expand Up @@ -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,
)
Expand Down
3 changes: 2 additions & 1 deletion aiogram_dialog/manager/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
12 changes: 12 additions & 0 deletions aiogram_dialog/manager/protocols.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion aiogram_dialog/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand All @@ -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.
Expand Down