Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
No new room version necessary and allow no prev_events via allow_no_p…
Browse files Browse the repository at this point in the history
  • Loading branch information
MadLittleMods committed Dec 1, 2021
1 parent 2370dca commit e2928b5
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 70 deletions.
2 changes: 1 addition & 1 deletion changelog.d/11243.feature
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Add experimental room version `org.matrix.msc2716v4` to allow events to be created without `prev_events` (only `auth_events`).
Allow events to be created without `prev_events` (only `auth_events`).
31 changes: 0 additions & 31 deletions synapse/api/room_versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ class RoomVersion:
msc2716_historical = attr.ib(type=bool)
# MSC2716: Adds support for redacting "insertion", "chunk", and "marker" events
msc2716_redactions = attr.ib(type=bool)
# MSC2716: Adds support for events with no `prev_events` but with some `auth_events`
msc2716_empty_prev_events = attr.ib(type=bool)


class RoomVersions:
Expand All @@ -101,7 +99,6 @@ class RoomVersions:
msc2403_knocking=False,
msc2716_historical=False,
msc2716_redactions=False,
msc2716_empty_prev_events=False,
)
V2 = RoomVersion(
"2",
Expand All @@ -118,7 +115,6 @@ class RoomVersions:
msc2403_knocking=False,
msc2716_historical=False,
msc2716_redactions=False,
msc2716_empty_prev_events=False,
)
V3 = RoomVersion(
"3",
Expand All @@ -135,7 +131,6 @@ class RoomVersions:
msc2403_knocking=False,
msc2716_historical=False,
msc2716_redactions=False,
msc2716_empty_prev_events=False,
)
V4 = RoomVersion(
"4",
Expand All @@ -152,7 +147,6 @@ class RoomVersions:
msc2403_knocking=False,
msc2716_historical=False,
msc2716_redactions=False,
msc2716_empty_prev_events=False,
)
V5 = RoomVersion(
"5",
Expand All @@ -169,7 +163,6 @@ class RoomVersions:
msc2403_knocking=False,
msc2716_historical=False,
msc2716_redactions=False,
msc2716_empty_prev_events=False,
)
V6 = RoomVersion(
"6",
Expand All @@ -186,7 +179,6 @@ class RoomVersions:
msc2403_knocking=False,
msc2716_historical=False,
msc2716_redactions=False,
msc2716_empty_prev_events=False,
)
MSC2176 = RoomVersion(
"org.matrix.msc2176",
Expand All @@ -203,7 +195,6 @@ class RoomVersions:
msc2403_knocking=False,
msc2716_historical=False,
msc2716_redactions=False,
msc2716_empty_prev_events=False,
)
V7 = RoomVersion(
"7",
Expand All @@ -220,7 +211,6 @@ class RoomVersions:
msc2403_knocking=True,
msc2716_historical=False,
msc2716_redactions=False,
msc2716_empty_prev_events=False,
)
V8 = RoomVersion(
"8",
Expand All @@ -237,7 +227,6 @@ class RoomVersions:
msc2403_knocking=True,
msc2716_historical=False,
msc2716_redactions=False,
msc2716_empty_prev_events=False,
)
V9 = RoomVersion(
"9",
Expand All @@ -254,7 +243,6 @@ class RoomVersions:
msc2403_knocking=True,
msc2716_historical=False,
msc2716_redactions=False,
msc2716_empty_prev_events=False,
)
MSC2716v3 = RoomVersion(
"org.matrix.msc2716v3",
Expand All @@ -271,24 +259,6 @@ class RoomVersions:
msc2403_knocking=True,
msc2716_historical=True,
msc2716_redactions=True,
msc2716_empty_prev_events=False,
)
MSC2716v4 = RoomVersion(
"org.matrix.msc2716v4",
RoomDisposition.UNSTABLE,
EventFormatVersions.V3,
StateResolutionVersions.V2,
enforce_key_validity=True,
special_case_aliases_auth=False,
strict_canonicaljson=True,
limit_notifications_power_levels=True,
msc2176_redaction_rules=False,
msc3083_join_rules=False,
msc3375_redaction_rules=False,
msc2403_knocking=True,
msc2716_historical=True,
msc2716_redactions=True,
msc2716_empty_prev_events=True,
)


Expand All @@ -306,7 +276,6 @@ class RoomVersions:
RoomVersions.V8,
RoomVersions.V9,
RoomVersions.MSC2716v3,
RoomVersions.MSC2716v4,
)
}

