Skip to content

Commit

Permalink
Upgrade Synapse to 1.19.1
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
ulope committed Sep 18, 2020
1 parent 6463c94 commit e57522e
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 99 deletions.
54 changes: 36 additions & 18 deletions raiden/network/transport/matrix/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from functools import wraps
from itertools import repeat
from typing import Any, Callable, Container, Dict, Iterable, Iterator, List, Optional, Tuple
from urllib.parse import quote, urlparse
from urllib.parse import quote
from uuid import UUID, uuid4

import gevent
Expand Down Expand Up @@ -54,6 +54,7 @@ def __init__(self, client: "GMatrixClient", room_id: str) -> None:
super().__init__(client, room_id)
self._members: Dict[str, User] = {}
self.aliases: List[str]
self.canonical_alias: str

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

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

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

try:
response = self.client.api.get_room_state_type(
self.room_id, "m.room.aliases", server_name
self.room_id, "m.room.canonical_alias", ""
)
except MatrixRequestError:
return False

if "aliases" in response:
if self.aliases != response["aliases"]:
self.aliases = response["aliases"]
changed = True
server_sent_alias = response.get("alias")
if server_sent_alias is not None and self.canonical_alias != server_sent_alias:
self.canonical_alias = server_sent_alias
changed = True

return changed


Expand Down Expand Up @@ -235,6 +237,22 @@ def answer(self, room_id: RoomID, answer: Dict[str, str]) -> None:
room_id=room_id, text_content=json.dumps(answer), msgtype="m.call.answer"
)

def get_aliases(self, room_id: str) -> Dict[str, Any]:
"""
Perform GET /rooms/{room_id}/aliases.
Requires Synapse >= 1.11.0 which implements the (as of yet) unstable MSC2432 room alias
semantics change.
"""
return self._send(
"GET",
f"/rooms/{room_id}/aliases",
api_path="/_matrix/client/unstable/org.matrix.msc2432",
)

def __repr__(self) -> str:
return f"<GMatrixHttpApi base_url={self.base_url}>"


class GMatrixClient(MatrixClient):
""" Gevent-compliant MatrixClient subclass """
Expand Down Expand Up @@ -535,8 +553,8 @@ def _mkroom(self, room_id: str) -> Room:
if room_id not in self.rooms:
self.rooms[room_id] = Room(self, room_id)
room = self.rooms[room_id]
if not room.aliases:
room.update_local_aliases()
if not room.canonical_alias:
room.update_local_alias()
return room

def get_user_presence(self, user_id: str) -> Optional[str]:
Expand Down
20 changes: 11 additions & 9 deletions raiden/network/transport/matrix/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -834,7 +834,10 @@ def _initialize_first_sync(self) -> None:
self._set_room_id_for_address(partner_address[0], room.room_id)

self.log.debug(
"Found room", room=room, aliases=room.aliases, members=room.get_joined_members()
"Found room",
room=room,
canonical_alias=room.canonical_alias,
members=room.get_joined_members(),
)

def _leave_unexpected_rooms(
Expand All @@ -853,7 +856,7 @@ def to_string_representation(partner: Optional[Address]) -> str:
self.log.warning(
"Leaving Room",
reason=reason,
room_aliases=room.aliases,
canonical_alias=room.canonical_alias,
room_id=room.room_id,
partners=[to_string_representation(partner) for partner in partners],
)
Expand Down Expand Up @@ -1070,7 +1073,7 @@ def _handle_invite(self, room_id: RoomID, state: dict) -> None:
self.log.debug(
"Joined from invite",
room_id=room_id,
aliases=room.aliases,
canonical_alias=room.canonical_alias,
inviting_address=to_checksum_address(peer_address),
)

Expand All @@ -1081,7 +1084,7 @@ def _handle_invite(self, room_id: RoomID, state: dict) -> None:
def _handle_member_join(self, room: Room) -> None:
if self._is_broadcast_room(room):
raise AssertionError(
f"Broadcast room events should be filtered in syncs: {room.aliases}."
f"Broadcast room events should be filtered in syncs: {room.canonical_alias}."
f"Joined Broadcast Rooms: {list(self._broadcast_rooms.keys())}"
f"Should be joined to: {self._config.broadcast_rooms}"
)
Expand Down Expand Up @@ -1142,7 +1145,7 @@ def _handle_text(self, room: Room, message: MatrixMessage) -> List[Message]:
if self._is_broadcast_room(room):
# This must not happen. Nodes must not listen on broadcast rooms.
raise RuntimeError(
f"Received message in broadcast room {room.aliases[0]}. Sending user: {user}"
f"Received message in broadcast room {room.canonical_alias}. Sending user: {user}"
)

if not self._address_mgr.is_address_known(peer_address):
Expand Down Expand Up @@ -1532,10 +1535,9 @@ def create_web_rtc_channel(self, partner_address: Address) -> None:
del self._web_rtc_manager.address_to_rtc_partners[partner_address]

def _is_broadcast_room(self, room: Room) -> bool:
return any(
suffix in room_alias
for suffix in self._config.broadcast_rooms
for room_alias in room.aliases
has_alias = room.canonical_alias is not None
return has_alias and any(
suffix in room.canonical_alias for suffix in self._config.broadcast_rooms
)

def _user_presence_changed(self, user: User, _presence: UserPresence) -> None:
Expand Down
2 changes: 1 addition & 1 deletion raiden/network/transport/matrix/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ def first_login(client: GMatrixClient, signer: Signer, username: str, cap_str: s

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

# Because this is the first login, the display name has to be set, this
# prevents the impersonation mentioned above. subsequent calls will reuse
Expand Down
2 changes: 1 addition & 1 deletion raiden/network/transport/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from raiden.utils.typing import Iterator
from typing import Iterator


def timeout_exponential_backoff(retries: int, timeout: float, maximum: float) -> Iterator[float]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ def test_assumption_user_goes_offline_if_sync_is_not_called_within_35s(local_mat
assert len(tracker3.address_presence) == 0, msg_no_sync

room: Room = client1.create_room("test", is_public=True)
client2.join_room(room.aliases[0])
client3.join_room(room.aliases[0])
client2.join_room(room.canonical_alias)
client3.join_room(room.canonical_alias)

client1.blocking_sync(timeout_ms=SHORT_TIMEOUT_MS, latency_ms=SHORT_TIMEOUT_MS)
client2.blocking_sync(timeout_ms=SHORT_TIMEOUT_MS, latency_ms=SHORT_TIMEOUT_MS)
Expand Down Expand Up @@ -224,7 +224,7 @@ def test_assumption_user_is_online_while_sync_is_blocking(local_matrix_servers):
client2.add_presence_listener(tracker2.presence_listener)

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

client2.blocking_sync(timeout_ms=SHORT_TIMEOUT_MS, latency_ms=SHORT_TIMEOUT_MS)
client1.blocking_sync(timeout_ms=SHORT_TIMEOUT_MS, latency_ms=SHORT_TIMEOUT_MS)
Expand Down Expand Up @@ -312,7 +312,7 @@ def test_assumption_cannot_override_room_alias(local_matrix_servers):
for local_server in local_matrix_servers[1:]:
client = new_client(ignore_messages, ignore_member_join, local_server)
assert public_room.room_id not in client.rooms
client.join_room(public_room.aliases[0])
client.join_room(public_room.canonical_alias)
assert public_room.room_id in client.rooms

alias_on_current_server = f"#{room_alias_prefix}:{local_server.netloc}"
Expand Down
Loading

0 comments on commit e57522e

Please sign in to comment.