Refactor room version support, add default room version config

This commit is contained in:
Nyaaori 2021-11-01 01:58:26 +00:00 committed by Timo Kösters
parent d81216cad7
commit 714873694d
No known key found for this signature in database
GPG key ID: 24DA7517711A2BA4
7 changed files with 96 additions and 28 deletions

View file

@ -1,9 +1,8 @@
use crate::{Result, Ruma};
use crate::{database::DatabaseGuard, Result, Ruma};
use ruma::{
api::client::discovery::get_capabilities::{
self, Capabilities, RoomVersionStability, RoomVersionsCapability,
},
RoomVersionId,
};
use std::collections::BTreeMap;
@ -11,15 +10,26 @@ use std::collections::BTreeMap;
///
/// Get information on the supported feature set and other relevent capabilities of this server.
pub async fn get_capabilities_route(
db: DatabaseGuard,
_body: Ruma<get_capabilities::v3::IncomingRequest>,
) -> Result<get_capabilities::v3::Response> {
let mut available = BTreeMap::new();
available.insert(RoomVersionId::V5, RoomVersionStability::Stable);
available.insert(RoomVersionId::V6, RoomVersionStability::Stable);
if db.globals.allow_unstable_room_versions() {
for room_version in &db.globals.unstable_room_versions {
available.insert(room_version.clone(), RoomVersionStability::Stable);
}
} else {
for room_version in &db.globals.unstable_room_versions {
available.insert(room_version.clone(), RoomVersionStability::Unstable);
}
}
for room_version in &db.globals.stable_room_versions {
available.insert(room_version.clone(), RoomVersionStability::Stable);
}
let mut capabilities = Capabilities::new();
capabilities.room_versions = RoomVersionsCapability {
default: RoomVersionId::V6,
default: db.globals.default_room_version(),
available,
};

View file

@ -492,7 +492,7 @@ async fn join_room_by_id_helper(
federation::membership::prepare_join_event::v1::Request {
room_id,
user_id: sender_user,
ver: &[RoomVersionId::V5, RoomVersionId::V6],
ver: &db.globals.supported_room_versions(),
},
)
.await;
@ -507,11 +507,7 @@ 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::V5 || room_version == RoomVersionId::V6 =>
{
room_version
}
Some(room_version) if db.rooms.is_supported_version(&db, &room_version) => room_version,
_ => return Err(Error::BadServerResponse("Room version is not supported")),
};
@ -828,9 +824,12 @@ pub(crate) async fn invite_helper<'a>(
})
.transpose()?;
// If there was no create event yet, assume we are creating a version 6 room right now
// If there was no create event yet, assume we are creating a room with the default
// version right now
let room_version_id = create_event_content
.map_or(RoomVersionId::V6, |create_event| create_event.room_version);
.map_or(db.globals.default_room_version(), |create_event| {
create_event.room_version
});
let room_version =
RoomVersion::new(&room_version_id).expect("room version is supported");

View file

