Make getting messages more usable

This commit is contained in:
Lonami Exo 2023-09-02 01:29:35 +02:00
parent 0dfa232070
commit 2cabf6aa25
5 changed files with 56 additions and 14 deletions

View File

@ -254,8 +254,8 @@ class Client:
chat: ChatLike,
limit: Optional[int] = None,
*,
offset_id: Optional[int],
offset_date: Optional[datetime.datetime],
offset_id: Optional[int] = None,
offset_date: Optional[datetime.datetime] = None,
) -> AsyncList[Message]:
return get_messages(
self, chat, limit, offset_id=offset_id, offset_date=offset_date
@ -274,8 +274,8 @@ class Client:
limit: Optional[int] = None,
*,
query: Optional[str] = None,
offset_id: int,
offset_date: datetime.datetime,
offset_id: Optional[int] = None,
offset_date: Optional[datetime.datetime] = None,
) -> AsyncList[Message]:
return search_messages(
self, chat, limit, query=query, offset_id=offset_id, offset_date=offset_date
@ -286,8 +286,8 @@ class Client:
limit: Optional[int] = None,
*,
query: Optional[str] = None,
offset_id: int,
offset_date: datetime.datetime,
offset_id: Optional[int] = None,
offset_date: Optional[datetime.datetime] = None,
) -> AsyncList[Message]:
return search_all_messages(
self, limit, query=query, offset_id=offset_id, offset_date=offset_date

View File

@ -239,8 +239,8 @@ def get_messages(
chat: ChatLike,
limit: Optional[int] = None,
*,
offset_id: Optional[int],
offset_date: Optional[datetime.datetime],
offset_id: Optional[int] = None,
offset_date: Optional[datetime.datetime] = None,
) -> AsyncList[Message]:
return HistoryList(
self,
@ -355,8 +355,8 @@ def search_messages(
limit: Optional[int] = None,
*,
query: Optional[str] = None,
offset_id: int,
offset_date: datetime.datetime,
offset_id: Optional[int] = None,
offset_date: Optional[datetime.datetime] = None,
) -> AsyncList[Message]:
return SearchList(
self,
@ -423,8 +423,8 @@ def search_all_messages(
limit: Optional[int] = None,
*,
query: Optional[str] = None,
offset_id: int,
offset_date: datetime.datetime,
offset_id: Optional[int] = None,
offset_date: Optional[datetime.datetime] = None,
) -> AsyncList[Message]:
return GlobalSearchList(
self,

View File

@ -2,7 +2,7 @@ from __future__ import annotations
from typing import TYPE_CHECKING, Optional
from ...session.chat.packed import PackedChat
from ...session.chat.packed import PackedChat, PackedType
from ...tl import abcs, types
from ..types.chat import Channel, ChatLike, Group, User
@ -46,6 +46,43 @@ async def resolve_to_packed(self: Client, chat: ChatLike) -> PackedChat:
if packed is None:
raise ValueError("Cannot resolve chat")
return packed
if isinstance(chat, abcs.InputPeer):
if isinstance(chat, types.InputPeerEmpty):
raise ValueError("Cannot resolve chat")
elif isinstance(chat, types.InputPeerSelf):
if not self._config.session.user:
raise ValueError("Cannot resolve chat")
return PackedChat(
ty=PackedType.BOT if self._config.session.user.bot else PackedType.USER,
id=self._config.session.user.id,
access_hash=0,
)
elif isinstance(chat, types.InputPeerChat):
return PackedChat(
ty=PackedType.CHAT,
id=chat.chat_id,
access_hash=None,
)
elif isinstance(chat, types.InputPeerUser):
return PackedChat(
ty=PackedType.USER,
id=chat.user_id,
access_hash=chat.access_hash,
)
elif isinstance(chat, types.InputPeerChannel):
return PackedChat(
ty=PackedType.BROADCAST,
id=chat.channel_id,
access_hash=chat.access_hash,
)
elif isinstance(chat, types.InputPeerUserFromMessage):
raise ValueError("Cannot resolve chat")
elif isinstance(chat, types.InputPeerChannelFromMessage):
raise ValueError("Cannot resolve chat")
else:
raise RuntimeError("unexpected case")
raise ValueError("Cannot resolve chat")

View File

@ -1,11 +1,12 @@
from typing import Union
from ....session.chat.packed import PackedChat
from ....tl import abcs
from .channel import Channel
from .group import Group
from .user import RestrictionReason, User
Chat = Union[Channel, Group, User]
ChatLike = Union[Chat, PackedChat, int, str]
ChatLike = Union[Chat, PackedChat, int, str, abcs.InputPeer]
__all__ = ["Chat", "Channel", "Group", "RestrictionReason", "User"]

View File

@ -23,6 +23,10 @@ class Message(metaclass=NoPublicConstructor):
def id(self) -> int:
return self._raw.id
@property
def text(self) -> Optional[str]:
return getattr(self._raw, "message", None)
@property
def date(self) -> Optional[datetime.datetime]:
date = getattr(self._raw, "date", None)