Rename Chat base class to Peer

This commit is contained in:
Lonami Exo 2024-03-17 13:14:44 +01:00
parent 031c128fef
commit c95e979119
18 changed files with 68 additions and 67 deletions

View File

@ -25,7 +25,6 @@ from ..types import (
AdminRight,
AlbumBuilder,
AsyncList,
Chat,
ChatLike,
ChatRestriction,
Dialog,
@ -38,6 +37,7 @@ from ..types import (
OutFileLike,
Participant,
PasswordToken,
Peer,
RecentAction,
User,
)
@ -253,7 +253,7 @@ class Client:
self._chat_hashes = ChatHashCache(None)
self._last_update_limit_warn: Optional[float] = None
self._updates: asyncio.Queue[
tuple[abcs.Update, dict[int, Chat]]
tuple[abcs.Update, dict[int, Peer]]
] = asyncio.Queue(maxsize=self._config.update_queue_limit or 0)
self._dispatcher: Optional[asyncio.Task[None]] = None
self._handlers: dict[
@ -676,11 +676,11 @@ class Client:
async def get_chats(
self, chats: list[ChatLike] | tuple[ChatLike, ...]
) -> list[Chat]:
) -> list[Peer]:
"""
Get the latest basic information about the given chats.
This method is most commonly used to turn one or more :class:`~types.PackedChat` into the original :class:`~types.Chat`.
This method is most commonly used to turn one or more :class:`~types.PackedChat` into the original :class:`~types.Peer`.
This includes users, groups and broadcast channels.
:param chats:
@ -1227,12 +1227,12 @@ class Client:
"""
return await request_login_code(self, phone)
async def resolve_username(self, username: str) -> Chat:
async def resolve_username(self, username: str) -> Peer:
"""
Resolve a username into a :term:`chat`.
This method is rather expensive to call.
It is recommended to use it once and then :meth:`types.Chat.pack` the result.
It is recommended to use it once and then :meth:`types.Peer.pack` the result.
The packed chat can then be used (and re-fetched) more cheaply.
:param username:

View File

@ -6,7 +6,7 @@ from typing import TYPE_CHECKING, Literal, Optional, Self
from ...session import PackedChat
from ...tl import abcs, functions, types
from ..types import AsyncList, Chat, ChatLike, Message, build_chat_map
from ..types import AsyncList, ChatLike, Message, Peer, build_chat_map
from ..types import buttons as btns
from ..types import generate_random_id, parse_message, peer_id
@ -198,7 +198,7 @@ class MessageList(AsyncList[Message]):
def _extend_buffer(
self, client: Client, messages: abcs.messages.Messages
) -> dict[int, Chat]:
) -> dict[int, Peer]:
if isinstance(messages, types.messages.MessagesNotModified):
self._total = messages.count
return {}

View File

@ -8,9 +8,9 @@ from ...tl import abcs, functions, types
from ..types import (
AsyncList,
Channel,
Chat,
ChatLike,
Group,
Peer,
User,
build_chat_map,
expand_peer,
@ -52,7 +52,7 @@ def get_contacts(self: Client) -> AsyncList[User]:
return ContactList(self)
def resolved_peer_to_chat(client: Client, resolved: abcs.contacts.ResolvedPeer) -> Chat:
def resolved_peer_to_chat(client: Client, resolved: abcs.contacts.ResolvedPeer) -> Peer:
assert isinstance(resolved, types.contacts.ResolvedPeer)
map = build_chat_map(client, resolved.users, resolved.chats)
@ -62,13 +62,13 @@ def resolved_peer_to_chat(client: Client, resolved: abcs.contacts.ResolvedPeer)
raise ValueError("no matching chat found in response")
async def resolve_phone(client: Client, phone: str) -> Chat:
async def resolve_phone(client: Client, phone: str) -> Peer:
return resolved_peer_to_chat(
client, await client(functions.contacts.resolve_phone(phone=phone))
)
async def resolve_username(self: Client, username: str) -> Chat:
async def resolve_username(self: Client, username: str) -> Peer:
return resolved_peer_to_chat(
self, await self(functions.contacts.resolve_username(username=username))
)
@ -76,7 +76,7 @@ async def resolve_username(self: Client, username: str) -> Chat:
async def get_chats(
self: Client, chats: list[ChatLike] | tuple[ChatLike, ...]
) -> list[Chat]:
) -> list[Peer]:
packed_chats: list[PackedChat] = []
input_users: list[types.InputUser] = []
input_chats: list[int] = []

View File

@ -4,7 +4,7 @@ import abc
from typing import TYPE_CHECKING, Optional, Self
from ...tl import abcs
from ..types import Chat, NoPublicConstructor
from ..types import NoPublicConstructor, Peer
if TYPE_CHECKING:
from ..client.client import Client
@ -25,7 +25,7 @@ class Event(metaclass=NoPublicConstructor):
@classmethod
@abc.abstractmethod
def _try_from_update(
cls, client: Client, update: abcs.Update, chat_map: dict[int, Chat]
cls, client: Client, update: abcs.Update, chat_map: dict[int, Peer]
) -> Optional[Self]:
pass

View File

@ -3,7 +3,7 @@ from __future__ import annotations
from typing import TYPE_CHECKING, Optional, Self, Sequence
from ...tl import abcs, types
from ..types import Chat, Message, expand_peer, peer_id
from ..types import Message, Peer, expand_peer, peer_id
from .event import Event
if TYPE_CHECKING:
@ -25,7 +25,7 @@ class NewMessage(Event, Message):
@classmethod
def _try_from_update(
cls, client: Client, update: abcs.Update, chat_map: dict[int, Chat]
cls, client: Client, update: abcs.Update, chat_map: dict[int, Peer]
) -> Optional[Self]:
if isinstance(update, (types.UpdateNewMessage, types.UpdateNewChannelMessage)):
if isinstance(update.message, types.Message):
@ -47,7 +47,7 @@ class MessageEdited(Event, Message):
@classmethod
def _try_from_update(
cls, client: Client, update: abcs.Update, chat_map: dict[int, Chat]
cls, client: Client, update: abcs.Update, chat_map: dict[int, Peer]
) -> Optional[Self]:
if isinstance(
update, (types.UpdateEditMessage, types.UpdateEditChannelMessage)
@ -75,7 +75,7 @@ class MessageDeleted(Event):
@classmethod
def _try_from_update(
cls, client: Client, update: abcs.Update, chat_map: dict[int, Chat]
cls, client: Client, update: abcs.Update, chat_map: dict[int, Peer]
) -> Optional[Self]:
if isinstance(update, types.UpdateDeleteMessages):
return cls._create(update.messages, None)
@ -115,7 +115,7 @@ class MessageRead(Event):
| types.UpdateReadChannelInbox
| types.UpdateReadChannelOutbox
),
chat_map: dict[int, Chat],
chat_map: dict[int, Peer],
) -> None:
self._client = client
self._raw = update
@ -123,7 +123,7 @@ class MessageRead(Event):
@classmethod
def _try_from_update(
cls, client: Client, update: abcs.Update, chat_map: dict[int, Chat]
cls, client: Client, update: abcs.Update, chat_map: dict[int, Peer]
) -> Optional[Self]:
if isinstance(
update,
@ -147,7 +147,7 @@ class MessageRead(Event):
return types.PeerChannel(channel_id=self._raw.channel_id)
@property
def chat(self) -> Chat:
def chat(self) -> Peer:
"""
The :term:`chat` when the messages were read.
"""

View File

@ -4,7 +4,7 @@ from typing import TYPE_CHECKING, Optional, Self
from ...tl import abcs, functions, types
from ..client.messages import CherryPickedList
from ..types import Chat, Message
from ..types import Message, Peer
from ..types.peer import peer_id
from .event import Event
@ -23,7 +23,7 @@ class ButtonCallback(Event):
self,
client: Client,
update: types.UpdateBotCallbackQuery,
chat_map: dict[int, Chat],
chat_map: dict[int, Peer],
):
self._client = client
self._raw = update
@ -31,7 +31,7 @@ class ButtonCallback(Event):
@classmethod
def _try_from_update(
cls, client: Client, update: abcs.Update, chat_map: dict[int, Chat]
cls, client: Client, update: abcs.Update, chat_map: dict[int, Peer]
) -> Optional[Self]:
if isinstance(update, types.UpdateBotCallbackQuery) and update.data is not None:
return cls._create(client, update, chat_map)
@ -105,7 +105,7 @@ class InlineQuery(Event):
@classmethod
def _try_from_update(
cls, client: Client, update: abcs.Update, chat_map: dict[int, Chat]
cls, client: Client, update: abcs.Update, chat_map: dict[int, Peer]
) -> Optional[Self]:
if isinstance(update, types.UpdateBotInlineQuery):
return cls._create(update)

View File

@ -27,9 +27,9 @@ from .participant import Participant
from .password_token import PasswordToken
from .peer import (
Channel,
Chat,
ChatLike,
Group,
Peer,
User,
build_chat_map,
expand_peer,
@ -44,7 +44,7 @@ __all__ = [
"ChatRestriction",
"CallbackAnswer",
"Channel",
"Chat",
"Peer",
"ChatLike",
"Group",
"User",

View File

@ -6,7 +6,7 @@ from ...tl import abcs, types
from .draft import Draft
from .message import Message
from .meta import NoPublicConstructor
from .peer import Chat, peer_id
from .peer import Peer, peer_id
if TYPE_CHECKING:
from ..client.client import Client
@ -27,7 +27,7 @@ class Dialog(metaclass=NoPublicConstructor):
self,
client: Client,
raw: types.Dialog | types.DialogFolder,
chat_map: dict[int, Chat],
chat_map: dict[int, Peer],
msg_map: dict[int, Message],
) -> None:
self._client = client
@ -40,14 +40,14 @@ class Dialog(metaclass=NoPublicConstructor):
cls,
client: Client,
dialog: abcs.Dialog,
chat_map: dict[int, Chat],
chat_map: dict[int, Peer],
msg_map: dict[int, Message],
) -> Self:
assert isinstance(dialog, (types.Dialog, types.DialogFolder))
return cls._create(client, dialog, chat_map, msg_map)
@property
def chat(self) -> Chat:
def chat(self) -> Peer:
"""
The chat where messages are sent in this dialog.
"""

View File

@ -8,7 +8,7 @@ from ...tl import abcs, functions, types
from ..parsers import generate_html_message, generate_markdown_message
from .message import Message, generate_random_id
from .meta import NoPublicConstructor
from .peer import Chat, expand_peer, peer_id
from .peer import Peer, expand_peer, peer_id
if TYPE_CHECKING:
from ..client.client import Client
@ -27,7 +27,7 @@ class Draft(metaclass=NoPublicConstructor):
peer: abcs.Peer,
top_msg_id: Optional[int],
raw: abcs.DraftMessage,
chat_map: dict[int, Chat],
chat_map: dict[int, Peer],
) -> None:
assert isinstance(raw, (types.DraftMessage, types.DraftMessageEmpty))
self._client = client
@ -38,7 +38,7 @@ class Draft(metaclass=NoPublicConstructor):
@classmethod
def _from_raw_update(
cls, client: Client, draft: types.UpdateDraftMessage, chat_map: dict[int, Chat]
cls, client: Client, draft: types.UpdateDraftMessage, chat_map: dict[int, Peer]
) -> Self:
return cls._create(client, draft.peer, draft.top_msg_id, draft.draft, chat_map)
@ -49,12 +49,12 @@ class Draft(metaclass=NoPublicConstructor):
peer: abcs.Peer,
top_msg_id: int,
draft: abcs.DraftMessage,
chat_map: dict[int, Chat],
chat_map: dict[int, Peer],
) -> Self:
return cls._create(client, peer, top_msg_id, draft, chat_map)
@property
def chat(self) -> Chat:
def chat(self) -> Peer:
"""
The chat where the draft is saved.

View File

@ -14,7 +14,7 @@ from ..parsers import (
from .buttons import Button, as_concrete_row, create_button
from .file import File
from .meta import NoPublicConstructor
from .peer import Chat, ChatLike, expand_peer, peer_id
from .peer import ChatLike, Peer, expand_peer, peer_id
if TYPE_CHECKING:
from ..client.client import Client
@ -58,7 +58,7 @@ class Message(metaclass=NoPublicConstructor):
"""
def __init__(
self, client: Client, message: abcs.Message, chat_map: dict[int, Chat]
self, client: Client, message: abcs.Message, chat_map: dict[int, Peer]
) -> None:
assert isinstance(
message, (types.Message, types.MessageService, types.MessageEmpty)
@ -69,7 +69,7 @@ class Message(metaclass=NoPublicConstructor):
@classmethod
def _from_raw(
cls, client: Client, message: abcs.Message, chat_map: dict[int, Chat]
cls, client: Client, message: abcs.Message, chat_map: dict[int, Peer]
) -> Self:
return cls._create(client, message, chat_map)
@ -77,7 +77,7 @@ class Message(metaclass=NoPublicConstructor):
def _from_defaults(
cls,
client: Client,
chat_map: dict[int, Chat],
chat_map: dict[int, Peer],
id: int,
peer_id: abcs.Peer,
date: int,
@ -184,7 +184,7 @@ class Message(metaclass=NoPublicConstructor):
return adapt_date(getattr(self._raw, "date", None))
@property
def chat(self) -> Chat:
def chat(self) -> Peer:
"""
The :term:`chat` when the message was sent.
"""
@ -197,7 +197,7 @@ class Message(metaclass=NoPublicConstructor):
return self._chat_map[pid]
@property
def sender(self) -> Optional[Chat]:
def sender(self) -> Optional[Peer]:
"""
The :term:`chat` that sent the message.
@ -502,7 +502,7 @@ class Message(metaclass=NoPublicConstructor):
def build_msg_map(
client: Client, messages: Sequence[abcs.Message], chat_map: dict[int, Chat]
client: Client, messages: Sequence[abcs.Message], chat_map: dict[int, Peer]
) -> dict[int, Message]:
return {
msg.id: msg

View File

@ -8,7 +8,7 @@ from ...tl import abcs, types
from .admin_right import AdminRight
from .chat_restriction import ChatRestriction
from .meta import NoPublicConstructor
from .peer import Chat, User, peer_id
from .peer import Peer, User, peer_id
if TYPE_CHECKING:
from ..client.client import Client
@ -36,7 +36,7 @@ class Participant(metaclass=NoPublicConstructor):
| types.ChatParticipantCreator
| types.ChatParticipantAdmin
),
chat_map: dict[int, Chat],
chat_map: dict[int, Peer],
) -> None:
self._client = client
self._chat = chat
@ -49,7 +49,7 @@ class Participant(metaclass=NoPublicConstructor):
client: Client,
chat: PackedChat,
participant: abcs.ChannelParticipant,
chat_map: dict[int, Chat],
chat_map: dict[int, Peer],
) -> Self:
if isinstance(
participant,
@ -72,7 +72,7 @@ class Participant(metaclass=NoPublicConstructor):
client: Client,
chat: PackedChat,
participant: abcs.ChatParticipant,
chat_map: dict[int, Chat],
chat_map: dict[int, Peer],
) -> Self:
if isinstance(
participant,
@ -129,7 +129,7 @@ class Participant(metaclass=NoPublicConstructor):
return None
@property
def banned(self) -> Optional[Chat]:
def banned(self) -> Optional[Peer]:
"""
The banned participant.
@ -141,7 +141,7 @@ class Participant(metaclass=NoPublicConstructor):
return None
@property
def left(self) -> Optional[Chat]:
def left(self) -> Optional[Peer]:
"""
The participant that has left the group.

View File

@ -9,18 +9,18 @@ from ....session import PackedChat
from ....tl import abcs, types
from .channel import Channel
from .group import Group
from .peer import Chat
from .peer import Peer
from .user import User
if TYPE_CHECKING:
from ...client.client import Client
ChatLike = Chat | PackedChat | int | str
ChatLike = Peer | PackedChat | int | str
def build_chat_map(
client: Client, users: Sequence[abcs.User], chats: Sequence[abcs.Chat]
) -> dict[int, Chat]:
) -> dict[int, Peer]:
users_iter = (User._from_raw(u) for u in users)
chats_iter = (
(
@ -31,7 +31,7 @@ def build_chat_map(
for c in chats
)
result: dict[int, Chat] = {c.id: c for c in itertools.chain(users_iter, chats_iter)}
result: dict[int, Peer] = {c.id: c for c in itertools.chain(users_iter, chats_iter)}
if len(result) != len(users) + len(chats):
# The fabled ID collision between different chat types.
@ -66,7 +66,7 @@ def peer_id(peer: abcs.Peer) -> int:
raise RuntimeError("unexpected case")
def expand_peer(client: Client, peer: abcs.Peer, *, broadcast: Optional[bool]) -> Chat:
def expand_peer(client: Client, peer: abcs.Peer, *, broadcast: Optional[bool]) -> Peer:
if isinstance(peer, types.PeerUser):
return User._from_raw(types.UserEmpty(id=peer.user_id))
elif isinstance(peer, types.PeerChat):
@ -93,4 +93,4 @@ def expand_peer(client: Client, peer: abcs.Peer, *, broadcast: Optional[bool]) -
raise RuntimeError("unexpected case")
__all__ = ["Channel", "Chat", "Group", "User"]
__all__ = ["Channel", "Peer", "Group", "User"]

View File

@ -3,10 +3,10 @@ from typing import Optional, Self
from ....session import PackedChat, PackedType
from ....tl import abcs, types
from ..meta import NoPublicConstructor
from .peer import Chat
from .peer import Peer
class Channel(Chat, metaclass=NoPublicConstructor):
class Channel(Peer, metaclass=NoPublicConstructor):
"""
A broadcast channel.

View File

@ -7,13 +7,13 @@ from ....session import PackedChat, PackedType
from ....tl import abcs, types
from ..chat_restriction import ChatRestriction
from ..meta import NoPublicConstructor
from .peer import Chat
from .peer import Peer
if TYPE_CHECKING:
from ...client.client import Client
class Group(Chat, metaclass=NoPublicConstructor):
class Group(Peer, metaclass=NoPublicConstructor):
"""
A small group or supergroup.

View File

@ -4,7 +4,7 @@ from typing import Optional
from ....session import PackedChat
class Chat(abc.ABC):
class Peer(abc.ABC):
"""
The base class for all chat types.

View File

@ -3,10 +3,10 @@ from typing import Optional, Self
from ....session import PackedChat, PackedType
from ....tl import abcs, types
from ..meta import NoPublicConstructor
from .peer import Chat
from .peer import Peer
class User(Chat, metaclass=NoPublicConstructor):
class User(Peer, metaclass=NoPublicConstructor):
"""
A user, representing either a bot account or an account created with a phone number.

View File

@ -1,6 +1,6 @@
from ...tl import abcs, types
from .meta import NoPublicConstructor
from .peer import Chat
from .peer import Peer
class RecentAction(metaclass=NoPublicConstructor):
@ -15,7 +15,7 @@ class RecentAction(metaclass=NoPublicConstructor):
def __init__(
self,
event: abcs.ChannelAdminLogEvent,
chat_map: dict[int, Chat],
chat_map: dict[int, Peer],
) -> None:
assert isinstance(event, types.ChannelAdminLogEvent)
self._raw = event

View File

@ -1,13 +1,13 @@
"""
Classes for the various objects the library returns.
"""
from .._impl.client.types import (
AdminRight,
AlbumBuilder,
AsyncList,
CallbackAnswer,
Channel,
Chat,
ChatRestriction,
Dialog,
Draft,
@ -18,6 +18,7 @@ from .._impl.client.types import (
Message,
Participant,
PasswordToken,
Peer,
RecentAction,
User,
)
@ -30,7 +31,7 @@ __all__ = [
"AsyncList",
"CallbackAnswer",
"Channel",
"Chat",
"Peer",
"ChatRestriction",
"Dialog",
"Draft",