Commit bd52d6ffd6
Unregistered key
Layout: unified · split
src/windows/room/chat.rs +81 −9
| @@ -1224,18 +1224,21 @@ fn extract_mentions(content: &TextMessageEventContent) -> Mentions { | |||
| 1224 | if !matches!(formatted.format, MessageFormat::Html) { | 1224 | if !matches!(formatted.format, MessageFormat::Html) { |
| 1225 | return Mentions::new(); | 1225 | return Mentions::new(); |
| 1226 | } | 1226 | } |
| 1227 | let html = formatted.body.as_str(); | 1227 | extract_mentions_str(formatted.body.as_str()) |
| 1228 | } | ||
| 1228 | 1229 | ||
| 1230 | fn extract_mentions_str(html: &str) -> Mentions { | ||
| 1229 | let re = Regex::new(r#"<a href="(https://matrix.to/#/@[^"]*:[^"]*)">"#).unwrap(); | 1231 | let re = Regex::new(r#"<a href="(https://matrix.to/#/@[^"]*:[^"]*)">"#).unwrap(); |
| 1230 | 1232 | ||
| 1231 | let user_ids = re.captures_iter(html).map(|capture| { | 1233 | let user_ids = re.captures_iter(html).filter_map(|capture| { |
| 1232 | let link = capture.get(1).unwrap().as_str(); | 1234 | let link = capture.get(1)?.as_str(); |
| 1233 | let uri = MatrixToUri::parse(link).unwrap(); | 1235 | let uri = MatrixToUri::parse(link).ok()?; |
| 1234 | let MatrixId::User(user_id) = uri.id() else { | 1236 | |
| 1235 | // we only matched user links (starting with `@`) | 1237 | if let MatrixId::User(user_id) = uri.id() { |
| 1236 | unreachable!() | 1238 | Some(user_id.to_owned()) |
| 1237 | }; | 1239 | } else { |
| 1238 | user_id.to_owned() | 1240 | None |
| 1241 | } | ||
| 1239 | }); | 1242 | }); |
| 1240 | 1243 | ||
| 1241 | Mentions::with_user_ids(user_ids) | 1244 | Mentions::with_user_ids(user_ids) |
| @@ -1333,6 +1336,14 @@ mod tests { | |||
| 1333 | 1336 | ||
| 1334 | use crate::tests::{TEST_ROOM1_ID, mock_store}; | 1337 | use crate::tests::{TEST_ROOM1_ID, mock_store}; |
| 1335 | 1338 | ||
| 1339 | fn mentions_in(html: &str) -> Vec<String> { | ||
| 1340 | extract_mentions_str(&html) | ||
| 1341 | .user_ids | ||
| 1342 | .iter() | ||
| 1343 | .map(|u| u.to_string()) | ||
| 1344 | .collect() | ||
| 1345 | } | ||
| 1346 | |||
| 1336 | #[test] | 1347 | #[test] |
| 1337 | fn test_location_geo_uri() { | 1348 | fn test_location_geo_uri() { |
| 1338 | use matrix_sdk::ruma::events::room::message::{ | 1349 | use matrix_sdk::ruma::events::room::message::{ |
| @@ -1350,6 +1361,67 @@ mod tests { | |||
| 1350 | assert_eq!(location_geo_uri(&text), None); | 1361 | assert_eq!(location_geo_uri(&text), None); |
| 1351 | } | 1362 | } |
| 1352 | 1363 | ||
| 1364 | #[test] | ||
| 1365 | fn test_extract_mentions_normal() { | ||
| 1366 | let res = mentions_in(r#"<a href="https://matrix.to/#/@user:example.com">user</a>"#); | ||
| 1367 | assert_eq!(res, vec!["@user:example.com"]); | ||
| 1368 | } | ||
| 1369 | |||
| 1370 | #[test] | ||
| 1371 | fn test_extract_mentions_ignore_parameters() { | ||
| 1372 | let res = | ||
| 1373 | mentions_in(r#"<a href="https://matrix.to/#/@user:example.com?via=example.com">u</a>"#); | ||
| 1374 | assert_eq!(res, vec!["@user:example.com"]); | ||
| 1375 | } | ||
| 1376 | |||
| 1377 | #[test] | ||
| 1378 | fn test_extract_mentions_multiple_dedupe() { | ||
| 1379 | let res = mentions_in( | ||
| 1380 | r#"<a href="https://matrix.to/#/@a:example.com">a</a> and | ||
| 1381 | <a href="https://matrix.to/#/@b:example.com">b</a> and | ||
| 1382 | <a href="https://matrix.to/#/@a:example.com">a again</a>"#, | ||
| 1383 | ); | ||
| 1384 | assert_eq!(res, vec!["@a:example.com", "@b:example.com"]); | ||
| 1385 | } | ||
| 1386 | |||
| 1387 | #[test] | ||
| 1388 | fn test_extract_mentions_skips_invalid_links() { | ||
| 1389 | let empty = vec![ | ||
| 1390 | r#"<a href="https://matrix.to/#/@user:example.com/$eventid">e</a>"#, | ||
| 1391 | r#"<a href="https://matrix.to/#/@bob:">bob</a>"#, | ||
| 1392 | r#"<a href="https://matrix.to/#/@user:%E4%BE%8B.com">x</a>"#, | ||
| 1393 | ]; | ||
| 1394 | |||
| 1395 | for html in empty { | ||
| 1396 | assert!(mentions_in(html).is_empty()); | ||
| 1397 | } | ||
| 1398 | |||
| 1399 | // An invalid link is ignored, but a valid one is still extracted: | ||
| 1400 | assert_eq!( | ||
| 1401 | mentions_in( | ||
| 1402 | r#"<a href="https://matrix.to/#/@bob:">bob</a> | ||
| 1403 | <a href="https://matrix.to/#/@user:example.com">user</a>"# | ||
| 1404 | ), | ||
| 1405 | vec!["@user:example.com"] | ||
| 1406 | ); | ||
| 1407 | } | ||
| 1408 | |||
| 1409 | #[test] | ||
| 1410 | fn test_extract_mentions_ignores_unformatted() { | ||
| 1411 | // URIs in plain text messages don't count as mentions: | ||
| 1412 | let plain = TextMessageEventContent::plain( | ||
| 1413 | r#"<a href="https://matrix.to/#/@user:example.com">user</a>"#, | ||
| 1414 | ); | ||
| 1415 | assert!(extract_mentions(&plain).user_ids.is_empty()); | ||
| 1416 | |||
| 1417 | // Links to things other than users aren't mentions: | ||
| 1418 | let room = r#"<a href="https://matrix.to/#/#room:example.com">room</a>"#; | ||
| 1419 | assert!(mentions_in(room).is_empty()); | ||
| 1420 | |||
| 1421 | let nonmx = r#"<a href="https://example.com/@user:example.com">nope</a>"#; | ||
| 1422 | assert!(mentions_in(nonmx).is_empty()); | ||
| 1423 | } | ||
| 1424 | |||
| 1353 | macro_rules! move_line { | 1425 | macro_rules! move_line { |
| 1354 | ($dir: expr, $count: expr) => { | 1426 | ($dir: expr, $count: expr) => { |
| 1355 | EditorAction::Edit( | 1427 | EditorAction::Edit( |