Remove self input user and bot cache from client

The session_state cache can be used instead.
This does put get_me with input_peer at a disadvantage, but I expect
this is not used all that often, since 'me' does just fine.
This commit is contained in:
Lonami Exo 2021-09-19 18:16:12 +02:00
parent cc3d4145d8
commit 3b1660669e
5 changed files with 6 additions and 47 deletions

View File

@ -377,8 +377,6 @@ async def log_out(self: 'TelegramClient') -> bool:
except errors.RPCError:
return False
self._bot = None
self._self_input_peer = None
self._authorized = False
self._state_cache.reset()

View File

@ -281,10 +281,6 @@ def init(
self._phone = None
self._tos = None
# Sometimes we need to know who we are, cache the self peer
self._self_input_peer = None
self._bot = None
# A place to store if channels are a megagroup or not (see `edit_admin`)
self._megagroup_cache = {}

View File

@ -255,18 +255,6 @@ async def _dispatch_update(self: 'TelegramClient', update, others, channel_id, p
# ValueError("Request was unsuccessful N time(s)") for whatever reasons.
pass
if not self._self_input_peer:
# Some updates require our own ID, so we must make sure
# that the event builder has offline access to it. Calling
# `get_me()` will cache it under `self._self_input_peer`.
#
# It will return `None` if we haven't logged in yet which is
# fine, we will just retry next time anyway.
try:
await self.get_me(input_peer=True)
except OSError:
pass # might not have connection
built = EventBuilderDict(self, update, others)
for builder, callback in self._event_builders:
@ -452,7 +440,7 @@ class EventBuilderDict:
return self.__dict__[builder]
except KeyError:
event = self.__dict__[builder] = builder.build(
self.update, self.others, self.client._self_id)
self.update, self.others, self.client._session_state.user_id)
if isinstance(event, EventCommon):
event.original_update = self.update

View File

@ -135,37 +135,14 @@ async def _call(self: 'TelegramClient', sender, request, ordered=False, flood_sl
async def get_me(self: 'TelegramClient', input_peer: bool = False) \
-> 'typing.Union[_tl.User, _tl.InputPeerUser]':
if input_peer and self._self_input_peer:
return self._self_input_peer
try:
me = (await self(
_tl.fn.users.GetUsers([_tl.InputUserSelf()])))[0]
self._bot = me.bot
if not self._self_input_peer:
self._self_input_peer = utils.get_input_peer(
me, allow_self=False
)
return self._self_input_peer if input_peer else me
me = (await self(_tl.fn.users.GetUsers([_tl.InputUserSelf()])))[0]
return utils.get_input_peer(me, allow_self=False) if input_peer else me
except errors.UnauthorizedError:
return None
def _self_id(self: 'TelegramClient') -> typing.Optional[int]:
"""
Returns the ID of the logged-in user, if known.
This property is used in every update, and some like `updateLoginToken`
occur prior to login, so it gracefully handles when no ID is known yet.
"""
return self._self_input_peer.user_id if self._self_input_peer else None
async def is_bot(self: 'TelegramClient') -> bool:
if self._bot is None:
self._bot = (await self.get_me()).bot
return self._bot
return self._session_state.bot if self._session_state else False
async def is_user_authorized(self: 'TelegramClient') -> bool:
if self._authorized is None:

View File

@ -255,7 +255,7 @@ class Message(ChatGetter, SenderGetter):
# Make messages sent to ourselves outgoing unless they're forwarded.
# This makes it consistent with official client's appearance.
if self.peer_id == _tl.PeerUser(client._self_id) and not self.fwd_from:
if self.peer_id == _tl.PeerUser(client._session_state.user_id) and not self.fwd_from:
self.out = True
cache = client._entity_cache
@ -644,7 +644,7 @@ class Message(ChatGetter, SenderGetter):
# If the client wasn't set we can't emulate the behaviour correctly,
# so as a best-effort simply return the chat peer.
if self._client and not self.out and self.is_private:
return _tl.PeerUser(self._client._self_id)
return _tl.PeerUser(self._client._session_state.user_id)
return self.peer_id