Skip to content

Commit

Permalink
Treat messages.affected* as updateDeleteMessages
Browse files Browse the repository at this point in the history
  • Loading branch information
Lonami committed Nov 8, 2023
1 parent f9435aa commit e1983b8
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 12 deletions.
8 changes: 8 additions & 0 deletions client/src/telethon/_impl/mtproto/mtp/encrypted.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@
UpdateShortSentMessage,
UpdatesTooLong,
)
from ...tl.types.messages import (
AffectedFoundMessages,
AffectedHistory,
AffectedMessages,
)
from ..utils import (
CONTAINER_MAX_LENGTH,
CONTAINER_MAX_SIZE,
Expand All @@ -69,6 +74,9 @@
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
Expand Down
44 changes: 38 additions & 6 deletions client/src/telethon/_impl/mtsender/sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
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 (
Expand All @@ -21,7 +21,10 @@
)
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)

Expand Down Expand Up @@ -315,12 +318,41 @@ def _process_mtp_buffer(self, updates: List[Updates]) -> None:
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):
Expand Down
1 change: 1 addition & 0 deletions client/src/telethon/_impl/session/message_box/defs.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def __repr__(self) -> str:
return "Gap()"


NO_DATE = 0 # used on adapted messages.affected* from lower layers
NO_SEQ = 0

NO_PTS = 0
Expand Down
8 changes: 5 additions & 3 deletions client/src/telethon/_impl/session/message_box/messagebox.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
ENTRY_ACCOUNT,
ENTRY_SECRET,
LOG_LEVEL_TRACE,
NO_DATE,
NO_PTS,
NO_SEQ,
NO_UPDATES_TIMEOUT,
Expand Down Expand Up @@ -294,9 +295,10 @@ def update_sort_key(update: abcs.Update) -> int:
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

Expand Down
5 changes: 2 additions & 3 deletions client/src/telethon/_impl/tl/core/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,8 @@ def read_serializable(self, cls: Type[T]) -> T:
assert self._pos <= self._len
cid = struct.unpack("<I", self._view[self._pos - 4 : self._pos])[0]
ty = self._get_ty(cid)
if ty is None:
raise ValueError(f"No type found for constructor ID: {cid:x}")
assert issubclass(ty, cls)
if ty is None or not issubclass(ty, cls):
raise ValueError(f"No type found for constructor ID of {cls}: {cid:x}")
return ty._read_from(self)


Expand Down

0 comments on commit e1983b8

Please sign in to comment.