diff options
author | Richard van der Hoff <1389908+richvdh@users.noreply.github.com> | 2020-02-07 15:30:04 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-07 15:30:04 +0000 |
commit | 799001f2c0b31d72b95a252a3808da25987e1ed3 (patch) | |
tree | 4ef02d19d99632feae18b69907d8a4913ea281ce /synapse/events/__init__.py | |
parent | Add typing to synapse.federation.sender (#6871) (diff) | |
download | synapse-799001f2c0b31d72b95a252a3808da25987e1ed3.tar.xz |
Add a `make_event_from_dict` method (#6858)
... and use it in places where it's trivial to do so. This will make it easier to pass room versions into the FrozenEvent constructors.
Diffstat (limited to 'synapse/events/__init__.py')
-rw-r--r-- | synapse/events/__init__.py | 16 |
1 files changed, 14 insertions, 2 deletions
diff --git a/synapse/events/__init__.py b/synapse/events/__init__.py index 89d41d82b6..a842661a90 100644 --- a/synapse/events/__init__.py +++ b/synapse/events/__init__.py @@ -16,12 +16,13 @@ import os from distutils.util import strtobool +from typing import Optional, Type import six from unpaddedbase64 import encode_base64 -from synapse.api.room_versions import EventFormatVersions +from synapse.api.room_versions import EventFormatVersions, RoomVersion, RoomVersions from synapse.types import JsonDict from synapse.util.caches import intern_dict from synapse.util.frozenutils import freeze @@ -407,7 +408,7 @@ class FrozenEventV3(FrozenEventV2): return self._event_id -def event_type_from_format_version(format_version): +def event_type_from_format_version(format_version: int) -> Type[EventBase]: """Returns the python type to use to construct an Event object for the given event format version. @@ -427,3 +428,14 @@ def event_type_from_format_version(format_version): return FrozenEventV3 else: raise Exception("No event format %r" % (format_version,)) + + +def make_event_from_dict( + event_dict: JsonDict, + room_version: RoomVersion = RoomVersions.V1, + internal_metadata_dict: JsonDict = {}, + rejected_reason: Optional[str] = None, +) -> EventBase: + """Construct an EventBase from the given event dict""" + event_type = event_type_from_format_version(room_version.event_format) + return event_type(event_dict, internal_metadata_dict, rejected_reason) |