Skip to content

Commit

Permalink
Fix .get_dialogs to never return more than limit
Browse files Browse the repository at this point in the history
  • Loading branch information
Lonami committed Oct 5, 2017
1 parent 427a6aa commit 7aa21db
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions telethon/telegram_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,11 +256,12 @@ def get_dialogs(self,
messages = {} # Used later for sorting TODO also return these?
entities = {}
while len(dialogs) < limit:
need = limit - len(dialogs)
r = self(GetDialogsRequest(
offset_date=offset_date,
offset_id=offset_id,
offset_peer=offset_peer,
limit=0 # limit 0 often means "as much as possible"
limit=need if need < float('inf') else 0
))
if not r.dialogs:
break
Expand Down Expand Up @@ -290,10 +291,12 @@ def get_dialogs(self,
# so we need to set at least one day ahead while still being
# the smallest date possible.
no_date = datetime.fromtimestamp(86400)
ds = sorted(
list(dialogs.values()),
ds = list(sorted(
dialogs.values(),
key=lambda d: getattr(messages[d.top_message], 'date', no_date)
)
))
if limit < float('inf'):
ds = ds[:limit]
return (
ds,
[utils.find_user_or_chat(d.peer, entities, entities) for d in ds]
Expand Down

0 comments on commit 7aa21db

Please sign in to comment.