mirror of
https://github.com/matrix-org/matrix-rust-sdk.git
synced 2026-05-18 13:40:55 -04:00
base: Ensure room name is not empty
Ruma used to handle this during deserialization when the field was an Option. Especially meaningful when computing the room name, where an empty string means the name is not set. Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
This commit is contained in:
committed by
Benjamin Bouvier
parent
ff7aa63c5c
commit
9b732e1895
@@ -350,6 +350,8 @@ impl Room {
|
||||
}
|
||||
|
||||
/// Get the `m.room.name` of this room.
|
||||
///
|
||||
/// The returned string is guaranteed not to be empty.
|
||||
pub fn name(&self) -> Option<String> {
|
||||
self.inner.read().name().map(ToOwned::to_owned)
|
||||
}
|
||||
@@ -1022,7 +1024,8 @@ impl RoomInfo {
|
||||
}
|
||||
|
||||
fn name(&self) -> Option<&str> {
|
||||
Some(&self.base_info.name.as_ref()?.as_original()?.content.name)
|
||||
let name = &self.base_info.name.as_ref()?.as_original()?.content.name;
|
||||
(!name.is_empty()).then_some(name)
|
||||
}
|
||||
|
||||
fn tombstone(&self) -> Option<&RoomTombstoneEventContent> {
|
||||
@@ -1356,6 +1359,19 @@ mod tests {
|
||||
assert_eq!(room.display_name().await.unwrap(), DisplayName::Empty);
|
||||
}
|
||||
|
||||
#[async_test]
|
||||
async fn display_name_for_invited_room_is_empty_if_room_name_empty() {
|
||||
let (_, room) = make_room(RoomState::Invited);
|
||||
|
||||
let room_name = MinimalStateEvent::Original(OriginalMinimalStateEvent {
|
||||
content: RoomNameEventContent::new(String::new()),
|
||||
event_id: None,
|
||||
});
|
||||
room.inner.update(|info| info.base_info.name = Some(room_name));
|
||||
|
||||
assert_eq!(room.display_name().await.unwrap(), DisplayName::Empty);
|
||||
}
|
||||
|
||||
#[async_test]
|
||||
async fn display_name_for_invited_room_uses_canonical_alias_if_available() {
|
||||
let (_, room) = make_room(RoomState::Invited);
|
||||
|
||||
Reference in New Issue
Block a user