Add StreamKeyType class and replace string literals with constants (#12567)

This commit is contained in:
Andrew Morgan 2022-05-16 16:35:31 +01:00 committed by GitHub
parent 3ce15cc7be
commit 83be72d76c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 125 additions and 80 deletions

View file

@ -37,7 +37,7 @@ import attr
from frozendict import frozendict
from signedjson.key import decode_verify_key_bytes
from signedjson.types import VerifyKey
from typing_extensions import TypedDict
from typing_extensions import Final, TypedDict
from unpaddedbase64 import decode_base64
from zope.interface import Interface
@ -630,6 +630,22 @@ class RoomStreamToken:
return "s%d" % (self.stream,)
class StreamKeyType:
"""Known stream types.
A stream is a list of entities ordered by an incrementing "stream token".
"""
ROOM: Final = "room_key"
PRESENCE: Final = "presence_key"
TYPING: Final = "typing_key"
RECEIPT: Final = "receipt_key"
ACCOUNT_DATA: Final = "account_data_key"
PUSH_RULES: Final = "push_rules_key"
TO_DEVICE: Final = "to_device_key"
DEVICE_LIST: Final = "device_list_key"
@attr.s(slots=True, frozen=True, auto_attribs=True)
class StreamToken:
"""A collection of keys joined together by underscores in the following
@ -743,9 +759,9 @@ class StreamToken:
:raises TypeError: if `key` is not the one of the keys tracked by a StreamToken.
"""
if key == "room_key":
if key == StreamKeyType.ROOM:
new_token = self.copy_and_replace(
"room_key", self.room_key.copy_and_advance(new_value)
StreamKeyType.ROOM, self.room_key.copy_and_advance(new_value)
)
return new_token