Fix update handling for channels

This commit is contained in:
Lonami Exo 2022-01-23 12:43:41 +01:00
parent de2cd1f2cf
commit 1f40372235
2 changed files with 14 additions and 11 deletions

View File

@ -96,6 +96,7 @@ async def _update_loop(self: 'TelegramClient'):
self._log[__name__].info('Getting difference for account updates') self._log[__name__].info('Getting difference for account updates')
diff = await self(get_diff) diff = await self(get_diff)
updates, users, chats = self._message_box.apply_difference(diff, self._entity_cache) updates, users, chats = self._message_box.apply_difference(diff, self._entity_cache)
self._entity_cache.extend(users, chats)
updates_to_dispatch.extend(updates) updates_to_dispatch.extend(updates)
continue continue
@ -103,7 +104,8 @@ async def _update_loop(self: 'TelegramClient'):
if get_diff: if get_diff:
self._log[__name__].info('Getting difference for channel updates') self._log[__name__].info('Getting difference for channel updates')
diff = await self(get_diff) diff = await self(get_diff)
updates, users, chats = self._message_box.apply_channel_difference(diff, self._entity_cache) updates, users, chats = self._message_box.apply_channel_difference(get_diff, diff, self._entity_cache)
self._entity_cache.extend(users, chats)
updates_to_dispatch.extend(updates) updates_to_dispatch.extend(updates)
continue continue
@ -119,6 +121,7 @@ async def _update_loop(self: 'TelegramClient'):
processed = [] processed = []
users, chats = self._message_box.process_updates(updates, self._entity_cache, processed) users, chats = self._message_box.process_updates(updates, self._entity_cache, processed)
self._entity_cache.extend(users, chats)
updates_to_dispatch.extend(processed) updates_to_dispatch.extend(processed)
except Exception: except Exception:
self._log[__name__].exception('Fatal error handling updates (this is a bug in Telethon, please report it)') self._log[__name__].exception('Fatal error handling updates (this is a bug in Telethon, please report it)')

View File

@ -232,7 +232,7 @@ class MessageBox:
# Convenience to reset a channel's deadline, with optional timeout. # Convenience to reset a channel's deadline, with optional timeout.
def reset_channel_deadline(self, channel_id, timeout): def reset_channel_deadline(self, channel_id, timeout):
self.reset_deadlines(channel_id, asyncio.get_running_loop().time() + (timeout or NO_UPDATES_TIMEOUT)) self.reset_deadline(channel_id, asyncio.get_running_loop().time() + (timeout or NO_UPDATES_TIMEOUT))
# Reset all the deadlines in `reset_deadlines_for` and then empty the set. # Reset all the deadlines in `reset_deadlines_for` and then empty the set.
def apply_deadlines_reset(self): def apply_deadlines_reset(self):
@ -524,10 +524,10 @@ class MessageBox:
return _tl.fn.updates.GetChannelDifference( return _tl.fn.updates.GetChannelDifference(
force=False, force=False,
channel=channel, channel=packed.try_to_input_channel(),
filter=_tl.ChannelMessagesFilterEmpty(), filter=_tl.ChannelMessagesFilterEmpty(),
pts=state.pts, pts=state.pts,
limit=BOT_CHANNEL_DIFF_LIMIT if chat_hashes.is_self_bot() else USER_CHANNEL_DIFF_LIMIT limit=BOT_CHANNEL_DIFF_LIMIT if chat_hashes.self_bot else USER_CHANNEL_DIFF_LIMIT
) )
# Similar to [`MessageBox::process_updates`], but using the result from getting difference. # Similar to [`MessageBox::process_updates`], but using the result from getting difference.
@ -538,7 +538,7 @@ class MessageBox:
chat_hashes, chat_hashes,
): ):
entry = request.channel.channel_id entry = request.channel.channel_id
self.possible_gaps.remove(entry) self.possible_gaps.pop(entry, None)
if isinstance(diff, _tl.updates.ChannelDifferenceEmpty): if isinstance(diff, _tl.updates.ChannelDifferenceEmpty):
assert diff.final assert diff.final
@ -549,7 +549,7 @@ class MessageBox:
assert diff.final assert diff.final
self.map[entry].pts = diff.dialog.pts self.map[entry].pts = diff.dialog.pts
chat_hashes.extend(diff.users, diff.chats) chat_hashes.extend(diff.users, diff.chats)
self.reset_channel_deadline(channel_id, diff.timeout) self.reset_channel_deadline(entry, diff.timeout)
# This `diff` has the "latest messages and corresponding chats", but it would # This `diff` has the "latest messages and corresponding chats", but it would
# be strange to give the user only partial changes of these when they would # be strange to give the user only partial changes of these when they would
# expect all updates to be fetched. Instead, nothing is returned. # expect all updates to be fetched. Instead, nothing is returned.
@ -558,15 +558,15 @@ class MessageBox:
if diff.final: if diff.final:
self.end_get_diff(entry) self.end_get_diff(entry)
self.map[entry].pts = pts self.map[entry].pts = diff.pts
updates.extend(_tl.UpdateNewMessage( diff.other_updates.extend(_tl.UpdateNewMessage(
message=m, message=m,
pts=NO_SEQ, pts=NO_SEQ,
pts_count=NO_SEQ, pts_count=NO_SEQ,
) for m in diff.new_messages) ) for m in diff.new_messages)
chat_hashes.extend(diff.users, diff.chats); chat_hashes.extend(diff.users, diff.chats)
self.reset_channel_deadline(channel_id, timeout) self.reset_channel_deadline(entry, None)
(diff.updates, diff.users, diff.chats) return diff.other_updates, diff.users, diff.chats
# endregion Getting and applying channel difference. # endregion Getting and applying channel difference.