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

@ -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")),
};