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.
This commit is contained in:
Lonami Exo 2023-04-06 14:18:42 +02:00
parent 88bc6a46a6
commit a657ae0134
4 changed files with 16 additions and 3 deletions

View File

@ -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

View File

@ -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()

View File

@ -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

View File

@ -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