From ccf677f3a026ec2071560e3893ea65c9769976f5 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Mon, 17 Dec 2018 16:13:28 +0100 Subject: [PATCH] Refactor Dialog and Dialogs --- .../client/types/user_and_chats/dialog.py | 26 ++++++++++++++- .../client/types/user_and_chats/dialogs.py | 32 +++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/pyrogram/client/types/user_and_chats/dialog.py b/pyrogram/client/types/user_and_chats/dialog.py index 3d4d0faf..0fc17fe0 100644 --- a/pyrogram/client/types/user_and_chats/dialog.py +++ b/pyrogram/client/types/user_and_chats/dialog.py @@ -16,7 +16,9 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from pyrogram.api import types from ..pyrogram_type import PyrogramType +from ..user_and_chats import Chat class Dialog(PyrogramType): @@ -51,4 +53,26 @@ class Dialog(PyrogramType): self.unread_messages_count = unread_messages_count self.unread_mentions_count = unread_mentions_count self.unread_mark = unread_mark - self.is_pinned = is_pinned \ No newline at end of file + self.is_pinned = is_pinned + + @staticmethod + def parse(client, dialog, messages, users, chats) -> "Dialog": + chat_id = dialog.peer + + if isinstance(chat_id, types.PeerUser): + chat_id = chat_id.user_id + elif isinstance(chat_id, types.PeerChat): + chat_id = -chat_id.chat_id + else: + chat_id = int("-100" + str(chat_id.channel_id)) + + return Dialog( + chat=Chat.parse_dialog(client, dialog.peer, users, chats), + top_message=messages.get(chat_id), + unread_messages_count=dialog.unread_count, + unread_mentions_count=dialog.unread_mentions_count, + unread_mark=dialog.unread_mark, + is_pinned=dialog.pinned, + client=client, + raw=dialog + ) diff --git a/pyrogram/client/types/user_and_chats/dialogs.py b/pyrogram/client/types/user_and_chats/dialogs.py index 030018e4..8261e97c 100644 --- a/pyrogram/client/types/user_and_chats/dialogs.py +++ b/pyrogram/client/types/user_and_chats/dialogs.py @@ -16,6 +16,9 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from pyrogram.api import types +from .dialog import Dialog +from ..messages_and_media import Message from ..pyrogram_type import PyrogramType @@ -35,3 +38,32 @@ class Dialogs(PyrogramType): self.total_count = total_count self.dialogs = dialogs + + @staticmethod + def parse(client, dialogs) -> "Dialogs": + users = {i.id: i for i in dialogs.users} + chats = {i.id: i for i in dialogs.chats} + + messages = {} + + for message in dialogs.messages: + to_id = message.to_id + + if isinstance(to_id, types.PeerUser): + if message.out: + chat_id = to_id.user_id + else: + 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] = Message.parse(client, message, users, chats) + + return Dialogs( + total_count=getattr(dialogs, "count", len(dialogs.dialogs)), + dialogs=[Dialog.parse(client, dialog, messages, users, chats) for dialog in dialogs.dialogs], + client=client, + raw=dialogs + )