From d2fc9b6cfce2a331b059081753693465ffbc016e Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Fri, 1 Sep 2023 19:49:36 +0200 Subject: [PATCH] Add some ways to convert input chats --- .../telethon/_impl/client/client/client.py | 17 +++++++- .../src/telethon/_impl/client/client/users.py | 40 ++++++++++++++++++- .../src/telethon/_impl/session/chat/packed.py | 31 +++++++------- 3 files changed, 71 insertions(+), 17 deletions(-) diff --git a/client/src/telethon/_impl/client/client/client.py b/client/src/telethon/_impl/client/client/client.py index 3d44ce1b..3670081f 100644 --- a/client/src/telethon/_impl/client/client/client.py +++ b/client/src/telethon/_impl/client/client/client.py @@ -5,9 +5,11 @@ from typing import Deque, Optional, Self, Type, TypeVar, Union from ...mtsender.sender import Sender from ...session.chat.hash_cache import ChatHashCache +from ...session.chat.packed import PackedChat from ...session.message_box.messagebox import MessageBox from ...tl import abcs from ...tl.core.request import Request +from ..types.chat import ChatLike from ..types.chat.user import User from ..types.login_token import LoginToken from ..types.password_token import PasswordToken @@ -64,7 +66,14 @@ from .updates import ( set_receive_updates, ) from .uploads import send_file, upload_file -from .users import get_entity, get_input_entity, get_me, get_peer_id +from .users import ( + get_entity, + get_input_entity, + get_me, + get_peer_id, + input_as_peer, + resolve_to_packed, +) Return = TypeVar("Return") @@ -229,6 +238,12 @@ class Client: async def get_input_entity(self) -> None: await get_input_entity(self) + async def _resolve_to_packed(self, chat: ChatLike) -> PackedChat: + return await resolve_to_packed(self, chat) + + def _input_as_peer(self, input: Optional[abcs.InputPeer]) -> Optional[abcs.Peer]: + return input_as_peer(self, input) + async def get_peer_id(self) -> None: await get_peer_id(self) diff --git a/client/src/telethon/_impl/client/client/users.py b/client/src/telethon/_impl/client/client/users.py index 63f1b5ea..a7408aa9 100644 --- a/client/src/telethon/_impl/client/client/users.py +++ b/client/src/telethon/_impl/client/client/users.py @@ -1,6 +1,10 @@ from __future__ import annotations -from typing import TYPE_CHECKING +from typing import TYPE_CHECKING, Optional + +from ...session.chat.packed import PackedChat +from ...tl import abcs, types +from ..types.chat import Channel, ChatLike, Group, User if TYPE_CHECKING: from .client import Client @@ -34,3 +38,37 @@ async def get_input_entity(self: Client) -> None: async def get_peer_id(self: Client) -> None: self raise NotImplementedError + + +async def resolve_to_packed(self: Client, chat: ChatLike) -> PackedChat: + if isinstance(chat, (User, Group, Channel)): + packed = chat.pack() + if packed is None: + raise ValueError("Cannot resolve chat") + return packed + raise ValueError("Cannot resolve chat") + + +def input_as_peer(self: Client, input: Optional[abcs.InputPeer]) -> Optional[abcs.Peer]: + if input is None: + return None + elif isinstance(input, types.InputPeerEmpty): + return None + elif isinstance(input, types.InputPeerSelf): + return ( + types.PeerUser(user_id=self._config.session.user.id) + if self._config.session.user + else None + ) + elif isinstance(input, types.InputPeerChat): + return types.PeerChat(chat_id=input.chat_id) + elif isinstance(input, types.InputPeerUser): + return types.PeerUser(user_id=input.user_id) + elif isinstance(input, types.InputPeerChannel): + return types.PeerChannel(channel_id=input.channel_id) + elif isinstance(input, types.InputPeerUserFromMessage): + return None + elif isinstance(input, types.InputPeerChannelFromMessage): + return None + else: + raise RuntimeError("unexpected case") diff --git a/client/src/telethon/_impl/session/chat/packed.py b/client/src/telethon/_impl/session/chat/packed.py index 3c06a74f..b6c806a6 100644 --- a/client/src/telethon/_impl/session/chat/packed.py +++ b/client/src/telethon/_impl/session/chat/packed.py @@ -51,7 +51,7 @@ class PackedChat: PackedType.GIGAGROUP, ) - def to_peer(self) -> abcs.Peer: + def _to_peer(self) -> abcs.Peer: if self.is_user(): return types.PeerUser(user_id=self.id) elif self.is_chat(): @@ -61,7 +61,7 @@ class PackedChat: else: raise RuntimeError("unexpected case") - def to_input_peer(self) -> abcs.InputPeer: + def _to_input_peer(self) -> abcs.InputPeer: if self.is_user(): return types.InputPeerUser( user_id=self.id, access_hash=self.access_hash or 0 @@ -75,24 +75,25 @@ class PackedChat: else: raise RuntimeError("unexpected case") - def try_to_input_user(self) -> Optional[abcs.InputUser]: + def _to_input_user(self) -> types.InputUser: if self.is_user(): return types.InputUser(user_id=self.id, access_hash=self.access_hash or 0) else: - return None + raise ValueError("chat is not user") - def to_input_user_lossy(self) -> abcs.InputUser: - return self.try_to_input_user() or types.InputUser(user_id=0, access_hash=0) + def _to_chat_id(self) -> int: + if self.is_chat(): + return self.id + else: + raise ValueError("chat is not small group") - def try_to_chat_id(self) -> Optional[int]: - return self.id if self.is_chat() else None - - def try_to_input_channel(self) -> Optional[abcs.InputChannel]: - return ( - types.InputChannel(channel_id=self.id, access_hash=self.access_hash or 0) - if self.is_channel() - else None - ) + def _to_input_channel(self) -> types.InputChannel: + if self.is_channel(): + return types.InputChannel( + channel_id=self.id, access_hash=self.access_hash or 0 + ) + else: + raise ValueError("chat is not channel") def __eq__(self, other: object) -> bool: if not isinstance(other, self.__class__):