Skip to content

Commit fbc914f

Browse files
committed
feat(ffi): add room display name to room alias transformation
1 parent bb2d19a commit fbc914f

File tree

5 files changed

+76
-13
lines changed

5 files changed

+76
-13
lines changed

Cargo.lock

Lines changed: 12 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bindings/matrix-sdk-ffi/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ mod notification;
1616
mod notification_settings;
1717
mod platform;
1818
mod room;
19+
mod room_alias;
1920
mod room_directory_search;
2021
mod room_info;
2122
mod room_list;
@@ -30,7 +31,6 @@ mod timeline_event_filter;
3031
mod tracing;
3132
mod utils;
3233
mod widget;
33-
mod room_alias;
3434

3535
use async_compat::TOKIO1 as RUNTIME;
3636
use matrix_sdk::ruma::events::room::{
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1+
use matrix_sdk::DisplayName;
12
use ruma::RoomAliasId;
23

34
/// Verifies the passed `String` matches the expected room alias format.
45
#[matrix_sdk_ffi_macros::export]
56
fn is_room_alias_format_valid(alias: String) -> bool {
67
RoomAliasId::parse(alias).is_ok()
7-
}
8+
}
9+
10+
/// Transforms a Room's display name into a valid room alias name.
11+
#[matrix_sdk_ffi_macros::export]
12+
fn room_alias_name_from_room_display_name(room_name: String) -> String {
13+
DisplayName::Named(room_name).to_room_alias_name()
14+
}

crates/matrix-sdk-base/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ tokio = { workspace = true }
6767
thiserror = { workspace = true }
6868
tracing = { workspace = true }
6969
uniffi = { workspace = true, optional = true }
70+
regex = "1.11.1"
7071

7172
[dev-dependencies]
7273
assert_matches = { workspace = true }

crates/matrix-sdk-base/src/rooms/mod.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub use normal::{
1515
Room, RoomHero, RoomInfo, RoomInfoNotableUpdate, RoomInfoNotableUpdateReasons, RoomState,
1616
RoomStateFilter,
1717
};
18+
use regex::Regex;
1819
use ruma::{
1920
assign,
2021
events::{
@@ -64,6 +65,38 @@ pub enum DisplayName {
6465
Empty,
6566
}
6667

68+
const WHITESPACE_REGEX: &str = r"\s+";
69+
const INVALID_SYMBOLS_REGEX: &str = r"[#,:]+";
70+
71+
impl DisplayName {
72+
/// Transforms the current display name into the name part of a
73+
/// `RoomAliasId`.
74+
pub fn to_room_alias_name(&self) -> String {
75+
let room_name = match self {
76+
Self::Named(name) => name,
77+
Self::Aliased(name) => name,
78+
Self::Calculated(name) => name,
79+
Self::EmptyWas(name) => name,
80+
Self::Empty => "",
81+
};
82+
83+
let whitespace_regex =
84+
Regex::new(WHITESPACE_REGEX).expect("`WHITESPACE_REGEX` should be valid");
85+
let symbol_regex =
86+
Regex::new(INVALID_SYMBOLS_REGEX).expect("`INVALID_SYMBOLS_REGEX` should be valid");
87+
88+
// Replace whitespaces with `-`
89+
let sanitised = whitespace_regex.replace_all(room_name, "-");
90+
// Remove non-ASCII characters and ASCII control characters
91+
let sanitised =
92+
String::from_iter(sanitised.chars().filter(|c| c.is_ascii() && !c.is_ascii_control()));
93+
// Remove other problematic ASCII symbols
94+
let sanitised = symbol_regex.replace_all(&sanitised, "");
95+
// Lowercased
96+
sanitised.to_lowercase()
97+
}
98+
}
99+
67100
impl fmt::Display for DisplayName {
68101
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
69102
match self {
@@ -541,6 +574,7 @@ mod tests {
541574
use ruma::events::tag::{TagInfo, TagName, Tags};
542575

543576
use super::{BaseRoomInfo, RoomNotableTags};
577+
use crate::DisplayName;
544578

545579
#[test]
546580
fn test_handle_notable_tags_favourite() {
@@ -571,4 +605,24 @@ mod tests {
571605
base_room_info.handle_notable_tags(&tags);
572606
assert!(base_room_info.notable_tags.contains(RoomNotableTags::LOW_PRIORITY).not());
573607
}
608+
609+
#[test]
610+
fn test_room_alias_from_room_display_name_lowercases() {
611+
assert_eq!("roomalias", DisplayName::Named("RoomAlias".to_owned()).to_room_alias_name());
612+
}
613+
614+
#[test]
615+
fn test_room_alias_from_room_display_name_removes_whitespace() {
616+
assert_eq!("room-alias", DisplayName::Named("Room Alias".to_owned()).to_room_alias_name());
617+
}
618+
619+
#[test]
620+
fn test_room_alias_from_room_display_name_removes_non_ascii_symbols() {
621+
assert_eq!("roomalias", DisplayName::Named("Room±Alias√".to_owned()).to_room_alias_name());
622+
}
623+
624+
#[test]
625+
fn test_room_alias_from_room_display_name_removes_invalid_ascii_symbols() {
626+
assert_eq!("roomalias", DisplayName::Named("#Room,Alias:".to_owned()).to_room_alias_name());
627+
}
574628
}

0 commit comments

Comments
 (0)