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

Commit

Permalink
Merge pull request #4447 from matrix-org/erikj/msc_1813
Browse files Browse the repository at this point in the history
Implement MSC 1813 - Add room version to make APIs
  • Loading branch information
erikjohnston authored Jan 25, 2019
2 parents 829a7b2 + 62514bb commit 28c21cd
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 7 deletions.
1 change: 1 addition & 0 deletions changelog.d/4447.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add infrastructure to support different event formats
18 changes: 17 additions & 1 deletion synapse/events/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import six

from synapse.api.constants import EventFormatVersions
from synapse.api.constants import KNOWN_ROOM_VERSIONS, EventFormatVersions
from synapse.util.caches import intern_dict
from synapse.util.frozenutils import freeze

Expand Down Expand Up @@ -240,3 +240,19 @@ def __repr__(self):
self.get("type", None),
self.get("state_key", None),
)


def room_version_to_event_format(room_version):
"""Converts a room version string to the event format
Args:
room_version (str)
Returns:
int
"""
if room_version not in KNOWN_ROOM_VERSIONS:
# We should have already checked version, so this should not happen
raise RuntimeError("Unrecognized room version %s" % (room_version,))

return EventFormatVersions.V1
15 changes: 11 additions & 4 deletions synapse/federation/federation_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
SynapseError,
)
from synapse.crypto.event_signing import add_hashes_and_signatures
from synapse.events import room_version_to_event_format
from synapse.federation.federation_base import FederationBase, event_from_pdu_json
from synapse.util import logcontext, unwrapFirstError
from synapse.util.caches.expiringcache import ExpiringCache
Expand Down Expand Up @@ -557,9 +558,10 @@ def make_membership_event(self, destinations, room_id, user_id, membership,
params (dict[str, str|Iterable[str]]): Query parameters to include in the
request.
Return:
Deferred[tuple[str, FrozenEvent]]: resolves to a tuple of `origin`
and event where origin is the remote homeserver which generated
the event.
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.constants.EventFormatVersions`.
Fails with a ``SynapseError`` if the chosen remote server
returns a 300/400 code.
Expand All @@ -579,6 +581,11 @@ def send_request(destination):
destination, room_id, user_id, membership, params,
)

# 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)
event_format = room_version_to_event_format(room_version)

pdu_dict = ret.get("event", None)
if not isinstance(pdu_dict, dict):
raise InvalidResponseError("Bad 'event' field in response")
Expand Down Expand Up @@ -607,7 +614,7 @@ def send_request(destination):
ev = builder.build()

defer.returnValue(
(destination, ev)
(destination, ev, event_format)
)

return self._try_destination_list(
Expand Down
8 changes: 7 additions & 1 deletion synapse/federation/federation_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,14 @@ def on_make_leave_request(self, origin, room_id, user_id):
origin_host, _ = parse_server_name(origin)
yield self.check_server_matches_acl(origin_host, room_id)
pdu = yield self.handler.on_make_leave_request(room_id, user_id)

room_version = yield self.store.get_room_version(room_id)

time_now = self._clock.time_msec()
defer.returnValue({"event": pdu.get_pdu_json(time_now)})
defer.returnValue({
"event": pdu.get_pdu_json(time_now),
"room_version": room_version,
})

@defer.inlineCallbacks
def on_send_leave_request(self, origin, content):
Expand Down
2 changes: 1 addition & 1 deletion synapse/handlers/federation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1336,7 +1336,7 @@ def do_remotely_reject_invite(self, target_hosts, room_id, user_id):
@defer.inlineCallbacks
def _make_and_verify_event(self, target_hosts, room_id, user_id, membership,
content={}, params=None):
origin, pdu = yield self.federation_client.make_membership_event(
origin, pdu, _ = yield self.federation_client.make_membership_event(
target_hosts,
room_id,
user_id,
Expand Down

0 comments on commit 28c21cd

Please sign in to comment.