From 918df9ca6d36c9fccb34c179459d12d06d9fb5e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timo=20K=C3=B6sters?= Date: Wed, 21 Jul 2021 11:29:13 +0200 Subject: [PATCH] feat: support room version 5 --- src/client_server/capabilities.rs | 1 + src/client_server/membership.rs | 11 ++++++++--- src/client_server/room.rs | 5 ++++- src/database/rooms.rs | 6 +++++- src/pdu.rs | 1 + src/server_server.rs | 33 +++++++++++++++---------------- 6 files changed, 35 insertions(+), 22 deletions(-) diff --git a/src/client_server/capabilities.rs b/src/client_server/capabilities.rs index 8740928d..65c88795 100644 --- a/src/client_server/capabilities.rs +++ b/src/client_server/capabilities.rs @@ -23,6 +23,7 @@ pub async fn get_capabilities_route( _body: Ruma, ) -> ConduitResult { let mut available = BTreeMap::new(); + available.insert(RoomVersionId::Version5, RoomVersionStability::Stable); available.insert(RoomVersionId::Version6, RoomVersionStability::Stable); let mut capabilities = Capabilities::new(); diff --git a/src/client_server/membership.rs b/src/client_server/membership.rs index e17a4d70..52bb18c4 100644 --- a/src/client_server/membership.rs +++ b/src/client_server/membership.rs @@ -511,7 +511,7 @@ async fn join_room_by_id_helper( federation::membership::create_join_event_template::v1::Request { room_id, user_id: sender_user, - ver: &[RoomVersionId::Version6], + ver: &[RoomVersionId::Version5, RoomVersionId::Version6], }, ) .await; @@ -526,7 +526,12 @@ async fn join_room_by_id_helper( let (make_join_response, remote_server) = make_join_response_and_server?; let room_version = match make_join_response.room_version { - Some(room_version) if room_version == RoomVersionId::Version6 => room_version, + Some(room_version) + if room_version == RoomVersionId::Version5 + || room_version == RoomVersionId::Version6 => + { + room_version + } _ => return Err(Error::BadServerResponse("Room version is not supported")), }; @@ -940,7 +945,7 @@ pub async fn invite_helper<'a>( create_invite::v2::Request { room_id: room_id.clone(), event_id: ruma::event_id!("$receivingservershouldsetthis"), - room_version: RoomVersionId::Version6, + room_version: room_version_id, event: PduEvent::convert_to_outgoing_federation_event(pdu_json), invite_room_state, }, diff --git a/src/client_server/room.rs b/src/client_server/room.rs index 6e271308..1d41fd8a 100644 --- a/src/client_server/room.rs +++ b/src/client_server/room.rs @@ -346,7 +346,10 @@ pub async fn upgrade_room_route( ) -> ConduitResult { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); - if !matches!(body.new_version, RoomVersionId::Version6) { + if !matches!( + body.new_version, + RoomVersionId::Version5 | RoomVersionId::Version6 + ) { return Err(Error::BadRequest( ErrorKind::UnsupportedRoomVersion, "This server does not support that room version.", diff --git a/src/database/rooms.rs b/src/database/rooms.rs index aad691b4..f0ec683d 100644 --- a/src/database/rooms.rs +++ b/src/database/rooms.rs @@ -1954,7 +1954,11 @@ impl Rooms { let (make_leave_response, remote_server) = make_leave_response_and_server?; let room_version_id = match make_leave_response.room_version { - Some(id @ RoomVersionId::Version6) => id, + Some(version) + if version == RoomVersionId::Version5 || version == RoomVersionId::Version6 => + { + version + } _ => return Err(Error::BadServerResponse("Room version is not supported")), }; diff --git a/src/pdu.rs b/src/pdu.rs index a72f04d2..c449eb89 100644 --- a/src/pdu.rs +++ b/src/pdu.rs @@ -328,6 +328,7 @@ pub(crate) fn gen_event_id_canonical_json( let event_id = EventId::try_from(&*format!( "${}", + // Anything higher than version3 behaves the same ruma::signatures::reference_hash(&value, &RoomVersionId::Version6) .expect("ruma can calculate reference hashes") )) diff --git a/src/server_server.rs b/src/server_server.rs index f666188b..f725dce4 100644 --- a/src/server_server.rs +++ b/src/server_server.rs @@ -1967,15 +1967,6 @@ pub fn create_join_event_template_route( )); } - if !body.ver.contains(&RoomVersionId::Version6) { - return Err(Error::BadRequest( - ErrorKind::IncompatibleRoomVersion { - room_version: RoomVersionId::Version6, - }, - "Room version not supported.", - )); - } - let prev_events = db .rooms .get_pdu_leaves(&body.room_id)? @@ -2006,12 +1997,19 @@ pub fn create_join_event_template_route( }; // If there was no create event yet, assume we are creating a version 6 room right now - let room_version = RoomVersion::new( - &create_event_content.map_or(RoomVersionId::Version6, |create_event| { - create_event.room_version - }), - ) - .expect("room version is supported"); + let room_version_id = create_event_content.map_or(RoomVersionId::Version6, |create_event| { + create_event.room_version + }); + let room_version = RoomVersion::new(&room_version_id).expect("room version is supported"); + + if !body.ver.contains(&room_version_id) { + return Err(Error::BadRequest( + ErrorKind::IncompatibleRoomVersion { + room_version: room_version_id, + }, + "Room version not supported.", + )); + } let content = serde_json::to_value(MemberEventContent { avatar_url: None, @@ -2108,7 +2106,7 @@ pub fn create_join_event_template_route( ); Ok(create_join_event_template::v1::Response { - room_version: Some(RoomVersionId::Version6), + room_version: Some(room_version_id), event: serde_json::from_value::>( serde_json::to_value(pdu_json).expect("CanonicalJson is valid serde_json::Value"), ) @@ -2238,7 +2236,8 @@ pub async fn create_invite_route( return Err(Error::bad_config("Federation is disabled.")); } - if body.room_version < RoomVersionId::Version6 { + if body.room_version != RoomVersionId::Version5 && body.room_version != RoomVersionId::Version6 + { return Err(Error::BadRequest( ErrorKind::IncompatibleRoomVersion { room_version: body.room_version.clone(),