diff --git a/pyrogram/client/ext/utils.py b/pyrogram/client/ext/utils.py index f87b9035..6dd98f5d 100644 --- a/pyrogram/client/ext/utils.py +++ b/pyrogram/client/ext/utils.py @@ -917,3 +917,12 @@ def parse_chat_full( parsed_chat.invite_link = full_chat.exported_invite.link return parsed_chat + + +def parse_dialog_chat(peer, users: dict, chats: dict): + if isinstance(peer, types.PeerUser): + return parse_user_chat(users[peer.user_id]) + elif isinstance(peer, types.PeerChat): + return parse_chat_chat(chats[peer.chat_id]) + else: + return parse_channel_chat(chats[peer.channel_id]) diff --git a/pyrogram/client/methods/messages/get_dialogs.py b/pyrogram/client/methods/messages/get_dialogs.py index 28687511..f43ca6a5 100644 --- a/pyrogram/client/methods/messages/get_dialogs.py +++ b/pyrogram/client/methods/messages/get_dialogs.py @@ -25,55 +25,54 @@ class GetDialogs(BaseClient): # TODO docstrings def get_dialogs(self, - last_chunk=None, - limit: int = 100): - offset_date = 0 - offset_id = 0 - offset_peer = types.InputPeerEmpty() + limit: int = 100, + pinned_only: bool = False, + last_chunk=None): + if pinned_only: + r = self.send(functions.messages.GetPinnedDialogs()) + else: + offset_date = 0 - if last_chunk: - for dialog in reversed(last_chunk.dialogs): - top_message = dialog.top_message + if last_chunk: + for dialog in reversed(last_chunk.dialogs): + top_message = dialog.top_message - if top_message: - message_date = top_message.date + if top_message: + message_date = top_message.date - if message_date: - offset_id = top_message.message_id - offset_date = message_date - offset_peer = self.resolve_peer(dialog.id) - break + if message_date: + offset_date = message_date + break - r = self.send( - functions.messages.GetDialogs( - offset_date=offset_date, - offset_id=offset_id, - offset_peer=offset_peer, - limit=limit, - hash=0, - exclude_pinned=True + r = self.send( + functions.messages.GetDialogs( + offset_date=offset_date, + offset_id=0, + offset_peer=types.InputPeerEmpty(), + limit=limit, + hash=0, + exclude_pinned=True + ) ) - ) users = {i.id: i for i in r.users} chats = {i.id: i for i in r.chats} messages = {} for message in r.messages: - if isinstance(message, (types.Message, types.MessageService)): - chat_id = message.to_id + to_id = message.to_id - if isinstance(chat_id, types.PeerUser): - chat_id = chat_id.user_id - elif isinstance(chat_id, types.PeerChat): - chat_id = -chat_id.chat_id + if isinstance(to_id, types.PeerUser): + if message.out: + chat_id = to_id.user_id else: - chat_id = int("-100" + str(chat_id.channel_id)) + chat_id = message.from_id + elif isinstance(to_id, types.PeerChat): + chat_id = -to_id.chat_id + else: + chat_id = int("-100" + str(to_id.channel_id)) - messages[chat_id] = utils.parse_messages( - self, message, - users, chats - ) + messages[chat_id] = utils.parse_messages(self, message, users, chats) dialogs = [] @@ -89,7 +88,7 @@ class GetDialogs(BaseClient): dialogs.append( pyrogram.Dialog( - id=chat_id, + chat=utils.parse_dialog_chat(dialog.peer, users, chats), top_message=messages.get(chat_id), unread_messages_count=dialog.unread_count, unread_mentions_count=dialog.unread_mentions_count, diff --git a/pyrogram/client/types/dialog.py b/pyrogram/client/types/dialog.py index 4210f105..5ad99477 100644 --- a/pyrogram/client/types/dialog.py +++ b/pyrogram/client/types/dialog.py @@ -23,13 +23,13 @@ class Dialog(Object): ID = 0xb0700028 def __init__(self, - id: int, + chat, top_message, unread_messages_count: int, unread_mentions_count: int, unread_mark: bool): # TODO docstrings - self.id = id + self.chat = chat self.top_message = top_message self.unread_messages_count = unread_messages_count self.unread_mentions_count = unread_mentions_count