Expand Down
6 changes: 4 additions & 2 deletions synapse/handlers/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,7 @@ async def create_event(
require_consent: bool = True,
outlier: bool = False,
historical: bool = False,
allow_no_prev_events: bool = False,
depth: Optional[int] = None,
) -> Tuple[EventBase, EventContext]:
"""
Expand Down Expand Up @@ -613,6 +614,7 @@ async def create_event(
prev_event_ids=prev_event_ids,
auth_event_ids=auth_event_ids,
depth=depth,
allow_no_prev_events=allow_no_prev_events,
)

# In an ideal world we wouldn't need the second part of this condition. However,
Expand Down Expand Up @@ -888,6 +890,7 @@ async def create_new_client_event(
prev_event_ids: Optional[List[str]] = None,
auth_event_ids: Optional[List[str]] = None,
depth: Optional[int] = None,
allow_no_prev_events: bool = False,
) -> Tuple[EventBase, EventContext]:
"""Create a new event for a local client
Expand Down Expand Up @@ -953,8 +956,7 @@ async def create_new_client_event(
# Do a quick sanity check here, rather than waiting until we've created the
# event and then try to auth it (which fails with a somewhat confusing "No
# create event in auth events")
room_version_obj = await self.store.get_room_version(builder.room_id)
if room_version_obj.msc2716_empty_prev_events:
if allow_no_prev_events:
# We allow events with no `prev_events` but it better have some `auth_events`
assert (
builder.type == EventTypes.Create
Expand Down
2 changes: 1 addition & 1 deletion synapse/handlers/room_member.py
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ async def update_membership_locked(
if block_invite:
raise SynapseError(403, "Invites have been disabled on this server")

# An empty prev_events list is allowed by room version "org.matrix.msc2716v4"
# An empty prev_events list is allowed as long as the auth_event_ids are present
if prev_event_ids is not None:
return await self._local_membership_update(
requester=requester,
Expand Down
98 changes: 63 additions & 35 deletions tests/handlers/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,24 @@ def prepare(self, reactor, clock, hs):

self.requester = create_requester(self.user_id, access_token_id=self.token_id)

def _create_and_persist_member_event(self) -> Tuple[EventBase, EventContext]:
# Create a member event we can use as an auth_event
memberEvent, memberEventContext = self.get_success(
create_event(
self.hs,
room_id=self.room_id,
type="m.room.member",
sender=self.requester.user.to_string(),
state_key=self.requester.user.to_string(),
content={"membership": "join"},
)
)
self.get_success(
self.persist_event_storage.persist_event(memberEvent, memberEventContext)
)

return memberEvent, memberEventContext

def _create_duplicate_event(self, txn_id: str) -> Tuple[EventBase, EventContext]:
"""Create a new event with the given transaction ID. All events produced
by this method will be considered duplicates.
Expand Down Expand Up @@ -158,76 +176,86 @@ def test_duplicated_txn_id_one_call(self):
self.assertEqual(len(events), 2)
self.assertEqual(events[0].event_id, events[1].event_id)

def test_create_empty_prev_events_in_msc2716_room_version(self):
"""Try to create an event without any prev_events (only auth_events).
This is currently only supported in the experimental MSC2716v4+ room versions.
def test_when_empty_prev_events_allowed_create_event_with_empty_prev_events(self):
"""When we set allow_no_prev_events=True, should be able to create a
event without any prev_events (only auth_events).
"""
room_id = self.helper.create_room_as(
self.user_id,
tok=self.access_token,
room_version=RoomVersions.MSC2716v4.identifier,
)

# Create a member event we can use as an auth_event
memberEvent, memberEventContext = self.get_success(
create_event(
self.hs,
room_id=room_id,
type="m.room.member",
sender=self.requester.user.to_string(),
state_key=self.requester.user.to_string(),
content={"membership": "join"},
)
)
self.get_success(
self.persist_event_storage.persist_event(memberEvent, memberEventContext)
)
memberEvent, _ = self._create_and_persist_member_event()

# Try to create the event with empty prev_events (only auth_events)
# Try to create the event with empty prev_events bit with some auth_events
event, _ = self.get_success(
self.handler.create_event(
self.requester,
{
"type": EventTypes.Message,
"room_id": room_id,
"room_id": self.room_id,
"sender": self.requester.user.to_string(),
"content": {"msgtype": "m.text", "body": random_string(5)},
},
# Empty prev_events is the key thing we're testing here
prev_event_ids=[],
# But with some auth_events
auth_event_ids=[memberEvent.event_id],
# Allow no prev_events!
allow_no_prev_events=True,
)
)
self.assertIsNotNone(event)

def test_reject_empty_prev_events_and_auth_events_in_msc2716_room_version(
def test_when_empty_prev_events_not_allowed_reject_event_with_empty_prev_events(
self,
):
"""Try to create an event without any prev_events or auth_events.
Expect an exception to be raised.
This is currently only supported in the experimental MSC2716v4 room versions.
"""When we set allow_no_prev_events=False, shouldn't be able to create a
event without any prev_events even if it has auth_events. Expect an
exception to be raised.
"""
room_id = self.helper.create_room_as(
self.user_id,
tok=self.access_token,
room_version=RoomVersions.MSC2716v4.identifier,
# Create a member event we can use as an auth_event
memberEvent, _ = self._create_and_persist_member_event()

# Try to create the event with empty prev_events but with some auth_events
self.get_failure(
self.handler.create_event(
self.requester,
{
"type": EventTypes.Message,
"room_id": self.room_id,
"sender": self.requester.user.to_string(),
"content": {"msgtype": "m.text", "body": random_string(5)},
},
# Empty prev_events is the key thing we're testing here
prev_event_ids=[],
# But with some auth_events
auth_event_ids=[memberEvent.event_id],
# We expect the test to fail because empty prev_events are not
# allowed here!
allow_no_prev_events=False,
),
AssertionError,
)

def test_when_empty_prev_events_allowed_reject_event_with_empty_prev_events_and_auth_events(
self,
):
"""When we set allow_no_prev_events=True, should be able to create a
event without any prev_events or auth_events. Expect an exception to be
raised.
"""
# Try to create the event with empty prev_events and empty auth_events
self.get_failure(
self.handler.create_event(
self.requester,
{
"type": EventTypes.Message,
"room_id": room_id,
"room_id": self.room_id,
"sender": self.requester.user.to_string(),
"content": {"msgtype": "m.text", "body": random_string(5)},
},
prev_event_ids=[],
# The event should be rejected when there are no auth_events
auth_event_ids=[],
# Allow no prev_events!
allow_no_prev_events=True,
),
AssertionError,
)
Expand Down

0 comments on commit e2928b5

Please sign in to comment.