2
0
mirror of https://github.com/pyrogram/pyrogram synced 2025-08-28 21:07:59 +00:00

Refactor Dialog and Dialogs

This commit is contained in:
Dan 2018-12-17 16:13:28 +01:00
parent 8c02a1553d
commit ccf677f3a0
2 changed files with 57 additions and 1 deletions

View File

@ -16,7 +16,9 @@
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
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
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
)

View File

@ -16,6 +16,9 @@
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
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
)