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

Add rooms.room_version column #6729

Merged
merged 13 commits into from
Jan 27, 2020
Merged
41 changes: 24 additions & 17 deletions synapse/federation/federation_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import copy
import itertools
import logging
from typing import Dict, Iterable

from prometheus_client import Counter

Expand Down Expand Up @@ -404,7 +405,13 @@ def _try_destination_list(self, description, destinations, callback):
raise SynapseError(502, "Failed to %s via any server" % (description,))

def make_membership_event(
self, destinations, room_id, user_id, membership, content, params
self,
destinations: Iterable[str],
room_id: str,
user_id: str,
membership: str,
content: dict,
params: Dict[str, str],
):
"""
Creates an m.room.member event, with context, without participating in the room.
Expand All @@ -417,21 +424,20 @@ def make_membership_event(
Note that this does not append any events to any graphs.

Args:
destinations (Iterable[str]): Candidate homeservers which are probably
destinations: Candidate homeservers which are probably
participating in the room.
room_id (str): The room in which the event will happen.
user_id (str): The user whose membership is being evented.
membership (str): The "membership" property of the event. Must be
one of "join" or "leave".
content (dict): Any additional data to put into the content field
of the event.
params (dict[str, str|Iterable[str]]): Query parameters to include in the
request.
room_id: The room in which the event will happen.
user_id: The user whose membership is being evented.
membership: The "membership" property of the event. Must be one of
"join" or "leave".
content: Any additional data to put into the content field of the
event.
params: Query parameters to include in the request.
Return:
Deferred[tuple[str, FrozenEvent, int]]: resolves to a tuple of
`(origin, event, event_format)` where origin is the remote
homeserver which generated the event, and event_format is one of
`synapse.api.room_versions.EventFormatVersions`.
Deferred[Tuple[str, FrozenEvent, RoomVersion]]: resolves to a tuple of
`(origin, event, room_version)` where origin is the remote
homeserver which generated the event, and room_version is the
version of the room.

Fails with a ``SynapseError`` if the chosen remote server
returns a 300/400 code.
Expand All @@ -453,8 +459,9 @@ def send_request(destination):

# Note: If not supplied, the room version may be either v1 or v2,
# however either way the event format version will be v1.
room_version = ret.get("room_version", RoomVersions.V1.identifier)
event_format = room_version_to_event_format(room_version)
room_version_id = ret.get("room_version", RoomVersions.V1.identifier)
room_version = KNOWN_ROOM_VERSIONS.get(room_version_id)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this could conceivably return None? in which case that should be documented in the docstring?

Or it should be changed to KNOWN_ROOM_VERSIONS[room_version_id]

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It now raises an UnsupportedRoomVersionError

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could probably do with putting that in the docstring too.

event_format = room_version_to_event_format(room_version_id)

pdu_dict = ret.get("event", None)
if not isinstance(pdu_dict, dict):
Expand All @@ -478,7 +485,7 @@ def send_request(destination):
event_dict=pdu_dict,
)

return (destination, ev, event_format)
return (destination, ev, room_version)

return self._try_destination_list(
"make_" + membership, destinations, send_request
Expand Down
16 changes: 11 additions & 5 deletions synapse/handlers/federation.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
from synapse.api.room_versions import KNOWN_ROOM_VERSIONS, RoomVersions
from synapse.crypto.event_signing import compute_event_signature
from synapse.event_auth import auth_types_for_event
from synapse.events import EventBase
from synapse.events import EventBase, room_version_to_event_format
from synapse.events.snapshot import EventContext
from synapse.events.validator import EventValidator
from synapse.logging.context import (
Expand Down Expand Up @@ -1186,7 +1186,7 @@ def do_invite_join(self, target_hosts, room_id, joinee, content):
"""
logger.debug("Joining %s to %s", joinee, room_id)

origin, event, event_format_version = yield self._make_and_verify_event(
origin, event, room_version = yield self._make_and_verify_event(
target_hosts,
room_id,
joinee,
Expand Down Expand Up @@ -1214,6 +1214,8 @@ def do_invite_join(self, target_hosts, room_id, joinee, content):
target_hosts.insert(0, origin)
except ValueError:
pass

event_format_version = room_version_to_event_format(room_version.identifier)
ret = yield self.federation_client.send_join(
target_hosts, event, event_format_version
)
Expand Down Expand Up @@ -1486,7 +1488,7 @@ def on_invite_request(self, origin, pdu):

@defer.inlineCallbacks
def do_remotely_reject_invite(self, target_hosts, room_id, user_id, content):
origin, event, event_format_version = yield self._make_and_verify_event(
origin, event, room_version = yield self._make_and_verify_event(
target_hosts, room_id, user_id, "leave", content=content
)
# Mark as outlier as we don't have any state for this event; we're not
Expand All @@ -1513,7 +1515,11 @@ def do_remotely_reject_invite(self, target_hosts, room_id, user_id, content):
def _make_and_verify_event(
self, target_hosts, room_id, user_id, membership, content={}, params=None
):
origin, event, format_ver = yield self.federation_client.make_membership_event(
(
origin,
event,
room_version,
) = yield self.federation_client.make_membership_event(
target_hosts, room_id, user_id, membership, content, params=params
)

Expand All @@ -1525,7 +1531,7 @@ def _make_and_verify_event(
assert event.user_id == user_id
assert event.state_key == user_id
assert event.room_id == room_id
return origin, event, format_ver
return origin, event, room_version

@defer.inlineCallbacks
@log_function
Expand Down