@@ -8,7 +8,7 @@ use std::env;
88use url:: Url ;
99use serde:: { Deserialize , Serialize } ;
1010use anyhow:: { Result , Context } ;
11- use matrix_sdk:: ruma:: { MatrixUri , MatrixToUri } ;
11+ use matrix_sdk:: ruma:: MatrixToUri ;
1212use regex:: Regex ;
1313
1414/// Configuration for MSC4352 feature
@@ -40,15 +40,7 @@ struct WellKnownClient {
4040
4141/// Convert HTTPS permalink to matrix: URI
4242pub fn convert_https_permalink_to_matrix_uri ( url_str : & str ) -> Option < String > {
43- // Try to parse as a MatrixToUri first
44- if let Ok ( matrix_to_uri) = MatrixToUri :: parse ( url_str) {
45- // Convert to MatrixUri
46- if let Ok ( matrix_uri) = MatrixUri :: try_from ( & matrix_to_uri) {
47- return Some ( matrix_uri. to_string ( ) ) ;
48- }
49- }
50-
51- // If not a standard matrix.to URL, try to handle custom base URLs
43+ // Parse the URL
5244 let url = Url :: parse ( url_str) . ok ( ) ?;
5345
5446 // Check if this looks like a resolver-style permalink
@@ -57,22 +49,29 @@ pub fn convert_https_permalink_to_matrix_uri(url_str: &str) -> Option<String> {
5749 return None ;
5850 }
5951
60- // Reconstruct as matrix.to URL and parse
61- let matrix_to_url = format ! ( "https://matrix.to{}" , fragment) ;
52+ // Remove the leading slash from fragment
53+ let stripped = & fragment[ 1 ..] ;
54+
55+ // Convert to matrix: URI format based on the identifier type
56+ let matrix_uri_str = if stripped. starts_with ( '#' ) {
57+ // Room alias - Example: #room:example.org -> matrix:r/room:example.org
58+ format ! ( "matrix:r/{}" , & stripped[ 1 ..] )
59+ } else if stripped. starts_with ( '!' ) {
60+ // Room ID - Example: !roomid:example.org -> matrix:roomid/roomid:example.org
61+ format ! ( "matrix:roomid/{}" , stripped)
62+ } else if stripped. starts_with ( '@' ) {
63+ // User ID - Example: @user:example.org -> matrix:u/user:example.org
64+ format ! ( "matrix:u/{}" , & stripped[ 1 ..] )
65+ } else {
66+ return None ;
67+ } ;
68+
69+ // Add query parameters if present
6270 if let Some ( query) = url. query ( ) {
63- let matrix_to_url = format ! ( "{}?{}" , matrix_to_url, query) ;
64- if let Ok ( matrix_to_uri) = MatrixToUri :: parse ( & matrix_to_url) {
65- if let Ok ( matrix_uri) = MatrixUri :: try_from ( & matrix_to_uri) {
66- return Some ( matrix_uri. to_string ( ) ) ;
67- }
68- }
69- } else if let Ok ( matrix_to_uri) = MatrixToUri :: parse ( & matrix_to_url) {
70- if let Ok ( matrix_uri) = MatrixUri :: try_from ( & matrix_to_uri) {
71- return Some ( matrix_uri. to_string ( ) ) ;
72- }
71+ Some ( format ! ( "{}?{}" , matrix_uri_str, query) )
72+ } else {
73+ Some ( matrix_uri_str)
7374 }
74-
75- None
7675}
7776
7877/// Discover permalink base URL from homeserver's well-known
@@ -207,11 +206,17 @@ mod tests {
207206
208207 #[ test]
209208 fn test_linkify_outgoing_text_to_html ( ) {
209+ // This test only works when MSC4352 is enabled, but linkify_outgoing_text_to_html
210+ // doesn't check the config - that's done by the caller.
211+ // So we test the function directly.
210212 let text = "Check out this room: https://links.example.org/#/#room:example.org" ;
211- let result = linkify_outgoing_text_to_html ( text) . unwrap ( ) ;
213+ let result = linkify_outgoing_text_to_html ( text) ;
212214
213- assert ! ( result. contains( r#"<a href="matrix:"# ) ) ;
214- assert ! ( result. contains( "https://links.example.org" ) ) ;
215+ // The function should find and convert the permalink
216+ assert ! ( result. is_some( ) ) ;
217+ let html = result. unwrap ( ) ;
218+ assert ! ( html. contains( r#"<a href="matrix:"# ) ) ;
219+ assert ! ( html. contains( "https://links.example.org" ) ) ;
215220 }
216221
217222 #[ test]
0 commit comments