From e1983b80c273c38c0e5a07ed2c649796cc12c3d6 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Wed, 8 Nov 2023 23:03:24 +0100 Subject: [PATCH] Treat messages.affected* as updateDeleteMessages --- .../telethon/_impl/mtproto/mtp/encrypted.py | 8 ++++ client/src/telethon/_impl/mtsender/sender.py | 44 ++++++++++++++++--- .../_impl/session/message_box/defs.py | 1 + .../_impl/session/message_box/messagebox.py | 8 ++-- client/src/telethon/_impl/tl/core/reader.py | 5 +-- 5 files changed, 54 insertions(+), 12 deletions(-) diff --git a/client/src/telethon/_impl/mtproto/mtp/encrypted.py b/client/src/telethon/_impl/mtproto/mtp/encrypted.py index 84e9cb8b..4031aa7f 100644 --- a/client/src/telethon/_impl/mtproto/mtp/encrypted.py +++ b/client/src/telethon/_impl/mtproto/mtp/encrypted.py @@ -45,6 +45,11 @@ from ...tl.types import ( UpdateShortSentMessage, UpdatesTooLong, ) +from ...tl.types.messages import ( + AffectedFoundMessages, + AffectedHistory, + AffectedMessages, +) from ..utils import ( CONTAINER_MAX_LENGTH, CONTAINER_MAX_SIZE, @@ -69,6 +74,9 @@ UPDATE_IDS = { UpdateShortMessage.constructor_id(), UpdateShortSentMessage.constructor_id(), UpdatesTooLong.constructor_id(), + AffectedFoundMessages.constructor_id(), + AffectedHistory.constructor_id(), + AffectedMessages.constructor_id(), } HEADER_LEN = 8 + 8 # salt, client_id diff --git a/client/src/telethon/_impl/mtsender/sender.py b/client/src/telethon/_impl/mtsender/sender.py index f0e20755..1b56340a 100644 --- a/client/src/telethon/_impl/mtsender/sender.py +++ b/client/src/telethon/_impl/mtsender/sender.py @@ -5,7 +5,7 @@ import time from abc import ABC from asyncio import FIRST_COMPLETED, Event, Future from dataclasses import dataclass -from typing import Generic, List, Optional, Protocol, Self, Tuple, TypeVar +from typing import Generic, List, Optional, Protocol, Self, Tuple, Type, TypeVar from ..crypto import AuthKey from ..mtproto import ( @@ -21,7 +21,10 @@ from ..mtproto import ( ) from ..tl import Request as RemoteCall from ..tl.abcs import Updates +from ..tl.core import Serializable from ..tl.mtproto.functions import ping_delay_disconnect +from ..tl.types import UpdateDeleteMessages, UpdateShort +from ..tl.types.messages import AffectedFoundMessages, AffectedHistory, AffectedMessages MAXIMUM_DATA = (1024 * 1024) + (8 * 1024) @@ -315,12 +318,41 @@ class Sender: try: u = Updates.from_bytes(update) except ValueError: - self._logger.warning( - "failed to deserialize incoming update; make sure the session is not in use elsewhere: %s", - update.hex(), + cid = struct.unpack_from("I", update)[0] + alt_classes: Tuple[Type[Serializable], ...] = ( + AffectedFoundMessages, + AffectedHistory, + AffectedMessages, ) - else: - updates.append(u) + for cls in alt_classes: + if cid == cls.constructor_id(): + affected = cls.from_bytes(update) + # mypy struggles with the types here quite a bit + assert isinstance( + affected, + ( + AffectedFoundMessages, + AffectedHistory, + AffectedMessages, + ), + ) + u = UpdateShort( + update=UpdateDeleteMessages( + messages=[], + pts=affected.pts, + pts_count=affected.pts_count, + ), + date=0, + ) + break + else: + self._logger.warning( + "failed to deserialize incoming update; make sure the session is not in use elsewhere: %s", + update.hex(), + ) + continue + + updates.append(u) for msg_id, ret in result.rpc_results: for i, req in enumerate(self._requests): diff --git a/client/src/telethon/_impl/session/message_box/defs.py b/client/src/telethon/_impl/session/message_box/defs.py index 3b2cb262..b3451031 100644 --- a/client/src/telethon/_impl/session/message_box/defs.py +++ b/client/src/telethon/_impl/session/message_box/defs.py @@ -61,6 +61,7 @@ class Gap(ValueError): return "Gap()" +NO_DATE = 0 # used on adapted messages.affected* from lower layers NO_SEQ = 0 NO_PTS = 0 diff --git a/client/src/telethon/_impl/session/message_box/messagebox.py b/client/src/telethon/_impl/session/message_box/messagebox.py index 15f1b544..7a35147e 100644 --- a/client/src/telethon/_impl/session/message_box/messagebox.py +++ b/client/src/telethon/_impl/session/message_box/messagebox.py @@ -13,6 +13,7 @@ from .defs import ( ENTRY_ACCOUNT, ENTRY_SECRET, LOG_LEVEL_TRACE, + NO_DATE, NO_PTS, NO_SEQ, NO_UPDATES_TIMEOUT, @@ -294,9 +295,10 @@ class MessageBox: if any_pts_applied: if __debug__: self._trace("updating seq as local pts was updated too") - self.date = datetime.datetime.fromtimestamp( - combined.date, tz=datetime.timezone.utc - ) + if combined.date != NO_DATE: + self.date = datetime.datetime.fromtimestamp( + combined.date, tz=datetime.timezone.utc + ) if combined.seq != NO_SEQ: self.seq = combined.seq diff --git a/client/src/telethon/_impl/tl/core/reader.py b/client/src/telethon/_impl/tl/core/reader.py index 7dd0ca03..2a91b17d 100644 --- a/client/src/telethon/_impl/tl/core/reader.py +++ b/client/src/telethon/_impl/tl/core/reader.py @@ -82,9 +82,8 @@ class Reader: assert self._pos <= self._len cid = struct.unpack("