Cleanup rooms.rs, globals.rs, and pdu.rs

This commit is contained in:
Nyaaori 2021-11-05 20:47:11 +00:00
parent 25065a58e8
commit 04fcac20b3
No known key found for this signature in database
GPG key ID: E7819C3ED4D1F82E
3 changed files with 19 additions and 26 deletions

View file

@ -144,7 +144,7 @@ impl Globals {
RoomVersionId::Version5, RoomVersionId::Version5,
]; ];
let s = Self { let mut s = Self {
globals, globals,
config, config,
keypair: Arc::new(keypair), keypair: Arc::new(keypair),
@ -169,6 +169,14 @@ impl Globals {
fs::create_dir_all(s.get_media_folder())?; fs::create_dir_all(s.get_media_folder())?;
if !s
.supported_room_versions()
.contains(&s.config.default_room_version)
{
error!("Room version in config isn't supported, falling back to Version 6");
s.config.default_room_version = RoomVersionId::Version6;
};
Ok(s) Ok(s)
} }
@ -233,15 +241,7 @@ impl Globals {
} }
pub fn default_room_version(&self) -> RoomVersionId { 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() self.config.default_room_version.clone()
} else {
error!("Room version in config isn't supported, falling back to Version 6");
RoomVersionId::Version6
}
} }
pub fn trusted_servers(&self) -> &[Box<ServerName>] { pub fn trusted_servers(&self) -> &[Box<ServerName>] {
@ -278,13 +278,9 @@ impl Globals {
pub fn supported_room_versions(&self) -> Vec<RoomVersionId> { pub fn supported_room_versions(&self) -> Vec<RoomVersionId> {
let mut room_versions: Vec<RoomVersionId> = vec![]; let mut room_versions: Vec<RoomVersionId> = vec![];
self.stable_room_versions room_versions.extend(self.stable_room_versions.clone());
.iter()
.for_each(|room_version| room_versions.push(room_version.clone()));
if self.allow_unstable_room_versions() { if self.allow_unstable_room_versions() {
self.unstable_room_versions room_versions.extend(self.unstable_room_versions.clone());
.iter()
.for_each(|room_version| room_versions.push(room_version.clone()));
}; };
room_versions room_versions
} }

View file

@ -3454,10 +3454,8 @@ impl Rooms {
/// Returns the room's version. /// Returns the room's version.
#[tracing::instrument(skip(self))] #[tracing::instrument(skip(self))]
pub fn get_room_version(&self, room_id: &RoomId) -> RoomVersionId { pub fn get_room_version(&self, room_id: &RoomId) -> Result<RoomVersionId> {
let create_event = self let create_event = self.room_state_get(room_id, &EventType::RoomCreate, "")?;
.room_state_get(room_id, &EventType::RoomCreate, "")
.unwrap();
let create_event_content: Option<RoomCreateEventContent> = create_event let create_event_content: Option<RoomCreateEventContent> = create_event
.as_ref() .as_ref()
@ -3467,11 +3465,10 @@ impl Rooms {
Error::bad_database("Invalid create event in db.") Error::bad_database("Invalid create event in db.")
}) })
}) })
.transpose() .transpose()?;
.unwrap(); let room_version = create_event_content
create_event_content
.map(|create_event| create_event.room_version) .map(|create_event| create_event.room_version)
.expect("Invalid room version") .ok_or_else(|| Error::BadDatabase("Invalid room version"))?;
Ok(room_version)
} }
} }

View file

@ -348,7 +348,7 @@ pub(crate) fn gen_event_id_canonical_json(
let event_id = EventId::try_from(&*format!( let event_id = EventId::try_from(&*format!(
"${}", "${}",
// Anything higher than version3 behaves the same // Anything higher than version3 behaves the same
ruma::signatures::reference_hash(&value, &room_version_id) ruma::signatures::reference_hash(&value, &room_version_id?)
.expect("ruma can calculate reference hashes") .expect("ruma can calculate reference hashes")
)) ))
.expect("ruma's reference hashes are valid event ids"); .expect("ruma's reference hashes are valid event ids");