Implement _load_entities for all events

Follow-up of c902428af1
This means that now input_chat (and even chat_id) and
similar can be safely used, without needing get_input
This commit is contained in:
Lonami Exo 2019-03-28 10:47:15 +01:00
parent 5554b414e1
commit 39d9531483
7 changed files with 40 additions and 12 deletions

View File

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

View File

@ -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):
"""

View File

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

View File

@ -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):
"""

View File

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

View File

@ -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`."""

View File

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