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

Update get_dialogs

This commit is contained in:
Dan 2018-07-05 14:57:45 +02:00
parent 8b43ad8a63
commit 2b6c30d0e1
3 changed files with 46 additions and 38 deletions

View File

@ -917,3 +917,12 @@ def parse_chat_full(
parsed_chat.invite_link = full_chat.exported_invite.link parsed_chat.invite_link = full_chat.exported_invite.link
return parsed_chat 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])

View File

@ -25,55 +25,54 @@ class GetDialogs(BaseClient):
# TODO docstrings # TODO docstrings
def get_dialogs(self, def get_dialogs(self,
last_chunk=None, limit: int = 100,
limit: int = 100): pinned_only: bool = False,
offset_date = 0 last_chunk=None):
offset_id = 0 if pinned_only:
offset_peer = types.InputPeerEmpty() r = self.send(functions.messages.GetPinnedDialogs())
else:
offset_date = 0
if last_chunk: if last_chunk:
for dialog in reversed(last_chunk.dialogs): for dialog in reversed(last_chunk.dialogs):
top_message = dialog.top_message top_message = dialog.top_message
if top_message: if top_message:
message_date = top_message.date message_date = top_message.date
if message_date: if message_date:
offset_id = top_message.message_id offset_date = message_date
offset_date = message_date break
offset_peer = self.resolve_peer(dialog.id)
break
r = self.send( r = self.send(
functions.messages.GetDialogs( functions.messages.GetDialogs(
offset_date=offset_date, offset_date=offset_date,
offset_id=offset_id, offset_id=0,
offset_peer=offset_peer, offset_peer=types.InputPeerEmpty(),
limit=limit, limit=limit,
hash=0, hash=0,
exclude_pinned=True exclude_pinned=True
)
) )
)
users = {i.id: i for i in r.users} users = {i.id: i for i in r.users}
chats = {i.id: i for i in r.chats} chats = {i.id: i for i in r.chats}
messages = {} messages = {}
for message in r.messages: for message in r.messages:
if isinstance(message, (types.Message, types.MessageService)): to_id = message.to_id
chat_id = message.to_id
if isinstance(chat_id, types.PeerUser): if isinstance(to_id, types.PeerUser):
chat_id = chat_id.user_id if message.out:
elif isinstance(chat_id, types.PeerChat): chat_id = to_id.user_id
chat_id = -chat_id.chat_id
else: 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( messages[chat_id] = utils.parse_messages(self, message, users, chats)
self, message,
users, chats
)
dialogs = [] dialogs = []
@ -89,7 +88,7 @@ class GetDialogs(BaseClient):
dialogs.append( dialogs.append(
pyrogram.Dialog( pyrogram.Dialog(
id=chat_id, chat=utils.parse_dialog_chat(dialog.peer, users, chats),
top_message=messages.get(chat_id), top_message=messages.get(chat_id),
unread_messages_count=dialog.unread_count, unread_messages_count=dialog.unread_count,
unread_mentions_count=dialog.unread_mentions_count, unread_mentions_count=dialog.unread_mentions_count,

View File

@ -23,13 +23,13 @@ class Dialog(Object):
ID = 0xb0700028 ID = 0xb0700028
def __init__(self, def __init__(self,
id: int, chat,
top_message, top_message,
unread_messages_count: int, unread_messages_count: int,
unread_mentions_count: int, unread_mentions_count: int,
unread_mark: bool): unread_mark: bool):
# TODO docstrings # TODO docstrings
self.id = id self.chat = chat
self.top_message = top_message self.top_message = top_message
self.unread_messages_count = unread_messages_count self.unread_messages_count = unread_messages_count
self.unread_mentions_count = unread_mentions_count self.unread_mentions_count = unread_mentions_count