mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2024-11-22 17:36:34 +03:00
Fix resolving events while disconnected
This commit is contained in:
parent
5ca1edb228
commit
576ac666d9
|
@ -89,7 +89,6 @@ class UpdateMethods(UserMethods):
|
||||||
elif not event:
|
elif not event:
|
||||||
event = events.Raw()
|
event = events.Raw()
|
||||||
|
|
||||||
event.ensure_resolve(self)
|
|
||||||
self._event_builders.append((event, callback))
|
self._event_builders.append((event, callback))
|
||||||
|
|
||||||
def remove_event_handler(self, callback, event=None):
|
def remove_event_handler(self, callback, event=None):
|
||||||
|
@ -266,8 +265,8 @@ class UpdateMethods(UserMethods):
|
||||||
if not event:
|
if not event:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if not builder.resolved.is_set():
|
if not builder.resolved:
|
||||||
await builder.resolved.wait()
|
await builder.resolve()
|
||||||
|
|
||||||
if not builder.filter(event):
|
if not builder.filter(event):
|
||||||
continue
|
continue
|
||||||
|
|
|
@ -58,37 +58,31 @@ class EventBuilder(abc.ABC):
|
||||||
def __init__(self, chats=None, blacklist_chats=False):
|
def __init__(self, chats=None, blacklist_chats=False):
|
||||||
self.chats = chats
|
self.chats = chats
|
||||||
self.blacklist_chats = blacklist_chats
|
self.blacklist_chats = blacklist_chats
|
||||||
self.resolved = None
|
self.resolved = False
|
||||||
|
self._resolve_lock = None
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
@abc.abstractmethod
|
@abc.abstractmethod
|
||||||
def build(cls, update):
|
def build(cls, update):
|
||||||
"""Builds an event for the given update if possible, or returns None"""
|
"""Builds an event for the given update if possible, or returns None"""
|
||||||
|
|
||||||
def ensure_resolve(self, client):
|
|
||||||
"""
|
|
||||||
Sets the event loop so that self.resolved can be used.
|
|
||||||
|
|
||||||
The expected workflow is:
|
|
||||||
1. Creating the event builder.
|
|
||||||
2a. Calling `ensure_resolve`.
|
|
||||||
2b. Awaiting `resolved.wait`.
|
|
||||||
OR
|
|
||||||
2a. Awaiting `resolve`.
|
|
||||||
3. Using `filter`.
|
|
||||||
"""
|
|
||||||
if not self.resolved:
|
|
||||||
self.resolved = asyncio.Event(loop=client.loop)
|
|
||||||
client.loop.create_task(self.resolve(client))
|
|
||||||
|
|
||||||
async def resolve(self, client):
|
async def resolve(self, client):
|
||||||
"""Helper method to allow event builders to be resolved before usage"""
|
"""Helper method to allow event builders to be resolved before usage"""
|
||||||
if not self.resolved.is_set():
|
if self.resolved:
|
||||||
self.chats = await _into_id_set(client, self.chats)
|
return
|
||||||
if not EventBuilder.self_id:
|
|
||||||
EventBuilder.self_id = await client.get_peer_id('me')
|
|
||||||
|
|
||||||
self.resolved.set()
|
if not self._resolve_lock:
|
||||||
|
self._resolve_lock = asyncio.Lock(loop=client.loop)
|
||||||
|
|
||||||
|
async with self._resolve_lock:
|
||||||
|
if not self.resolved:
|
||||||
|
await self._resolve(client)
|
||||||
|
self.resolved = True
|
||||||
|
|
||||||
|
async def _resolve(self, client):
|
||||||
|
self.chats = await _into_id_set(client, self.chats)
|
||||||
|
if not EventBuilder.self_id:
|
||||||
|
EventBuilder.self_id = await client.get_peer_id('me')
|
||||||
|
|
||||||
def filter(self, event):
|
def filter(self, event):
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -71,10 +71,9 @@ class NewMessage(EventBuilder):
|
||||||
self.from_users, self.forwards, self.from_users
|
self.from_users, self.forwards, self.from_users
|
||||||
))
|
))
|
||||||
|
|
||||||
async def resolve(self, client):
|
async def _resolve(self, client):
|
||||||
if not self.resolved.is_set():
|
await super()._resolve(client)
|
||||||
self.from_users = await _into_id_set(client, self.from_users)
|
self.from_users = await _into_id_set(client, self.from_users)
|
||||||
await super().resolve(client)
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def build(cls, update):
|
def build(cls, update):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user