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 5, 2024
1 parent abecbf8 commit ba5ba66
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 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.

1 change: 1 addition & 0 deletions bindings/matrix-sdk-ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ url = { workspace = true }
zeroize = { workspace = true }
uuid = { version = "1.4.1", features = ["v4"] }
language-tags = "0.3.2"
regex = "1.11.1"

[target.'cfg(target_os = "android")'.dependencies]
paranoid-android = "0.2.1"
Expand Down
15 changes: 15 additions & 0 deletions bindings/matrix-sdk-ffi/src/room_alias.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
use regex::Regex;
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.
#[matrix_sdk_ffi_macros::export]
fn room_alias_from_room_display_name(room_name: String) -> String {
let whitespace_regex = Regex::new(r"\s+").unwrap();
let symbol_regex = Regex::new(r"[!#$&'()*+,/:;=?@\[\]]+").unwrap();

// Replace whitespaces with `-`
let sanitised = whitespace_regex.replace_all(&room_name, "-");
// Remove problematic symbols
let sanitised = symbol_regex.replace_all(&sanitised, "");
// Lowercased
sanitised.to_lowercase()
}

0 comments on commit ba5ba66

Please sign in to comment.