From 3b1660669e6eecd789b16de27596a33f7bc73518 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Sun, 19 Sep 2021 18:16:12 +0200 Subject: [PATCH] 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. --- telethon/_client/auth.py | 2 -- telethon/_client/telegrambaseclient.py | 4 ---- telethon/_client/updates.py | 14 +------------ telethon/_client/users.py | 29 +++----------------------- telethon/types/_custom/message.py | 4 ++-- 5 files changed, 6 insertions(+), 47 deletions(-) diff --git a/telethon/_client/auth.py b/telethon/_client/auth.py index 365e7257..031b3a11 100644 --- a/telethon/_client/auth.py +++ b/telethon/_client/auth.py @@ -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() diff --git a/telethon/_client/telegrambaseclient.py b/telethon/_client/telegrambaseclient.py index 38ad77ed..8bbf6051 100644 --- a/telethon/_client/telegrambaseclient.py +++ b/telethon/_client/telegrambaseclient.py @@ -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 = {} diff --git a/telethon/_client/updates.py b/telethon/_client/updates.py index 2e7483ac..2a03a0b3 100644 --- a/telethon/_client/updates.py +++ b/telethon/_client/updates.py @@ -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 diff --git a/telethon/_client/users.py b/telethon/_client/users.py index 5f3d7116..b653af94 100644 --- a/telethon/_client/users.py +++ b/telethon/_client/users.py @@ -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: diff --git a/telethon/types/_custom/message.py b/telethon/types/_custom/message.py index 2d8f3fbe..ae0b395e 100644 --- a/telethon/types/_custom/message.py +++ b/telethon/types/_custom/message.py @@ -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