@ -23,7 +23,7 @@ use ruma::{
},
int,
serde::{CanonicalJsonObject, JsonObject},
RoomAliasId, RoomId, RoomVersionId,
RoomAliasId, RoomId,
};
use serde_json::{json, value::to_raw_value};
use std::{cmp::max, collections::BTreeMap, sync::Arc};
@ -100,7 +100,7 @@ pub async fn create_room_route(
let room_version = match body.room_version.clone() {
Some(room_version) => {
if room_version == RoomVersionId::V5 || room_version == RoomVersionId::V6 {
if db.rooms.is_supported_version(&db, &room_version) {
room_version
} else {
return Err(Error::BadRequest(
@ -109,7 +109,7 @@ pub async fn create_room_route(
));
}
}
None => RoomVersionId::V6,
None => db.globals.default_room_version(),
};
let content = match &body.creation_content {
@ -484,7 +484,7 @@ pub async fn upgrade_room_route(
) -> Result<upgrade_room::v3::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
if !matches!(body.new_version, RoomVersionId::V5 | RoomVersionId::V6) {
if !db.rooms.is_supported_version(&db, &body.new_version) {
return Err(Error::BadRequest(
ErrorKind::UnsupportedRoomVersion,
"This server does not support that room version.",

View file

@ -4,7 +4,7 @@ use std::{
net::{IpAddr, Ipv4Addr},
};
use ruma::ServerName;
use ruma::{RoomVersionId, ServerName};
use serde::{de::IgnoredAny, Deserialize};
use tracing::warn;
@ -46,6 +46,10 @@ pub struct Config {
pub allow_federation: bool,
#[serde(default = "true_fn")]
pub allow_room_creation: bool,
#[serde(default = "true_fn")]
pub allow_unstable_room_versions: bool,
#[serde(default = "default_default_room_version")]
pub default_room_version: RoomVersionId,
#[serde(default = "false_fn")]
pub allow_jaeger: bool,
#[serde(default = "false_fn")]
@ -246,3 +250,8 @@ fn default_log() -> String {
fn default_turn_ttl() -> u64 {
60 * 60 * 24
}
// I know, it's a great name
fn default_default_room_version() -> RoomVersionId {
RoomVersionId::V6
}

View file

@ -4,7 +4,8 @@ use ruma::{
client::sync::sync_events,
federation::discovery::{ServerSigningKeys, VerifyKey},
},
DeviceId, EventId, MilliSecondsSinceUnixEpoch, RoomId, ServerName, ServerSigningKeyId, UserId,
DeviceId, EventId, MilliSecondsSinceUnixEpoch, RoomId, RoomVersionId, ServerName,
ServerSigningKeyId, UserId,
};
use std::{
collections::{BTreeMap, HashMap},
@ -41,6 +42,8 @@ pub struct Globals {
jwt_decoding_key: Option<jsonwebtoken::DecodingKey<'static>>,
federation_client: reqwest::Client,
default_client: reqwest::Client,
pub stable_room_versions: Vec<RoomVersionId>,
pub unstable_room_versions: Vec<RoomVersionId>,
pub(super) server_signingkeys: Arc<dyn Tree>,
pub bad_event_ratelimiter: Arc<RwLock<HashMap<Box<EventId>, RateLimitState>>>,
pub bad_signature_ratelimiter: Arc<RwLock<HashMap<Vec<String>, RateLimitState>>>,
@ -145,6 +148,11 @@ impl Globals {
})
.build()?;
// Supported and stable room versions
let stable_room_versions = vec![RoomVersionId::V6];
// Experimental, partially supported room versions
let unstable_room_versions = vec![RoomVersionId::V5];
let s = Self {
globals,
config,
@ -162,6 +170,8 @@ impl Globals {
default_client,
server_signingkeys,
jwt_decoding_key,
stable_room_versions,
unstable_room_versions,
bad_event_ratelimiter: Arc::new(RwLock::new(HashMap::new())),
bad_signature_ratelimiter: Arc::new(RwLock::new(HashMap::new())),
servername_ratelimiter: Arc::new(RwLock::new(HashMap::new())),
@ -232,6 +242,22 @@ impl Globals {
self.config.allow_room_creation
}
pub fn allow_unstable_room_versions(&self) -> bool {
self.config.allow_unstable_room_versions
}
pub fn default_room_version(&self) -> RoomVersionId {
if self
.supported_room_versions()
.contains(&self.config.default_room_version.clone())
{
self.config.default_room_version.clone()
} else {
error!("Room version in config isn't supported, falling back to Version 6");
RoomVersionId::V6
}
}
pub fn trusted_servers(&self) -> &[Box<ServerName>] {
&self.config.trusted_servers
}
@ -268,6 +294,19 @@ impl Globals {
&self.config.emergency_password
}
pub fn supported_room_versions(&self) -> Vec<RoomVersionId> {
let mut room_versions: Vec<RoomVersionId> = vec![];
self.stable_room_versions
.iter()
.for_each(|room_version| room_versions.push(room_version.clone()));
if self.allow_unstable_room_versions() {
self.unstable_room_versions
.iter()
.for_each(|room_version| room_versions.push(room_version.clone()));
};
room_versions
}
/// TODO: the key valid until timestamp is only honored in room version > 4
/// Remove the outdated keys and insert the new ones.
///

View file

@ -133,6 +133,12 @@ pub struct Rooms {
}
impl Rooms {
/// Returns true if a given room version is supported
#[tracing::instrument(skip(self, db))]
pub fn is_supported_version(&self, db: &Database, room_version: &RoomVersionId) -> bool {
db.globals.supported_room_versions().contains(room_version)
}
/// Builds a StateMap by iterating over all keys that start
/// with state_hash, this gives the full state for the given state_hash.
#[tracing::instrument(skip(self))]
@ -1839,9 +1845,13 @@ impl Rooms {
})
.transpose()?;
// If there was no create event yet, assume we are creating a version 6 room right now
// If there was no create event yet, assume we are creating a room with the default
// version right now
let room_version_id = create_event_content
.map_or(RoomVersionId::V6, |create_event| create_event.room_version);
.map_or(db.globals.default_room_version(), |create_event| {
create_event.room_version
});
let room_version = RoomVersion::new(&room_version_id).expect("room version is supported");
let auth_events =
@ -2672,9 +2682,7 @@ impl Rooms {
let (make_leave_response, remote_server) = make_leave_response_and_server?;
let room_version_id = match make_leave_response.room_version {
Some(version) if version == RoomVersionId::V5 || version == RoomVersionId::V6 => {
version
}
Some(version) if self.is_supported_version(&db, &version) => version,
_ => return Err(Error::BadServerResponse("Room version is not supported")),
};

View file

@ -2639,9 +2639,12 @@ pub async fn create_join_event_template_route(
})
.transpose()?;
// If there was no create event yet, assume we are creating a version 6 room right now
let room_version_id =
create_event_content.map_or(RoomVersionId::V6, |create_event| create_event.room_version);
// If there was no create event yet, assume we are creating a room with the default version
// right now
let room_version_id = create_event_content
.map_or(db.globals.default_room_version(), |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) {
@ -2943,7 +2946,7 @@ pub async fn create_invite_route(
acl_check(sender_servername, &body.room_id, &db)?;
if body.room_version != RoomVersionId::V5 && body.room_version != RoomVersionId::V6 {
if !db.rooms.is_supported_version(&db, &body.room_version) {
return Err(Error::BadRequest(
ErrorKind::IncompatibleRoomVersion {
room_version: body.room_version.clone(),