diff --git a/telethon/client/updates.py b/telethon/client/updates.py index ea129cf4..6b35f4c8 100644 --- a/telethon/client/updates.py +++ b/telethon/client/updates.py @@ -366,7 +366,7 @@ class EventBuilderDict: self.client._log[__name__].debug('Getting difference for entities') result = await self.client(functions.updates.GetDifferenceRequest( - pts, date, 0 + pts - 1, date, 0 )) if isinstance(result, (types.updates.Difference, diff --git a/telethon/events/callbackquery.py b/telethon/events/callbackquery.py index 975800e3..1c7c8764 100644 --- a/telethon/events/callbackquery.py +++ b/telethon/events/callbackquery.py @@ -101,6 +101,10 @@ class CallbackQuery(EventBuilder): self._message = None self._answered = False + def _load_entities(self): + self._sender, self._input_sender = self._get_entity_pair(self.sender_id) + return super()._load_entities() and self._input_sender is not None + @property def id(self): """ diff --git a/telethon/events/common.py b/telethon/events/common.py index db644efd..7940a94a 100644 --- a/telethon/events/common.py +++ b/telethon/events/common.py @@ -147,22 +147,31 @@ class EventCommon(ChatGetter, abc.ABC): """ self._client = client + def _get_entity_pair(self, entity_id): + """ + Returns ``(entity, input_entity)`` for the given entity ID. + """ + entity = self._entities.get(entity_id) + try: + input_entity = utils.get_input_peer(entity) + except TypeError: + try: + input_entity = self._client._entity_cache[entity_id] + except KeyError: + input_entity = None + + return entity, input_entity + def _load_entities(self): """ Must load all the entities it needs from cache, and return ``False`` if it could not find all of them. """ - # TODO Make sure all subclasses implement this - self._chat = self._entities.get(self.chat_id) - try: - self._input_chat = utils.get_input_peer(self._chat) - except TypeError: - try: - self._input_chat = self._client._entity_cache[self._chat_peer] - except KeyError: - return False + if not self._chat_peer: + return True # Nothing to load (e.g. MessageDeleted) - return True + self._chat, self._input_chat = self._get_entity_pair(self.chat_id) + return self._input_chat is not None @property def client(self): diff --git a/telethon/events/inlinequery.py b/telethon/events/inlinequery.py index 0655afef..7bd75cc6 100644 --- a/telethon/events/inlinequery.py +++ b/telethon/events/inlinequery.py @@ -87,6 +87,10 @@ class InlineQuery(EventBuilder): self._input_sender = None self._sender = None + def _load_entities(self): + self._sender, self._input_sender = self._get_entity_pair(self.sender_id) + return super()._load_entities() and self._input_sender is not None + @property def id(self): """ diff --git a/telethon/events/newmessage.py b/telethon/events/newmessage.py index 01f378ef..1b21224f 100644 --- a/telethon/events/newmessage.py +++ b/telethon/events/newmessage.py @@ -207,6 +207,13 @@ class NewMessage(EventBuilder): self.message._finish_init(client, self._entities, None) self.__dict__['_init'] = True # No new attributes can be set + def _load_entities(self): + m = self.message + m._chat, m._input_chat = self._get_entity_pair(m.chat_id) + m._sender, m._input_sender = self._get_entity_pair(m.sender_id) + return m._input_chat is not None and ( + not m.sender_id or m._input_sender is not None) + def __getattr__(self, item): if item in self.__dict__: return self.__dict__[item] diff --git a/telethon/events/userupdate.py b/telethon/events/userupdate.py index 9d4dab44..93c1a397 100644 --- a/telethon/events/userupdate.py +++ b/telethon/events/userupdate.py @@ -185,6 +185,10 @@ class UserUpdate(EventBuilder): super()._set_client(client) + def _load_entities(self): + self._sender, self._input_sender = self._get_entity_pair(self.sender_id) + return super()._load_entities() and self._input_sender is not None + @property def user(self): """Alias for `sender`.""" diff --git a/telethon/tl/custom/chatgetter.py b/telethon/tl/custom/chatgetter.py index 8eb904d4..7fdb79a0 100644 --- a/telethon/tl/custom/chatgetter.py +++ b/telethon/tl/custom/chatgetter.py @@ -49,7 +49,7 @@ class ChatGetter(abc.ABC): """ if self._input_chat is None and self._chat_peer: try: - self._input_chat = self._client._entity_cache(self._chat_peer) + self._input_chat = self._client._entity_cache[self._chat_peer] except KeyError: pass