mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-02-22 06:25:11 +03:00
Add some ways to convert input chats
This commit is contained in:
parent
5081910c08
commit
d2fc9b6cfc
|
@ -5,9 +5,11 @@ from typing import Deque, Optional, Self, Type, TypeVar, Union
|
||||||
|
|
||||||
from ...mtsender.sender import Sender
|
from ...mtsender.sender import Sender
|
||||||
from ...session.chat.hash_cache import ChatHashCache
|
from ...session.chat.hash_cache import ChatHashCache
|
||||||
|
from ...session.chat.packed import PackedChat
|
||||||
from ...session.message_box.messagebox import MessageBox
|
from ...session.message_box.messagebox import MessageBox
|
||||||
from ...tl import abcs
|
from ...tl import abcs
|
||||||
from ...tl.core.request import Request
|
from ...tl.core.request import Request
|
||||||
|
from ..types.chat import ChatLike
|
||||||
from ..types.chat.user import User
|
from ..types.chat.user import User
|
||||||
from ..types.login_token import LoginToken
|
from ..types.login_token import LoginToken
|
||||||
from ..types.password_token import PasswordToken
|
from ..types.password_token import PasswordToken
|
||||||
|
@ -64,7 +66,14 @@ from .updates import (
|
||||||
set_receive_updates,
|
set_receive_updates,
|
||||||
)
|
)
|
||||||
from .uploads import send_file, upload_file
|
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")
|
Return = TypeVar("Return")
|
||||||
|
|
||||||
|
@ -229,6 +238,12 @@ class Client:
|
||||||
async def get_input_entity(self) -> None:
|
async def get_input_entity(self) -> None:
|
||||||
await get_input_entity(self)
|
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:
|
async def get_peer_id(self) -> None:
|
||||||
await get_peer_id(self)
|
await get_peer_id(self)
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,10 @@
|
||||||
from __future__ import annotations
|
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:
|
if TYPE_CHECKING:
|
||||||
from .client import Client
|
from .client import Client
|
||||||
|
@ -34,3 +38,37 @@ async def get_input_entity(self: Client) -> None:
|
||||||
async def get_peer_id(self: Client) -> None:
|
async def get_peer_id(self: Client) -> None:
|
||||||
self
|
self
|
||||||
raise NotImplementedError
|
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")
|
||||||
|
|
|
@ -51,7 +51,7 @@ class PackedChat:
|
||||||
PackedType.GIGAGROUP,
|
PackedType.GIGAGROUP,
|
||||||
)
|
)
|
||||||
|
|
||||||
def to_peer(self) -> abcs.Peer:
|
def _to_peer(self) -> abcs.Peer:
|
||||||
if self.is_user():
|
if self.is_user():
|
||||||
return types.PeerUser(user_id=self.id)
|
return types.PeerUser(user_id=self.id)
|
||||||
elif self.is_chat():
|
elif self.is_chat():
|
||||||
|
@ -61,7 +61,7 @@ class PackedChat:
|
||||||
else:
|
else:
|
||||||
raise RuntimeError("unexpected case")
|
raise RuntimeError("unexpected case")
|
||||||
|
|
||||||
def to_input_peer(self) -> abcs.InputPeer:
|
def _to_input_peer(self) -> abcs.InputPeer:
|
||||||
if self.is_user():
|
if self.is_user():
|
||||||
return types.InputPeerUser(
|
return types.InputPeerUser(
|
||||||
user_id=self.id, access_hash=self.access_hash or 0
|
user_id=self.id, access_hash=self.access_hash or 0
|
||||||
|
@ -75,24 +75,25 @@ class PackedChat:
|
||||||
else:
|
else:
|
||||||
raise RuntimeError("unexpected case")
|
raise RuntimeError("unexpected case")
|
||||||
|
|
||||||
def try_to_input_user(self) -> Optional[abcs.InputUser]:
|
def _to_input_user(self) -> types.InputUser:
|
||||||
if self.is_user():
|
if self.is_user():
|
||||||
return types.InputUser(user_id=self.id, access_hash=self.access_hash or 0)
|
return types.InputUser(user_id=self.id, access_hash=self.access_hash or 0)
|
||||||
else:
|
else:
|
||||||
return None
|
raise ValueError("chat is not user")
|
||||||
|
|
||||||
def to_input_user_lossy(self) -> abcs.InputUser:
|
def _to_chat_id(self) -> int:
|
||||||
return self.try_to_input_user() or types.InputUser(user_id=0, access_hash=0)
|
if self.is_chat():
|
||||||
|
return self.id
|
||||||
|
else:
|
||||||
|
raise ValueError("chat is not small group")
|
||||||
|
|
||||||
def try_to_chat_id(self) -> Optional[int]:
|
def _to_input_channel(self) -> types.InputChannel:
|
||||||
return self.id if self.is_chat() else None
|
if self.is_channel():
|
||||||
|
return types.InputChannel(
|
||||||
def try_to_input_channel(self) -> Optional[abcs.InputChannel]:
|
channel_id=self.id, access_hash=self.access_hash or 0
|
||||||
return (
|
|
||||||
types.InputChannel(channel_id=self.id, access_hash=self.access_hash or 0)
|
|
||||||
if self.is_channel()
|
|
||||||
else None
|
|
||||||
)
|
)
|
||||||
|
else:
|
||||||
|
raise ValueError("chat is not channel")
|
||||||
|
|
||||||
def __eq__(self, other: object) -> bool:
|
def __eq__(self, other: object) -> bool:
|
||||||
if not isinstance(other, self.__class__):
|
if not isinstance(other, self.__class__):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user