mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-06-17 18:46:40 +00:00
Fix StateCache accessing None to_id and add logging (#1160)
This commit is contained in:
parent
fee0923dd1
commit
a151d24951
@ -308,7 +308,8 @@ class TelegramBaseClient(abc.ABC):
|
|||||||
|
|
||||||
# Update state (for catching up after a disconnection)
|
# Update state (for catching up after a disconnection)
|
||||||
# TODO Get state from channels too
|
# TODO Get state from channels too
|
||||||
self._state_cache = StateCache(self.session.get_update_state(0))
|
self._state_cache = StateCache(
|
||||||
|
self.session.get_update_state(0), self._log)
|
||||||
|
|
||||||
# Some further state for subclasses
|
# Some further state for subclasses
|
||||||
self._event_builders = []
|
self._event_builders = []
|
||||||
|
@ -213,7 +213,7 @@ class UpdateMethods(UserMethods):
|
|||||||
self._state_cache.update(update)
|
self._state_cache.update(update)
|
||||||
|
|
||||||
def _process_update(self, update, entities=None):
|
def _process_update(self, update, entities=None):
|
||||||
update._channel_id = StateCache.get_channel_id(update)
|
update._channel_id = self._state_cache.get_channel_id(update)
|
||||||
update._pts_date = self._state_cache[update._channel_id]
|
update._pts_date = self._state_cache[update._channel_id]
|
||||||
update._entities = entities or {}
|
update._entities = entities or {}
|
||||||
if self._updates_queue is None:
|
if self._updates_queue is None:
|
||||||
|
@ -7,10 +7,11 @@ class StateCache:
|
|||||||
"""
|
"""
|
||||||
In-memory update state cache, defaultdict-like behaviour.
|
In-memory update state cache, defaultdict-like behaviour.
|
||||||
"""
|
"""
|
||||||
def __init__(self, initial):
|
def __init__(self, initial, loggers):
|
||||||
# We only care about the pts and the date. By using a tuple which
|
# We only care about the pts and the date. By using a tuple which
|
||||||
# is lightweight and immutable we can easily copy them around to
|
# is lightweight and immutable we can easily copy them around to
|
||||||
# each update in case they need to fetch missing entities.
|
# each update in case they need to fetch missing entities.
|
||||||
|
self._logger = loggers[__name__]
|
||||||
if initial:
|
if initial:
|
||||||
self._pts_date = initial.pts, initial.date
|
self._pts_date = initial.pts, initial.date
|
||||||
else:
|
else:
|
||||||
@ -82,12 +83,13 @@ class StateCache:
|
|||||||
channel_id = self.get_channel_id(update)
|
channel_id = self.get_channel_id(update)
|
||||||
|
|
||||||
if channel_id is None:
|
if channel_id is None:
|
||||||
pass # TODO log, but shouldn't happen
|
self._logger.info(
|
||||||
|
'Failed to retrieve channel_id from %s', update)
|
||||||
else:
|
else:
|
||||||
self.__dict__[channel_id] = update.pts
|
self.__dict__[channel_id] = update.pts
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def get_channel_id(
|
def get_channel_id(
|
||||||
|
self,
|
||||||
update,
|
update,
|
||||||
has_channel_id=(
|
has_channel_id=(
|
||||||
types.UpdateChannelTooLong,
|
types.UpdateChannelTooLong,
|
||||||
@ -103,9 +105,12 @@ class StateCache:
|
|||||||
if isinstance(update, has_channel_id):
|
if isinstance(update, has_channel_id):
|
||||||
return update.channel_id
|
return update.channel_id
|
||||||
elif isinstance(update, has_message):
|
elif isinstance(update, has_message):
|
||||||
return update.message.to_id.channel_id
|
if update.message.to_id is None:
|
||||||
else:
|
self._logger.info('Update has None to_id %s', update)
|
||||||
return None
|
else:
|
||||||
|
return update.message.to_id.channel_id
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
def __getitem__(self, item):
|
def __getitem__(self, item):
|
||||||
"""
|
"""
|
||||||
|
Loading…
Reference in New Issue
Block a user