diff --git a/Plans/02 Verify/S009-verify-integrations.md b/Plans/02 Verify/S009-verify-integrations.md index d4e884c..e9ce494 100644 --- a/Plans/02 Verify/S009-verify-integrations.md +++ b/Plans/02 Verify/S009-verify-integrations.md @@ -17,6 +17,7 @@ Verify Discord Rich Presence, media session and visualizer, spell check, update - [ ] Updates: point the update URL at a local JSON file; check the notice and that nothing downloads automatically - [ ] Gamepad: check it enumerates without a device and doesn't spin the CPU; single instance: second launch forwards a URL - [ ] Performance: freeze/discard idle tabs and memory saver numbers; mark FEATURES rows verified/broken +- [ ] R4-09 Discord site icon: `icon_url` in `src/discord.rs` (https image/favicon, else the origin's /favicon.ico) is written, **not compiled yet**; build, run `nice cargo test --release -j 2 icon_prefers`, see the icon in the fake-IPC `SET_ACTIVITY` frame (`assets.large_image`), user confirms in real Discord; app ID stays in Settings, never in code - [ ] PROGRESS entry, INDEX line, commit `S009: …` and push ## End criteria diff --git a/src/actions.rs b/src/actions.rs index 684557f..679b6c3 100644 --- a/src/actions.rs +++ b/src/actions.rs @@ -2088,7 +2088,7 @@ impl App { && let Some(tab) = win.tab() && tab.page.is_none() && let Ok(url) = Url::parse(&tab.url) - && let Some(act) = crate::discord::browse_activity(&settings, &url, &tab.title, &tab.site_name, if tab.page_image.is_empty() { &tab.favicon_url } else { &tab.page_image }, tab.arrived_ms as i64) + && let Some(act) = crate::discord::browse_activity(&settings, &url, &tab.title, &tab.site_name, &tab.page_image, &tab.favicon_url, tab.arrived_ms as i64) { activity = Some((act, settings.client_id.clone())); } diff --git a/src/discord.rs b/src/discord.rs index 1bc7d97..1691799 100644 --- a/src/discord.rs +++ b/src/discord.rs @@ -111,6 +111,19 @@ fn https_short(url: &str) -> bool { url.starts_with("https://") && url.len() <= 256 } +/// Image for the activity's large icon: the first of `images` Discord can fetch (short https URL), +/// else the page origin's /favicon.ico (data:/http:/long favicon URLs are unusable; no third-party +/// service, so no visited host leaks beyond Discord). Empty for pages without a host. +fn icon_url(images: &[&str], page: &Url) -> String { + if let Some(img) = images.iter().find(|i| https_short(i)) { + return (*img).to_owned(); + } + match page.host_str() { + Some(host) if matches!(page.scheme(), "http" | "https") => format!("https://{host}/favicon.ico"), + _ => String::new(), + } +} + /// Options that Discord clients may refuse; the worker turns them off when it is told so. #[derive(Clone, Copy, Debug)] pub struct Quirks { @@ -137,7 +150,7 @@ pub fn media_activity(settings: &Rpc, tab_url: &Url, tab_title: &str, favicon: & } } if !https_short(&art) { - art = if https_short(favicon) { favicon.to_owned() } else { String::new() }; + art = icon_url(&[favicon], tab_url); } let by = if creator != name { format!("by {creator}") } else { name.clone() }; let mut act = json!({ @@ -179,7 +192,7 @@ pub fn media_activity(settings: &Rpc, tab_url: &Url, tab_title: &str, favicon: & } /// "Browsing" activity for an ordinary tab. `since_ms` is when the user arrived on the page. -pub fn browse_activity(settings: &Rpc, url: &Url, title: &str, site_name: &str, image: &str, since_ms: i64) -> Option { +pub fn browse_activity(settings: &Rpc, url: &Url, title: &str, site_name: &str, image: &str, favicon: &str, since_ms: i64) -> Option { if settings.browsing == RpcBrowsing::Off || !matches!(url.scheme(), "http" | "https") { return None; } @@ -199,8 +212,9 @@ pub fn browse_activity(settings: &Rpc, url: &Url, title: &str, site_name: &str, "timestamps": {"start": since_ms}, "assets": {"large_text": clip(&host, 128)}, }); - if https_short(image) { - act["assets"]["large_image"] = json!(image); + let icon = icon_url(&[image, favicon], url); + if !icon.is_empty() { + act["assets"]["large_image"] = json!(icon); } if by_title && settings.button { act["buttons"] = json!([{"label": util::ellipsize(&format!("Open on {site}"), 32), "url": util::ellipsize(url.as_str(), 512)}]); @@ -469,3 +483,18 @@ fn worker(rx: Receiver, status: Arc>, wake: Arc