From a657ae01345378f8ef5b5cb58473f6645af91c2a Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Thu, 6 Apr 2023 14:18:42 +0200 Subject: [PATCH] Save self user ID in session file Should result in one less request after connecting, as there is no longer a need to fetch the self user. --- telethon/_updates/entitycache.py | 2 +- telethon/client/auth.py | 2 +- telethon/client/telegrambaseclient.py | 13 +++++++++++++ telethon/client/users.py | 2 +- 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/telethon/_updates/entitycache.py b/telethon/_updates/entitycache.py index 3a8f49ed..fd56c3e1 100644 --- a/telethon/_updates/entitycache.py +++ b/telethon/_updates/entitycache.py @@ -9,7 +9,7 @@ class EntityCache: self, hash_map: dict = _sentinel, self_id: int = None, - self_bot: bool = False + self_bot: bool = None ): self.hash_map = {} if hash_map is _sentinel else hash_map self.self_id = self_id diff --git a/telethon/client/auth.py b/telethon/client/auth.py index 3cdf998e..520efc56 100644 --- a/telethon/client/auth.py +++ b/telethon/client/auth.py @@ -530,7 +530,7 @@ class AuthMethods: except errors.RPCError: return False - self._mb_entity_cache.set_self_user(None, False, None) + self._mb_entity_cache.set_self_user(None, None, None) self._authorized = False await self.disconnect() diff --git a/telethon/client/telegrambaseclient.py b/telethon/client/telegrambaseclient.py index 4d0bb800..2581a50e 100644 --- a/telethon/client/telegrambaseclient.py +++ b/telethon/client/telegrambaseclient.py @@ -549,6 +549,14 @@ class TelegramBaseClient(abc.ABC): self.session.auth_key = self._sender.auth_key self.session.save() + try: + # See comment when saving entities to understand this hack + self_id = self.session.get_input_entity(0).access_hash + self_user = self.session.get_input_entity(self_id) + self._mb_entity_cache.set_self_user(self_id, None, self_user.access_hash) + except ValueError: + pass + if self._catch_up: ss = SessionState(0, 0, False, 0, 0, 0, 0, None) cs = [] @@ -670,6 +678,11 @@ class TelegramBaseClient(abc.ABC): # It doesn't matter if we put users in the list of chats. self.session.process_entities(types.contacts.ResolvedPeer(None, [e._as_input_peer() for e in entities], [])) + # As a hack to not need to change the session files, save ourselves with ``id=0`` and ``access_hash`` of our ``id``. + # This way it is possible to determine our own ID by querying for 0. However, whether we're a bot is not saved. + if self._mb_entity_cache.self_id: + self.session.process_entities(types.contacts.ResolvedPeer(None, [types.InputPeerUser(0, self._mb_entity_cache.self_id)], [])) + ss, cs = self._message_box.session_state() self.session.set_update_state(0, types.updates.State(**ss, unread_count=0)) now = datetime.datetime.now() # any datetime works; channels don't need it diff --git a/telethon/client/users.py b/telethon/client/users.py index bbb264fb..ab05ece2 100644 --- a/telethon/client/users.py +++ b/telethon/client/users.py @@ -188,7 +188,7 @@ class UserMethods: else: print('Hello') """ - if self._mb_entity_cache.self_id is None: + if self._mb_entity_cache.self_bot is None: await self.get_me(input_peer=True) return self._mb_entity_cache.self_bot