Call .get_dialogs only once on entity not found

This commit is contained in:
Lonami Exo 2018-03-14 21:01:00 +01:00
parent 81944262fb
commit 1ff5826c26

View File

@ -192,6 +192,9 @@ class TelegramClient(TelegramBareClient):
# Sometimes we need to know who we are, cache the self peer # Sometimes we need to know who we are, cache the self peer
self._self_input_peer = None self._self_input_peer = None
# Don't call .get_dialogs() every time a .get_entity() fails
self._called_get_dialogs = False
# endregion # endregion
# region Telegram requests functions # region Telegram requests functions
@ -2401,14 +2404,16 @@ class TelegramClient(TelegramBareClient):
# Add the mark to the peers if the user passed a Peer (not an int), # Add the mark to the peers if the user passed a Peer (not an int),
# or said ID is negative. If it's negative it's been marked already. # or said ID is negative. If it's negative it's been marked already.
# Look in the dialogs with the hope to find it. # Look in the dialogs with the hope to find it.
if not self._called_get_dialogs:
self._called_get_dialogs = True
mark = not isinstance(peer, int) or peer < 0 mark = not isinstance(peer, int) or peer < 0
target_id = utils.get_peer_id(peer) target_id = utils.get_peer_id(peer)
if mark: if mark:
for dialog in self.iter_dialogs(): for dialog in self.get_dialogs(100):
if utils.get_peer_id(dialog.entity) == target_id: if utils.get_peer_id(dialog.entity) == target_id:
return utils.get_input_peer(dialog.entity) return utils.get_input_peer(dialog.entity)
else: else:
for dialog in self.iter_dialogs(): for dialog in self.get_dialogs(100):
if dialog.entity.id == target_id: if dialog.entity.id == target_id:
return utils.get_input_peer(dialog.entity) return utils.get_input_peer(dialog.entity)