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

Commit 27c375f

Browse files
authored
Sort child events according to MSC1772 for the spaces summary API. (#9954)
This should help ensure that equivalent results are achieved between homeservers querying for the summary of a space. This implements modified MSC1772 rules, according to MSC2946. The different is that the origin_server_ts of the m.room.create event is not used as a tie-breaker since this might not be known if the homeserver is not part of the room.
1 parent f4833e0 commit 27c375f

File tree

3 files changed

+151
-2
lines changed

3 files changed

+151
-2
lines changed

changelog.d/9954.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Update support for [MSC2946](https://github.com/matrix-org/matrix-doc/pull/2946): Spaces Summary.

synapse/handlers/space_summary.py

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import itertools
1616
import logging
17+
import re
1718
from collections import deque
1819
from typing import TYPE_CHECKING, Iterable, List, Optional, Sequence, Set, Tuple, cast
1920

@@ -226,6 +227,23 @@ async def _summarize_local_room(
226227
suggested_only: bool,
227228
max_children: Optional[int],
228229
) -> Tuple[Sequence[JsonDict], Sequence[JsonDict]]:
230+
"""
231+
Generate a room entry and a list of event entries for a given room.
232+
233+
Args:
234+
requester: The requesting user, or None if this is over federation.
235+
room_id: The room ID to summarize.
236+
suggested_only: True if only suggested children should be returned.
237+
Otherwise, all children are returned.
238+
max_children: The maximum number of children to return for this node.
239+
240+
Returns:
241+
A tuple of:
242+
An iterable of a single value of the room.
243+
244+
An iterable of the sorted children events. This may be limited
245+
to a maximum size or may include all children.
246+
"""
229247
if not await self._is_room_accessible(room_id, requester):
230248
return (), ()
231249

@@ -357,6 +375,18 @@ async def _build_room_entry(self, room_id: str) -> JsonDict:
357375
return room_entry
358376

359377
async def _get_child_events(self, room_id: str) -> Iterable[EventBase]:
378+
"""
379+
Get the child events for a given room.
380+
381+
The returned results are sorted for stability.
382+
383+
Args:
384+
room_id: The room id to get the children of.
385+
386+
Returns:
387+
An iterable of sorted child events.
388+
"""
389+
360390
# look for child rooms/spaces.
361391
current_state_ids = await self._store.get_current_state_ids(room_id)
362392

@@ -370,8 +400,9 @@ async def _get_child_events(self, room_id: str) -> Iterable[EventBase]:
370400
]
371401
)
372402

373-
# filter out any events without a "via" (which implies it has been redacted)
374-
return (e for e in events if _has_valid_via(e))
403+
# filter out any events without a "via" (which implies it has been redacted),
404+
# and order to ensure we return stable results.
405+
return sorted(filter(_has_valid_via, events), key=_child_events_comparison_key)
375406

376407

377408
@attr.s(frozen=True, slots=True)
@@ -397,3 +428,39 @@ def _is_suggested_child_event(edge_event: EventBase) -> bool:
397428
return True
398429
logger.debug("Ignorning not-suggested child %s", edge_event.state_key)
399430
return False
431+
432+
433+
# Order may only contain characters in the range of \x20 (space) to \x7F (~).
434+
_INVALID_ORDER_CHARS_RE = re.compile(r"[^\x20-\x7F]")
435+
436+
437+
def _child_events_comparison_key(child: EventBase) -> Tuple[bool, Optional[str], str]:
438+
"""
439+
Generate a value for comparing two child events for ordering.
440+
441+
The rules for ordering are supposed to be:
442+
443+
1. The 'order' key, if it is valid.
444+
2. The 'origin_server_ts' of the 'm.room.create' event.
445+
3. The 'room_id'.
446+
447+
But we skip step 2 since we may not have any state from the room.
448+
449+
Args:
450+
child: The event for generating a comparison key.
451+
452+
Returns:
453+
The comparison key as a tuple of:
454+
False if the ordering is valid.
455+
The ordering field.
456+
The room ID.
457+
"""
458+
order = child.content.get("order")
459+
# If order is not a string or doesn't meet the requirements, ignore it.
460+
if not isinstance(order, str):
461+
order = None
462+
elif len(order) > 50 or _INVALID_ORDER_CHARS_RE.search(order):
463+
order = None
464+
465+
# Items without an order come last.
466+
return (order is None, order, child.room_id)
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Copyright 2021 The Matrix.org Foundation C.I.C.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
from typing import Any, Optional
15+
from unittest import mock
16+
17+
from synapse.handlers.space_summary import _child_events_comparison_key
18+
19+
from tests import unittest
20+
21+
22+
def _create_event(room_id: str, order: Optional[Any] = None):
23+
result = mock.Mock()
24+
result.room_id = room_id
25+
result.content = {}
26+
if order is not None:
27+
result.content["order"] = order
28+
return result
29+
30+
31+
def _order(*events):
32+
return sorted(events, key=_child_events_comparison_key)
33+
34+
35+
class TestSpaceSummarySort(unittest.TestCase):
36+
def test_no_order_last(self):
37+
"""An event with no ordering is placed behind those with an ordering."""
38+
ev1 = _create_event("!abc:test")
39+
ev2 = _create_event("!xyz:test", "xyz")
40+
41+
self.assertEqual([ev2, ev1], _order(ev1, ev2))
42+
43+
def test_order(self):
44+
"""The ordering should be used."""
45+
ev1 = _create_event("!abc:test", "xyz")
46+
ev2 = _create_event("!xyz:test", "abc")
47+
48+
self.assertEqual([ev2, ev1], _order(ev1, ev2))
49+
50+
def test_order_room_id(self):
51+
"""Room ID is a tie-breaker for ordering."""
52+
ev1 = _create_event("!abc:test", "abc")
53+
ev2 = _create_event("!xyz:test", "abc")
54+
55+
self.assertEqual([ev1, ev2], _order(ev1, ev2))
56+
57+
def test_invalid_ordering_type(self):
58+
"""Invalid orderings are considered the same as missing."""
59+
ev1 = _create_event("!abc:test", 1)
60+
ev2 = _create_event("!xyz:test", "xyz")
61+
62+
self.assertEqual([ev2, ev1], _order(ev1, ev2))
63+
64+
ev1 = _create_event("!abc:test", {})
65+
self.assertEqual([ev2, ev1], _order(ev1, ev2))
66+
67+
ev1 = _create_event("!abc:test", [])
68+
self.assertEqual([ev2, ev1], _order(ev1, ev2))
69+
70+
ev1 = _create_event("!abc:test", True)
71+
self.assertEqual([ev2, ev1], _order(ev1, ev2))
72+
73+
def test_invalid_ordering_value(self):
74+
"""Invalid orderings are considered the same as missing."""
75+
ev1 = _create_event("!abc:test", "foo\n")
76+
ev2 = _create_event("!xyz:test", "xyz")
77+
78+
self.assertEqual([ev2, ev1], _order(ev1, ev2))
79+
80+
ev1 = _create_event("!abc:test", "a" * 51)
81+
self.assertEqual([ev2, ev1], _order(ev1, ev2))

0 commit comments

Comments
 (0)