Treat messages.affected* as updateDeleteMessages

This commit is contained in:
Lonami Exo 2023-11-08 23:03:24 +01:00
parent f9435aa1f6
commit e1983b80c2
5 changed files with 54 additions and 12 deletions

View File

@ -45,6 +45,11 @@ from ...tl.types import (
UpdateShortSentMessage, UpdateShortSentMessage,
UpdatesTooLong, UpdatesTooLong,
) )
from ...tl.types.messages import (
AffectedFoundMessages,
AffectedHistory,
AffectedMessages,
)
from ..utils import ( from ..utils import (
CONTAINER_MAX_LENGTH, CONTAINER_MAX_LENGTH,
CONTAINER_MAX_SIZE, CONTAINER_MAX_SIZE,
@ -69,6 +74,9 @@ UPDATE_IDS = {
UpdateShortMessage.constructor_id(), UpdateShortMessage.constructor_id(),
UpdateShortSentMessage.constructor_id(), UpdateShortSentMessage.constructor_id(),
UpdatesTooLong.constructor_id(), UpdatesTooLong.constructor_id(),
AffectedFoundMessages.constructor_id(),
AffectedHistory.constructor_id(),
AffectedMessages.constructor_id(),
} }
HEADER_LEN = 8 + 8 # salt, client_id HEADER_LEN = 8 + 8 # salt, client_id

View File

@ -5,7 +5,7 @@ import time
from abc import ABC from abc import ABC
from asyncio import FIRST_COMPLETED, Event, Future from asyncio import FIRST_COMPLETED, Event, Future
from dataclasses import dataclass 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 ..crypto import AuthKey
from ..mtproto import ( from ..mtproto import (
@ -21,7 +21,10 @@ from ..mtproto import (
) )
from ..tl import Request as RemoteCall from ..tl import Request as RemoteCall
from ..tl.abcs import Updates from ..tl.abcs import Updates
from ..tl.core import Serializable
from ..tl.mtproto.functions import ping_delay_disconnect 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) MAXIMUM_DATA = (1024 * 1024) + (8 * 1024)
@ -315,11 +318,40 @@ class Sender:
try: try:
u = Updates.from_bytes(update) u = Updates.from_bytes(update)
except ValueError: except ValueError:
cid = struct.unpack_from("I", update)[0]
alt_classes: Tuple[Type[Serializable], ...] = (
AffectedFoundMessages,
AffectedHistory,
AffectedMessages,
)
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( self._logger.warning(
"failed to deserialize incoming update; make sure the session is not in use elsewhere: %s", "failed to deserialize incoming update; make sure the session is not in use elsewhere: %s",
update.hex(), update.hex(),
) )
else: continue
updates.append(u) updates.append(u)
for msg_id, ret in result.rpc_results: for msg_id, ret in result.rpc_results:

View File

@ -61,6 +61,7 @@ class Gap(ValueError):
return "Gap()" return "Gap()"
NO_DATE = 0 # used on adapted messages.affected* from lower layers
NO_SEQ = 0 NO_SEQ = 0
NO_PTS = 0 NO_PTS = 0

View File

@ -13,6 +13,7 @@ from .defs import (
ENTRY_ACCOUNT, ENTRY_ACCOUNT,
ENTRY_SECRET, ENTRY_SECRET,
LOG_LEVEL_TRACE, LOG_LEVEL_TRACE,
NO_DATE,
NO_PTS, NO_PTS,
NO_SEQ, NO_SEQ,
NO_UPDATES_TIMEOUT, NO_UPDATES_TIMEOUT,
@ -294,6 +295,7 @@ class MessageBox:
if any_pts_applied: if any_pts_applied:
if __debug__: if __debug__:
self._trace("updating seq as local pts was updated too") self._trace("updating seq as local pts was updated too")
if combined.date != NO_DATE:
self.date = datetime.datetime.fromtimestamp( self.date = datetime.datetime.fromtimestamp(
combined.date, tz=datetime.timezone.utc combined.date, tz=datetime.timezone.utc
) )

View File

@ -82,9 +82,8 @@ class Reader:
assert self._pos <= self._len assert self._pos <= self._len
cid = struct.unpack("<I", self._view[self._pos - 4 : self._pos])[0] cid = struct.unpack("<I", self._view[self._pos - 4 : self._pos])[0]
ty = self._get_ty(cid) ty = self._get_ty(cid)
if ty is None: if ty is None or not issubclass(ty, cls):
raise ValueError(f"No type found for constructor ID: {cid:x}") raise ValueError(f"No type found for constructor ID of {cls}: {cid:x}")
assert issubclass(ty, cls)
return ty._read_from(self) return ty._read_from(self)