Skip to content

Commit 7e9160e

Browse files
committed
Upgrade Synapse to 1.19.1
This upgrades the Synapse requirement to 1.19.1 and fixes all backwards incompatibilities that have been introduced. This is mainly related to the [alias semantics change (MSC2432)](matrix-org/matrix-spec-proposals#2432). (cherry picked from commit 14164ec)
1 parent 8189c11 commit 7e9160e

File tree

10 files changed

+210
-174
lines changed

10 files changed

+210
-174
lines changed

raiden/network/transport/matrix/client.py

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from functools import wraps
55
from itertools import repeat
66
from typing import Any, Callable, Container, Dict, Iterable, Iterator, List, Optional, Tuple
7-
from urllib.parse import quote, urlparse
7+
from urllib.parse import quote
88
from uuid import UUID, uuid4
99

1010
import gevent
@@ -54,6 +54,7 @@ def __init__(self, client: "GMatrixClient", room_id: str) -> None:
5454
super().__init__(client, room_id)
5555
self._members: Dict[str, User] = {}
5656
self.aliases: List[str]
57+
self.canonical_alias: str
5758

5859
def get_joined_members(self, force_resync: bool = False) -> List[User]:
5960
""" Return a list of members of this room. """
@@ -81,38 +82,39 @@ def _rmmembers(self, user_id: str) -> None:
8182
self._members.pop(user_id, None)
8283

8384
def __repr__(self) -> str:
84-
return f"<Room id={self.room_id!r} aliases={self.aliases!r}>"
85+
return f"<Room id={self.room_id!r} canonical_alias={self.canonical_alias!r}>"
8586

86-
def update_local_aliases(self) -> bool:
87-
""" Fetch server local aliases for the room.
87+
def update_local_alias(self) -> bool:
88+
""" Fetch the server local canonical alias for the room.
8889
8990
This is an optimization over the general `update_aliases()` method which fetches the
9091
entire room state (which can be large in Raiden) and then discards all non-alias events.
9192
92-
Unfortunately due to a limitation in the Matrix API it's not possible to query for all
93-
aliases of a room. Only aliases for a specific server can be fetched, see:
94-
https://github.com/matrix-org/synapse/issues/6908
93+
With MSC2432[1] implemented only ``m.room.canonical_alias`` events exist.
94+
They represent the server local canonical_alias.
9595
96-
Since in Raiden we always have server local aliases set, this method is sufficient for our
97-
use case.
96+
Since in Raiden broadcast rooms always have a server local alias set, this method is
97+
sufficient for our use case.
98+
99+
[1] https://github.com/matrix-org/matrix-doc/pull/2432
98100
99101
Returns:
100-
boolean: True if the aliases changed, False if not
102+
boolean: True if the canonical_alias changed, False if not
101103
"""
102-
server_name = urlparse(self.client.api.base_url).netloc
103104
changed = False
104105

105106
try:
106107
response = self.client.api.get_room_state_type(
107-
self.room_id, "m.room.aliases", server_name
108+
self.room_id, "m.room.canonical_alias", ""
108109
)
109110
except MatrixRequestError:
110111
return False
111112

112-
if "aliases" in response:
113-
if self.aliases != response["aliases"]:
114-
self.aliases = response["aliases"]
115-
changed = True
113+
server_sent_alias = response.get("alias")
114+
if server_sent_alias is not None and self.canonical_alias != server_sent_alias:
115+
self.canonical_alias = server_sent_alias
116+
changed = True
117+
116118
return changed
117119

118120

@@ -227,6 +229,22 @@ def create_room(
227229
def get_presence(self, user_id: str) -> Dict[str, Any]:
228230
return self._send("GET", f"/presence/{quote(user_id)}/status")
229231

232+
def get_aliases(self, room_id: str) -> Dict[str, Any]:
233+
"""
234+
Perform GET /rooms/{room_id}/aliases.
235+
236+
Requires Synapse >= 1.11.0 which implements the (as of yet) unstable MSC2432 room alias
237+
semantics change.
238+
"""
239+
return self._send(
240+
"GET",
241+
f"/rooms/{room_id}/aliases",
242+
api_path="/_matrix/client/unstable/org.matrix.msc2432",
243+
)
244+
245+
def __repr__(self) -> str:
246+
return f"<GMatrixHttpApi base_url={self.base_url}>"
247+
230248

231249
class GMatrixClient(MatrixClient):
232250
""" Gevent-compliant MatrixClient subclass """
@@ -527,8 +545,8 @@ def _mkroom(self, room_id: str) -> Room:
527545
if room_id not in self.rooms:
528546
self.rooms[room_id] = Room(self, room_id)
529547
room = self.rooms[room_id]
530-
if not room.aliases:
531-
room.update_local_aliases()
548+
if not room.canonical_alias:
549+
room.update_local_alias()
532550
return room
533551

534552
def get_user_presence(self, user_id: str) -> Optional[str]:

raiden/network/transport/matrix/transport.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -810,7 +810,10 @@ def _initialize_first_sync(self) -> None:
810810
self._set_room_id_for_address(partner_address[0], room.room_id)
811811

812812
self.log.debug(
813-
"Found room", room=room, aliases=room.aliases, members=room.get_joined_members()
813+
"Found room",
814+
room=room,
815+
canonical_alias=room.canonical_alias,
816+
members=room.get_joined_members(),
814817
)
815818

816819
def _leave_unexpected_rooms(
@@ -829,7 +832,7 @@ def to_string_representation(partner: Optional[Address]) -> str:
829832
self.log.warning(
830833
"Leaving Room",
831834
reason=reason,
832-
room_aliases=room.aliases,
835+
canonical_alias=room.canonical_alias,
833836
room_id=room.room_id,
834837
partners=[to_string_representation(partner) for partner in partners],
835838
)
@@ -1046,7 +1049,7 @@ def _handle_invite(self, room_id: RoomID, state: dict) -> None:
10461049
self.log.debug(
10471050
"Joined from invite",
10481051
room_id=room_id,
1049-
aliases=room.aliases,
1052+
canonical_alias=room.canonical_alias,
10501053
inviting_address=to_checksum_address(peer_address),
10511054
)
10521055

@@ -1057,7 +1060,7 @@ def _handle_invite(self, room_id: RoomID, state: dict) -> None:
10571060
def _handle_member_join(self, room: Room) -> None:
10581061
if self._is_broadcast_room(room):
10591062
raise AssertionError(
1060-
f"Broadcast room events should be filtered in syncs: {room.aliases}."
1063+
f"Broadcast room events should be filtered in syncs: {room.canonical_alias}."
10611064
f"Joined Broadcast Rooms: {list(self._broadcast_rooms.keys())}"
10621065
f"Should be joined to: {self._config.broadcast_rooms}"
10631066
)
@@ -1104,7 +1107,7 @@ def _handle_text(self, room: Room, message: MatrixMessage) -> List[Message]:
11041107
if self._is_broadcast_room(room):
11051108
# This must not happen. Nodes must not listen on broadcast rooms.
11061109
raise RuntimeError(
1107-
f"Received message in broadcast room {room.aliases[0]}. Sending user: {user}"
1110+
f"Received message in broadcast room {room.canonical_alias}. Sending user: {user}"
11081111
)
11091112

11101113
if not self._address_mgr.is_address_known(peer_address):
@@ -1366,10 +1369,9 @@ def partner_joined(fetched_members: Optional[List[User]]) -> bool:
13661369
return room
13671370

13681371
def _is_broadcast_room(self, room: Room) -> bool:
1369-
return any(
1370-
suffix in room_alias
1371-
for suffix in self._config.broadcast_rooms
1372-
for room_alias in room.aliases
1372+
has_alias = room.canonical_alias is not None
1373+
return has_alias and any(
1374+
suffix in room.canonical_alias for suffix in self._config.broadcast_rooms
13731375
)
13741376

13751377
def _user_presence_changed(self, user: User, _presence: UserPresence) -> None:

raiden/network/transport/matrix/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ def first_login(client: GMatrixClient, signer: Signer, username: str, cap_str: s
648648

649649
# Disabling sync because login is done before the transport is fully
650650
# initialized, i.e. the inventory rooms don't have the callbacks installed.
651-
client.login(username, password, sync=False)
651+
client.login(username, password, sync=False, device_id="raiden")
652652

653653
# Because this is the first login, the display name has to be set, this
654654
# prevents the impersonation metioned above. subsequent calls will reuse

raiden/network/transport/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from raiden.utils.typing import Iterator
1+
from typing import Iterator
22

33

44
def timeout_exponential_backoff(retries: int, timeout: float, maximum: float) -> Iterator[float]:

raiden/tests/integration/network/transport/test_matrix_transport_assumptions.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ def test_assumption_user_goes_offline_if_sync_is_not_called_within_35s(local_mat
170170
assert len(tracker3.address_presence) == 0, msg_no_sync
171171

172172
room: Room = client1.create_room("test", is_public=True)
173-
client2.join_room(room.aliases[0])
174-
client3.join_room(room.aliases[0])
173+
client2.join_room(room.canonical_alias)
174+
client3.join_room(room.canonical_alias)
175175

176176
client1.blocking_sync(timeout_ms=SHORT_TIMEOUT_MS, latency_ms=SHORT_TIMEOUT_MS)
177177
client2.blocking_sync(timeout_ms=SHORT_TIMEOUT_MS, latency_ms=SHORT_TIMEOUT_MS)
@@ -224,7 +224,7 @@ def test_assumption_user_is_online_while_sync_is_blocking(local_matrix_servers):
224224
client2.add_presence_listener(tracker2.presence_listener)
225225

226226
room: Room = client1.create_room("test", is_public=True)
227-
client2.join_room(room.aliases[0])
227+
client2.join_room(room.canonical_alias)
228228

229229
client2.blocking_sync(timeout_ms=SHORT_TIMEOUT_MS, latency_ms=SHORT_TIMEOUT_MS)
230230
client1.blocking_sync(timeout_ms=SHORT_TIMEOUT_MS, latency_ms=SHORT_TIMEOUT_MS)
@@ -312,7 +312,7 @@ def test_assumption_cannot_override_room_alias(local_matrix_servers):
312312
for local_server in local_matrix_servers[1:]:
313313
client = new_client(ignore_messages, ignore_member_join, local_server)
314314
assert public_room.room_id not in client.rooms
315-
client.join_room(public_room.aliases[0])
315+
client.join_room(public_room.canonical_alias)
316316
assert public_room.room_id in client.rooms
317317

318318
alias_on_current_server = f"#{room_alias_prefix}:{local_server.netloc}"

0 commit comments

Comments
 (0)