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.
pull/6883/head
Richard van der Hoff 2020-02-07 15:30:04 +00:00 committed by GitHub
parent b08b0a22d5
commit 799001f2c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 52 additions and 39 deletions

1
changelog.d/6858.misc Normal file
View File

@ -0,0 +1 @@
Refactoring work in preparation for changing the event redaction algorithm.

View File

@ -16,12 +16,13 @@
import os import os
from distutils.util import strtobool from distutils.util import strtobool
from typing import Optional, Type
import six import six
from unpaddedbase64 import encode_base64 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.types import JsonDict
from synapse.util.caches import intern_dict from synapse.util.caches import intern_dict
from synapse.util.frozenutils import freeze from synapse.util.frozenutils import freeze
@ -407,7 +408,7 @@ class FrozenEventV3(FrozenEventV2):
return self._event_id 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 """Returns the python type to use to construct an Event object for the
given event format version. given event format version.
@ -427,3 +428,14 @@ def event_type_from_format_version(format_version):
return FrozenEventV3 return FrozenEventV3
else: else:
raise Exception("No event format %r" % (format_version,)) 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)

View File

@ -28,11 +28,7 @@ from synapse.api.room_versions import (
RoomVersion, RoomVersion,
) )
from synapse.crypto.event_signing import add_hashes_and_signatures from synapse.crypto.event_signing import add_hashes_and_signatures
from synapse.events import ( from synapse.events import EventBase, _EventInternalMetadata, make_event_from_dict
EventBase,
_EventInternalMetadata,
event_type_from_format_version,
)
from synapse.types import EventID, JsonDict from synapse.types import EventID, JsonDict
from synapse.util import Clock from synapse.util import Clock
from synapse.util.stringutils import random_string from synapse.util.stringutils import random_string
@ -256,8 +252,8 @@ def create_local_event_from_event_dict(
event_dict.setdefault("signatures", {}) event_dict.setdefault("signatures", {})
add_hashes_and_signatures(room_version, event_dict, hostname, signing_key) add_hashes_and_signatures(room_version, event_dict, hostname, signing_key)
return event_type_from_format_version(format_version)( return make_event_from_dict(
event_dict, internal_metadata_dict=internal_metadata_dict event_dict, room_version, internal_metadata_dict=internal_metadata_dict
) )

View File

@ -29,7 +29,7 @@ from synapse.api.room_versions import (
RoomVersion, RoomVersion,
) )
from synapse.crypto.event_signing import check_event_content_hash from synapse.crypto.event_signing import check_event_content_hash
from synapse.events import EventBase, event_type_from_format_version from synapse.events import EventBase, make_event_from_dict
from synapse.events.utils import prune_event from synapse.events.utils import prune_event
from synapse.http.servlet import assert_params_in_dict from synapse.http.servlet import assert_params_in_dict
from synapse.logging.context import ( from synapse.logging.context import (
@ -374,8 +374,7 @@ def event_from_pdu_json(
elif depth > MAX_DEPTH: elif depth > MAX_DEPTH:
raise SynapseError(400, "Depth too large", Codes.BAD_JSON) raise SynapseError(400, "Depth too large", Codes.BAD_JSON)
event = event_type_from_format_version(room_version.event_format)(pdu_json) event = make_event_from_dict(pdu_json, room_version)
event.internal_metadata.outlier = outlier event.internal_metadata.outlier = outlier
return event return event

View File

@ -25,7 +25,7 @@ from twisted.internet import defer
from synapse.api.constants import EventContentFields from synapse.api.constants import EventContentFields
from synapse.api.errors import SynapseError from synapse.api.errors import SynapseError
from synapse.api.filtering import Filter from synapse.api.filtering import Filter
from synapse.events import FrozenEvent from synapse.events import make_event_from_dict
from tests import unittest from tests import unittest
from tests.utils import DeferredMockCallable, MockHttpResource, setup_test_homeserver from tests.utils import DeferredMockCallable, MockHttpResource, setup_test_homeserver
@ -38,7 +38,7 @@ def MockEvent(**kwargs):
kwargs["event_id"] = "fake_event_id" kwargs["event_id"] = "fake_event_id"
if "type" not in kwargs: if "type" not in kwargs:
kwargs["type"] = "fake_type" kwargs["type"] = "fake_type"
return FrozenEvent(kwargs) return make_event_from_dict(kwargs)
class FilteringTestCase(unittest.TestCase): class FilteringTestCase(unittest.TestCase):

View File

@ -19,7 +19,7 @@ from unpaddedbase64 import decode_base64
from synapse.api.room_versions import RoomVersions from synapse.api.room_versions import RoomVersions
from synapse.crypto.event_signing import add_hashes_and_signatures from synapse.crypto.event_signing import add_hashes_and_signatures
from synapse.events import FrozenEvent from synapse.events import make_event_from_dict
from tests import unittest from tests import unittest
@ -54,7 +54,7 @@ class EventSigningTestCase(unittest.TestCase):
RoomVersions.V1, event_dict, HOSTNAME, self.signing_key RoomVersions.V1, event_dict, HOSTNAME, self.signing_key
) )
event = FrozenEvent(event_dict) event = make_event_from_dict(event_dict)
self.assertTrue(hasattr(event, "hashes")) self.assertTrue(hasattr(event, "hashes"))
self.assertIn("sha256", event.hashes) self.assertIn("sha256", event.hashes)
@ -88,7 +88,7 @@ class EventSigningTestCase(unittest.TestCase):
RoomVersions.V1, event_dict, HOSTNAME, self.signing_key RoomVersions.V1, event_dict, HOSTNAME, self.signing_key
) )
event = FrozenEvent(event_dict) event = make_event_from_dict(event_dict)
self.assertTrue(hasattr(event, "hashes")) self.assertTrue(hasattr(event, "hashes"))
self.assertIn("sha256", event.hashes) self.assertIn("sha256", event.hashes)

View File

@ -13,8 +13,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
from synapse.events import make_event_from_dict
from synapse.events import FrozenEvent
from synapse.events.utils import ( from synapse.events.utils import (
copy_power_levels_contents, copy_power_levels_contents,
prune_event, prune_event,
@ -30,7 +29,7 @@ def MockEvent(**kwargs):
kwargs["event_id"] = "fake_event_id" kwargs["event_id"] = "fake_event_id"
if "type" not in kwargs: if "type" not in kwargs:
kwargs["type"] = "fake_type" kwargs["type"] = "fake_type"
return FrozenEvent(kwargs) return make_event_from_dict(kwargs)
class PruneEventTestCase(unittest.TestCase): class PruneEventTestCase(unittest.TestCase):
@ -38,7 +37,9 @@ class PruneEventTestCase(unittest.TestCase):
`matchdict` when it is redacted. """ `matchdict` when it is redacted. """
def run_test(self, evdict, matchdict): def run_test(self, evdict, matchdict):
self.assertEquals(prune_event(FrozenEvent(evdict)).get_dict(), matchdict) self.assertEquals(
prune_event(make_event_from_dict(evdict)).get_dict(), matchdict
)
def test_minimal(self): def test_minimal(self):
self.run_test( self.run_test(

View File

@ -15,7 +15,7 @@
# limitations under the License. # limitations under the License.
import logging import logging
from synapse.events import FrozenEvent from synapse.events import make_event_from_dict
from synapse.federation.federation_server import server_matches_acl_event from synapse.federation.federation_server import server_matches_acl_event
from synapse.rest import admin from synapse.rest import admin
from synapse.rest.client.v1 import login, room from synapse.rest.client.v1 import login, room
@ -105,7 +105,7 @@ class StateQueryTests(unittest.FederatingHomeserverTestCase):
def _create_acl_event(content): def _create_acl_event(content):
return FrozenEvent( return make_event_from_dict(
{ {
"room_id": "!a:b", "room_id": "!a:b",
"event_id": "$a:b", "event_id": "$a:b",

View File

@ -15,7 +15,7 @@ import logging
from canonicaljson import encode_canonical_json from canonicaljson import encode_canonical_json
from synapse.events import FrozenEvent, _EventInternalMetadata from synapse.events import FrozenEvent, _EventInternalMetadata, make_event_from_dict
from synapse.events.snapshot import EventContext from synapse.events.snapshot import EventContext
from synapse.handlers.room import RoomEventSource from synapse.handlers.room import RoomEventSource
from synapse.replication.slave.storage.events import SlavedEventStore from synapse.replication.slave.storage.events import SlavedEventStore
@ -90,7 +90,9 @@ class SlavedEventStoreTestCase(BaseSlavedStoreTestCase):
msg_dict["content"] = {} msg_dict["content"] = {}
msg_dict["unsigned"]["redacted_by"] = redaction.event_id msg_dict["unsigned"]["redacted_by"] = redaction.event_id
msg_dict["unsigned"]["redacted_because"] = redaction msg_dict["unsigned"]["redacted_because"] = redaction
redacted = FrozenEvent(msg_dict, msg.internal_metadata.get_dict()) redacted = make_event_from_dict(
msg_dict, internal_metadata_dict=msg.internal_metadata.get_dict()
)
self.check("get_event", [msg.event_id], redacted) self.check("get_event", [msg.event_id], redacted)
def test_backfilled_redactions(self): def test_backfilled_redactions(self):
@ -110,7 +112,9 @@ class SlavedEventStoreTestCase(BaseSlavedStoreTestCase):
msg_dict["content"] = {} msg_dict["content"] = {}
msg_dict["unsigned"]["redacted_by"] = redaction.event_id msg_dict["unsigned"]["redacted_by"] = redaction.event_id
msg_dict["unsigned"]["redacted_because"] = redaction msg_dict["unsigned"]["redacted_because"] = redaction
redacted = FrozenEvent(msg_dict, msg.internal_metadata.get_dict()) redacted = make_event_from_dict(
msg_dict, internal_metadata_dict=msg.internal_metadata.get_dict()
)
self.check("get_event", [msg.event_id], redacted) self.check("get_event", [msg.event_id], redacted)
def test_invites(self): def test_invites(self):
@ -345,7 +349,7 @@ class SlavedEventStoreTestCase(BaseSlavedStoreTestCase):
if redacts is not None: if redacts is not None:
event_dict["redacts"] = redacts event_dict["redacts"] = redacts
event = FrozenEvent(event_dict, internal_metadata_dict=internal) event = make_event_from_dict(event_dict, internal_metadata_dict=internal)
self.event_id += 1 self.event_id += 1

View File

@ -22,7 +22,7 @@ import attr
from synapse.api.constants import EventTypes, JoinRules, Membership from synapse.api.constants import EventTypes, JoinRules, Membership
from synapse.api.room_versions import RoomVersions from synapse.api.room_versions import RoomVersions
from synapse.event_auth import auth_types_for_event from synapse.event_auth import auth_types_for_event
from synapse.events import FrozenEvent from synapse.events import make_event_from_dict
from synapse.state.v2 import lexicographical_topological_sort, resolve_events_with_store from synapse.state.v2 import lexicographical_topological_sort, resolve_events_with_store
from synapse.types import EventID from synapse.types import EventID
@ -89,7 +89,7 @@ class FakeEvent(object):
if self.state_key is not None: if self.state_key is not None:
event_dict["state_key"] = self.state_key event_dict["state_key"] = self.state_key
return FrozenEvent(event_dict) return make_event_from_dict(event_dict)
# All graphs start with this set of events # All graphs start with this set of events

View File

@ -18,7 +18,7 @@ import unittest
from synapse import event_auth from synapse import event_auth
from synapse.api.errors import AuthError from synapse.api.errors import AuthError
from synapse.api.room_versions import RoomVersions from synapse.api.room_versions import RoomVersions
from synapse.events import FrozenEvent from synapse.events import make_event_from_dict
class EventAuthTestCase(unittest.TestCase): class EventAuthTestCase(unittest.TestCase):
@ -94,7 +94,7 @@ TEST_ROOM_ID = "!test:room"
def _create_event(user_id): def _create_event(user_id):
return FrozenEvent( return make_event_from_dict(
{ {
"room_id": TEST_ROOM_ID, "room_id": TEST_ROOM_ID,
"event_id": _get_event_id(), "event_id": _get_event_id(),
@ -106,7 +106,7 @@ def _create_event(user_id):
def _join_event(user_id): def _join_event(user_id):
return FrozenEvent( return make_event_from_dict(
{ {
"room_id": TEST_ROOM_ID, "room_id": TEST_ROOM_ID,
"event_id": _get_event_id(), "event_id": _get_event_id(),
@ -119,7 +119,7 @@ def _join_event(user_id):
def _power_levels_event(sender, content): def _power_levels_event(sender, content):
return FrozenEvent( return make_event_from_dict(
{ {
"room_id": TEST_ROOM_ID, "room_id": TEST_ROOM_ID,
"event_id": _get_event_id(), "event_id": _get_event_id(),
@ -132,7 +132,7 @@ def _power_levels_event(sender, content):
def _random_state_event(sender): def _random_state_event(sender):
return FrozenEvent( return make_event_from_dict(
{ {
"room_id": TEST_ROOM_ID, "room_id": TEST_ROOM_ID,
"event_id": _get_event_id(), "event_id": _get_event_id(),

View File

@ -2,7 +2,7 @@ from mock import Mock
from twisted.internet.defer import ensureDeferred, maybeDeferred, succeed from twisted.internet.defer import ensureDeferred, maybeDeferred, succeed
from synapse.events import FrozenEvent from synapse.events import make_event_from_dict
from synapse.logging.context import LoggingContext from synapse.logging.context import LoggingContext
from synapse.types import Requester, UserID from synapse.types import Requester, UserID
from synapse.util import Clock from synapse.util import Clock
@ -43,7 +43,7 @@ class MessageAcceptTests(unittest.TestCase):
) )
)[0] )[0]
join_event = FrozenEvent( join_event = make_event_from_dict(
{ {
"room_id": self.room_id, "room_id": self.room_id,
"sender": "@baduser:test.serv", "sender": "@baduser:test.serv",
@ -105,7 +105,7 @@ class MessageAcceptTests(unittest.TestCase):
)[0] )[0]
# Now lie about an event # Now lie about an event
lying_event = FrozenEvent( lying_event = make_event_from_dict(
{ {
"room_id": self.room_id, "room_id": self.room_id,
"sender": "@baduser:test.serv", "sender": "@baduser:test.serv",

View File

@ -20,7 +20,7 @@ from twisted.internet import defer
from synapse.api.auth import Auth from synapse.api.auth import Auth
from synapse.api.constants import EventTypes, Membership from synapse.api.constants import EventTypes, Membership
from synapse.api.room_versions import RoomVersions from synapse.api.room_versions import RoomVersions
from synapse.events import FrozenEvent from synapse.events import make_event_from_dict
from synapse.events.snapshot import EventContext from synapse.events.snapshot import EventContext
from synapse.state import StateHandler, StateResolutionHandler from synapse.state import StateHandler, StateResolutionHandler
@ -66,7 +66,7 @@ def create_event(
d.update(kwargs) d.update(kwargs)
event = FrozenEvent(d) event = make_event_from_dict(d)
return event return event