diff --git a/synapse/types/rest/client/__init__.py b/synapse/types/rest/client/__init__.py index 489aa90809..21e18341ce 100644 --- a/synapse/types/rest/client/__init__.py +++ b/synapse/types/rest/client/__init__.py @@ -142,104 +142,6 @@ class MsisdnRequestTokenBody(ThreepidRequestTokenBody): phone_number: StrictStr -class SlidingSyncStickyEventsToken: - """ - A token returned by `next_batch` of the MSC4354 Sticky Events extension to Sliding Sync - and then accepted as the `since` parameter in the requests of the same extension. - - Current format: - SlidingSyncStickyEventsToken ::= 'sticky_' UnresolvedMultiWriterStreamToken - - where `MultiWriterStreamToken` is the serialised form of a (potentially sharded) - `MultiWriterStreamToken` for the sticky events stream, e.g. `42` or `m42~1.45`. - - The `sticky_` prefix allows us to make sure it's not swapped for another token - or to evolve the type of token accepted with backwards compatibility in the future. - - Note that the inner stream token can only be interpreted with access to the - database (to resolve instance IDs to instance names), so this class holds the - serialised form and converts on demand. - """ - - PATTERN = re.compile(r"^sticky_([0-9]+|m[0-9]+(~[0-9]+\.[0-9]+)*)$") - START: ClassVar["SlidingSyncStickyEventsToken"] - - def __init__( - self, *, unresolved_stream_token: UnresolvedMultiWriterStreamToken - ) -> None: - self._unresolved_stream_token = unresolved_stream_token - - @classmethod - async def from_stream_token( - cls, store: "DataStore", token: MultiWriterStreamToken - ) -> "SlidingSyncStickyEventsToken": - return cls(unresolved_stream_token=await token.to_unresolved(store)) - - async def to_stream_token(self, store: "DataStore") -> MultiWriterStreamToken: - return await MultiWriterStreamToken.from_unresolved( - store, self._unresolved_stream_token - ) - - @classmethod - def __get_pydantic_core_schema__( - cls, source_type: object, handler: GetCoreSchemaHandler - ) -> CoreSchema: - """ - This function is checked for and used by Pydantic when - attempting to deserialise/validate a field of this type. - - This returns a schema that will parse a string into an - instance of `SlidingSyncStickyEventsToken`. - """ - - return pydantic_core.core_schema.no_info_plain_validator_function( - cls._validate, - serialization=pydantic_core.core_schema.plain_serializer_function_ser_schema( - cls.serialise, - info_arg=False, - ), - ) - - @classmethod - def _validate(cls, v: object) -> Self: - """ - Create an instance from serialised string form. - - The inverse of `serialise`. - """ - if isinstance(v, cls): - return v - if isinstance(v, str): - match = cls.PATTERN.match(v) - if match is None: - raise ValueError(f"Invalid SlidingSyncStickyEventsToken format: {v!r}") - - return cls( - unresolved_stream_token=UnresolvedMultiWriterStreamToken.parse( - match.group(1) - ) - ) - raise ValueError(f"Cannot parse SlidingSyncStickyEventsToken from {type(v)}") - - def serialise(self) -> str: - """ - Convert this instance to string. - - The inverse of `_validate`. - """ - return f"sticky_{self._unresolved_stream_token.to_string()}" - - def __repr__(self) -> str: - # Use the serialised form as debug output. - return self.serialise() - - -# Starting reading a stream at 0 ensures all stream fact rows will be read -SlidingSyncStickyEventsToken.START = SlidingSyncStickyEventsToken( - unresolved_stream_token=UnresolvedMultiWriterStreamToken(stream=0) -) - - class SlidingSyncBody(RequestBodyModel): """ Sliding Sync API request body.