Fix EventsCommon still having async properties

This commit is contained in:
Lonami Exo 2018-06-24 13:04:55 +02:00
parent d4479a0a4e
commit 266d44dd86
2 changed files with 51 additions and 32 deletions

View File

@ -114,37 +114,46 @@ class EventCommon(abc.ABC):
self._client = client self._client = client
@property @property
async def input_chat(self): def input_chat(self):
""" """
The (:tl:`InputPeer`) (group, megagroup or channel) on which This (:tl:`InputPeer`) is the input version of the chat where the
the event occurred. This doesn't have the title or anything, event occurred. This doesn't have things like username or similar,
but is useful if you don't need those to avoid further but is still useful in some cases.
requests.
Note that this might be ``None`` if the library can't find it. Note that this might not be available if the library doesn't have
enough information available.
""" """
if self._input_chat is None and self._chat_peer is not None: if self._input_chat is None and self._chat_peer is not None:
try: try:
self._input_chat = await self._client.get_input_entity( self._input_chat =\
self._chat_peer self._client.session.get_input_entity(self._chat_peer)
)
except ValueError: except ValueError:
ch = isinstance(self._chat_peer, types.PeerChannel) pass
if not ch and self._message_id is not None:
msg = await self._client.get_messages( return self._input_chat
None, ids=self._message_id)
self._chat = msg._chat async def get_input_chat(self):
self._input_chat = msg._input_chat """
else: Returns `input_chat`, but will make an API call to find the
target = utils.get_peer_id(self._chat_peer) input chat unless it's already cached.
async for d in self._client.iter_dialogs(): """
if d.id == target: if self.input_chat is None and self._chat_peer is not None:
self._chat = d.entity ch = isinstance(self._chat_peer, types.PeerChannel)
self._input_chat = d.input_entity if not ch and self._message_id is not None:
# TODO Don't break, exhaust the iterator, otherwise msg = await self._client.get_messages(
# async_generator raises RuntimeError: partially- None, ids=self._message_id)
# exhausted async_generator 'xyz' garbage collected self._chat = msg._chat
# break self._input_chat = msg._input_chat
else:
target = utils.get_peer_id(self._chat_peer)
async for d in self._client.iter_dialogs(100):
if d.id == target:
self._chat = d.entity
self._input_chat = d.input_entity
# TODO Don't break, exhaust the iterator, otherwise
# async_generator raises RuntimeError: partially-
# exhausted async_generator 'xyz' garbage collected
# break
return self._input_chat return self._input_chat
@ -156,12 +165,13 @@ class EventCommon(abc.ABC):
return self._client return self._client
@property @property
async def chat(self): def chat(self):
""" """
The (:tl:`User` | :tl:`Chat` | :tl:`Channel`, optional) on which The :tl:`User`, :tl:`Chat` or :tl:`Channel` on which
the event occurred. This property may make an API call the first time the event occurred. This property may make an API call the first time
to get the most up to date version of the chat (mostly when the event to get the most up to date version of the chat (mostly when the event
doesn't belong to a channel), so keep that in mind. doesn't belong to a channel), so keep that in mind. You should use
`get_chat` instead, unless you want to avoid an API call.
""" """
if not self.input_chat: if not self.input_chat:
return None return None
@ -169,9 +179,19 @@ class EventCommon(abc.ABC):
if self._chat is None: if self._chat is None:
self._chat = self._entities.get(utils.get_peer_id(self._chat_peer)) self._chat = self._entities.get(utils.get_peer_id(self._chat_peer))
if self._chat is None: return self._chat
self._chat = await self._client.get_entity(self._input_chat)
async def get_chat(self):
"""
Returns `chat`, but will make an API call to find the
chat unless it's already cached.
"""
if self.chat is None and await self.get_input_chat():
try:
self._chat =\
await self._client.get_entity(self._input_chat)
except ValueError:
pass
return self._chat return self._chat
@property @property

View File

@ -253,8 +253,7 @@ class Message:
things like username or similar, but still useful in some cases. things like username or similar, but still useful in some cases.
Note that this might not be available if the library doesn't know Note that this might not be available if the library doesn't know
where the message came from, and it may fetch the dialogs to try where the message came from.
to find it in the worst case.
""" """
if self._input_chat is None: if self._input_chat is None:
try: try: