| 1 | use gpui_kit::*; |
| 2 | |
| 3 | #[derive(Clone, Copy)] |
| 4 | pub struct RenderHooks { |
| 5 | pub open_profile_link: fn(&str, Point<Pixels>, &mut App) -> bool, |
| 6 | pub jump_to_quoted: fn(String, &mut App), |
| 7 | pub inline_image: fn(&str, &App) -> Option<std::path::PathBuf>, |
| 8 | pub open_link: fn(&str, &mut App), |
| 9 | } |
| 10 | |
| 11 | impl Global for RenderHooks {} |
| 12 | |
| 13 | pub fn open_profile_link(url: &str, position: Point<Pixels>, cx: &mut App) -> bool { |
| 14 | match cx.try_global::<RenderHooks>().copied() { |
| 15 | Some(hooks) => (hooks.open_profile_link)(url, position, cx), |
| 16 | None => false, |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | pub fn is_openable_link(url: &str) -> bool { |
| 21 | app_base::open_link::is_openable_url(url) |
| 22 | } |
| 23 | |
| 24 | pub fn open_link(url: &str, cx: &mut App) { |
| 25 | if !is_openable_link(url) { |
| 26 | return; |
| 27 | } |
| 28 | match cx.try_global::<RenderHooks>().copied() { |
| 29 | Some(hooks) => (hooks.open_link)(url, cx), |
| 30 | None => app_base::open_link::open_url(cx, url), |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | pub fn jump_to_quoted(message_id: String, cx: &mut App) { |
| 35 | if let Some(hooks) = cx.try_global::<RenderHooks>().copied() { |
| 36 | (hooks.jump_to_quoted)(message_id, cx); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | pub fn inline_image(url: &str, cx: &App) -> Option<std::path::PathBuf> { |
| 41 | let hooks = cx.try_global::<RenderHooks>().copied()?; |
| 42 | (hooks.inline_image)(url, cx) |
| 43 | } |
| 44 | |
| 45 | #[cfg(test)] |
| 46 | mod tests { |
| 47 | use super::is_openable_link; |
| 48 | |
| 49 | #[test] |
| 50 | fn only_web_mail_phone_and_teams_links_open() { |
| 51 | assert!(is_openable_link("https://example.test/a")); |
| 52 | assert!(is_openable_link(" HTTP://example.test")); |
| 53 | assert!(is_openable_link("mailto:someone@example.test")); |
| 54 | assert!(is_openable_link("msteams:/l/chat/0/0")); |
| 55 | assert!(is_openable_link( |
| 56 | "ms-word:ofe|u|https://example.test/a.docx" |
| 57 | )); |
| 58 | assert!(!is_openable_link("file:///etc/passwd")); |
| 59 | assert!(!is_openable_link("search-ms:query=x")); |
| 60 | assert!(!is_openable_link("javascript:alert(1)")); |
| 61 | assert!(!is_openable_link("//example.test")); |
| 62 | } |
| 63 | } |