Create Channel and Group types

This commit is contained in:
Lonami Exo 2023-09-01 19:47:35 +02:00
parent da011e4b1d
commit 5081910c08
3 changed files with 119 additions and 0 deletions

View File

@ -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"]

View File

@ -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)

View File

@ -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))