diff --git a/telethon/telegram_client.py b/telethon/telegram_client.py index c508ad52..3b17e4c2 100644 --- a/telethon/telegram_client.py +++ b/telethon/telegram_client.py @@ -325,11 +325,11 @@ class TelegramClient(TelegramBareClient): total_count = getattr(r, 'count', len(r.dialogs)) messages = {m.id: m for m in r.messages} - entities = {utils.get_peer_id(x, add_mark=True): x + entities = {utils.get_peer_id(x): x for x in itertools.chain(r.users, r.chats)} for d in r.dialogs: - dialogs[utils.get_peer_id(d.peer, add_mark=True)] = \ + dialogs[utils.get_peer_id(d.peer)] = \ Dialog(self, d, entities, messages) if len(r.dialogs) < real_limit or not isinstance(r, DialogsSlice): @@ -338,8 +338,7 @@ class TelegramClient(TelegramBareClient): break offset_date = r.messages[-1].date - offset_peer = entities[ - utils.get_peer_id(r.dialogs[-1].peer, add_mark=True)] + offset_peer = entities[utils.get_peer_id(r.dialogs[-1].peer)] offset_id = r.messages[-1].id & 4294967296 # Telegram/danog magic dialogs = UserList(dialogs.values()) @@ -536,9 +535,9 @@ class TelegramClient(TelegramBareClient): # TODO We can potentially use self.session.database, but since # it might be disabled, use a local dictionary. for u in result.users: - entities[utils.get_peer_id(u, add_mark=True)] = u + entities[utils.get_peer_id(u)] = u for c in result.chats: - entities[utils.get_peer_id(c, add_mark=True)] = c + entities[utils.get_peer_id(c)] = c if len(result.messages) < real_limit: break @@ -558,23 +557,21 @@ class TelegramClient(TelegramBareClient): for m in messages: # TODO Better way to return a total without tuples? m.sender = (None if not m.from_id else - entities[utils.get_peer_id(m.from_id, add_mark=True)]) + entities[utils.get_peer_id(m.from_id)]) if getattr(m, 'fwd_from', None): m.fwd_from.sender = ( None if not m.fwd_from.from_id else - entities[utils.get_peer_id( - m.fwd_from.from_id, add_mark=True - )] + entities[utils.get_peer_id(m.fwd_from.from_id)] ) m.fwd_from.channel = ( None if not m.fwd_from.channel_id else entities[utils.get_peer_id( - PeerChannel(m.fwd_from.channel_id), add_mark=True + PeerChannel(m.fwd_from.channel_id) )] ) - m.to = entities[utils.get_peer_id(m.to_id, add_mark=True)] + m.to = entities[utils.get_peer_id(m.to_id)] return messages @@ -1073,7 +1070,7 @@ class TelegramClient(TelegramBareClient): # Merge users, chats and channels into a single dictionary id_entity = { - utils.get_peer_id(x, add_mark=True): x + utils.get_peer_id(x): x for x in itertools.chain(users, chats, channels) } @@ -1083,7 +1080,7 @@ class TelegramClient(TelegramBareClient): # username changes. result = [ self._get_entity_from_string(x) if isinstance(x, str) - else id_entity[utils.get_peer_id(x, add_mark=True)] + else id_entity[utils.get_peer_id(x)] for x in inputs ] return result[0] if single else result @@ -1185,9 +1182,9 @@ class TelegramClient(TelegramBareClient): exclude_pinned=True )) - target = utils.get_peer_id(peer, add_mark=True) + target = utils.get_peer_id(peer) for entity in itertools.chain(dialogs.users, dialogs.chats): - if utils.get_peer_id(entity, add_mark=True) == target: + if utils.get_peer_id(entity) == target: return utils.get_input_peer(entity) raise TypeError( diff --git a/telethon/tl/custom/dialog.py b/telethon/tl/custom/dialog.py index bac8b0de..fd36ba8f 100644 --- a/telethon/tl/custom/dialog.py +++ b/telethon/tl/custom/dialog.py @@ -17,7 +17,7 @@ class Dialog: self.message = messages.get(dialog.top_message, None) self.date = getattr(self.message, 'date', None) - self.entity = entities[utils.get_peer_id(dialog.peer, add_mark=True)] + self.entity = entities[utils.get_peer_id(dialog.peer)] self.input_entity = utils.get_input_peer(self.entity) self.name = utils.get_display_name(self.entity) diff --git a/telethon/tl/session.py b/telethon/tl/session.py index 030b4e13..bb38f489 100644 --- a/telethon/tl/session.py +++ b/telethon/tl/session.py @@ -285,7 +285,7 @@ class Session: continue try: p = utils.get_input_peer(e, allow_self=False) - marked_id = utils.get_peer_id(p, add_mark=True) + marked_id = utils.get_peer_id(p) except ValueError: continue @@ -327,7 +327,7 @@ class Session: key = utils.get_input_peer(key) if type(key).SUBCLASS_OF_ID == 0xc91c90b6: # crc32(b'InputPeer') return key - key = utils.get_peer_id(key, add_mark=True) + key = utils.get_peer_id(key) c = self._conn.cursor() if isinstance(key, str): diff --git a/telethon/utils.py b/telethon/utils.py index 531b0dc7..48c867d1 100644 --- a/telethon/utils.py +++ b/telethon/utils.py @@ -338,9 +338,14 @@ def parse_username(username): return username.lower(), False -def get_peer_id(peer, add_mark=False): - """Finds the ID of the given peer, and optionally converts it to - the "bot api" format if 'add_mark' is set to True. +def get_peer_id(peer): + """ + Finds the ID of the given peer, and converts it to the "bot api" format + so it the peer can be identified back. User ID is left unmodified, + chat ID is negated, and channel ID is prefixed with -100. + + The original ID and the peer type class can be returned with + a call to utils.resolve_id(marked_id). """ # First we assert it's a Peer TLObject, or early return for integers if not isinstance(peer, TLObject): @@ -357,7 +362,7 @@ def get_peer_id(peer, add_mark=False): if isinstance(peer, (PeerUser, InputPeerUser)): return peer.user_id elif isinstance(peer, (PeerChat, InputPeerChat)): - return -peer.chat_id if add_mark else peer.chat_id + return -peer.chat_id elif isinstance(peer, (PeerChannel, InputPeerChannel, ChannelFull)): if isinstance(peer, ChannelFull): # Special case: .get_input_peer can't return InputChannel from @@ -365,12 +370,9 @@ def get_peer_id(peer, add_mark=False): i = peer.id else: i = peer.channel_id - if add_mark: - # Concat -100 through math tricks, .to_supergroup() on Madeline - # IDs will be strictly positive -> log works - return -(i + pow(10, math.floor(math.log10(i) + 3))) - else: - return i + # Concat -100 through math tricks, .to_supergroup() on Madeline + # IDs will be strictly positive -> log works + return -(i + pow(10, math.floor(math.log10(i) + 3))) _raise_cast_fail(peer, 'int')