From 3c3062a316d37e9f097f8afcc50d028845eb66eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timo=20K=C3=B6sters?= Date: Wed, 28 Apr 2021 17:52:58 +0200 Subject: [PATCH] improvement: optimize room directory --- src/client_server/directory.rs | 57 +++++++++++++++++----------------- 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/src/client_server/directory.rs b/src/client_server/directory.rs index ae70ec5..018050d 100644 --- a/src/client_server/directory.rs +++ b/src/client_server/directory.rs @@ -209,17 +209,15 @@ pub async fn get_public_rooms_filtered_helper( .map(|room_id| { let room_id = room_id?; - // TODO: Do not load full state? - let state = db.rooms.room_state_full(&room_id)?; - let chunk = PublicRoomsChunk { aliases: Vec::new(), - canonical_alias: state - .get(&(EventType::RoomCanonicalAlias, "".to_owned())) + canonical_alias: db + .rooms + .room_state_get(&room_id, &EventType::RoomCanonicalAlias, "")? .map_or(Ok::<_, Error>(None), |s| { Ok(serde_json::from_value::< Raw, - >(s.content.clone()) + >(s.content) .expect("from_value::> can never fail") .deserialize() .map_err(|_| { @@ -227,11 +225,12 @@ pub async fn get_public_rooms_filtered_helper( })? .alias) })?, - name: state.get(&(EventType::RoomName, "".to_owned())).map_or( - Ok::<_, Error>(None), - |s| { + name: db + .rooms + .room_state_get(&room_id, &EventType::RoomName, "")? + .map_or(Ok::<_, Error>(None), |s| { Ok(serde_json::from_value::>( - s.content.clone(), + s.content, ) .expect("from_value::> can never fail") .deserialize() @@ -240,16 +239,15 @@ pub async fn get_public_rooms_filtered_helper( })? .name() .map(|n| n.to_owned())) - }, - )?, + })?, num_joined_members: (db.rooms.room_members(&room_id).count() as u32).into(), - room_id, - topic: state.get(&(EventType::RoomTopic, "".to_owned())).map_or( - Ok::<_, Error>(None), - |s| { + topic: db + .rooms + .room_state_get(&room_id, &EventType::RoomTopic, "")? + .map_or(Ok::<_, Error>(None), |s| { Ok(Some( serde_json::from_value::>( - s.content.clone(), + s.content, ) .expect("from_value::> can never fail") .deserialize() @@ -258,14 +256,14 @@ pub async fn get_public_rooms_filtered_helper( })? .topic, )) - }, - )?, - world_readable: state - .get(&(EventType::RoomHistoryVisibility, "".to_owned())) + })?, + world_readable: db + .rooms + .room_state_get(&room_id, &EventType::RoomHistoryVisibility, "")? .map_or(Ok::<_, Error>(false), |s| { Ok(serde_json::from_value::< Raw, - >(s.content.clone()) + >(s.content) .expect("from_value::> can never fail") .deserialize() .map_err(|_| { @@ -276,12 +274,13 @@ pub async fn get_public_rooms_filtered_helper( .history_visibility == history_visibility::HistoryVisibility::WorldReadable) })?, - guest_can_join: state - .get(&(EventType::RoomGuestAccess, "".to_owned())) + guest_can_join: db + .rooms + .room_state_get(&room_id, &EventType::RoomGuestAccess, "")? .map_or(Ok::<_, Error>(false), |s| { Ok( serde_json::from_value::>( - s.content.clone(), + s.content, ) .expect("from_value::> can never fail") .deserialize() @@ -292,12 +291,13 @@ pub async fn get_public_rooms_filtered_helper( == guest_access::GuestAccess::CanJoin, ) })?, - avatar_url: state - .get(&(EventType::RoomAvatar, "".to_owned())) + avatar_url: db + .rooms + .room_state_get(&room_id, &EventType::RoomAvatar, "")? .map(|s| { Ok::<_, Error>( serde_json::from_value::>( - s.content.clone(), + s.content, ) .expect("from_value::> can never fail") .deserialize() @@ -310,6 +310,7 @@ pub async fn get_public_rooms_filtered_helper( .transpose()? // url is now an Option so we must flatten .flatten(), + room_id, }; Ok(chunk) })