From 5081910c082cd4c0cb66b7fa977b50b09780e6e7 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Fri, 1 Sep 2023 19:47:35 +0200 Subject: [PATCH] Create Channel and Group types --- .../_impl/client/types/chat/__init__.py | 11 ++++ .../_impl/client/types/chat/channel.py | 50 ++++++++++++++++ .../telethon/_impl/client/types/chat/group.py | 58 +++++++++++++++++++ 3 files changed, 119 insertions(+) create mode 100644 client/src/telethon/_impl/client/types/chat/__init__.py create mode 100644 client/src/telethon/_impl/client/types/chat/channel.py create mode 100644 client/src/telethon/_impl/client/types/chat/group.py diff --git a/client/src/telethon/_impl/client/types/chat/__init__.py b/client/src/telethon/_impl/client/types/chat/__init__.py new file mode 100644 index 00000000..239c3bd6 --- /dev/null +++ b/client/src/telethon/_impl/client/types/chat/__init__.py @@ -0,0 +1,11 @@ +from typing import Union + +from ....session.chat.packed import PackedChat +from .channel import Channel +from .group import Group +from .user import RestrictionReason, User + +Chat = Union[Channel, Group, User] +ChatLike = Union[Chat, PackedChat, int, str] + +__all__ = ["Chat", "Channel", "Group", "RestrictionReason", "User"] diff --git a/client/src/telethon/_impl/client/types/chat/channel.py b/client/src/telethon/_impl/client/types/chat/channel.py new file mode 100644 index 00000000..c258cb5e --- /dev/null +++ b/client/src/telethon/_impl/client/types/chat/channel.py @@ -0,0 +1,50 @@ +from typing import Optional, Self, Union + +from ....session.chat.packed import PackedChat, PackedType +from ....tl import abcs, types +from ..meta import NoPublicConstructor + + +class Channel(metaclass=NoPublicConstructor): + __slots__ = ("_raw",) + + def __init__( + self, + raw: Union[types.Channel, types.ChannelForbidden], + ) -> None: + self._raw = raw + + @classmethod + def _from_raw(cls, chat: abcs.Chat) -> Self: + if isinstance(chat, (types.ChatEmpty, types.Chat, types.ChatForbidden)): + raise RuntimeError("cannot create channel from group chat") + elif isinstance(chat, (types.Channel, types.ChannelForbidden)): + if not chat.broadcast: + raise RuntimeError("cannot create group from broadcast channel") + return cls._create(chat) + else: + raise RuntimeError("unexpected case") + + @property + def id(self) -> int: + return self._raw.id + + def pack(self) -> Optional[PackedChat]: + if self._raw.access_hash is None: + return None + else: + return PackedChat( + ty=PackedType.GIGAGROUP + if getattr(self._raw, "gigagroup", False) + else PackedType.BROADCAST, + id=self._raw.id, + access_hash=None, + ) + + @property + def title(self) -> str: + return getattr(self._raw, "title", None) or "" + + @property + def username(self) -> Optional[str]: + return getattr(self._raw, "username", None) diff --git a/client/src/telethon/_impl/client/types/chat/group.py b/client/src/telethon/_impl/client/types/chat/group.py new file mode 100644 index 00000000..a16e8081 --- /dev/null +++ b/client/src/telethon/_impl/client/types/chat/group.py @@ -0,0 +1,58 @@ +from typing import Optional, Self, Union + +from ....session.chat.packed import PackedChat, PackedType +from ....tl import abcs, types +from ..meta import NoPublicConstructor + + +class Group(metaclass=NoPublicConstructor): + __slots__ = ("_raw",) + + def __init__( + self, + raw: Union[ + types.ChatEmpty, + types.Chat, + types.ChatForbidden, + types.Channel, + types.ChannelForbidden, + ], + ) -> None: + self._raw = raw + + @classmethod + def _from_raw(cls, chat: abcs.Chat) -> Self: + if isinstance(chat, (types.ChatEmpty, types.Chat, types.ChatForbidden)): + return cls._create(chat) + elif isinstance(chat, (types.Channel, types.ChannelForbidden)): + if chat.broadcast: + raise RuntimeError("cannot create group from broadcast channel") + return cls._create(chat) + else: + raise RuntimeError("unexpected case") + + @property + def id(self) -> int: + return self._raw.id + + def pack(self) -> Optional[PackedChat]: + if isinstance(self._raw, (types.ChatEmpty, types.Chat, types.ChatForbidden)): + return PackedChat(ty=PackedType.CHAT, id=self._raw.id, access_hash=None) + elif self._raw.access_hash is None: + return None + else: + return PackedChat( + ty=PackedType.MEGAGROUP, id=self._raw.id, access_hash=None + ) + + @property + def title(self) -> str: + return getattr(self._raw, "title", None) or "" + + @property + def username(self) -> Optional[str]: + return getattr(self._raw, "username", None) + + @property + def is_megagroup(self) -> bool: + return isinstance(self._raw, (types.Channel, types.ChannelForbidden))