S009: R4-09 Discord icon falls back to the origin's /favicon.ico

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
astar76 2026-11-12 14:23:35 -06:00
commit 613ac50443
3 changed files with 35 additions and 5 deletions

View file

@ -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

View file

@ -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()));
}

View file

@ -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<Value> {
pub fn browse_activity(settings: &Rpc, url: &Url, title: &str, site_name: &str, image: &str, favicon: &str, since_ms: i64) -> Option<Value> {
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<Command>, status: Arc<Mutex<String>>, wake: Arc<dyn Fn()
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn icon_prefers_fetchable_image_then_falls_back() {
let page = Url::parse("https://example.org/a").unwrap();
assert_eq!(icon_url(&["https://x.test/a.png", "https://y.test/f.png"], &page), "https://x.test/a.png");
assert_eq!(icon_url(&["data:image/png;base64,AAAA", "https://y.test/f.png"], &page), "https://y.test/f.png");
let long = format!("https://x.test/{}", "a".repeat(300));
assert_eq!(icon_url(&["", "http://y.test/f.ico", &long], &page), "https://example.org/favicon.ico");
assert_eq!(icon_url(&[""], &Url::parse("about:blank").unwrap()), "");
}
}