Skip to content

Commit

Permalink
feat(ffi): add room display name to room alias transformation
Browse files Browse the repository at this point in the history
  • Loading branch information
jmartinesp committed Nov 6, 2024
1 parent bb2d19a commit fbc914f
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 13 deletions.
23 changes: 12 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion bindings/matrix-sdk-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ mod notification;
mod notification_settings;
mod platform;
mod room;
mod room_alias;
mod room_directory_search;
mod room_info;
mod room_list;
Expand All @@ -30,7 +31,6 @@ mod timeline_event_filter;
mod tracing;
mod utils;
mod widget;
mod room_alias;

use async_compat::TOKIO1 as RUNTIME;
use matrix_sdk::ruma::events::room::{
Expand Down
9 changes: 8 additions & 1 deletion bindings/matrix-sdk-ffi/src/room_alias.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
use matrix_sdk::DisplayName;
use ruma::RoomAliasId;

/// Verifies the passed `String` matches the expected room alias format.
#[matrix_sdk_ffi_macros::export]
fn is_room_alias_format_valid(alias: String) -> bool {
RoomAliasId::parse(alias).is_ok()
}
}

/// Transforms a Room's display name into a valid room alias name.
#[matrix_sdk_ffi_macros::export]
fn room_alias_name_from_room_display_name(room_name: String) -> String {
DisplayName::Named(room_name).to_room_alias_name()
}
1 change: 1 addition & 0 deletions crates/matrix-sdk-base/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ tokio = { workspace = true }
thiserror = { workspace = true }
tracing = { workspace = true }
uniffi = { workspace = true, optional = true }
regex = "1.11.1"

[dev-dependencies]
assert_matches = { workspace = true }
Expand Down
54 changes: 54 additions & 0 deletions crates/matrix-sdk-base/src/rooms/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub use normal::{
Room, RoomHero, RoomInfo, RoomInfoNotableUpdate, RoomInfoNotableUpdateReasons, RoomState,
RoomStateFilter,
};
use regex::Regex;
use ruma::{
assign,
events::{
Expand Down Expand Up @@ -64,6 +65,38 @@ pub enum DisplayName {
Empty,
}

const WHITESPACE_REGEX: &str = r"\s+";
const INVALID_SYMBOLS_REGEX: &str = r"[#,:]+";

impl DisplayName {
/// Transforms the current display name into the name part of a
/// `RoomAliasId`.
pub fn to_room_alias_name(&self) -> String {
let room_name = match self {
Self::Named(name) => name,
Self::Aliased(name) => name,
Self::Calculated(name) => name,
Self::EmptyWas(name) => name,
Self::Empty => "",
};

let whitespace_regex =
Regex::new(WHITESPACE_REGEX).expect("`WHITESPACE_REGEX` should be valid");
let symbol_regex =
Regex::new(INVALID_SYMBOLS_REGEX).expect("`INVALID_SYMBOLS_REGEX` should be valid");

// Replace whitespaces with `-`
let sanitised = whitespace_regex.replace_all(room_name, "-");
// Remove non-ASCII characters and ASCII control characters
let sanitised =
String::from_iter(sanitised.chars().filter(|c| c.is_ascii() && !c.is_ascii_control()));
// Remove other problematic ASCII symbols
let sanitised = symbol_regex.replace_all(&sanitised, "");
// Lowercased
sanitised.to_lowercase()
}
}

impl fmt::Display for DisplayName {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Expand Down Expand Up @@ -541,6 +574,7 @@ mod tests {
use ruma::events::tag::{TagInfo, TagName, Tags};

use super::{BaseRoomInfo, RoomNotableTags};
use crate::DisplayName;

#[test]
fn test_handle_notable_tags_favourite() {
Expand Down Expand Up @@ -571,4 +605,24 @@ mod tests {
base_room_info.handle_notable_tags(&tags);
assert!(base_room_info.notable_tags.contains(RoomNotableTags::LOW_PRIORITY).not());
}

#[test]
fn test_room_alias_from_room_display_name_lowercases() {
assert_eq!("roomalias", DisplayName::Named("RoomAlias".to_owned()).to_room_alias_name());
}

#[test]
fn test_room_alias_from_room_display_name_removes_whitespace() {
assert_eq!("room-alias", DisplayName::Named("Room Alias".to_owned()).to_room_alias_name());
}

#[test]
fn test_room_alias_from_room_display_name_removes_non_ascii_symbols() {
assert_eq!("roomalias", DisplayName::Named("Room±Alias√".to_owned()).to_room_alias_name());
}

#[test]
fn test_room_alias_from_room_display_name_removes_invalid_ascii_symbols() {
assert_eq!("roomalias", DisplayName::Named("#Room,Alias:".to_owned()).to_room_alias_name());
}
}

0 comments on commit fbc914f

Please sign in to comment.