From 790b0d2d23a7b827088cc21591f0c7dc71ae6262 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Sat, 24 Mar 2018 18:34:07 +0100 Subject: [PATCH] Guess entity type on positive IDs in events and avoid some RPCs Now specifying a single positive integer ID will add all the types to the white/blacklist so it can be "guessed". Explicit peers will always be only that type, and an RPC is avoided (since it was not needed to begin with). --- telethon/events/__init__.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/telethon/events/__init__.py b/telethon/events/__init__.py index 8b80bfd8..ff8f8d48 100644 --- a/telethon/events/__init__.py +++ b/telethon/events/__init__.py @@ -20,10 +20,24 @@ def _into_id_set(client, chats): result = set() for chat in chats: - chat = client.get_input_entity(chat) - if isinstance(chat, types.InputPeerSelf): - chat = client.get_me(input_peer=True) - result.add(utils.get_peer_id(chat)) + if isinstance(chat, int): + if chat < 0: + result.add(chat) # Explicitly marked IDs are negative + else: + result.update({ # Support all valid types of peers + utils.get_peer_id(types.PeerUser(chat)), + utils.get_peer_id(types.PeerChat(chat)), + utils.get_peer_id(types.PeerChannel(chat)), + }) + elif isinstance(chat, TLObject) and chat.SUBCLASS_OF_ID == 0x2d45687: + # 0x2d45687 == crc32(b'Peer') + result.add(utils.get_peer_id(chat)) + else: + chat = client.get_input_entity(chat) + if isinstance(chat, types.InputPeerSelf): + chat = client.get_me(input_peer=True) + result.add(utils.get_peer_id(chat)) + return result