diff --git a/crates/ipx/CHANGELOG.md b/crates/ipx/CHANGELOG.md index bd19839..67c8c63 100644 --- a/crates/ipx/CHANGELOG.md +++ b/crates/ipx/CHANGELOG.md @@ -31,6 +31,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +- A feed whose items have not changed is checked less often even when its server rewrites it at every read, as Substack's build time and Patreon's signed file links do: it is judged by its items, not its bytes. An item's text edited with nothing else changed shows at the next full read, at most four hours on (#503). - An item its feed gives no title is named as it is stored, by the opening of its text or else its file's name, so search, Signals, the iOS widget and CarPlay show the same name the page does; the untitled items already stored are named once at start (#502). - Torrents go to qBittorrent through its Web API, set in `[torrent]` (`qbittorrent_url`, `username`, `password`, `save_path`), which downloads and seeds them until the episode is deleted; ipx no longer runs a BitTorrent client of its own or listens on 6881, and `port_range` is gone. Without a qBittorrent set, torrents are held back as when they are turned off (#494, #495). - The server's command is `ipxd`, not `ipx`: `ipxd daemon`, `ipxd status`, `ipxd user add` and the rest, in the container as `docker exec iPX ipxd ...`. `ipx` is now the reader in a terminal (#437). diff --git a/crates/ipx/src/scan.rs b/crates/ipx/src/scan.rs index 980e428..415a8d0 100644 --- a/crates/ipx/src/scan.rs +++ b/crates/ipx/src/scan.rs @@ -229,6 +229,38 @@ pub(crate) fn quiet(usual: u64, same_for: Option) -> u64 { } } +fn sha256(bytes: &[u8]) -> String { + use sha2::Digest as _; + sha2::Sha256::digest(bytes).iter().map(|b| format!("{b:02x}")).collect() +} + +/// What a feed's "the same as last time" is decided on (#242): its items and what it calls +/// itself, not its bytes. Substack stamps a new build time on every read and Patreon signs its +/// file links afresh, so their bytes were never the same and they stayed hourly with nothing +/// new (#503). An item's text edited with nothing else changed waits for the next full read. +fn identity(p: &feed::ParsedFeed) -> Vec { + let bare = |u: &str| u.split(['?', '#']).next().unwrap_or_default().to_owned(); + let mut out = String::new(); + let mut put = |s: &str| { + out.push_str(s); + out.push('\0'); + }; + put(p.title.as_deref().unwrap_or_default()); + put(&bare(p.image.as_deref().unwrap_or_default())); + put(p.description.as_deref().unwrap_or_default()); + put(&p.ttl_mins.map(|t| t.to_string()).unwrap_or_default()); + for e in &p.entries { + put(&e.guid); + put(e.title.as_deref().unwrap_or_default()); + put(&e.published.map(|t| t.to_string()).unwrap_or_default()); + for enc in &e.enclosures { + put(&bare(&enc.url)); + } + put("\x1e"); + } + out.into_bytes() +} + pub(crate) fn backoff(usual: u64, failing_for: Option) -> u64 { const CEILING: u64 = 86_400; match failing_for { @@ -354,26 +386,27 @@ pub(crate) async fn scan_one( return Ok(Outcome::Empty); } - // The same body as last time, from a server that ignores "not modified": nothing to read, + // A subscribed OPML is a list of feeds, not a feed. The original matched on a ".opml" + // URL; sniffing the body also catches one served from a URL without that extension. + let opml = feed::is_opml(&bytes); + let parsed = if opml { None } else { Some(feed::parse(&bytes)?) }; + + // The same feed as last time, from a server that ignores "not modified": nothing to read, // as a 304 would have said (#242). Not when something is missing that a read would fill, as // above, nor when someone asked for a full check. - let hash = { - use sha2::Digest as _; - sha2::Sha256::digest(&bytes).iter().map(|b| format!("{b:02x}")).collect::() - }; + let hash = sha256(&match &parsed { + Some(p) => identity(p), + None => bytes.to_vec(), + }); if !force && state.body_hash.as_deref() == Some(hash.as_str()) && stored.entries > 0 && stored.image.is_some() { ctx.db.touch_feed(id, &feed_cfg.url).await?; return Ok(Outcome::NotModified); } - // A subscribed OPML is a list of feeds, not a feed. The original matched on a ".opml" - // URL; sniffing the body also catches one served from a URL without that extension. - if feed::is_opml(&bytes) { + let Some(mut parsed) = parsed else { ctx.db.touch_feed(id, &feed_cfg.url).await?; return lists::sync_opml(ctx, id, &bytes).await; - } - - let mut parsed = feed::parse(&bytes)?; + }; // Artwork on http moves to https where its host serves it (#110), before it is compared // with what is stored, which is then the https address too. let mut secure = std::collections::HashMap::new(); @@ -964,6 +997,22 @@ mod tests { assert_eq!(same(&set), hour, "someone's own schedule wins"); } + #[test] + fn a_feed_is_the_same_by_its_items_not_its_build_time_or_signed_links() { + let feed = |built: &str, token: &str, title: &str| { + let xml = format!( + "Show{built}\ + 1{title}\ + \ + " + ); + sha256(&identity(&feed::parse(xml.as_bytes()).unwrap())) + }; + let first = feed("Mon, 01 Jan 2024 00:00:00 GMT", "a", "One"); + assert_eq!(first, feed("Tue, 02 Jan 2024 00:00:00 GMT", "b", "One"), "rebuilt and re-signed, the same"); + assert_ne!(first, feed("Mon, 01 Jan 2024 00:00:00 GMT", "a", "Two"), "an item renamed is a change"); + } + #[tokio::test] async fn the_same_body_again_is_not_modified_unless_a_check_is_forced() { let base = empty_or_feed_server().await; @@ -983,10 +1032,9 @@ mod tests { matches!(scan_one(&ctx, "same", &cfg, &state, true, None).await, Ok(Outcome::Feed(_))), "a forced check reads it" ); - // Changed: read, and the same from now. - let moved = config::Feed { url: format!("{base}/fixed"), ..feed() }; - assert!(matches!(scan_one(&ctx, "same", &moved, &state, false, None).await, Ok(Outcome::Feed(_)))); - assert_ne!(ctx.db.http_state("same").await.unwrap().body_hash, state.body_hash); + // Only an item's text edited: the same feed, read at the next forced or changed read (#503). + let edited = config::Feed { url: format!("{base}/fixed"), ..feed() }; + assert!(matches!(scan_one(&ctx, "same", &edited, &state, false, None).await, Ok(Outcome::NotModified))); } #[test]