You are a constitutional council ranking individual git commits for ownership allocation. Compare these two commits. Decide which contributed more lasting value to the project. Judge substance, not spectacle: - Prefer correct, lasting design and real bugfixes over churn, formatting, renames, or generated noise. - Prefer clarity and necessity over sheer line count. A small precise change can beat a large diffuse one. - Do not favor a side merely because its patch is longer or noisier. - Weight what the change does for the project, not the contributor's name. Return ONLY a JSON object: {"winner": "A" or "B", "ratio": "N:M", "explanation": "..."} The explanation must cite concrete differences in the patches (1-3 sentences). Side A — contributor: tommy-mor Side A — commit message: [239c074b] url schema stuff Side A — unified diff (full patch): diff --git a/AGENTS.md b/AGENTS.md index 426a88e7c1da54fe0a28c5c76fa4e1f1bc117fcf..e60b9ba6012593361ef10e8fdd9439cd9932e09b 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -58,3 +58,4 @@ Use **tmux** for `cargo run --package sorter2-server` (dev server). Rebuild afte - First `cargo test` / `cargo build --release` is slow; Clojure smoke test always does a release build. - `legacy/` and `ideas/` are not part of the workspace build. +- **ItemId** for web URLs is a canonical full URL (`https://reddit.com/r/rust`). Rules live in [`server/src/url_rules/`](server/src/url_rules/) (composable Rust, not a config DSL). After changing canonicalization rules, rebuild the projection: `cargo run --package sorter2-server -- replay-index`. diff --git a/Cargo.lock b/Cargo.lock index 0dd4fce5fb6400ae153cca4e3dbf5a5158e6d8b4..49a908ef935c430dbe63c6a28d8a24e38b489486 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1951,6 +1951,7 @@ dependencies = [ "tower-http 0.5.2", "tracing", "tracing-subscriber", + "url", "urlencoding", ] diff --git a/REPLAY.sh b/REPLAY.sh new file mode 100755 index 0000000000000000000000000000000000000000..f2dbd8aea60c02d2feef74805f7ef5c2b7022537 --- /dev/null +++ b/REPLAY.sh @@ -0,0 +1,2 @@ +cargo run --package sorter2-server -- replay-index + diff --git a/server/Cargo.toml b/server/Cargo.toml index 27f552c20b97ef28cdde4cb6b1a4980375135111..ad4912791aff59fb1d3293f66ad381ae618cd60b 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -24,6 +24,7 @@ async-stream = "0.3" futures-util = { version = "0.3", default-features = false, features = ["std"] } rand = "0.8" urlencoding = "2" +url = "2" durable = { path = "../durable" } [dev-dependencies] diff --git a/server/src/entity_store.rs b/server/src/entity_store.rs index d5d17c3676e4a8ddec998e9f5a9dbafe9c2d9d0e..d29f39aecca6f12cdcf263cf77c3654eb4ee6cfa 100644 --- a/server/src/entity_store.rs +++ b/server/src/entity_store.rs @@ -124,7 +124,7 @@ mod tests { fn round_trip_payload() { let tmp = tempfile::tempdir().unwrap(); let store = EntityStore::open(tmp.path()).unwrap(); - let id = ItemId::parse("reddit.com/r/rust").unwrap(); + let id = ItemId::from_url("https://reddit.com/r/rust").unwrap(); let payload = json!({"kind": "t5", "data": {"display_name": "rust"}}); store.put(&id, &payload).unwrap(); diff --git a/server/src/event_log.rs b/server/src/event_log.rs index 36f5b406084065b608735987cdb483c236e03081..2c9290b6fdbf2c2ad1c0f1ffd7374b2d9cc97f36 100644 --- a/server/src/event_log.rs +++ b/server/src/event_log.rs @@ -199,7 +199,7 @@ mod tests { log.append(&sample_record( 1, Event::NodeEnsured { - id: "reddit.com/r/rust".into(), + id: "https://reddit.com/r/rust".into(), }, )) .await @@ -237,7 +237,7 @@ mod tests { let path = tmp.path().join("events.jsonl"); let log = EventLog::new(&path); let event = Event::NodeEnsured { - id: "reddit.com/r/rust".into(), + id: "https://reddit.com/r/rust".into(), }; log.append(&sample_record(1, event)).await.unwrap(); @@ -255,7 +255,7 @@ mod tests { let path = tmp.path().join("events.jsonl"); std::fs::write( &path, - r#"{"type":"node_ensured","id":"reddit.com/r/rust"} + r#"{"type":"node_ensured","id":"https://reddit.com/r/rust"} {"schema":1,"seq":1,"ts":1,"event":{"type":"vote_recorded","ts":1,"a":"a","b":"b","ratio_left":2,"ratio_right":1,"scope":""}} "#, ) @@ -295,7 +295,7 @@ mod tests { log.append(&sample_record( 1, Event::NodeEnsured { - id: "reddit.com/r/rust".into(), + id: "https://reddit.com/r/rust".into(), }, )) .await @@ -303,7 +303,7 @@ mod tests { log.append(&sample_record( 3, Event::NodeEnsured { - id: "reddit.com/r/python".into(), + id: "https://reddit.com/r/python".into(), }, )) .await diff --git a/server/src/journal.rs b/server/src/journal.rs index 521a108019de1ea870d14c4fafbfe572c20ce0de..50bc89f976edb82b7b0e49e954a8eccbbe82bf87 100644 --- a/server/src/journal.rs +++ b/server/src/journal.rs @@ -141,10 +141,10 @@ mod tests { let j2 = journal.clone(); let (r1, r2) = tokio::join!( j1.append(Event::NodeEnsured { - id: "reddit.com/r/rust".into(), + id: "https://reddit.com/r/rust".into(), }), j2.append(Event::NodeEnsured { - id: "reddit.com/r/python".into(), + id: "https://reddit.com/r/python".into(), }), ); r1.unwrap(); @@ -153,10 +153,10 @@ mod tests { assert_eq!(projection_store.last_applied_event_count().unwrap(), 2); let tree = projection_store.load_tree().unwrap(); assert!(tree - .get(&ItemId::parse("reddit.com/r/rust").unwrap()) + .get(&ItemId::parse("https://reddit.com/r/rust").unwrap()) .is_some()); assert!(tree - .get(&ItemId::parse("reddit.com/r/python").unwrap()) + .get(&ItemId::parse("https://reddit.com/r/python").unwrap()) .is_some()); } @@ -170,7 +170,7 @@ mod tests { 1, 1, Event::NodeEnsured { - id: "reddit.com/r/rust".into(), + id: "https://reddit.com/r/rust".into(), }, )) .await @@ -186,7 +186,7 @@ mod tests { 1, 1, Event::NodeEnsured { - id: "reddit.com/r/rust".into(), + id: "https://reddit.com/r/rust".into(), }, )], ) @@ -202,7 +202,7 @@ mod tests { ); journal .append(Event::NodeEnsured { - id: "reddit.com/r/python".into(), + id: "https://reddit.com/r/python".into(), }) .await .unwrap(); @@ -227,13 +227,13 @@ mod tests { journal .append_many(vec![ Event::NodeEnsured { - id: "reddit.com/r/rust".into(), + id: "https://reddit.com/r/rust".into(), }, Event::NodeEnsured { - id: "reddit.com/r/python".into(), + id: "https://reddit.com/r/python".into(), }, Event::NodeEnsured { - id: "reddit.com/r/clojure".into(), + id: "https://reddit.com/r/clojure".into(), }, ]) .await @@ -245,7 +245,7 @@ mod tests { assert_eq!(projection_store.last_applied_event_count().unwrap(), 3); let tree = projection_store.load_tree().unwrap(); assert!(tree - .get(&ItemId::parse("reddit.com/r/clojure").unwrap()) + .get(&ItemId::parse("https://reddit.com/r/clojure").unwrap()) .is_some()); } } diff --git a/server/src/lib.rs b/server/src/lib.rs index 9bd5f76fd1406b9b1be4c272f4ba8647edde2678..5c02c8e704e4664453bad75d819df8a067668176 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -9,6 +9,7 @@ pub mod journal; pub mod pair; pub mod parser; pub mod path_types; +pub mod url_rules; pub mod projection_apply; pub mod projection_store; pub mod ranking; diff --git a/server/src/pair.rs b/server/src/pair.rs index 43f780ba6ea6ce1cdc2e1f4cbb252ba8a10684b9..815a97b80e3e9f348e0937a4f147f2862018edb0 100644 --- a/server/src/pair.rs +++ b/server/src/pair.rs @@ -381,42 +381,42 @@ mod tests { #[test] fn suggest_prefers_unvoted_pair() { - let parent = ItemId::parse("reddit.com/r/rust").unwrap(); + let parent = ItemId::parse("https://reddit.com/r/rust").unwrap(); let mut tree = seed_children( &parent, &[ - "reddit.com/r/rust/a", - "reddit.com/r/rust/b", - "reddit.com/r/rust/c", + "https://reddit.com/r/rust/a", + "https://reddit.com/r/rust/b", + "https://reddit.com/r/rust/c", ], ); let vote = - VoteData::from_recorded(1, "reddit.com/r/rust/a", "reddit.com/r/rust/b", 2, 1).unwrap(); + VoteData::from_recorded(1, "https://reddit.com/r/rust/a", "https://reddit.com/r/rust/b", 2, 1).unwrap(); tree.apply_vote(&parent, vote); let group = tree.get(&parent).unwrap().local_ranking.clone(); let pool = children_of(&tree, &parent); let (l, r) = suggest_next_pair_in_pool(&group, &pool, None).unwrap(); - let voted_ab = (l.as_str() == "reddit.com/r/rust/a" && r.as_str() == "reddit.com/r/rust/b") - || (l.as_str() == "reddit.com/r/rust/b" && r.as_str() == "reddit.com/r/rust/a"); + let voted_ab = (l.as_str() == "https://reddit.com/r/rust/a" && r.as_str() == "https://reddit.com/r/rust/b") + || (l.as_str() == "https://reddit.com/r/rust/b" && r.as_str() == "https://reddit.com/r/rust/a"); assert!(!voted_ab); } #[test] fn suggest_bridges_separate_components() { - let parent = ItemId::parse("reddit.com/r/rust").unwrap(); + let parent = ItemId::parse("https://reddit.com/r/rust").unwrap(); let mut tree = seed_children( &parent, &[ - "reddit.com/r/rust/a", - "reddit.com/r/rust/b", - "reddit.com/r/rust/c", - "reddit.com/r/rust/d", + "https://reddit.com/r/rust/a", + "https://reddit.com/r/rust/b", + "https://reddit.com/r/rust/c", + "https://reddit.com/r/rust/d", ], ); let ab = - VoteData::from_recorded(1, "reddit.com/r/rust/a", "reddit.com/r/rust/b", 2, 1).unwrap(); + VoteData::from_recorded(1, "https://reddit.com/r/rust/a", "https://reddit.com/r/rust/b", 2, 1).unwrap(); let cd = - VoteData::from_recorded(2, "reddit.com/r/rust/c", "reddit.com/r/rust/d", 2, 1).unwrap(); + VoteData::from_recorded(2, "https://reddit.com/r/rust/c", "https://reddit.com/r/rust/d", 2, 1).unwrap(); tree.apply_vote(&parent, ab); tree.apply_vote(&parent, cd); let group = tree.get(&parent).unwrap().local_ranking.clone(); @@ -424,37 +424,37 @@ mod tests { let pair = suggest_next_pair_in_pool(&group, &pool, None).unwrap(); let chosen = pair_set(&pair); let from_ab = - chosen.contains("reddit.com/r/rust/a") || chosen.contains("reddit.com/r/rust/b"); + chosen.contains("https://reddit.com/r/rust/a") || chosen.contains("https://reddit.com/r/rust/b"); let from_cd = - chosen.contains("reddit.com/r/rust/c") || chosen.contains("reddit.com/r/rust/d"); + chosen.contains("https://reddit.com/r/rust/c") || chosen.contains("https://reddit.com/r/rust/d"); assert!(from_ab && from_cd, "expected bridge pair, got {:?}", chosen); } #[test] fn suggest_prefers_attach_over_isolate_pair_among_many_unranked() { - let parent = ItemId::parse("reddit.com/r/rust").unwrap(); + let parent = ItemId::parse("https://reddit.com/r/rust").unwrap(); let mut tree = seed_children( &parent, &[ - "reddit.com/r/rust/a", - "reddit.com/r/rust/b", - "reddit.com/r/rust/c", - "reddit.com/r/rust/d", - "reddit.com/r/rust/e", + "https://reddit.com/r/rust/a", + "https://reddit.com/r/rust/b", + "https://reddit.com/r/rust/c", + "https://reddit.com/r/rust/d", + "https://reddit.com/r/rust/e", ], ); let ab = - VoteData::from_recorded(1, "reddit.com/r/rust/a", "reddit.com/r/rust/b", 2, 1).unwrap(); + VoteData::from_recorded(1, "https://reddit.com/r/rust/a", "https://reddit.com/r/rust/b", 2, 1).unwrap(); tree.apply_vote(&parent, ab); let group = tree.get(&parent).unwrap().local_ranking.clone(); let pool = children_of(&tree, &parent); let pair = suggest_next_pair_in_pool(&group, &pool, None).unwrap(); let chosen = pair_set(&pair); let from_ab = - chosen.contains("reddit.com/r/rust/a") || chosen.contains("reddit.com/r/rust/b"); - let from_cde = chosen.contains("reddit.com/r/rust/c") - || chosen.contains("reddit.com/r/rust/d") - || chosen.contains("reddit.com/r/rust/e"); + chosen.contains("https://reddit.com/r/rust/a") || chosen.contains("https://reddit.com/r/rust/b"); + let from_cde = chosen.contains("https://reddit.com/r/rust/c") + || chosen.contains("https://reddit.com/r/rust/d") + || chosen.contains("https://reddit.com/r/rust/e"); assert!( from_ab && from_cde, "expected ranked+unranked attach, got {:?}", @@ -464,40 +464,40 @@ mod tests { #[test] fn suggest_connects_isolate_to_existing_component() { - let parent = ItemId::parse("reddit.com/r/rust").unwrap(); + let parent = ItemId::parse("https://reddit.com/r/rust").unwrap(); let mut tree = seed_children( &parent, &[ - "reddit.com/r/rust/a", - "reddit.com/r/rust/b", - "reddit.com/r/rust/c", + "https://reddit.com/r/rust/a", + "https://reddit.com/r/rust/b", + "https://reddit.com/r/rust/c", ], ); let ab = - VoteData::from_recorded(1, "reddit.com/r/rust/a", "reddit.com/r/rust/b", 2, 1).unwrap(); + VoteData::from_recorded(1, "https://reddit.com/r/rust/a", "https://reddit.com/r/rust/b", 2, 1).unwrap(); tree.apply_vote(&parent, ab); let group = tree.get(&parent).unwrap().local_ranking.clone(); let pool = children_of(&tree, &parent); let pair = suggest_next_pair_in_pool(&group, &pool, None).unwrap(); let chosen = pair_set(&pair); - assert!(chosen.contains("reddit.com/r/rust/c")); - assert!(chosen.contains("reddit.com/r/rust/a") || chosen.contains("reddit.com/r/rust/b")); + assert!(chosen.contains("https://reddit.com/r/rust/c")); + assert!(chosen.contains("https://reddit.com/r/rust/a") || chosen.contains("https://reddit.com/r/rust/b")); } #[test] fn suggest_zips_adjacent_ranks_when_tree_complete() { - let parent = ItemId::parse("reddit.com/r/rust").unwrap(); + let parent = ItemId::parse("https://reddit.com/r/rust").unwrap(); let mut tree = seed_children( &parent, &[ - "reddit.com/r/rust/a", - "reddit.com/r/rust/b", - "reddit.com/r/rust/c", + "https://reddit.com/r/rust/a", + "https://reddit.com/r/rust/b", + "https://reddit.com/r/rust/c", ], ); for (a, b, l, r) in [ - ("reddit.com/r/rust/a", "reddit.com/r/rust/b", 3, 1), - ("reddit.com/r/rust/a", "reddit.com/r/rust/c", 2, 1), + ("https://reddit.com/r/rust/a", "https://reddit.com/r/rust/b", 3, 1), + ("https://reddit.com/r/rust/a", "https://reddit.com/r/rust/c", 2, 1), ] { let v = VoteData::from_recorded(1, a, b, l, r).unwrap(); tree.apply_vote(&parent, v); @@ -506,26 +506,26 @@ mod tests { let pool = children_of(&tree, &parent); let pair = suggest_next_pair_in_pool(&group, &pool, None).unwrap(); let chosen = pair_set(&pair); - assert!(chosen.contains("reddit.com/r/rust/b")); - assert!(chosen.contains("reddit.com/r/rust/c")); + assert!(chosen.contains("https://reddit.com/r/rust/b")); + assert!(chosen.contains("https://reddit.com/r/rust/c")); } #[test] fn suggest_zip_prefers_1v2_before_2v3_when_both_unvoted() { - let parent = ItemId::parse("reddit.com/r/rust").unwrap(); + let parent = ItemId::parse("https://reddit.com/r/rust").unwrap(); let mut tree = seed_children( &parent, &[ - "reddit.com/r/rust/a", - "reddit.com/r/rust/b", - "reddit.com/r/rust/c", - "reddit.com/r/rust/d", + "https://reddit.com/r/rust/a", + "https://reddit.com/r/rust/b", + "https://reddit.com/r/rust/c", + "https://reddit.com/r/rust/d", ], ); for (a, b, l, r) in [ - ("reddit.com/r/rust/c", "reddit.com/r/rust/d", 3, 1), - ("reddit.com/r/rust/b", "reddit.com/r/rust/c", 2, 1), - ("reddit.com/r/rust/a", "reddit.com/r/rust/c", 2, 1), + ("https://reddit.com/r/rust/c", "https://reddit.com/r/rust/d", 3, 1), + ("https://reddit.com/r/rust/b", "https://reddit.com/r/rust/c", 2, 1), + ("https://reddit.com/r/rust/a", "https://reddit.com/r/rust/c", 2, 1), ] { let v = VoteData::from_recorded(1, a, b, l, r).unwrap(); tree.apply_vote(&parent, v); @@ -534,16 +534,16 @@ mod tests { let pool = children_of(&tree, &parent); let pair = suggest_next_pair_in_pool(&group, &pool, None).unwrap(); let chosen = pair_set(&pair); - assert!(chosen.contains("reddit.com/r/rust/a")); - assert!(chosen.contains("reddit.com/r/rust/b")); + assert!(chosen.contains("https://reddit.com/r/rust/a")); + assert!(chosen.contains("https://reddit.com/r/rust/b")); } #[test] fn resolve_pair_picks_from_pool() { - let parent = ItemId::parse("reddit.com/r/rust").unwrap(); - let tree = seed_children(&parent, &["reddit.com/r/rust/a", "reddit.com/r/rust/b"]); + let parent = ItemId::parse("https://reddit.com/r/rust").unwrap(); + let tree = seed_children(&parent, &["https://reddit.com/r/rust/a", "https://reddit.com/r/rust/b"]); let pair = resolve_pair(&tree, &parent, None, None).unwrap(); - let pool: HashSet<_> = ["reddit.com/r/rust/a", "reddit.com/r/rust/b"] + let pool: HashSet<_> = ["https://reddit.com/r/rust/a", "https://reddit.com/r/rust/b"] .into_iter() .collect(); assert!(pool.contains(pair.0.as_str())); diff --git a/server/src/parser.rs b/server/src/parser.rs index 9df2dcc9313f7fe250ce3b6aa167f6ec5d57951f..b2a963dd6cab415576c8d3a9a241966758565bb8 100644 --- a/server/src/parser.rs +++ b/server/src/parser.rs @@ -23,7 +23,7 @@ mod tests { fn parses_short_path() { assert_eq!( parse_reddit_url("r/rust").unwrap().as_str(), - "reddit.com/r/rust" + "https://reddit.com/r/rust" ); } @@ -33,7 +33,7 @@ mod tests { parse_reddit_url("https://www.reddit.com/r/programming/hot") .unwrap() .as_str(), - "reddit.com/r/programming" + "https://reddit.com/r/programming" ); } @@ -43,7 +43,10 @@ mod tests { "https://old.reddit.com/r/AmItheAsshole/comments/1trnvdl/aita_for_cancelling/", ) .unwrap(); - assert_eq!(id.as_str(), "reddit.com/r/amitheasshole/comments/1trnvdl"); + assert_eq!( + id.as_str(), + "https://reddit.com/r/amitheasshole/comments/1trnvdl" + ); } #[test] diff --git a/server/src/path_types.rs b/server/src/path_types.rs index fafd924452f6fd85e7a5b27ed2653a19581e6e56..71ffc01f35c5589686ff05dae3b5610fa01f30ce 100644 --- a/server/src/path_types.rs +++ b/server/src/path_types.rs @@ -1,13 +1,14 @@ use serde::{Deserialize, Serialize}; use std::fmt; -/// Canonical hierarchical identity for any URL/path in the fractal tree. +use crate::url_rules::{looks_like_url, navigable_breadcrumbs, parent_url, resolve_canonical}; + +/// Canonical identity: a real URL (with scheme) or an opaque non-URL key. #[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, Default)] pub struct ItemId(String); impl ItemId { - /// Parse an already-canonical path (no URL normalization). Empty string is invalid here; - /// use [`Self::root`] for the tree root. + /// Parse an already-canonical id (no normalization). Empty string is invalid; use [`Self::root`]. pub fn parse(s: &str) -> Option { let t = s.trim(); if t.is_empty() { @@ -16,12 +17,12 @@ impl ItemId { Some(Self(t.to_string())) } - /// Build an opaque item key (legacy demo votes, non-URL items). + /// Build an opaque item key (demo votes, non-URL items). pub fn opaque(s: impl Into) -> Self { Self(s.into()) } - /// Root of the internet tree (empty path). + /// Root of the internet tree. pub fn root() -> Self { Self(String::new()) } @@ -34,23 +35,18 @@ impl ItemId { &self.0 } - /// Creates a canonical ID from a raw URL or path. Normalizes domains and - /// trims tracking query params. + /// Canonical URL from a raw pasted or fetched URL. pub fn from_url(raw_url: &str) -> Option { - Self::canonicalize(raw_url).map(Self) + resolve_canonical(raw_url).map(Self) } - /// Normalize strings from forms, events, and Reddit imports into the same - /// stored id shape (e.g. drop post title slug after comment id). + /// Normalize strings from forms, events, and imports into canonical identity. pub fn from_storage(s: &str) -> Option { let t = s.trim(); if t.is_empty() { return None; } - if t.contains("://") || t.starts_with("r/") { - return Self::from_url(t).or_else(|| Self::parse(t)); - } - if t.starts_with("reddit.com/") && t.contains("/comments/") { + if looks_like_url(t) { return Self::from_url(t).or_else(|| Self::parse(t)); } Self::parse(t).or_else(|| Self::from_url(t)) @@ -62,35 +58,52 @@ impl ItemId { if s.is_empty() { return Self::root(); } - Self(format!("reddit.com/r/{s}")) + if looks_like_url(s) || s.contains('/') { + Self::from_storage(s).unwrap_or_else(|| Self::opaque(s)) + } else { + Self(format!("https://reddit.com/r/{s}")) + } } - /// Extract the parent, e.g. `reddit.com/r/aww/comments/1trnvdl` → - /// `reddit.com/r/aww`. + /// Immediate parent scope in the tree. pub fn parent(&self) -> Option { - if self.0.is_empty() { + if self.is_root() { return None; } - + if looks_like_url(self.0.as_str()) { + return parent_url(self.0.as_str()).map(Self); + } let parts: Vec<&str> = self.0.trim_end_matches('/').split('/').collect(); if parts.len() <= 1 { return None; } - - if self.0.contains("/comments/") { - return Some(Self(parts[..parts.len().saturating_sub(2)].join("/"))); - } - Some(Self(parts[..parts.len() - 1].join("/"))) } pub fn segments(&self) -> Vec<&str> { + if self.is_root() { + return vec![]; + } + if let Some(rest) = self.0.strip_prefix("https://") { + return rest.split('/').filter(|s| !s.is_empty()).collect(); + } + if let Some(rest) = self.0.strip_prefix("http://") { + return rest.split('/').filter(|s| !s.is_empty()).collect(); + } self.0.split('/').filter(|s| !s.is_empty()).collect() } - /// Cumulative paths for breadcrumb rendering, e.g. - /// `reddit.com/r/movies` → `["reddit.com", "reddit.com/r", "reddit.com/r/movies"]`. + /// Cumulative navigable paths for breadcrumbs and tree wiring (includes self). pub fn breadcrumb_paths(&self) -> Vec { + if self.is_root() { + return vec![]; + } + if looks_like_url(self.0.as_str()) { + return navigable_breadcrumbs(self.0.as_str()) + .into_iter() + .map(ItemId) + .collect(); + } let segs = self.segments(); let mut paths = Vec::with_capacity(segs.len()); let mut current = String::new(); @@ -111,13 +124,13 @@ impl ItemId { if self.is_root() { return String::new(); } - if self.as_str().contains("://") { - return self.as_str().to_string(); + if self.0.contains("://") { + return self.0.clone(); } if self.segments().first().is_some_and(|s| s.contains('.')) { - format!("https://{}", self.as_str()) + format!("https://{}", self.0) } else { - self.as_str().to_string() + self.0.clone() } } @@ -144,70 +157,6 @@ impl ItemId { pub fn from_browse_uri(path: &str) -> Option { path.strip_prefix("/~/").map(ItemId::from_browse_tail) } - - fn canonicalize(raw: &str) -> Option { - let s = raw.trim(); - if s.is_empty() { - return None; - } - - let owned = if let Some(rest) = s.strip_prefix("r/") { - format!("reddit.com/r/{rest}") - } else if let Some(rest) = s.strip_prefix("/r/") { - format!("reddit.com/r/{rest}") - } else { - s.to_string() - }; - - let (host_path, _query) = split_query(&owned); - let host_path = host_path.trim_end_matches('/'); - - let path = if host_path.contains("://") { - parse_url_host_path(host_path)? - } else if host_path.starts_with("reddit.com") || host_path.starts_with("www.reddit.com") { - normalize_reddit_host_path(host_path) - } else if host_path.contains('/') { - host_path.to_string() - } else { - return None; - }; - - Some(normalize_reddit_path(&path)) - } -} - -fn split_query(s: &str) -> (&str, Option<&str>) { - if let Some((path, q)) = s.split_once('?') { - (path, Some(q)) - } else { - (s, None) - } -} - -fn parse_url_host_path(url: &str) -> Option { - let rest = url - .strip_prefix("https://") - .or_else(|| url.strip_prefix("http://")) - .unwrap_or(url); - let (host, path) = rest.split_once('/').unwrap_or((rest, "")); - let host = normalize_host(host); - if path.is_empty() { - Some(host) - } else { - Some(format!("{host}/{path}")) - } -} - -fn normalize_host(host: &str) -> String { - let h = host - .strip_prefix("www.") - .unwrap_or(host) - .to_ascii_lowercase(); - if h == "old.reddit.com" || h == "new.reddit.com" || h == "reddit.com" { - "reddit.com".to_string() - } else { - h - } } fn normalize_browse_tail(tail: &str) -> String { @@ -215,7 +164,6 @@ fn normalize_browse_tail(tail: &str) -> String { if t.is_empty() { return String::new(); } - // Some HTTP stacks collapse `https://` → `https:/` inside a path segment. if t.starts_with("https:/") && !t.starts_with("https://") { return format!("https://{}", &t[7..]); } @@ -225,33 +173,6 @@ fn normalize_browse_tail(tail: &str) -> String { t.to_string() } -fn normalize_reddit_host_path(s: &str) -> String { - let (host, path) = s.split_once('/').unwrap_or((s, "")); - let host = normalize_host(host); - if path.is_empty() { - host - } else { - format!("{host}/{path}") - } -} - -/// Lowercase subreddit segment, drop listing suffixes, drop title slug after post id. -fn normalize_reddit_path(path: &str) -> String { - let mut parts: Vec = path.split('/').map(str::to_string).collect(); - if parts.len() >= 3 && parts[1] == "r" { - parts[2] = parts[2].to_ascii_lowercase(); - } - if let Some(i) = parts.iter().position(|p| p == "comments") { - if parts.len() > i + 2 { - parts.truncate(i + 2); - } - } else if parts.len() > 3 && parts.get(1).map(|s| s.as_str()) == Some("r") { - // reddit.com/r/{sub}/hot → reddit.com/r/{sub} - parts.truncate(3); - } - parts.join("/") -} - impl fmt::Display for ItemId { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str(&self.0) @@ -268,43 +189,59 @@ mod tests { "https://old.reddit.com/r/AmItheAsshole/comments/1trnvdl/aita_for_cancelling/", ) .unwrap(); - assert_eq!(id.as_str(), "reddit.com/r/amitheasshole/comments/1trnvdl"); + assert_eq!( + id.as_str(), + "https://reddit.com/r/amitheasshole/comments/1trnvdl" + ); } #[test] fn from_url_strips_query() { let id = ItemId::from_url("https://www.reddit.com/r/rust/?sort=top").unwrap(); - assert_eq!(id.as_str(), "reddit.com/r/rust"); + assert_eq!(id.as_str(), "https://reddit.com/r/rust"); } #[test] fn from_url_short_path() { assert_eq!( ItemId::from_url("r/rust").unwrap().as_str(), - "reddit.com/r/rust" + "https://reddit.com/r/rust" ); } #[test] fn parent_of_post_is_subreddit() { - let id = ItemId::parse("reddit.com/r/aww/comments/1trnvdl").unwrap(); - assert_eq!(id.parent().unwrap().as_str(), "reddit.com/r/aww"); + let id = ItemId::from_url("https://reddit.com/r/aww/comments/1trnvdl").unwrap(); + assert_eq!(id.parent().unwrap().as_str(), "https://reddit.com/r/aww"); } #[test] fn parent_of_subreddit_is_r_segment() { - let id = ItemId::parse("reddit.com/r/movies").unwrap(); - assert_eq!(id.parent().unwrap().as_str(), "reddit.com/r"); + let id = ItemId::from_url("https://reddit.com/r/movies").unwrap(); + assert_eq!(id.parent().unwrap().as_str(), "https://reddit.com/r"); + } + + #[test] + fn breadcrumb_paths_skip_phantom_comments() { + let id = ItemId::from_url("https://reddit.com/r/aww/comments/1trnvdl").unwrap(); + let crumbs = id.breadcrumb_paths(); + let paths: Vec<_> = crumbs.iter().map(|p| p.as_str()).collect(); + assert!(!paths.iter().any(|p| p.ends_with("/comments"))); + assert!(paths.contains(&"https://reddit.com/r/aww")); } #[test] - fn breadcrumb_paths() { - let id = ItemId::parse("reddit.com/r/movies").unwrap(); + fn breadcrumb_paths_subreddit() { + let id = ItemId::from_url("https://reddit.com/r/movies").unwrap(); let crumbs = id.breadcrumb_paths(); let paths: Vec<_> = crumbs.iter().map(|p| p.as_str()).collect(); assert_eq!( paths, - vec!["reddit.com", "reddit.com/r", "reddit.com/r/movies"] + vec![ + "https://reddit.com", + "https://reddit.com/r", + "https://reddit.com/r/movies" + ] ); } @@ -312,33 +249,33 @@ mod tests { fn legacy_scope_maps_to_reddit_sub() { assert_eq!( ItemId::from_legacy_scope("rust").as_str(), - "reddit.com/r/rust" + "https://reddit.com/r/rust" ); assert!(ItemId::from_legacy_scope("").is_root()); } #[test] - fn browse_href_wraps_canonical_path() { - let id = ItemId::parse("reddit.com/r/rust").unwrap(); + fn browse_href_wraps_canonical_url() { + let id = ItemId::from_url("https://reddit.com/r/rust").unwrap(); assert_eq!(id.browse_href(), "/~/https://reddit.com/r/rust"); } #[test] fn from_browse_tail_parses_full_url() { let id = ItemId::from_browse_tail("https://reddit.com/r/AmITheAsshole"); - assert_eq!(id.as_str(), "reddit.com/r/amitheasshole"); + assert_eq!(id.as_str(), "https://reddit.com/r/amitheasshole"); } #[test] fn from_storage_strips_post_title_slug() { let id = ItemId::from_storage("reddit.com/r/rust/comments/aaa/announcing_rust_199").unwrap(); - assert_eq!(id.as_str(), "reddit.com/r/rust/comments/aaa"); + assert_eq!(id.as_str(), "https://reddit.com/r/rust/comments/aaa"); } #[test] fn from_browse_uri_strips_prefix() { let id = ItemId::from_browse_uri("/~/https://reddit.com/r/rust").unwrap(); - assert_eq!(id.as_str(), "reddit.com/r/rust"); + assert_eq!(id.as_str(), "https://reddit.com/r/rust"); } } diff --git a/server/src/projection_apply.rs b/server/src/projection_apply.rs index ebe122e417bda1d9369a53443de93d28213c5a0d..5644557a41b3e9497c7421b444155ae629fa79f1 100644 --- a/server/src/projection_apply.rs +++ b/server/src/projection_apply.rs @@ -19,13 +19,21 @@ use crate::{ storage_schema::{ensure_path_writes, entity_view_writes, vote_writes}, }; -/// Legacy-compatible scope parsing for persisted vote events. +fn parse_event_id(id: &str) -> Result { + ItemId::from_storage(id) + .or_else(|| ItemId::parse(id)) + .ok_or_else(|| EventLogError::Apply(format!("invalid id: {id}"))) +} + +/// Scope key from a vote event (canonicalized at apply time). fn parent_from_event_scope(scope: &str) -> ItemId { - if scope.contains('/') { - ItemId::parse(scope).unwrap_or_else(|| ItemId::from_legacy_scope(scope)) - } else { - ItemId::from_legacy_scope(scope) + let s = scope.trim(); + if s.is_empty() { + return ItemId::root(); } + ItemId::from_storage(s) + .or_else(|| ItemId::parse(s)) + .unwrap_or_else(|| ItemId::from_legacy_scope(s)) } pub fn apply_records( @@ -68,15 +76,11 @@ pub fn apply_records( vote_parents.insert(parent); } Event::NodeEnsured { id } => { - let parsed = ItemId::parse(id) - .or_else(|| ItemId::from_url(id)) - .ok_or_else(|| EventLogError::Apply(format!("invalid node id: {id}")))?; + let parsed = parse_event_id(id)?; ensure_path_writes(&mut batch, &parsed); } Event::EntityImported { id, payload, .. } => { - let parsed = ItemId::parse(id) - .or_else(|| ItemId::from_url(id)) - .ok_or_else(|| EventLogError::Apply(format!("invalid entity id: {id}")))?; + let parsed = parse_event_id(id)?; let view = entity_view_from_payload(&parsed, payload); entity_view_writes(&mut batch, &parsed, view.as_ref()); entity_store @@ -92,7 +96,6 @@ pub fn apply_records( .commit_with(durable::Durability::DisableWal) .map_err(|e| EventLogError::Apply(e.to_string()))?; - // Cap recent-vote windows (idempotent, blind; not part of the cursor batch). for parent in vote_parents { projection_store .trim_recent_votes(&parent) diff --git a/server/src/reddit.rs b/server/src/reddit.rs index 72caf7c33b9dd44ea15b62b59e91227cf96a3431..20b7f9e3f8be39268a1767d09f5cf81eaa6ae0df 100644 --- a/server/src/reddit.rs +++ b/server/src/reddit.rs @@ -183,7 +183,7 @@ pub fn entity_view_from_payload( id: &ItemId, payload: &Value, ) -> Option { - if id.as_str().starts_with("reddit.com") { + if id.as_str().contains("reddit.com") { return parse_reddit_view(id, payload); } None @@ -495,41 +495,59 @@ fn rate_limit_reset_secs(resp: &reqwest::Response) -> u64 { .unwrap_or(5) } +fn reddit_path_segments(id: &ItemId) -> Option> { + let s = id.as_str(); + let rest = s + .strip_prefix("https://reddit.com/") + .or_else(|| s.strip_prefix("http://reddit.com/")) + .or_else(|| s.strip_prefix("reddit.com/"))?; + let segments: Vec = rest + .split('/') + .filter(|p| !p.is_empty()) + .map(str::to_string) + .collect(); + Some(segments) +} + pub fn map_item_to_reddit_api(id: &ItemId, api_base: &str) -> String { - let path = id.as_str(); - if !path.starts_with("reddit.com/") && path != "reddit.com" { - return String::new(); - } + let segments = match reddit_path_segments(id) { + Some(s) => s, + None if matches!( + id.as_str(), + "https://reddit.com" | "http://reddit.com" | "reddit.com" + ) => + { + return String::new(); + } + None => return String::new(), + }; let base = api_base.trim_end_matches('/'); - let segments: Vec<&str> = path.split('/').collect(); - - if let Some(i) = segments.iter().position(|&p| p == "comments") { + if let Some(i) = segments.iter().position(|p| p == "comments") { if segments.len() > i + 1 { - let api_path = segments[1..=i + 1].join("/"); + let api_path = segments[..=i + 1].join("/"); return format!("{base}/{api_path}.json?raw_json=1"); } } - if segments.len() == 3 && segments[1] == "r" { - return format!("{base}/r/{}/about.json?raw_json=1", segments[2]); + if segments.len() == 2 && segments[0] == "r" { + return format!("{base}/r/{}/about.json?raw_json=1", segments[1]); } String::new() } /// Listing URL for a node's children. Currently only subreddits -/// (`reddit.com/r/` → `/r/.json`) expose a child listing. +/// (`https://reddit.com/r/` → `/r/.json`) expose a child listing. pub fn map_children_url(id: &ItemId, api_base: &str) -> String { - let path = id.as_str(); - if !path.starts_with("reddit.com/") { - return String::new(); - } + let segments = match reddit_path_segments(id) { + Some(s) => s, + None => return String::new(), + }; let base = api_base.trim_end_matches('/'); - let segments: Vec<&str> = path.split('/').collect(); - if segments.len() == 3 && segments[1] == "r" { - return format!("{base}/r/{}.json?raw_json=1&limit=25", segments[2]); + if segments.len() == 2 && segments[0] == "r" { + return format!("{base}/r/{}.json?raw_json=1&limit=25", segments[1]); } String::new() } @@ -548,8 +566,8 @@ fn parse_children(_parent: &ItemId, payload: &Value) -> Vec<(ItemId, Value)> { Some(p) if !p.is_empty() => p, _ => continue, }; - let path = format!("reddit.com{}", permalink.trim_end_matches('/')); - if let Some(id) = ItemId::from_storage(&path) { + let raw = format!("https://reddit.com{}", permalink.trim_end_matches('/')); + if let Some(id) = ItemId::from_url(&raw) { out.push((id, child.clone())); } } @@ -683,7 +701,7 @@ mod tests { #[test] fn map_subreddit_about_url() { - let id = ItemId::parse("reddit.com/r/rust").unwrap(); + let id = ItemId::from_url("https://reddit.com/r/rust").unwrap(); assert_eq!( map_item_to_reddit_api(&id, "https://www.reddit.com"), "https://www.reddit.com/r/rust/about.json?raw_json=1" @@ -699,7 +717,8 @@ mod tests { let json = include_str!("../../test/fixtures/reddit/r_rust_about.json"); let v: Value = serde_json::from_str(json).unwrap(); let entity = - entity_view_from_payload(&ItemId::parse("reddit.com/r/rust").unwrap(), &v).unwrap(); + entity_view_from_payload(&ItemId::from_url("https://reddit.com/r/rust").unwrap(), &v) + .unwrap(); assert_eq!(entity.title, "The Rust Programming Language"); } @@ -707,7 +726,8 @@ mod tests { fn parse_post_listing_extracts_thumb_and_full_preview() { let json = include_str!("../../test/fixtures/reddit/post_preview.json"); let v: Value = serde_json::from_str(json).unwrap(); - let id = ItemId::parse("reddit.com/r/nsfw/comments/1tpy6a1/angel_eyes").unwrap(); + let id = + ItemId::from_url("https://reddit.com/r/nsfw/comments/1tpy6a1/angel_eyes").unwrap(); let entity = entity_view_from_payload(&id, &v).unwrap(); assert_eq!(entity.title, "Angel Eyes"); assert!(entity.thumb_url.as_ref().unwrap().contains("width=140")); diff --git a/server/src/reducer.rs b/server/src/reducer.rs index 6578f64a41726845517cdbf59a359c69e0aa56db..5179ddeca7a7cb0fb92dfc4aa9d5a80bd9125611 100644 --- a/server/src/reducer.rs +++ b/server/src/reducer.rs @@ -248,16 +248,18 @@ mod from_recorded_tests { #[test] fn ensure_path_wires_children() { let mut tree = GlobalTree::new(); - let id = ItemId::parse("reddit.com/r/rust").unwrap(); + let id = ItemId::from_url("https://reddit.com/r/rust").unwrap(); tree.ensure_path(&id); let root = tree.get(&ItemId::root()).unwrap(); assert!(root .children - .contains(&ItemId::parse("reddit.com").unwrap())); - let reddit = tree.get(&ItemId::parse("reddit.com").unwrap()).unwrap(); + .contains(&ItemId::from_url("https://reddit.com").unwrap())); + let reddit = tree + .get(&ItemId::from_url("https://reddit.com").unwrap()) + .unwrap(); assert!(reddit .children - .contains(&ItemId::parse("reddit.com/r").unwrap())); + .contains(&ItemId::from_url("https://reddit.com/r").unwrap())); let sub = tree.get(&id).unwrap(); assert_eq!(sub.id, id); } diff --git a/server/src/render/reddit.rs b/server/src/render/reddit.rs index 7f840aa33b734a31d8cf3341a0581c8bcb9bbcf3..595e202436040b0bfc419e68f083f94757ba5d0c 100644 --- a/server/src/render/reddit.rs +++ b/server/src/render/reddit.rs @@ -9,7 +9,7 @@ use crate::{ }; pub fn is_reddit_post(id: &ItemId) -> bool { - id.as_str().starts_with("reddit.com/") && id.as_str().contains("/comments/") + id.as_str().contains("reddit.com/") && id.as_str().contains("/comments/") } /// Post detail card (inside [`crate::fetch::html::entity_panel`]). diff --git a/server/src/state.rs b/server/src/state.rs index 78126f08d90f8069a586279d79258c27a9f9f7a4..513b329ef45fc332e63b3f8ed8498a1f59feb07c 100644 --- a/server/src/state.rs +++ b/server/src/state.rs @@ -217,15 +217,21 @@ impl AppState { ratio_right: i32, ) -> Result<(), String> { let ts = crate::html::now_ms(); - let vote = VoteData::from_recorded(ts, a, b, ratio_left, ratio_right) - .ok_or_else(|| "invalid vote: need two distinct non-empty items".to_string())?; + let a_raw = a.trim(); + let b_raw = b.trim(); + if a_raw.is_empty() || b_raw.is_empty() || a_raw == b_raw { + return Err("invalid vote: need two distinct non-empty items".to_string()); + } + // Validate items canonicalize (or are opaque keys) before append. + let _ = VoteData::from_recorded(ts, a_raw, b_raw, ratio_left, ratio_right) + .ok_or_else(|| "invalid vote: need two distinct parseable items".to_string())?; let event = Event::VoteRecorded { ts, - a: vote.a.as_str().to_string(), - b: vote.b.as_str().to_string(), - ratio_left: vote.ratio_left, - ratio_right: vote.ratio_right, + a: a_raw.to_string(), + b: b_raw.to_string(), + ratio_left, + ratio_right, scope: parent.as_str().to_string(), }; @@ -253,7 +259,7 @@ mod tests { let log = EventLog::new(log_path.to_string_lossy().into_owned()); let payload = json!({"kind":"t5","data":{"title":"Rust","display_name":"rust"}}); let event = Event::EntityImported { - id: "reddit.com/r/rust".into(), + id: "https://reddit.com/r/rust".into(), ts: 1, payload: payload.clone(), }; @@ -266,14 +272,14 @@ mod tests { .await .unwrap(); let tree = projection_store - .scope_tree(&ItemId::parse("reddit.com/r/rust").unwrap()) + .scope_tree(&ItemId::parse("https://reddit.com/r/rust").unwrap()) .unwrap(); let node = tree - .get(&ItemId::parse("reddit.com/r/rust").unwrap()) + .get(&ItemId::parse("https://reddit.com/r/rust").unwrap()) .unwrap(); assert_eq!(node.data.as_ref().unwrap().title, "Rust"); let stored = entity_store - .get(&ItemId::parse("reddit.com/r/rust").unwrap()) + .get(&ItemId::parse("https://reddit.com/r/rust").unwrap()) .unwrap() .unwrap(); assert_eq!(stored["data"]["display_name"], "rust"); @@ -289,13 +295,13 @@ mod tests { event_record( 1, Event::NodeEnsured { - id: "reddit.com/r/rust".into(), + id: "https://reddit.com/r/rust".into(), }, ), event_record( 2, Event::EntityImported { - id: "reddit.com/r/rust".into(), + id: "https://reddit.com/r/rust".into(), ts: 2, payload: payload.clone(), }, @@ -325,7 +331,7 @@ mod tests { &[event_record( 1, Event::NodeEnsured { - id: "reddit.com/r/stale".into(), + id: "https://reddit.com/r/stale".into(), }, )], ) @@ -352,11 +358,11 @@ mod tests { let root = tree.get(&ItemId::root()).unwrap(); assert!(root.children.contains(&ItemId::parse("alpha").unwrap())); assert!(projection_store - .load_node(&ItemId::parse("reddit.com/r/stale").unwrap()) + .load_node(&ItemId::parse("https://reddit.com/r/stale").unwrap()) .unwrap() .is_none()); let stored = entity_store - .get(&ItemId::parse("reddit.com/r/rust").unwrap()) + .get(&ItemId::parse("https://reddit.com/r/rust").unwrap()) .unwrap() .unwrap(); assert_eq!(stored["data"]["display_name"], "rust"); @@ -370,7 +376,7 @@ mod tests { log.append(&event_record( 1, Event::NodeEnsured { - id: "reddit.com/r/rust".into(), + id: "https://reddit.com/r/rust".into(), }, )) .await @@ -387,7 +393,7 @@ mod tests { &[event_record( 2, Event::NodeEnsured { - id: "reddit.com/r/rust".into(), + id: "https://reddit.com/r/rust".into(), }, )], ) @@ -454,7 +460,7 @@ mod tests { port: 0, }) .await; - let id = ItemId::parse("reddit.com/r/rust").unwrap(); + let id = ItemId::parse("https://reddit.com/r/rust").unwrap(); state.ensure_node(&id).await.unwrap(); @@ -465,11 +471,11 @@ mod tests { let projected = state.projection_store.load_tree().unwrap(); assert!(projected.get(&id).is_some()); let reddit = projected - .get(&ItemId::parse("reddit.com").unwrap()) + .get(&ItemId::from_url("https://reddit.com").unwrap()) .unwrap(); assert!(reddit .children - .contains(&ItemId::parse("reddit.com/r").unwrap())); + .contains(&ItemId::from_url("https://reddit.com/r").unwrap())); } #[tokio::test] @@ -510,7 +516,7 @@ mod tests { log.append(&event_record( 1, Event::NodeEnsured { - id: "reddit.com/r/rust".into(), + id: "https://reddit.com/r/rust".into(), }, )) .await @@ -518,7 +524,7 @@ mod tests { log.append(&event_record( 2, Event::NodeEnsured { - id: "reddit.com/r/python".into(), + id: "https://reddit.com/r/python".into(), }, )) .await @@ -544,13 +550,13 @@ mod tests { 2 ); let tree = second - .scope_tree(&ItemId::parse("reddit.com/r/rust").unwrap()) + .scope_tree(&ItemId::parse("https://reddit.com/r/rust").unwrap()) .unwrap(); assert!(tree - .get(&ItemId::parse("reddit.com/r/rust").unwrap()) + .get(&ItemId::parse("https://reddit.com/r/rust").unwrap()) .is_some()); assert!(tree - .get(&ItemId::parse("reddit.com/r/python").unwrap()) + .get(&ItemId::parse("https://reddit.com/r/python").unwrap()) .is_none()); } @@ -611,7 +617,7 @@ mod tests { #[test] fn parse_item_param_from_url() { let id = parse_item_param("https://reddit.com/r/rust"); - assert_eq!(id.as_str(), "reddit.com/r/rust"); + assert_eq!(id.as_str(), "https://reddit.com/r/rust"); } #[test] diff --git a/server/src/url_rules/engine.rs b/server/src/url_rules/engine.rs new file mode 100644 index 0000000000000000000000000000000000000000..e29b6b48c08deb7bffe031b1e542b1e25a7bef15 --- /dev/null +++ b/server/src/url_rules/engine.rs @@ -0,0 +1,187 @@ +//! Composable URL normalization primitives. + +use std::collections::HashMap; + +use url::Url; + +/// Mutable URL view used by rule combinators before serializing to a canonical string. +#[derive(Debug, Clone)] +pub struct ParsedUrl { + pub scheme: String, + pub host: String, + pub path_segments: Vec, + pub query: HashMap, + pub fragment: Option, +} + +impl ParsedUrl { + pub fn parse(raw: &str) -> Option { + let trimmed = raw.trim(); + if trimmed.is_empty() { + return None; + } + + let with_scheme = if trimmed.contains("://") { + trimmed.to_string() + } else if trimmed.starts_with("r/") || trimmed.starts_with("/r/") { + let rest = trimmed.trim_start_matches('/').trim_start_matches("r/"); + format!("https://reddit.com/r/{rest}") + } else if trimmed.contains('.') && !trimmed.starts_with('/') { + format!("https://{trimmed}") + } else { + trimmed.to_string() + }; + + let url = Url::parse(&with_scheme).ok()?; + let host = url.host_str()?.to_string(); + let path_segments: Vec = url + .path_segments() + .map(|segs| segs.filter(|s| !s.is_empty()).map(str::to_string).collect()) + .unwrap_or_default(); + + let mut query = HashMap::new(); + for (k, v) in url.query_pairs() { + query.insert(k.into_owned(), v.into_owned()); + } + + Some(Self { + scheme: url.scheme().to_string(), + path_segments, + query, + fragment: url.fragment().map(str::to_string), + host, + }) + } + + pub fn with_path_segments(&self, segments: &[String]) -> Self { + let mut u = self.clone(); + u.path_segments = segments.to_vec(); + u + } + + pub fn to_url(&self) -> Option { + let mut url = if self.path_segments.is_empty() { + Url::parse(&format!("{}://{}", self.scheme, self.host)).ok()? + } else { + let path = format!("/{}", self.path_segments.join("/")); + Url::parse(&format!("{}://{}{}", self.scheme, self.host, path)).ok()? + }; + if !self.query.is_empty() { + let mut pairs: Vec<_> = self.query.iter().collect(); + pairs.sort_by(|a, b| a.0.cmp(b.0)); + url.query_pairs_mut().clear(); + for (k, v) in pairs { + url.query_pairs_mut().append_pair(k, v); + } + } + if let Some(ref frag) = self.fragment { + url.set_fragment(Some(frag)); + } + Some(url) + } + + pub fn canonical_string(&self) -> Option { + let url = self.to_url()?; + let mut s = url.to_string(); + if self.path_segments.is_empty() { + s = s.trim_end_matches('/').to_string(); + } + Some(s) + } +} + +pub fn force_https(u: &mut ParsedUrl) { + if u.scheme == "http" { + u.scheme = "https".to_string(); + } +} + +pub fn drop_fragment(u: &mut ParsedUrl) { + u.fragment = None; +} + +pub fn strip_www(u: &mut ParsedUrl) { + if u.host.starts_with("www.") { + u.host = u.host[4..].to_string(); + } +} + +pub fn lowercase_host(u: &mut ParsedUrl) { + u.host = u.host.to_ascii_lowercase(); +} + +pub fn lowercase_path(u: &mut ParsedUrl) { + for seg in &mut u.path_segments { + *seg = seg.to_ascii_lowercase(); + } +} + +pub fn clear_query(u: &mut ParsedUrl) { + u.query.clear(); +} + +pub fn keep_only_query(u: &mut ParsedUrl, keys: &[&str]) { + u.query + .retain(|k, _| keys.iter().any(|want| want == &k.as_str())); +} + +pub fn strip_tracking_params(u: &mut ParsedUrl) { + u.query.retain(|k, _| { + let lower = k.to_ascii_lowercase(); + !(lower.starts_with("utm_") + || matches!( + lower.as_str(), + "fbclid" | "gclid" | "ref" | "ref_src" | "ref_source" | "mc_cid" | "mc_eid" + )) + }); +} + +pub fn truncate_after_segment(u: &mut ParsedUrl, name: &str, keep: usize) { + if let Some(i) = u.path_segments.iter().position(|s| s == name) { + let end = (i + 1 + keep).min(u.path_segments.len()); + u.path_segments.truncate(end); + } +} + +pub fn drop_listing_suffix(u: &mut ParsedUrl, suffixes: &[&str]) { + if u.path_segments.len() >= 3 && u.path_segments.first().map(String::as_str) == Some("r") { + if let Some(last) = u.path_segments.last() { + if suffixes.iter().any(|s| *s == last.as_str()) { + u.path_segments.pop(); + } + } + } +} + +pub fn normalize_reddit_host(u: &mut ParsedUrl) { + if matches!( + u.host.as_str(), + "old.reddit.com" | "new.reddit.com" | "www.reddit.com" + ) { + u.host = "reddit.com".to_string(); + } +} + +pub fn rewrite_youtu_be(u: &mut ParsedUrl) { + if u.host == "youtu.be" && u.path_segments.len() == 1 { + let id = u.path_segments[0].clone(); + u.host = "youtube.com".to_string(); + u.path_segments = vec!["watch".to_string()]; + u.query.insert("v".to_string(), id); + } +} + +pub fn rewrite_youtube_shorts(u: &mut ParsedUrl) { + if u.host == "youtube.com" && u.path_segments.first().map(String::as_str) == Some("shorts") { + if let Some(id) = u.path_segments.get(1).cloned() { + u.path_segments = vec!["watch".to_string()]; + u.query.insert("v".to_string(), id); + } + } +} + +pub fn normalize_youtube_host(u: &mut ParsedUrl) { + if matches!(u.host.as_str(), "m.youtube.com" | "www.youtube.com") { + u.host = "youtube.com".to_string(); + } +} diff --git a/server/src/url_rules/mod.rs b/server/src/url_rules/mod.rs new file mode 100644 index 0000000000000000000000000000000000000000..03d53bd3e82d704a01ba3fd8dd02b7d31422c0de --- /dev/null +++ b/server/src/url_rules/mod.rs @@ -0,0 +1,13 @@ +//! URL canonicalization and hierarchy rules for [`crate::path_types::ItemId`]. + +mod engine; +mod registry; + +pub use registry::{ + canonicalize_raw, looks_like_url, navigable_breadcrumbs, parent_url, resolve_id, CanonicalResult, +}; + +/// Resolve raw input to canonical URL. +pub fn resolve_canonical(raw: &str) -> Option { + canonicalize_raw(raw.trim()).map(|r| r.canonical) +} diff --git a/server/src/url_rules/registry.rs b/server/src/url_rules/registry.rs new file mode 100644 index 0000000000000000000000000000000000000000..14514e9af8385fb2b9b2f35eb9ee14d453d4b97c --- /dev/null +++ b/server/src/url_rules/registry.rs @@ -0,0 +1,235 @@ +//! Per-domain canonicalization and hierarchy rules. + +use std::collections::HashSet; + +use super::engine::{ + clear_query, drop_fragment, drop_listing_suffix, force_https, keep_only_query, lowercase_host, + lowercase_path, normalize_reddit_host, normalize_youtube_host, rewrite_youtu_be, + rewrite_youtube_shorts, strip_tracking_params, strip_www, truncate_after_segment, ParsedUrl, +}; + +/// Result of canonicalizing a raw URL string. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct CanonicalResult { + pub canonical: String, + /// When the input normalizes to a different string, the original is an alias. + pub alias_of: Option, +} + +fn apply_global(u: &mut ParsedUrl) { + force_https(u); + drop_fragment(u); + strip_www(u); + lowercase_host(u); + strip_tracking_params(u); +} + +fn normalize_reddit(u: &mut ParsedUrl) { + normalize_reddit_host(u); + lowercase_path(u); + truncate_after_segment(u, "comments", 1); + drop_listing_suffix(u, &["hot", "top", "new", "rising", "controversial"]); + clear_query(u); +} + +fn normalize_youtube(u: &mut ParsedUrl) { + rewrite_youtu_be(u); + normalize_youtube_host(u); + rewrite_youtube_shorts(u); + keep_only_query(u, &["v", "list"]); +} + +fn normalize_default(_u: &mut ParsedUrl) { + // Global rules only. +} + +fn domain_key(host: &str) -> &'static str { + if host == "reddit.com" || host.ends_with(".reddit.com") { + "reddit.com" + } else if host == "youtube.com" || host == "youtu.be" { + "youtube.com" + } else { + "default" + } +} + +fn normalize_for_host(u: &mut ParsedUrl) { + apply_global(u); + match domain_key(&u.host) { + "reddit.com" => normalize_reddit(u), + "youtube.com" => normalize_youtube(u), + _ => normalize_default(u), + } +} + +/// Structural path segments that must not become standalone tree nodes when more path follows. +fn structural_trailing(host: &str) -> &'static [&'static str] { + match domain_key(host) { + "reddit.com" => &["comments"], + _ => &[], + } +} + +/// Canonicalize a raw URL. Returns `None` if the input is not URL-like. +pub fn canonicalize_raw(raw: &str) -> Option { + let trimmed = raw.trim(); + if trimmed.is_empty() { + return None; + } + let mut u = ParsedUrl::parse(trimmed)?; + let input_snapshot = u.canonical_string()?; + normalize_for_host(&mut u); + let canonical = u.canonical_string()?; + let alias_of = if input_snapshot != canonical { + Some(trimmed.to_string()) + } else { + None + }; + Some(CanonicalResult { + canonical, + alias_of, + }) +} + +/// Resolve a stored or event id string to its canonical URL identity. +pub fn resolve_id(raw: &str) -> Option { + canonicalize_raw(raw).map(|r| r.canonical) +} + +/// Navigable ancestor URLs from domain root up to and including `canonical` (full URLs). +pub fn navigable_breadcrumbs(canonical: &str) -> Vec { + let Some(u) = ParsedUrl::parse(canonical) else { + return vec![canonical.to_string()]; + }; + let structural: HashSet<&str> = structural_trailing(&u.host).iter().copied().collect(); + let n = u.path_segments.len(); + let mut out = Vec::new(); + + // Domain root (no path segments). + if let Some(base) = u.with_path_segments(&[]).canonical_string() { + out.push(base); + } + + for i in 0..n { + let segs: Vec = u.path_segments[..=i].to_vec(); + let is_last = i == n - 1; + let seg = u.path_segments[i].as_str(); + if structural.contains(seg) && !is_last { + continue; + } + if let Some(url) = u.with_path_segments(&segs).canonical_string() { + if out.last() != Some(&url) { + out.push(url); + } + } + } + out +} + +/// Immediate parent scope URL, or `None` for tree root / opaque single-segment ids. +pub fn parent_url(canonical: &str) -> Option { + let crumbs = navigable_breadcrumbs(canonical); + if crumbs.len() <= 1 { + None + } else { + crumbs.get(crumbs.len() - 2).cloned() + } +} + +/// True when `raw` looks like a URL (has scheme or host-like shape). +pub fn looks_like_url(raw: &str) -> bool { + let t = raw.trim(); + t.contains("://") + || t.starts_with("r/") + || t.starts_with("/r/") + || (t.contains('.') && t.contains('/')) + || t.starts_with("reddit.com") + || t.starts_with("www.") + || t.starts_with("youtu.be/") +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn reddit_post_drops_slug_and_normalizes_host() { + let r = canonicalize_raw( + "https://old.reddit.com/r/AmItheAsshole/comments/1trnvdl/aita_for_cancelling/", + ) + .unwrap(); + assert_eq!( + r.canonical, + "https://reddit.com/r/amitheasshole/comments/1trnvdl" + ); + } + + #[test] + fn reddit_strips_query_and_listing() { + assert_eq!( + canonicalize_raw("https://www.reddit.com/r/rust/?sort=top") + .unwrap() + .canonical, + "https://reddit.com/r/rust" + ); + assert_eq!( + canonicalize_raw("https://www.reddit.com/r/programming/hot") + .unwrap() + .canonical, + "https://reddit.com/r/programming" + ); + } + + #[test] + fn reddit_short_path() { + assert_eq!( + canonicalize_raw("r/rust").unwrap().canonical, + "https://reddit.com/r/rust" + ); + } + + #[test] + fn reddit_breadcrumbs_skip_phantom_comments() { + let post = "https://reddit.com/r/aww/comments/1trnvdl"; + let crumbs = navigable_breadcrumbs(post); + assert!(!crumbs.iter().any(|c| c.ends_with("/comments"))); + assert_eq!( + crumbs.last().map(String::as_str), + Some(post) + ); + assert!(crumbs.contains(&"https://reddit.com/r/aww".to_string())); + } + + #[test] + fn reddit_parent_of_post_is_subreddit() { + assert_eq!( + parent_url("https://reddit.com/r/aww/comments/1trnvdl").as_deref(), + Some("https://reddit.com/r/aww") + ); + } + + #[test] + fn youtube_youtu_be_and_watch_same_canonical() { + let a = canonicalize_raw("https://youtu.be/dQw4w9WgXcQ").unwrap().canonical; + let b = canonicalize_raw("https://www.youtube.com/watch?v=dQw4w9WgXcQ&t=10").unwrap(); + assert_eq!(a, b.canonical); + assert_eq!(a, "https://youtube.com/watch?v=dQw4w9WgXcQ"); + } + + #[test] + fn legacy_schemeless_upgrades() { + assert_eq!( + canonicalize_raw("reddit.com/r/rust/comments/aaa/announcing_rust_199") + .unwrap() + .canonical, + "https://reddit.com/r/rust/comments/aaa" + ); + } + + #[test] + fn alias_recorded_when_input_differs() { + let r = canonicalize_raw("https://youtu.be/abc123").unwrap(); + assert_eq!(r.canonical, "https://youtube.com/watch?v=abc123"); + assert!(r.alias_of.is_some()); + } +} Side B — contributor: tommy-mor Side B — commit message: [3b3d5873] item refactor Side B — unified diff (full patch): diff --git a/plan.md b/plan.md deleted file mode 100644 index 00d6867a1e0ed144a16a020ea037f685ce646c73..0000000000000000000000000000000000000000 --- a/plan.md +++ /dev/null @@ -1,155 +0,0 @@ -# Plan: `ItemId` + `RouteContext` (identity vs hrefs) - -This document is for **the next agent** to continue the refactor without re-deriving context from chat. It supersedes ad-hoc notes: treat it as the checklist of record until the work lands and this file is deleted or trimmed. - -## Goal - -- **Identity** (what lives in the reducer graph, votes, indexes) becomes a **structural `ItemId` enum** in `slug-types`, not a canonical `String` / `CanonicalItemUrl` newtype. -- **Presentation** (tilde / dash display, breadcrumbs) derives from `ItemId` via explicit methods, not string stripping. -- **Routing** (browser `href`s for public vs room) goes through **`RouteContext`** (started in `server/src/html/routing.rs`) so Maud/handlers do not stitch `/r/…` vs `/~` ad hoc. - -**Non-goals for v1 of the migration:** backward-compatible JSONL or dual-read of old canonical strings in the event log (project has accepted breaking changes). If you reintroduce compat, document it here. - -## Current state (as of this plan) - -- **`CanonicalItemUrl`** (`types/src/paths.rs`): newtype around `String`; `parse` / `parent` / `display_path` / `tilde_tail` / etc. Reducer `ContentState`, `VoteData`, ranking, RPC, search, garden, breadcrumbs all use it or `String` keys derived from it. -- **`ThreadNav`** (`server/src/html/forum/nav.rs`): encodes scope prefixes for threads and garden URLs; **`RouteContext`** now wraps `ThreadNav` (`server/src/html/routing.rs`, re-exported from `server/src/html/mod.rs`) but **most HTML still takes `&ThreadNav` directly** — migration incomplete. -- **URL normalization** lives in `types/src/url_normalize.rs` + `canonicalize_item` / `finalize_external_identity_url` in `paths.rs` (YouTube, sorted query params, room path `room_route_segment` in `paths.rs`). -- **Room HTTP paths** are `/r/{short}{slug}` (fused segment); wire **`room_id`** remains `short/slug` for RPC/events. - -## Target architecture - -### `ItemId` (types) - -Suggested shape (adjust after profiling `Ord` / `Hash` / serde size): - -```text -ItemId::Root — tilde ontology root (today `SLUG_TILDE_ONTOLOGY_ROOT`) -ItemId::Local { segments } — slug.social ~/… path as Vec (lowercase segments, non-empty for non-root) -ItemId::External { url: Url } — normalized `url::Url` (crate `url` already in `slug-types`) -``` - -**API surface (minimum):** - -- `ItemId::parse(&str) -> Option` — single entry from DSL / user input / legacy wire (internally may call `canonicalize_item` + structured split). -- `ItemId::to_wire_url(&self) -> String` — only for **external** boundaries if needed (HTTP fetch, rare assertions); avoid using as the primary key once maps use `ItemId`. -- `parent`, `display_path`, `tilde_tail` / `tilde_http_tail`, `tilde_segments`, `last_segment`, `normalized_storage` — port from `CanonicalItemUrl`. -- **`Ord` + `Hash` + `Eq`** stable for `BTreeSet` / `HashMap` (see `write_actor` scope-rank snapshots). -- **`Serialize` / `Deserialize`** — decide **tagged JSON** for any persisted or API-carried structs (e.g. `VoteData` in tests). If RPC must stay stringy for clients, use a **DTO layer** that converts `ItemId` ↔ wire at the boundary only. - -**Remove:** `CanonicalItemUrl` type and all `path_types::CanonicalItemUrl` / `slug_types::paths::CanonicalItemUrl` exports once call sites are migrated. **`Borrow`** on the old newtype goes away; update `nav!` / any code that assumed map keys borrowed as `str`. - -### `RouteContext` (server HTML) - -- **File:** `server/src/html/routing.rs` — **`RouteContext(ThreadNav)`** with `item_href`, `item_href_raw`, `thread_url`, `garden_root_url`, `room_url`, `From`/`Into` `ThreadNav`. -- **Direction:** new code and refactored Maud should take **`&RouteContext`** (or owned where appropriate) instead of `&ThreadNav` when building links. Long term, **`item_href(&ItemId)`** should not parse strings — it should pattern-match `ItemId` and append tilde tail or `/-/…` external tail using the same rules as today’s `ThreadNav::garden_item_url`. - -### Axum / garden routes - -- **No** single catch-all route (explicit decision): keep the existing router layout in `server/src/lib.rs`. -- Room routes stay **`/r/:room_key/...`** with `room_key` fused; parsing via `slug_types::room_id_from_route_segment` / `room_route_segment` in `paths.rs`. - -## Phased execution (recommended order) - -### Phase 0 — Preconditions (quick) - -1. Read **`AGENTS.md`** (UI contract, durability matrix, `RpcCommand` vs `HtmlUiAction`). -2. Run **`cargo test --workspace`** and **`./scripts/clj-test.sh`** on clean `main` before large diffs; repeat after each phase. - -### Phase 1 — `ItemId` in `slug-types` (no server yet) - -1. Add **`ItemId`** (new file e.g. `types/src/item_id.rs` **or** inline at bottom of `paths.rs` — see **Module cycle** below). -2. Implement **`ItemId::parse`** using existing **`canonicalize_item`** + normalization; port **`CanonicalItemUrl`** methods to **`ItemId`** with tests ported from `paths.rs` `#[cfg(test)] mod tests`. -3. **`GardenItemUrl::from_stored(&ItemId, room_wire)`** (and thread helpers) — build absolute hrefs from structure, not from re-parsing a canonical string. -4. **`TildeHttpPathTail::to_item_id`** (rename from `to_canonical`) / **`tilde_http_path_to_item_id`**. -5. **`TildeOntologyPath::from_stored(&ItemId)`**. -6. Export **`ItemId`** from **`types/src/lib.rs`**; update **`server/src/path_types.rs`** re-exports. -7. **Delete `CanonicalItemUrl`** and fix all **in-crate** references in `types` only until `cargo test` passes for `slug-types`. - -**Module cycle trap:** `item_id.rs` must not `use crate::paths::{...}` if `paths.rs` also imports `ItemId` for `GardenItemUrl` in the same module. **Fix one of:** - -- **A)** Put `ItemId` **inside `paths.rs`** below `canonicalize_item` / helpers (simplest, large file), or -- **B)** Split **`canonicalize_item`** (+ dash host helpers + `finalize_external_identity_url`) into **`types/src/item_wire.rs`**, then `paths.rs` + `item_id.rs` both depend on `item_wire` only (cleaner, more files). - -### Phase 2 — Reducer + ranking (server core) - -1. **`server/src/reducer.rs`**: `ContentState` / `GroupState` / **`VoteData`** — replace **`CanonicalItemUrl`** with **`ItemId`** on all maps, sets, deques, vectors. -2. **`apply_vote`**: normalize `a`/`b` via **`ItemId::parse`** or **`ItemId`**-aware logic (remove string round-trip). -3. **`apply_ingest_to_content`**: **`dsl`** still yields strings for item titles in statements; normalize to **`ItemId`** at ingest boundary via **`ItemId::parse`** once per item. -4. **`server/src/ranking.rs`**, **`server/src/scope_rank.rs`**, **`server/src/api/write_actor.rs`** (including **`BTreeSet`** ordering), **`server/src/api/validate.rs`**, **`server/src/api/helpers.rs`** — propagate **`ItemId`**. -5. **`server/tests/basic.rs`** and any reducer tests constructing **`VoteData`** — use **`ItemId::parse(...).unwrap()`** or helpers. - -### Phase 3 — RPC + search + external resolver - -1. **`server/src/api/rpc.rs`**: rank/pair/matchup/search payloads; today many paths use **`GardenItemUrl::from_storage_str(item.as_str(), …)`** — switch to **`ItemId`** + **`GardenItemUrl::from_stored(&item_id, …)`** (or equivalent). -2. **`server/src/html/search.rs`**: scoring uses item path strings — derive from **`ItemId::display_path`** / **`to_wire_url`** only at the scoring boundary if needed. -3. **`server/src/external_resolver.rs`**: take **`&ItemId`** or **`ItemId::external_url()`** instead of **`&CanonicalItemUrl`**. - -### Phase 4 — HTML / Maud - -1. **`ThreadNav::garden_item_url`**: overload or replace with **`garden_item_href(&self, item: &ItemId)`** (no `CanonicalItemUrl::parse` inside). -2. **`RouteContext`**: extend **`item_href(&ItemId)`**; migrate call sites from **`ThreadNav`** to **`RouteContext`** where only link-building is needed (keep **`ThreadNav`** where scope / auth helpers need the full struct). -3. **`server/src/html/garden.rs`**, **`breadcrumb_path.rs`**, **`forum/*`**, **`editor.rs`**: replace **`CanonicalItemUrl`** with **`ItemId`**; breadcrumbs should walk **`ItemId::parent`** without string `rsplit`. -4. **`types` JSON types** (`RankRow`, etc.): decide whether **`GardenItemUrl`** stays string for JSON or becomes a structured field; keep **one** wire format for the public API. - -### Phase 5 — Cleanup + docs - -1. Remove dead **`canonical_path`** / **`breadcrumb_path`** string logic if fully superseded. -2. Update **`AGENTS.md`** if durability, `POST /ui`, or command surfaces change. -3. Delete or shrink **`plan.md`** when done. - -## File / symbol checklist (non-exhaustive — grep-driven) - -Run periodically: - -```bash -rg "CanonicalItemUrl" -g'*.rs' -rg "path_types::CanonicalItemUrl" -g'*.rs' -rg "tilde_http_path_to_canonical" -g'*.rs' -``` - -**High-touch files (from prior exploration):** - -| Area | Files | -|------|--------| -| Types | `types/src/paths.rs`, `types/src/lib.rs`, `types/src/url_normalize.rs`, (optional) `types/src/item_id.rs`, `types/src/item_wire.rs` | -| Server re-exports | `server/src/path_types.rs`, `server/src/canonical_path.rs` | -| Reducer / ingest | `server/src/reducer.rs`, `server/src/dsl.rs` (parse output types if changed) | -| Ranking | `server/src/ranking.rs`, `server/src/scope_rank.rs` | -| Writer / RPC | `server/src/api/write_actor.rs`, `server/src/api/rpc.rs`, `server/src/api/helpers.rs`, `server/src/api/validate.rs` | -| HTML | `server/src/html/garden.rs`, `server/src/html/breadcrumb_path.rs`, `server/src/html/forum/nav.rs`, `server/src/html/routing.rs`, `server/src/html/search.rs`, `server/src/html/editor.rs`, `server/src/html/forum/ingest.rs`, … | -| Tests | `server/tests/basic.rs`, `server/tests/integration.rs`, `types/src/paths.rs` tests, Clojure under `test/` if URLs/assertions mention canonical shapes | - -## Events / JSONL - -- **`Ingest`** events store **`raw` DSL** only — no change required for item identity inside the event. -- If any future event type stores item ids as strings, migrate to **structured `ItemId` serde** or accept string only at the event boundary with immediate parse into **`ItemId`** on `apply_event`. - -## `nav!` macro (`server/src/paths.rs`) - -- Macros use **`keypath($key)`** with **`.clone()`** — **`ItemId`** must be **`Clone`** (already for enums). Remove any reliance on **`Borrow`** for map keys. - -## Testing gate - -After each phase: - -```bash -cargo test --workspace -./scripts/clj-test.sh -``` - -## Risks / gotchas - -1. **`Ord` on `ItemId`**: must match prior **`CanonicalItemUrl`** / `String` ordering wherever **`BTreeSet`** is used (e.g. deterministic scope-rank snapshots in **`write_actor`**). -2. **External `ItemId`**: **`Url`** equality / hashing — normalization is already centralized in **`url_normalize`**; ensure **`ItemId::parse`** always inserts normalized **`Url`** into **`External`**. -3. **Fake parent URLs** in garden (e.g. **`https://.`** for external root ranking): find all **`parse("https://.")`** style hacks and express as **`ItemId`** or a dedicated sentinel. -4. **Serde**: tests and any RPC clients that snapshot JSON may need expectation updates if **`VoteData`** shape changes. - -## Optional follow-ups (not blocking `ItemId`) - -- More **domain normalizers** in **`url_normalize.rs`** (e.g. `music.youtube.com`, Spotify, etc.). -- **Room wire** vs **HTTP segment** helpers already in **`paths.rs`** (`ROOM_SHORT_ID_LEN`, `room_route_segment`, `room_id_from_route_segment`). - ---- - -**End state criteria:** `rg CanonicalItemUrl` returns nothing; reducer maps use **`ItemId`**; HTML link generation for items goes through **`RouteContext` + `ItemId`**; tests and Kaocha green. diff --git a/server/src/api/helpers.rs b/server/src/api/helpers.rs index 1b291db83df7364a026f2e147e0a29a70a399371..8cd23a02fa5219d6aa375e766e6b2bd2c7bb7dfb 100644 --- a/server/src/api/helpers.rs +++ b/server/src/api/helpers.rs @@ -4,7 +4,8 @@ use axum::{ Json, }; use sha2::{Digest, Sha256}; -use slug_types::paths::{CanonicalItemUrl, GardenItemUrl}; +use slug_types::paths::GardenItemUrl; +use slug_types::ItemId; use slug_types::*; use std::collections::HashMap; @@ -31,12 +32,12 @@ pub fn now_ms() -> i64 { } /// Resolve DSL/user input to a stored canonical item id. -pub fn resolve_item(item: &str) -> Result { +pub fn resolve_item(item: &str) -> Result { let canonical = canonicalize_item(item); if canonical.is_empty() { return Err(format!("empty item path: `{}`", item)); } - Ok(CanonicalItemUrl(canonical)) + ItemId::parse(&canonical).ok_or_else(|| format!("invalid item path: `{}`", item)) } pub fn parse_parent_specs(parent: Option<&String>) -> Vec { @@ -94,7 +95,7 @@ pub fn paginate_rankings( (out_components, out_unranked) } -pub fn pick_random_distinct_canonical(items: &[CanonicalItemUrl]) -> Option<(CanonicalItemUrl, CanonicalItemUrl)> { +pub fn pick_random_distinct_canonical(items: &[ItemId]) -> Option<(ItemId, ItemId)> { use rand::seq::SliceRandom; if items.len() < 2 { return None; @@ -115,15 +116,15 @@ pub fn pick_random_distinct_canonical(items: &[CanonicalItemUrl]) -> Option<(Can } pub fn is_pair_voted(group: &crate::reducer::GroupState, a: &str, b: &str) -> bool { - let a_key = CanonicalItemUrl(a.to_string()); - let b_key = CanonicalItemUrl(b.to_string()); + let a_key = ItemId::parse(a).unwrap_or_else(|| ItemId::opaque(a.to_string())); + let b_key = ItemId::parse(b).unwrap_or_else(|| ItemId::opaque(b.to_string())); let Some(&a_idx) = group.item_to_idx.get(&a_key) else { return false; }; let Some(&b_idx) = group.item_to_idx.get(&b_key) else { return false; }; let (i, j) = if a_idx < b_idx { (a_idx, b_idx) } else { (b_idx, a_idx) }; group.voted_pairs.contains(&(i, j)) } -pub fn compute_connectivity_stats(group: &crate::reducer::GroupState, pool: &[CanonicalItemUrl]) -> ConnectivityStats { +pub fn compute_connectivity_stats(group: &crate::reducer::GroupState, pool: &[ItemId]) -> ConnectivityStats { let n = pool.len(); let global_idxs: Vec> = pool diff --git a/server/src/api/rpc.rs b/server/src/api/rpc.rs index 0ff2d701dd1e8661f58abd55672cd91280e9491e..079d96eb6f1d717c203b1d4aec09f3916384b919 100644 --- a/server/src/api/rpc.rs +++ b/server/src/api/rpc.rs @@ -17,7 +17,7 @@ use crate::{ dsl, events::{Event, Ingest, ThreadCapability}, identity::{parse_agent, parse_username}, - path_types::CanonicalItemUrl, + path_types::ItemId, ranking::{connected_components_from_voted_pairs, ranked_items_subset}, reducer::{scope_from_room_wire, ReducerState, ScopeId}, state::{AppState, InviteState}, @@ -150,7 +150,7 @@ fn build_rank_response_for_content( if !is_global && !specs.is_empty() { let none_exist = specs.iter().all(|spec| { - let Some(canon) = CanonicalItemUrl::parse(spec) else { return true }; + let Some(canon) = ItemId::parse(spec) else { return true }; !content.items.contains(&canon) && !content.item_children.contains_key(&canon) }); if none_exist { @@ -163,10 +163,10 @@ fn build_rank_response_for_content( let depth = depth.max(1); let rankings = if is_global { - let all_items: Vec = content.items.iter().cloned().collect(); + let all_items: Vec = content.items.iter().cloned().collect(); crate::scope_rank::build_rankings_for_item_set(content, &all_items) } else if specs.is_empty() { - crate::scope_rank::build_children_rankings(content, &CanonicalItemUrl::ontology_root()) + crate::scope_rank::build_children_rankings(content, &ItemId::ontology_root()) } else if depth > 1 { let items = crate::scope_rank::resolve_scope_recursive(content, &specs, depth); crate::scope_rank::build_rankings_for_item_set(content, &items) @@ -359,8 +359,8 @@ async fn rpc_check( let mut simulated = { reduced_arc.read().await.clone() }; simulated.apply_event(event); - let voted_parents: Vec = { - let mut parents: HashSet = HashSet::new(); + let voted_parents: Vec = { + let mut parents: HashSet = HashSet::new(); for s in &v.doc.statements { if let dsl::Stmt::Vote { item1, item2, .. } = s { if let (Ok(a), Ok(b)) = (resolve_item(item1), resolve_item(item2)) { @@ -369,7 +369,7 @@ async fn rpc_check( } } } - let mut out: Vec = parents.into_iter().collect(); + let mut out: Vec = parents.into_iter().collect(); out.sort(); out }; @@ -695,7 +695,7 @@ fn rpc_search(reduced: &ReducerState, q: &str, limit: usize, principal: Option<& async fn rpc_get_pair(state: &AppState, room: String, parent_path: String) -> Result { let scope = scope_from_room_wire(&room); let reduced_arc = state.reduced.clone(); - let pool: Vec = { + let pool: Vec = { let reduced = reduced_arc.read().await; let content = content_for_room(&reduced, &room); let tmp = if parent_path.trim().is_empty() { @@ -716,7 +716,7 @@ async fn rpc_get_pair(state: &AppState, room: String, parent_path: String) -> Re Some("add items via ingest".into()), )); } - let selected: Option<(CanonicalItemUrl, CanonicalItemUrl)> = { + let selected: Option<(ItemId, ItemId)> = { let mut reduced = reduced_arc.write().await; let content = reduced.content.entry(scope.clone()).or_default(); let group = &mut content.ranking_group; @@ -729,16 +729,16 @@ async fn rpc_get_pair(state: &AppState, room: String, parent_path: String) -> Re .filter_map(|it| group.item_to_idx.get(it).copied()) .collect(); let ranked = ranked_items_subset(group, &idxs, 10000, 1e-8); - let ranked_set: HashSet = ranked.iter().map(|r| r.item.clone()).collect(); - let unsorted: Vec = pool + let ranked_set: HashSet = ranked.iter().map(|r| r.item.clone()).collect(); + let unsorted: Vec = pool .iter() .filter(|it| !ranked_set.contains(*it)) .cloned() .collect(); - let mut pick: Option<(CanonicalItemUrl, CanonicalItemUrl)> = None; + let mut pick: Option<(ItemId, ItemId)> = None; if !unsorted.is_empty() { if let Some(left) = unsorted.choose(&mut rng).cloned() { - let mut candidates: Vec = if !ranked.is_empty() { + let mut candidates: Vec = if !ranked.is_empty() { ranked.iter().map(|r| r.item.clone()).collect() } else { pool.clone() @@ -865,7 +865,7 @@ pub async fn handle_rpc_batch( } else { let content = content_for_room(&reduced, &room); let item_str = canonicalize_item(&item_path); - let item = CanonicalItemUrl(item_str.clone()); + let item = ItemId::parse(&item_str).unwrap_or_else(|| ItemId::opaque(item_str.clone())); if !content.items.contains(&item) { line_err( "item not found", @@ -1212,7 +1212,7 @@ pub async fn handle_rpc_batch( } let ranked_total = ranked.len(); - let mut unranked: Vec = content + let mut unranked: Vec = content .items .iter() .filter(|it| !group.item_to_idx.contains_key(*it)) @@ -1263,7 +1263,7 @@ pub async fn handle_rpc_batch( } else { let content = content_for_room(&reduced, &room); let item_str = canonicalize_item(&item_path); - let item = CanonicalItemUrl(item_str.clone()); + let item = ItemId::parse(&item_str).unwrap_or_else(|| ItemId::opaque(item_str.clone())); let limit = limit.unwrap_or(50).clamp(1, 200); if !content.items.contains(&item) { line_err( @@ -1304,7 +1304,7 @@ pub async fn handle_rpc_batch( let content = content_for_room(&reduced, &room); let scope = scope_from_room_wire(&room); let item_str = canonicalize_item(&item_path); - let item = CanonicalItemUrl(item_str.clone()); + let item = ItemId::parse(&item_str).unwrap_or_else(|| ItemId::opaque(item_str.clone())); let entries = content.rank_history.get(&item).cloned().unwrap_or_default(); let history: Vec = entries.iter().map(|e| { let caused_by: Vec = reduced.ingests_by_id.get(&e.post_id) @@ -1361,7 +1361,11 @@ pub async fn handle_rpc_batch( line_err(e, h) } else { let content = content_for_room(&reduced, &room); - let parents: HashSet<&str> = content.item_children.keys().map(|s| s.as_str()).collect(); + let parents: HashSet = content + .item_children + .keys() + .map(|k| k.to_storage_string()) + .collect(); let mut paths: Vec = content .items .iter() @@ -1380,11 +1384,11 @@ pub async fn handle_rpc_batch( let content = content_for_room(&reduced, &room); let out: Vec = content .item_children - .get(&CanonicalItemUrl::ontology_root()) + .get(&ItemId::ontology_root()) .map(|roots| { let mut v: Vec = roots.iter() .map(|path| { - let children = content.item_children.get(path.as_str()).map(|s| s.len()).unwrap_or(0); + let children = content.item_children.get(path).map(|s| s.len()).unwrap_or(0); PathSummary { path: TildeOntologyPath::from_stored(path), children, diff --git a/server/src/api/validate.rs b/server/src/api/validate.rs index 3c7aa80fd485cf247231fe5c5c7e6fea62f22534..3f657a88ef9328a31e3e29c3bc3bd32bec6fd7a1 100644 --- a/server/src/api/validate.rs +++ b/server/src/api/validate.rs @@ -4,7 +4,7 @@ use std::collections::HashSet; use crate::{ canonical_path::canonicalize_tag, dsl, - path_types::CanonicalItemUrl, + path_types::ItemId, reducer::{ReducerState, ScopeId}, }; use slug_types::paths::GardenItemUrl; @@ -32,11 +32,11 @@ pub fn validate_ingest_document( ScopeId::Public => None, _ => reduced.content_for_scope(scope), }; - let item_exists = |key: &CanonicalItemUrl| { + let item_exists = |key: &ItemId| { scoped_content.map(|c| c.items.contains(key)).unwrap_or(false) || public_content.items.contains(key) }; - let body_exists = |key: &CanonicalItemUrl| { + let body_exists = |key: &ItemId| { scoped_content.map(|c| c.item_bodies.contains_key(key)).unwrap_or(false) || public_content.item_bodies.contains_key(key) }; @@ -52,7 +52,7 @@ pub fn validate_ingest_document( }; let ts = super::helpers::now_ms(); - let mut defined_in_doc: HashSet = HashSet::new(); + let mut defined_in_doc: HashSet = HashSet::new(); for s in &doc.statements { match s { diff --git a/server/src/api/write_actor.rs b/server/src/api/write_actor.rs index f9c3b8bd3fbf8fcb9c035e1a1572fef0b08fa8a9..3e96b015def54921962f74b4cd884808ff6c3bcc 100644 --- a/server/src/api/write_actor.rs +++ b/server/src/api/write_actor.rs @@ -10,7 +10,7 @@ use crate::{ events::{AgentBound, Event, GrantAdded, Ingest, PostRedacted, RoomDeleted, UserRegistered}, html::JsBuilder, identity::parse_agent, - path_types::CanonicalItemUrl, + path_types::ItemId, reducer::{scope_from_room_wire, ReducerState, ScopeId}, state::AppState, write_cmd::WriteCmd, @@ -90,7 +90,7 @@ async fn broadcast_web_refresh(state: &AppState, room_key: &str, thread_id: &str } fn compute_scope_rank_changes( - parent: &CanonicalItemUrl, + parent: &ItemId, before: &crate::scope_rank::ChildrenRankings, after: &crate::scope_rank::ChildrenRankings, room_wire: &str, @@ -99,7 +99,7 @@ fn compute_scope_rank_changes( use std::collections::BTreeSet; fn build_positions( rankings: &crate::scope_rank::ChildrenRankings, - ) -> HashMap> { + ) -> HashMap> { let mut map = HashMap::new(); for comp in &rankings.component_rankings { let total = comp.ranked.len(); @@ -116,7 +116,7 @@ fn compute_scope_rank_changes( let before_pos = build_positions(before); let after_pos = build_positions(after); - let all_items: BTreeSet = before_pos + let all_items: BTreeSet = before_pos .keys() .cloned() .chain(after_pos.keys().cloned()) @@ -287,8 +287,8 @@ pub async fn writer_actor(mut rx: mpsc::Receiver, state: AppState) { .map(|d| reduced.agent_bindings.get(d).is_none()) .unwrap_or(false); - let voted_parent_scopes: Vec = { - let mut parents: HashSet = HashSet::new(); + let voted_parent_scopes: Vec = { + let mut parents: HashSet = HashSet::new(); for s in &v.doc.statements { if let dsl::Stmt::Vote { item1, item2, .. } = s { if let (Ok(a), Ok(b)) = (resolve_item(item1), resolve_item(item2)) { @@ -301,12 +301,12 @@ pub async fn writer_actor(mut rx: mpsc::Receiver, state: AppState) { } } } - let mut out: Vec = parents.into_iter().collect(); + let mut out: Vec = parents.into_iter().collect(); out.sort(); out }; - let pre_rankings: HashMap = + let pre_rankings: HashMap = if !voted_parent_scopes.is_empty() { let content = content_for_room(&reduced, &room_key); voted_parent_scopes diff --git a/server/src/external_resolver.rs b/server/src/external_resolver.rs index 87409c4cab5f1930d2cd075f3417fa824fd791c7..6f5c982a627f9f620f0e3be0ba0cb92a3d7d4bb7 100644 --- a/server/src/external_resolver.rs +++ b/server/src/external_resolver.rs @@ -1,6 +1,6 @@ use async_trait::async_trait; -use crate::path_types::CanonicalItemUrl; +use crate::path_types::ItemId; #[async_trait] pub trait ExternalResolver: Send + Sync { @@ -11,7 +11,7 @@ pub trait ExternalResolver: Send + Sync { fn normalize(&self, path: &str) -> String; /// Fetches body when missing; GitHub hook lands here in a follow-up. - async fn fetch_body(&self, canonical_url: &CanonicalItemUrl) -> Result; + async fn fetch_body(&self, canonical_url: &ItemId) -> Result; } /// Placeholder until domain-specific resolvers exist. @@ -27,7 +27,7 @@ impl ExternalResolver for DefaultExternalResolver { path.to_string() } - async fn fetch_body(&self, _canonical_url: &CanonicalItemUrl) -> Result { + async fn fetch_body(&self, _canonical_url: &ItemId) -> Result { Err("external fetch not implemented".to_string()) } } diff --git a/server/src/html/breadcrumb_path.rs b/server/src/html/breadcrumb_path.rs index c8a3937923161a6ff248bd77e87eff0dc6fe9ab0..5743a98753f579c70e10961278469afd7cb9ddcf 100644 --- a/server/src/html/breadcrumb_path.rs +++ b/server/src/html/breadcrumb_path.rs @@ -1,8 +1,8 @@ -use crate::path_types::{tilde_http_path_to_canonical, CanonicalItemUrl}; +use crate::path_types::{tilde_http_path_to_item_id, ItemId}; /// Semantic view of an ontology path for rendering and routing decisions. pub(super) struct OntologyPath { - canonical: CanonicalItemUrl, + canonical: ItemId, /// Breadcrumb segments: for `~/a/b` this is `["a", "b"]` (leading `~` rendered separately). segments: Vec, } @@ -11,11 +11,11 @@ impl OntologyPath { /// Path is the `*path` segment from `/~/*path` (e.g. `topic/a`). Always treat it as under `~/` /// so it canonicalizes to `https://slug.social/~/…`, not the non-tilde site path. pub(super) fn from_input(path: &str) -> Self { - let canonical = tilde_http_path_to_canonical(path); + let canonical = tilde_http_path_to_item_id(path); Self::from_canonical(canonical) } - pub(super) fn from_canonical(canonical: CanonicalItemUrl) -> Self { + pub(super) fn from_canonical(canonical: ItemId) -> Self { // tilde_segments() returns ["~", "a", "b"] but bc_path() renders "~" itself, // so we skip the leading "~" segment here. let segments = canonical @@ -28,7 +28,7 @@ impl OntologyPath { } pub(super) fn root() -> Self { - Self::from_canonical(CanonicalItemUrl::ontology_root()) + Self::from_canonical(ItemId::ontology_root()) } pub(super) fn is_root(&self) -> bool { @@ -56,7 +56,7 @@ impl OntologyPath { /// External `https://host/…` items addressed as `/-/host/…` in the URL bar. pub(super) struct ExternalOntologyPath { - canonical: CanonicalItemUrl, + canonical: ItemId, /// e.g. `["github.com", "org", "repo", "issues"]` segments: Vec, } @@ -71,13 +71,13 @@ impl ExternalOntologyPath { } else { format!("-/{}", p.trim_start_matches('/')) }; - let Some(canonical) = CanonicalItemUrl::parse(&raw) else { - return Self::from_canonical(CanonicalItemUrl("https://.".to_string())); + let Some(canonical) = ItemId::parse(&raw) else { + return Self::from_canonical(ItemId::opaque("https://.".to_string())); }; Self::from_canonical(canonical) } - pub(super) fn from_canonical(canonical: CanonicalItemUrl) -> Self { + pub(super) fn from_canonical(canonical: ItemId) -> Self { let s = canonical.as_str(); let rest = s .strip_prefix("https://") diff --git a/server/src/html/editor.rs b/server/src/html/editor.rs index 80657ac0df9b4bb67e4cdacca296f70267840bd1..c76417acd7a164fda23537cd4e7fb9f1f1dcd895 100644 --- a/server/src/html/editor.rs +++ b/server/src/html/editor.rs @@ -97,7 +97,7 @@ pub async fn editor_check( simulated.apply_event(event); // Collect voted parent scopes. - let voted_parents: Vec = { + let voted_parents: Vec = { let mut parents = std::collections::HashSet::new(); for s in &v.doc.statements { if let crate::dsl::Stmt::Vote { item1, item2, .. } = s { @@ -107,7 +107,7 @@ pub async fn editor_check( } } } - let mut out: Vec = parents.into_iter().collect(); + let mut out: Vec = parents.into_iter().collect(); out.sort(); out }; diff --git a/server/src/html/forum/nav.rs b/server/src/html/forum/nav.rs index 48fe11e46731670874ff8b6b05baa6f09ae0b7e4..7ca5b491f38104ec8c81db43a95c59da00abd8c4 100644 --- a/server/src/html/forum/nav.rs +++ b/server/src/html/forum/nav.rs @@ -52,9 +52,14 @@ impl ThreadNav { } pub(crate) fn garden_item_url(&self, item: &str) -> String { - let Some(c) = crate::path_types::CanonicalItemUrl::parse(item) else { + let Some(c) = crate::path_types::ItemId::parse(item) else { return format!("{}/{}", self.garden_path_prefix, canonicalize_item(item)); }; + self.garden_item_href(&c) + } + + /// Relative href for a structured [`crate::path_types::ItemId`] in this scope’s garden. + pub(crate) fn garden_item_href(&self, c: &crate::path_types::ItemId) -> String { if let Some(tail) = c.tilde_tail().map(str::to_owned) { format!("{}/{}", self.garden_path_prefix, tail) } else if c.as_str().starts_with("http://") || c.as_str().starts_with("https://") { @@ -63,7 +68,7 @@ impl ThreadNav { let ext_prefix = format!("{}-", self.garden_path_prefix.trim_end_matches('~')); format!("{}/{}", ext_prefix, rest) } else { - format!("{}/{}", self.garden_path_prefix, canonicalize_item(item)) + format!("{}/{}", self.garden_path_prefix, canonicalize_item(c.as_str())) } } diff --git a/server/src/html/garden.rs b/server/src/html/garden.rs index e615dd356bcf634232d85610c0a26235ead125fd..2af9ac5bfdee1630883e6f8257883baeafcb5a44 100644 --- a/server/src/html/garden.rs +++ b/server/src/html/garden.rs @@ -10,7 +10,7 @@ use crate::{ api::optional_principal, canonical_path::canonicalize_item, events::ThreadCapability, - path_types::CanonicalItemUrl, + path_types::ItemId, reducer::{ContentState, ReducerState, ScopeId}, ranking::{connected_components_from_voted_pairs, ranked_items_subset}, scope_rank::{build_children_rankings, ChildrenRankings}, @@ -27,7 +27,7 @@ use super::{ /// Display path for an item: `~/…` or `-/…` form. fn item_display_path(item: &str) -> String { - CanonicalItemUrl::parse(item) + ItemId::parse(item) .map(|c| c.display_path()) .unwrap_or_else(|| canonicalize_item(item)) } @@ -159,7 +159,7 @@ pub async fn garden_index( let nav = ThreadNav::public(); let child_rankings = { let reduced = state.reduced.read().await; - build_children_rankings(reduced.public(), &CanonicalItemUrl::ontology_root()) + build_children_rankings(reduced.public(), &ItemId::ontology_root()) }; let page = layout( @@ -236,7 +236,7 @@ pub async fn external_garden_index( ) -> impl IntoResponse { let nav = ThreadNav::public(); let ext_path = ExternalOntologyPath::from_input(""); - let parent = CanonicalItemUrl::parse("https://.").unwrap(); + let parent = ItemId::parse("https://.").unwrap(); let child_rankings = { let reduced = state.reduced.read().await; build_children_rankings(reduced.public(), &parent) @@ -365,7 +365,7 @@ pub async fn room_external_garden_index( return room_not_found_page(&jar, &uri).into_response(); } let ext_path = ExternalOntologyPath::from_input(""); - let parent = CanonicalItemUrl::parse("https://.").unwrap(); + let parent = ItemId::parse("https://.").unwrap(); let child_rankings = build_children_rankings( content_for_garden_view(&reduced, &nav.scope()), &parent, @@ -509,13 +509,13 @@ struct ItemPageViewModel { fn build_sibling_rank( reduced: &crate::reducer::ReducerState, scope: &ScopeId, - item: &CanonicalItemUrl, + item: &ItemId, ) -> Option { let item = item.clone().normalized_storage(); let content = content_for_garden_view(reduced, scope); let group = &content.ranking_group; let parent = item.parent()?.normalized_storage(); - let siblings: Vec = content + let siblings: Vec = content .item_children .get(&parent) .map(|s| s.iter().cloned().collect()) @@ -574,7 +574,7 @@ fn build_rank_history( item: &str, ) -> Vec { let content = content_for_garden_view(reduced, scope); - let item_key = CanonicalItemUrl(item.to_string()); + let item_key = ItemId::parse(item).unwrap_or_else(|| ItemId::opaque(item.to_string())); let entries = match content.rank_history.get(&item_key) { None => return vec![], Some(e) => e, @@ -592,8 +592,8 @@ fn build_rank_history( if a_str == item || b_str == item { Some(crate::reducer::VoteData { ts: e.ts, - a: CanonicalItemUrl(a_str), - b: CanonicalItemUrl(b_str), + a: ItemId::parse(&a_str).unwrap_or_else(|| ItemId::opaque(a_str)), + b: ItemId::parse(&b_str).unwrap_or_else(|| ItemId::opaque(b_str)), ratio_left, ratio_right, body: explanation, principal: reduced.ingests_by_id.get(&e.post_id) @@ -629,8 +629,8 @@ fn build_item_page_view_model( item: &str, ) -> ItemPageViewModel { let content = content_for_garden_view(reduced, scope); - let item_key = CanonicalItemUrl::parse(item) - .unwrap_or_else(|| CanonicalItemUrl::parse("~/").unwrap()) + let item_key = ItemId::parse(item) + .unwrap_or_else(|| ItemId::parse("~/").unwrap()) .normalized_storage(); let item_has_parent = item_key.parent().is_some(); let child_rankings = build_children_rankings(content, &item_key); @@ -925,10 +925,10 @@ mod tests { .map(|r| r.item.as_str()) .collect(); assert_eq!(names, vec!["https://slug.social/~/topic/a", "https://slug.social/~/topic/b"]); - use crate::path_types::CanonicalItemUrl; + use crate::path_types::ItemId; assert!( - model.child_rankings.unranked_items.contains(&CanonicalItemUrl("https://slug.social/~/topic/kid1".to_string())) - || model.child_rankings.unranked_items.contains(&CanonicalItemUrl("https://slug.social/~/topic/kid2".to_string())) + model.child_rankings.unranked_items.contains(&ItemId::parse("https://slug.social/~/topic/kid1").unwrap()) + || model.child_rankings.unranked_items.contains(&ItemId::parse("https://slug.social/~/topic/kid2").unwrap()) ); } @@ -941,8 +941,8 @@ mod tests { "9ab12cd/my-room", "@00000000-0000-0000-0000-000000000000:test:local/test\n~/t1 {a}\n~/t2 {b}\n", ); - use crate::path_types::CanonicalItemUrl; - let root = CanonicalItemUrl::ontology_root(); + use crate::path_types::ItemId; + let root = ItemId::ontology_root(); let model = build_item_page_view_model( &reduced, &ScopeId::Room("9ab12cd/my-room".to_string()), @@ -971,8 +971,8 @@ mod tests { "@00000000-0000-0000-0000-000000000000:test:local/test\n\ ~/a {a}\n~/b {b}\n~/a 2:1 ~/b {because}\n", ); - use crate::path_types::CanonicalItemUrl; - let root = CanonicalItemUrl::ontology_root(); + use crate::path_types::ItemId; + let root = ItemId::ontology_root(); let model = build_item_page_view_model( &reduced, &ScopeId::Room("9ab12cd/my-room".to_string()), diff --git a/server/src/html/routing.rs b/server/src/html/routing.rs index 13e9ae0cbe32d454e34a7737edf8234d2a558502..df545a8195aa97484ef0012cff31d802ac67d9df 100644 --- a/server/src/html/routing.rs +++ b/server/src/html/routing.rs @@ -4,7 +4,7 @@ //! Prefer `RouteContext::item_href` / [`RouteContext::thread_url`] in new Maud over stitching //! `/r/…` vs `/~` manually. Call sites can migrate incrementally from passing `&ThreadNav`. -use crate::path_types::CanonicalItemUrl; +use crate::path_types::ItemId; use super::forum::ThreadNav; @@ -32,9 +32,9 @@ impl RouteContext { self.0 } - /// Relative path for a stored canonical item in this scope’s garden. - pub fn item_href(&self, item: &CanonicalItemUrl) -> String { - self.0.garden_item_url(item.as_str()) + /// Relative path for a stored item in this scope’s garden. + pub fn item_href(&self, item: &ItemId) -> String { + self.0.garden_item_href(item) } /// Same as [`Self::item_href`] but parses `item` first (raw DSL / user paste). diff --git a/server/src/path_types.rs b/server/src/path_types.rs index 361a9d446c9043cbdea5f060db2e8633c2fd9bf9..a6e0b90a1aaead0bc1f7e0f4c5296506edf3f7ac 100644 --- a/server/src/path_types.rs +++ b/server/src/path_types.rs @@ -1,5 +1,6 @@ -//! Re-exports — implementations live in `slug-types` (`paths` module). +//! Re-exports — implementations live in `slug-types` (`paths` / `item_id` modules). +pub use slug_types::ItemId; pub use slug_types::paths::{ - tilde_http_path_to_canonical, CanonicalItemUrl, RelativePath, TildeHttpPathTail, TildePath, + tilde_http_path_to_item_id, RelativePath, TildeHttpPathTail, TildePath, }; diff --git a/server/src/ranking.rs b/server/src/ranking.rs index b24e80ddd5f34b4e454bb81b541e8f54bd97a541..3710c9f64437f5bef3b2121905b6f3bcb7611047 100644 --- a/server/src/ranking.rs +++ b/server/src/ranking.rs @@ -1,11 +1,11 @@ use std::collections::HashMap; -use crate::path_types::CanonicalItemUrl; +use crate::path_types::ItemId; use crate::reducer::GroupState; #[derive(Debug, Clone)] pub struct RankedItem { - pub item: CanonicalItemUrl, + pub item: ItemId, pub score: f64, } @@ -239,7 +239,7 @@ pub fn group_summary_scores( group: &mut GroupState, max_iters: usize, tol: f64, -) -> HashMap { +) -> HashMap { ranked_items(group, max_iters, tol) .into_iter() .map(|r| (r.item, r.score)) @@ -256,11 +256,11 @@ mod tests { } fn vote(ts: i64, a: &str, b: &str, l: i32, r: i32) -> VoteData { - use crate::path_types::CanonicalItemUrl; + use crate::path_types::ItemId; VoteData { ts, - a: CanonicalItemUrl(a.to_string()), - b: CanonicalItemUrl(b.to_string()), + a: ItemId::parse(a).unwrap(), + b: ItemId::parse(b).unwrap(), ratio_left: l, ratio_right: r, body: "because".to_string(), diff --git a/server/src/reducer.rs b/server/src/reducer.rs index a8651994f2c062af25b6ad792a014dcc083296f9..cd11a5391de8e36b1c3bb6c9b8067ceba0878e31 100644 --- a/server/src/reducer.rs +++ b/server/src/reducer.rs @@ -5,7 +5,7 @@ use serde::{Deserialize, Serialize}; use crate::canonical_path::canonicalize_tag; use crate::dsl; use crate::events::{Event, Ingest, ThreadCapability}; -use crate::path_types::CanonicalItemUrl; +use crate::path_types::ItemId; #[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)] pub enum ScopeId { @@ -27,8 +27,8 @@ pub fn scope_from_room_wire(room: &str) -> ScopeId { #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] pub struct VoteData { pub ts: i64, - pub a: CanonicalItemUrl, - pub b: CanonicalItemUrl, + pub a: ItemId, + pub b: ItemId, pub ratio_left: i32, pub ratio_right: i32, pub body: String, @@ -40,8 +40,8 @@ pub struct VoteData { #[derive(Debug, Clone)] pub struct GroupState { - pub item_to_idx: HashMap, - pub idx_to_item: Vec, + pub item_to_idx: HashMap, + pub idx_to_item: Vec, /// Aggregated directed edge weights: (src_idx, dst_idx) -> weight. pub edges: HashMap<(usize, usize), f64>, @@ -72,7 +72,7 @@ impl GroupState { } } - fn ensure_item(&mut self, item: &CanonicalItemUrl) -> usize { + fn ensure_item(&mut self, item: &ItemId) -> usize { if let Some(&idx) = self.item_to_idx.get(item) { return idx; } @@ -85,11 +85,11 @@ impl GroupState { /// Public test helper: insert an item into the group without a vote (for unit tests). pub fn ensure_item_pub(&mut self, item: &str) -> usize { - if let Some(canon) = CanonicalItemUrl::parse(item) { + if let Some(canon) = ItemId::parse(item) { self.ensure_item(&canon) } else { // Fallback: treat as raw canonical string - let canon = CanonicalItemUrl(item.to_string()); + let canon = ItemId::opaque(item.to_string()); self.ensure_item(&canon) } } @@ -103,8 +103,8 @@ impl GroupState { } pub fn apply_vote(&mut self, mut vote: VoteData) { - vote.a = CanonicalItemUrl::parse(vote.a.as_str()).unwrap_or(vote.a); - vote.b = CanonicalItemUrl::parse(vote.b.as_str()).unwrap_or(vote.b); + vote.a = ItemId::parse(vote.a.as_str()).unwrap_or_else(|| vote.a.clone()); + vote.b = ItemId::parse(vote.b.as_str()).unwrap_or_else(|| vote.b.clone()); vote.thread_tag = canonicalize_tag(&vote.thread_tag); if vote.ratio_left < 0 { vote.ratio_left = 0; @@ -210,18 +210,18 @@ impl Default for ForumThreadState { #[derive(Debug, Clone, Default)] pub struct ContentState { pub ranking_group: GroupState, - pub items: HashSet, - pub item_bodies: HashMap, + pub items: HashSet, + pub item_bodies: HashMap, /// Parent canonical URL -> direct children. - pub item_children: HashMap>, + pub item_children: HashMap>, /// Per-item vote history (most recent first). - pub item_votes: HashMap>, + pub item_votes: HashMap>, /// Per-item ingest references (most recent first). - pub item_snippets: HashMap>, + pub item_snippets: HashMap>, /// Item path -> threads that mention or vote on this item. - pub item_threads: HashMap>, + pub item_threads: HashMap>, /// Per-item rank history, oldest first. - pub rank_history: HashMap>, + pub rank_history: HashMap>, } #[derive(Debug, Clone)] @@ -351,7 +351,7 @@ impl ReducerState { /// For `a/b/c/d` this creates: `a/b/c→d`, `a/b→a/b/c`, `a→a/b`, `""→a`. /// Stops early when an intermediate is already registered (its ancestors must be too). /// @e2bdefa9-a6fa-4725-b0a2-c0b09d95bb20:claudecode:anthropic/claude-opus-4 - fn add_child_edge(content: &mut ContentState, item: &CanonicalItemUrl) { + fn add_child_edge(content: &mut ContentState, item: &ItemId) { let mut child = item.clone(); loop { let Some(parent) = child.parent() else { break }; @@ -366,16 +366,16 @@ impl ReducerState { } /// Resolve an item path as a first-class canonical path. - fn normalize_item(item: &str) -> Option { - CanonicalItemUrl::parse(item) + fn normalize_item(item: &str) -> Option { + ItemId::parse(item) } /// 1-indexed rank of `item` within its connected component in the parent scope. /// 0 if the item has no votes connecting it to siblings (unranked). fn scope_rank_of( group: &GroupState, - item: &CanonicalItemUrl, - item_children: &HashMap>, + item: &ItemId, + item_children: &HashMap>, ) -> usize { let scope = match item.parent() { Some(p) => p, @@ -421,7 +421,7 @@ impl ReducerState { /// 1-indexed position of `item` in the component-aware global flat list. /// Components sorted largest-first; items ranked within each component. /// 0 if the item is not in the ranking group. - fn global_rank_of(group: &GroupState, item: &CanonicalItemUrl) -> usize { + fn global_rank_of(group: &GroupState, item: &ItemId) -> usize { if !group.item_to_idx.contains_key(item) { return 0; } @@ -448,7 +448,7 @@ impl ReducerState { let doc = dsl::parse_full(&ing.raw).map_err(|_| ())?; let canonical_thread = canonicalize_tag(&ing.thread_tag); - let voted_items: Vec = doc + let voted_items: Vec = doc .statements .iter() .filter_map(|s| { @@ -467,7 +467,7 @@ impl ReducerState { let principal = ing.principal.clone(); let delegate = ing.delegate.clone(); - let before: HashMap = if !voted_items.is_empty() { + let before: HashMap = if !voted_items.is_empty() { crate::ranking::compute_group_ranking(&mut content.ranking_group, 10000, 1e-8); voted_items .iter() @@ -485,7 +485,7 @@ impl ReducerState { HashMap::new() }; - let mut ingest_items: HashSet = HashSet::new(); + let mut ingest_items: HashSet = HashSet::new(); for stmt in doc.statements { match stmt { diff --git a/server/src/scope_rank.rs b/server/src/scope_rank.rs index d656b2f0a0a3623ca6b234b746beaaca8ae6017d..4fdaae150c74ec5162c2accc06d4cc778ad79914 100644 --- a/server/src/scope_rank.rs +++ b/server/src/scope_rank.rs @@ -3,7 +3,7 @@ use std::collections::{HashMap, HashSet}; -use crate::path_types::CanonicalItemUrl; +use crate::path_types::ItemId; use crate::ranking::{connected_components_from_voted_pairs, ranked_items_subset, RankedItem}; use crate::reducer::ContentState; @@ -17,12 +17,12 @@ pub struct ScopedComponent { pub struct ChildrenRankings { pub component_rankings: Vec, /// Items in scope with no rank (no votes connecting them to others in this scope). - pub unranked_items: Vec, + pub unranked_items: Vec, } /// Resolve one scope spec (literal path) to direct children of that parent. No wildcards. -fn resolve_one_scope(content: &ContentState, spec: &str) -> HashSet { - let Some(parent) = CanonicalItemUrl::parse(spec.trim()) else { +fn resolve_one_scope(content: &ContentState, spec: &str) -> HashSet { + let Some(parent) = ItemId::parse(spec.trim()) else { return HashSet::new(); }; content @@ -36,12 +36,12 @@ fn resolve_one_scope(content: &ContentState, spec: &str) -> HashSet Vec { +pub fn resolve_scope(content: &ContentState, specs: &[String]) -> Vec { let mut set = HashSet::new(); for spec in specs { set.extend(resolve_one_scope(content, spec)); } - let mut out: Vec = set.into_iter().collect(); + let mut out: Vec = set.into_iter().collect(); out.sort(); out } @@ -49,18 +49,18 @@ pub fn resolve_scope(content: &ContentState, specs: &[String]) -> Vec Vec { +pub fn resolve_scope_recursive(content: &ContentState, specs: &[String], depth: usize) -> Vec { if depth == 0 { return vec![]; } - let mut visited: HashSet = HashSet::new(); - let mut frontier: Vec = specs + let mut visited: HashSet = HashSet::new(); + let mut frontier: Vec = specs .iter() - .filter_map(|s| CanonicalItemUrl::parse(s)) + .filter_map(|s| ItemId::parse(s)) .collect(); for _level in 0..depth { - let mut next_frontier: Vec = Vec::new(); + let mut next_frontier: Vec = Vec::new(); for parent in &frontier { if let Some(children) = content.item_children.get(parent) { for child in children { @@ -76,16 +76,16 @@ pub fn resolve_scope_recursive(content: &ContentState, specs: &[String], depth: frontier = next_frontier; } - let mut out: Vec = visited.into_iter().collect(); + let mut out: Vec = visited.into_iter().collect(); out.sort(); out } /// Build connected-component rankings for an explicit set of item paths. /// Use this when scope comes from multiple parents (resolve_scope). -pub fn build_rankings_for_item_set(content: &ContentState, items_in_scope: &[CanonicalItemUrl]) -> ChildrenRankings { +pub fn build_rankings_for_item_set(content: &ContentState, items_in_scope: &[ItemId]) -> ChildrenRankings { let group = &content.ranking_group; - let mut items_in_scope: Vec = items_in_scope.to_vec(); + let mut items_in_scope: Vec = items_in_scope.to_vec(); items_in_scope.sort(); let scoped_idxs: Vec = items_in_scope @@ -127,7 +127,7 @@ pub fn build_rankings_for_item_set(content: &ContentState, items_in_scope: &[Can }) .collect(); - let mut unranked_items: Vec = isolate_local_idxs + let mut unranked_items: Vec = isolate_local_idxs .into_iter() .filter_map(|li| local_to_global.get(li).copied()) .filter_map(|idx| group.idx_to_item.get(idx).cloned()) @@ -148,9 +148,9 @@ pub fn build_rankings_for_item_set(content: &ContentState, items_in_scope: &[Can /// Build connected-component rankings for direct children of parent_scope. /// Matches the HTML garden view: multiple components, isolates, no-vote items. -pub fn build_children_rankings(content: &ContentState, parent: &CanonicalItemUrl) -> ChildrenRankings { +pub fn build_children_rankings(content: &ContentState, parent: &ItemId) -> ChildrenRankings { let parent = parent.clone().normalized_storage(); - let items: Vec = content + let items: Vec = content .item_children .get(&parent) .map(|s| s.iter().cloned().collect()) @@ -164,10 +164,13 @@ mod tests { use std::collections::{HashMap, HashSet}; fn content_with_children(edges: &[(&str, &[&str])]) -> ContentState { - let mut item_children: HashMap> = HashMap::new(); + let mut item_children: HashMap> = HashMap::new(); for (parent, children) in edges { - let parent = CanonicalItemUrl((*parent).to_string()); - let set: HashSet = children.iter().map(|s| CanonicalItemUrl((*s).to_string())).collect(); + let parent = ItemId::parse(parent).unwrap(); + let set: HashSet = children + .iter() + .map(|s| ItemId::parse(s).unwrap()) + .collect(); item_children.insert(parent, set); } ContentState { @@ -189,8 +192,8 @@ mod tests { ]); let out = resolve_one_scope(&content, "models"); assert_eq!(out.len(), 2); - assert!(out.contains(&CanonicalItemUrl("https://slug.social/models/x".to_string()))); - assert!(out.contains(&CanonicalItemUrl("https://slug.social/models/y".to_string()))); + assert!(out.contains(&ItemId::parse("https://slug.social/models/x").unwrap())); + assert!(out.contains(&ItemId::parse("https://slug.social/models/y").unwrap())); } #[test] @@ -201,9 +204,9 @@ mod tests { ]); let out = resolve_scope(&content, &["a".into(), "b".into()]); assert_eq!(out.len(), 4); - assert!(out.contains(&CanonicalItemUrl("https://slug.social/a/1".to_string()))); - assert!(out.contains(&CanonicalItemUrl("https://slug.social/a/2".to_string()))); - assert!(out.contains(&CanonicalItemUrl("https://slug.social/b/1".to_string()))); - assert!(out.contains(&CanonicalItemUrl("https://slug.social/b/2".to_string()))); + assert!(out.contains(&ItemId::parse("https://slug.social/a/1").unwrap())); + assert!(out.contains(&ItemId::parse("https://slug.social/a/2").unwrap())); + assert!(out.contains(&ItemId::parse("https://slug.social/b/1").unwrap())); + assert!(out.contains(&ItemId::parse("https://slug.social/b/2").unwrap())); } } diff --git a/server/tests/basic.rs b/server/tests/basic.rs index 17db86894f18d6b542d2943f4cedb0572b247171..c8efea2976e20b443d78a5d4c3b244bc4f2fd6da 100644 --- a/server/tests/basic.rs +++ b/server/tests/basic.rs @@ -7,8 +7,15 @@ use slugsocial_server::{ }; +use slugsocial_server::path_types::ItemId; + use tempfile::TempDir; +#[inline] +fn item_id(s: &str) -> ItemId { + ItemId::parse(s).unwrap() +} + fn ingest_event(ts: i64, raw: &str) -> Event { Event::Ingest(Ingest { ts, @@ -47,7 +54,7 @@ fn reducer_external_namespace_ranking() { let g = &content.ranking_group; assert_eq!(g.idx_to_item.len(), 2); - let parent = slugsocial_server::path_types::CanonicalItemUrl::parse("https://github.com/iss").unwrap(); + let parent = slugsocial_server::path_types::ItemId::parse("https://github.com/iss").unwrap(); let children = slugsocial_server::scope_rank::build_children_rankings(content, &parent); assert_eq!(children.component_rankings.len(), 1); let names: Vec<&str> = children.component_rankings[0] @@ -77,9 +84,9 @@ fn reducer_and_ranking_linear_chain() { let mut group = state.public().ranking_group.clone(); let ranked = ranked_items(&mut group, 20000, 1e-9); assert_eq!(ranked.len(), 3); - assert_eq!(ranked[0].item, "https://slug.social/~/t/a"); - assert_eq!(ranked[1].item, "https://slug.social/~/t/b"); - assert_eq!(ranked[2].item, "https://slug.social/~/t/c"); + assert_eq!(ranked[0].item.as_str(), "https://slug.social/~/t/a"); + assert_eq!(ranked[1].item.as_str(), "https://slug.social/~/t/b"); + assert_eq!(ranked[2].item.as_str(), "https://slug.social/~/t/c"); } #[test] @@ -112,15 +119,15 @@ fn reducer_handles_item_and_body_from_ingest() { )); let content = state.public(); - assert!(content.items.contains("https://slug.social/~/t/test-item")); + assert!(content.items.contains(&item_id("https://slug.social/~/t/test-item"))); assert_eq!( - content.item_bodies.get("https://slug.social/~/t/test-item"), + content.item_bodies.get(&item_id("https://slug.social/~/t/test-item")), Some(&"Description here".to_string()) ); assert!(content .item_children - .get("https://slug.social/~/t") - .map(|c| c.contains("https://slug.social/~/t/test-item")) + .get(&item_id("https://slug.social/~/t")) + .map(|c| c.contains(&item_id("https://slug.social/~/t/test-item"))) .unwrap_or(false)); } @@ -136,12 +143,12 @@ fn reducer_indexes_item_threads_and_vote_thread() { state.apply_event(Event::Ingest(ev)); let content = state.public(); - let threads_for_insertion = content.item_threads.get("https://slug.social/~/sorts/insertion").unwrap(); + let threads_for_insertion = content.item_threads.get(&item_id("https://slug.social/~/sorts/insertion")).unwrap(); assert!(threads_for_insertion.contains("sorting-hat")); - let threads_for_mergesort = content.item_threads.get("https://slug.social/~/sorts/mergesort").unwrap(); + let threads_for_mergesort = content.item_threads.get(&item_id("https://slug.social/~/sorts/mergesort")).unwrap(); assert!(threads_for_mergesort.contains("sorting-hat")); - let vote = content.item_votes.get("https://slug.social/~/sorts/insertion").unwrap().front().unwrap(); + let vote = content.item_votes.get(&item_id("https://slug.social/~/sorts/insertion")).unwrap().front().unwrap(); assert_eq!(vote.thread_tag, "sorting-hat"); } @@ -155,8 +162,8 @@ fn reducer_aggregates_multiple_votes() { } let group = &state.public().ranking_group; - let a_idx = group.item_to_idx["https://slug.social/~/t/a"]; - let b_idx = group.item_to_idx["https://slug.social/~/t/b"]; + let a_idx = group.item_to_idx[&item_id("https://slug.social/~/t/a")]; + let b_idx = group.item_to_idx[&item_id("https://slug.social/~/t/b")]; // Should have accumulated edge weights in both directions. assert!(group.edges.contains_key(&(a_idx, b_idx))); @@ -232,7 +239,7 @@ fn ranking_dominant_item_wins() { let mut group = state.public().ranking_group.clone(); let ranked = ranked_items(&mut group, 20000, 1e-9); - assert_eq!(ranked[0].item, "https://slug.social/~/t/champion"); + assert_eq!(ranked[0].item.as_str(), "https://slug.social/~/t/champion"); assert!(ranked[0].score > ranked[1].score); } @@ -379,7 +386,7 @@ async fn full_workflow_reducer_and_ranking() { let ranked = ranked_items(&mut group, 20000, 1e-9); assert_eq!(ranked.len(), 2); - assert_eq!(ranked[0].item, "https://slug.social/~/langs/rust"); // Should win + assert_eq!(ranked[0].item.as_str(), "https://slug.social/~/langs/rust"); // Should win assert!(ranked[0].score > ranked[1].score); } @@ -395,27 +402,27 @@ fn reducer_materializes_ancestor_path_segments() { // The intermediate path "https://slug.social/~/ai-models/anthropic" should appear as a child of "https://slug.social/~/ai-models". let content = state.public(); - let ai_models_children = content.item_children.get("https://slug.social/~/ai-models").expect("ai-models should have children"); + let ai_models_children = content.item_children.get(&item_id("https://slug.social/~/ai-models")).expect("ai-models should have children"); assert!( - ai_models_children.contains("https://slug.social/~/ai-models/anthropic"), + ai_models_children.contains(&item_id("https://slug.social/~/ai-models/anthropic")), "ai-models/anthropic should be a child of ai-models" ); // The leaf items should still be children of "https://slug.social/~/ai-models/anthropic". - let anthropic_children = content.item_children.get("https://slug.social/~/ai-models/anthropic").expect("ai-models/anthropic should have children"); - assert!(anthropic_children.contains("https://slug.social/~/ai-models/anthropic/claude-opus")); - assert!(anthropic_children.contains("https://slug.social/~/ai-models/anthropic/claude-sonnet")); + let anthropic_children = content.item_children.get(&item_id("https://slug.social/~/ai-models/anthropic")).expect("ai-models/anthropic should have children"); + assert!(anthropic_children.contains(&item_id("https://slug.social/~/ai-models/anthropic/claude-opus"))); + assert!(anthropic_children.contains(&item_id("https://slug.social/~/ai-models/anthropic/claude-sonnet"))); // Root should contain "https://slug.social/~/ai-models". - let root_children = content.item_children.get("https://slug.social/~").expect("root should have children"); - assert!(root_children.contains("https://slug.social/~/ai-models")); + let root_children = content.item_children.get(&item_id("https://slug.social/~")).expect("root should have children"); + assert!(root_children.contains(&item_id("https://slug.social/~/ai-models"))); // The phantom intermediates should NOT be in the items set (they weren't explicitly created). - assert!(!content.items.contains("https://slug.social/~/ai-models")); - assert!(!content.items.contains("https://slug.social/~/ai-models/anthropic")); + assert!(!content.items.contains(&item_id("https://slug.social/~/ai-models"))); + assert!(!content.items.contains(&item_id("https://slug.social/~/ai-models/anthropic"))); // But the leaf items should be. - assert!(content.items.contains("https://slug.social/~/ai-models/anthropic/claude-opus")); - assert!(content.items.contains("https://slug.social/~/ai-models/anthropic/claude-sonnet")); + assert!(content.items.contains(&item_id("https://slug.social/~/ai-models/anthropic/claude-opus"))); + assert!(content.items.contains(&item_id("https://slug.social/~/ai-models/anthropic/claude-sonnet"))); } #[test] @@ -469,9 +476,9 @@ fn ranking_repeated_votes_normalized() { // Same winner regardless of how many times voted. assert_eq!(ranked_once[0].item, ranked_many[0].item); - assert_eq!(ranked_once[0].item, "https://slug.social/~/norm/a"); + assert_eq!(ranked_once[0].item.as_str(), "https://slug.social/~/norm/a"); assert_eq!(ranked_once[1].item, ranked_many[1].item); - assert_eq!(ranked_once[1].item, "https://slug.social/~/norm/b"); + assert_eq!(ranked_once[1].item.as_str(), "https://slug.social/~/norm/b"); // Scores should be identical (normalization makes repeated votes idempotent). let eps = 1e-6; @@ -506,8 +513,8 @@ fn reducer_zero_zero_vote_ratio_normalizes_to_one_one() { "~/t/a {a}\n~/t/b {b}\n~/t/a 0:0 ~/t/b {zero}\n", )); let group = &state.public().ranking_group; - let a_idx = group.item_to_idx["https://slug.social/~/t/a"]; - let b_idx = group.item_to_idx["https://slug.social/~/t/b"]; + let a_idx = group.item_to_idx[&item_id("https://slug.social/~/t/a")]; + let b_idx = group.item_to_idx[&item_id("https://slug.social/~/t/b")]; // 0:0 should normalize to 1:1 — both directions should have weight assert!(group.edges.contains_key(&(a_idx, b_idx))); assert!(group.edges.contains_key(&(b_idx, a_idx))); @@ -523,8 +530,8 @@ fn reducer_negative_ratio_clamped_to_zero() { let mut group = GroupState::new(); group.apply_vote(slugsocial_server::reducer::VoteData { ts: 1, - a: slugsocial_server::path_types::CanonicalItemUrl("https://slug.social/~/t/a".to_string()), - b: slugsocial_server::path_types::CanonicalItemUrl("https://slug.social/~/t/b".to_string()), + a: slugsocial_server::path_types::ItemId::parse("https://slug.social/~/t/a").unwrap(), + b: slugsocial_server::path_types::ItemId::parse("https://slug.social/~/t/b").unwrap(), ratio_left: -5, ratio_right: -3, body: "negative".to_string(), @@ -534,8 +541,8 @@ fn reducer_negative_ratio_clamped_to_zero() { }); assert_eq!(group.idx_to_item.len(), 2); // Both edges should exist (negatives clamped to 0, then 0:0 -> 1:1) - let a_idx = group.item_to_idx["https://slug.social/~/t/a"]; - let b_idx = group.item_to_idx["https://slug.social/~/t/b"]; + let a_idx = group.item_to_idx[&item_id("https://slug.social/~/t/a")]; + let b_idx = group.item_to_idx[&item_id("https://slug.social/~/t/b")]; assert!(group.edges.contains_key(&(a_idx, b_idx))); assert!(group.edges.contains_key(&(b_idx, a_idx))); } @@ -553,24 +560,24 @@ fn reducer_deep_path_ancestor_materialization_four_levels() { let content = state.public(); let tilde_scope = content .item_children - .get("https://slug.social/~") + .get(&item_id("https://slug.social/~")) .expect("~/ scope should have children"); - assert!(tilde_scope.contains("https://slug.social/~/a")); + assert!(tilde_scope.contains(&item_id("https://slug.social/~/a"))); - let a_children = content.item_children.get("https://slug.social/~/a").expect("a should have children"); - assert!(a_children.contains("https://slug.social/~/a/b")); + let a_children = content.item_children.get(&item_id("https://slug.social/~/a")).expect("a should have children"); + assert!(a_children.contains(&item_id("https://slug.social/~/a/b"))); - let ab_children = content.item_children.get("https://slug.social/~/a/b").expect("a/b should have children"); - assert!(ab_children.contains("https://slug.social/~/a/b/c")); + let ab_children = content.item_children.get(&item_id("https://slug.social/~/a/b")).expect("a/b should have children"); + assert!(ab_children.contains(&item_id("https://slug.social/~/a/b/c"))); - let abc_children = content.item_children.get("https://slug.social/~/a/b/c").expect("a/b/c should have children"); - assert!(abc_children.contains("https://slug.social/~/a/b/c/d")); + let abc_children = content.item_children.get(&item_id("https://slug.social/~/a/b/c")).expect("a/b/c should have children"); + assert!(abc_children.contains(&item_id("https://slug.social/~/a/b/c/d"))); // Only the leaf should be in items set - assert!(content.items.contains("https://slug.social/~/a/b/c/d")); - assert!(!content.items.contains("https://slug.social/~/a")); - assert!(!content.items.contains("https://slug.social/~/a/b")); - assert!(!content.items.contains("https://slug.social/~/a/b/c")); + assert!(content.items.contains(&item_id("https://slug.social/~/a/b/c/d"))); + assert!(!content.items.contains(&item_id("https://slug.social/~/a"))); + assert!(!content.items.contains(&item_id("https://slug.social/~/a/b"))); + assert!(!content.items.contains(&item_id("https://slug.social/~/a/b/c"))); } // ============================================================================ @@ -610,7 +617,7 @@ fn ranking_convergence_tolerance_triggers_early_exit() { // Very tight tolerance but huge max_iters — should still converge fast let ranked = ranked_items(&mut group, 1_000_000, 1e-15); assert_eq!(ranked.len(), 2); - assert_eq!(ranked[0].item, "https://slug.social/~/t/a"); + assert_eq!(ranked[0].item.as_str(), "https://slug.social/~/t/a"); } // ============================================================================ @@ -624,14 +631,14 @@ fn test_item_body_overwrite() { 1, "~/t/x {first}\n", )); - assert_eq!(state.public().item_bodies.get("https://slug.social/~/t/x"), Some(&"first".to_string())); + assert_eq!(state.public().item_bodies.get(&item_id("https://slug.social/~/t/x")), Some(&"first".to_string())); state.apply_event(ingest_event( 2, "~/t/x {second}\n", )); assert_eq!( - state.public().item_bodies.get("https://slug.social/~/t/x"), + state.public().item_bodies.get(&item_id("https://slug.social/~/t/x")), Some(&"second".to_string()), "last writer should win for item bodies" ); @@ -645,9 +652,9 @@ fn test_empty_body_not_stored() { "~/t/blank { }\n", )); let content = state.public(); - assert!(content.items.contains("https://slug.social/~/t/blank"), "item should exist"); + assert!(content.items.contains(&item_id("https://slug.social/~/t/blank")), "item should exist"); assert!( - !content.item_bodies.contains_key("https://slug.social/~/t/blank"), + !content.item_bodies.contains_key(&item_id("https://slug.social/~/t/blank")), "whitespace-only body should not be stored" ); } @@ -664,7 +671,7 @@ fn test_duplicate_items_across_ingests() { "~/t/dup {second}\n", )); let content = state.public(); - let count = content.items.iter().filter(|i| *i == "https://slug.social/~/t/dup").count(); + let count = content.items.iter().filter(|i| i.as_str() == "https://slug.social/~/t/dup").count(); assert_eq!(count, 1, "items set should deduplicate across ingests"); } @@ -736,7 +743,7 @@ fn test_thread_id_is_used_for_votes_and_indexes() { let content = state.public(); let vote = content .item_votes - .get("https://slug.social/~/t/a") + .get(&item_id("https://slug.social/~/t/a")) .unwrap() .front() .unwrap(); @@ -745,7 +752,7 @@ fn test_thread_id_is_used_for_votes_and_indexes() { assert!(state .public() .item_threads - .get("https://slug.social/~/t/a") + .get(&item_id("https://slug.social/~/t/a")) .is_some_and(|threads| threads.contains("first"))); } @@ -757,11 +764,11 @@ fn test_rank_history_created_for_voted_items() { "~/t/a {a}\n~/t/b {b}\n~/t/a 3:1 ~/t/b {reason}\n", )); assert!( - state.public().rank_history.contains_key("https://slug.social/~/t/a"), + state.public().rank_history.contains_key(&item_id("https://slug.social/~/t/a")), "rank_history should have entry for voted item a" ); assert!( - state.public().rank_history.contains_key("https://slug.social/~/t/b"), + state.public().rank_history.contains_key(&item_id("https://slug.social/~/t/b")), "rank_history should have entry for voted item b" ); } @@ -774,7 +781,7 @@ fn test_rank_history_not_created_for_unvoted_items() { "~/t/c {just a definition}\n", )); assert!( - !state.public().rank_history.contains_key("https://slug.social/~/t/c"), + !state.public().rank_history.contains_key(&item_id("https://slug.social/~/t/c")), "rank_history should NOT have entry for item with no votes" ); } @@ -786,7 +793,7 @@ fn test_rank_history_first_entry_delta_zero() { 1, "~/t/a {a}\n~/t/b {b}\n~/t/a 3:1 ~/t/b {reason}\n", )); - let history_a = state.public().rank_history.get("https://slug.social/~/t/a").unwrap(); + let history_a = state.public().rank_history.get(&item_id("https://slug.social/~/t/a")).unwrap(); assert_eq!(history_a.len(), 1); assert_eq!( history_a[0].scope_rank_delta, 0, diff --git a/types/src/item_id.rs b/types/src/item_id.rs new file mode 100644 index 0000000000000000000000000000000000000000..5fe9fad39c0d58e546c0a0162eba4348d64e0865 --- /dev/null +++ b/types/src/item_id.rs @@ -0,0 +1,225 @@ +//! Structural item identity for the reducer graph and ranking (vs presentation-only strings). + +use std::cmp::Ordering; +use std::fmt; + +use serde::{Deserialize, Deserializer, Serialize, Serializer}; + +use crate::item_wire::{ + canonicalize_item, external_display_dash_prefix, normalize_slug_ontology_storage_url, + SLUG_TILDE_ONTOLOGY_ROOT, +}; + +/// Structural key for items in [`slug_types`] and the server reducer. +/// +/// Wire / JSON uses the same single string as the former canonical item URL (via serde). +#[derive(Debug, Clone, Hash, PartialEq, Eq)] +pub enum ItemId { + /// Tilde ontology root (`~/`); storage [`SLUG_TILDE_ONTOLOGY_ROOT`]. + Root, + /// `https://slug.social/~/…` (non-root; normalized trailing path). + Local(String), + /// Normalized `http(s)://…` storage form, including non-`~/` paths on `slug.social`. + Web(String), + /// Raw key material that did not round-trip through [`Self::parse`] (historical edge case). + Opaque(String), +} + +impl ItemId { + pub fn parse(input: &str) -> Option { + let c = normalize_slug_ontology_storage_url(&canonicalize_item(input)); + if c.is_empty() { + return None; + } + if c == SLUG_TILDE_ONTOLOGY_ROOT { + return Some(Self::Root); + } + if c.starts_with("https://slug.social/~/") && c != SLUG_TILDE_ONTOLOGY_ROOT { + return Some(Self::Local(c)); + } + Some(Self::Web(c)) + } + + /// Same as the old `ensure_item` fallback: use `s` verbatim as the map key. + pub fn opaque(raw: String) -> Self { + Self::Opaque(raw) + } + + pub fn ontology_root() -> Self { + Self::Root + } + + /// Collapses legacy slug ontology root spellings so [`std::collections::HashMap`] keys match the graph. + pub fn normalized_storage(self) -> Self { + Self::parse(self.as_str()).unwrap_or(self) + } + + pub fn as_str(&self) -> &str { + match self { + ItemId::Root => SLUG_TILDE_ONTOLOGY_ROOT, + ItemId::Local(s) | ItemId::Web(s) | ItemId::Opaque(s) => s, + } + } + + pub fn to_storage_string(&self) -> String { + self.as_str().to_string() + } + + pub fn tilde_tail(&self) -> Option<&str> { + match self { + ItemId::Root => Some(""), + ItemId::Local(s) => s.strip_prefix("https://slug.social/~/"), + ItemId::Web(s) | ItemId::Opaque(s) => { + if let Some(tail) = s.strip_prefix("https://slug.social/~/") { + return Some(tail); + } + if s == SLUG_TILDE_ONTOLOGY_ROOT || s == "https://slug.social/~/" { + return Some(""); + } + None + } + } + } + + /// HTTP garden tail after `~/` (empty at ontology root), or `None` if not under tilde ontology. + pub fn tilde_http_tail(&self) -> Option { + self.tilde_tail().map(str::to_owned) + } + + pub fn last_segment(&self) -> &str { + let s = self.as_str(); + s.rsplit('/').find(|x| !x.is_empty()).unwrap_or(s) + } + + pub fn parent(&self) -> Option { + match self { + ItemId::Root => None, + ItemId::Local(s) => { + if s == SLUG_TILDE_ONTOLOGY_ROOT || s == "https://slug.social/~/" { + return None; + } + let last_slash = s.rfind('/')?; + let parent_str = &s[..last_slash]; + if parent_str.is_empty() { + None + } else { + Self::parse(parent_str) + } + } + ItemId::Web(s) | ItemId::Opaque(s) => { + if s == SLUG_TILDE_ONTOLOGY_ROOT || s == "https://slug.social/~/" { + return None; + } + if let Some(rest) = s.strip_prefix("https://slug.social/~/") { + if rest.is_empty() { + return None; + } + let last_slash = s.rfind('/')?; + let parent_str = &s[..last_slash]; + Self::parse(parent_str) + } else if s.starts_with("https://") { + let rest = s.strip_prefix("https://").unwrap(); + Self::parent_http_url("https://", rest) + } else if s.starts_with("http://") { + let rest = s.strip_prefix("http://").unwrap(); + Self::parent_http_url("http://", rest) + } else { + None + } + } + } + } + + fn parent_http_url(scheme: &'static str, rest: &str) -> Option { + let (host, path) = rest.split_once('/').map_or((rest, ""), |(h, p)| (h, p)); + let host = host.trim(); + let path = path.trim_end_matches('/'); + if path.is_empty() { + return None; + } + let parent_path = path.rsplit_once('/').map(|(p, _)| p).unwrap_or(""); + if parent_path.is_empty() { + Self::parse(&format!("{scheme}{}", host)) + } else { + Self::parse(&format!("{scheme}{}/{}", host, parent_path)) + } + } + + /// `-/` representation for external `https://…` items, `~/…` for slug ontology, else unchanged. + pub fn display_path(&self) -> String { + if let Some(tail) = self.tilde_tail() { + if tail.is_empty() { + return "~/".to_string(); + } + return format!("~/{}", tail); + } + let s = self.as_str(); + if let Some(tail) = s.strip_prefix("https://") { + if tail.starts_with("slug.social") { + return s.to_string(); + } + return external_display_dash_prefix(tail); + } + if let Some(tail) = s.strip_prefix("http://") { + if tail.starts_with("slug.social") { + return s.to_string(); + } + return external_display_dash_prefix(tail); + } + s.to_string() + } + + pub fn tilde_segments(&self) -> Vec<&str> { + match self.tilde_tail() { + Some(tail) if !tail.is_empty() => std::iter::once("~") + .chain(tail.split('/').filter(|s| !s.is_empty())) + .collect(), + Some(_) => vec!["~"], + None => vec![], + } + } + + /// Normalized URL string for HTTP fetch boundaries (external identities). + pub fn to_wire_url(&self) -> String { + self.to_storage_string() + } +} + +impl PartialOrd for ItemId { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} + +impl Ord for ItemId { + fn cmp(&self, other: &Self) -> Ordering { + self.as_str().cmp(other.as_str()) + } +} + +impl fmt::Display for ItemId { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.as_str().fmt(f) + } +} + +impl Serialize for ItemId { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + self.as_str().serialize(serializer) + } +} + +impl<'de> Deserialize<'de> for ItemId { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + let s = String::deserialize(deserializer)?; + ItemId::parse(&s).ok_or_else(|| { + serde::de::Error::custom(format!("invalid item id: {s:?}")) + }) + } +} diff --git a/types/src/item_wire.rs b/types/src/item_wire.rs new file mode 100644 index 0000000000000000000000000000000000000000..20e3e7df77a74713c146c7e7222a809173723c7d --- /dev/null +++ b/types/src/item_wire.rs @@ -0,0 +1,184 @@ +//! Wire normalization for item identity strings (`canonicalize_item`, path segments). +//! Split from `paths` so [`crate::item_id::ItemId`] can depend on this without import cycles. + +use crate::url_normalize::{host_preserves_dash_path_case, normalize_http_identity_url}; + +/// Canonical absolute URL for the tilde ontology **root** (`~/` in UI). +pub const SLUG_TILDE_ONTOLOGY_ROOT: &str = "https://slug.social/~"; + +/// Collapse legacy or parser variants of the ontology root to [`SLUG_TILDE_ONTOLOGY_ROOT`]. +pub fn normalize_slug_ontology_storage_url(s: &str) -> String { + if s == "https://slug.social/~/" { + SLUG_TILDE_ONTOLOGY_ROOT.to_string() + } else { + s.to_string() + } +} + +fn finalize_external_identity_url(s: String) -> String { + if s.starts_with("https://slug.social/") { + return s; + } + let normalized = normalize_http_identity_url(&s).unwrap_or_else(|| s.clone()); + strip_redundant_root_slash(&normalized).unwrap_or(normalized) +} + +/// `url::Url` serializes bare hosts with a `/` path; we keep host-only items slash-free for stable +/// keys matching the pre-normalizer spellings. +fn strip_redundant_root_slash(s: &str) -> Option { + let u = url::Url::parse(s).ok()?; + if u.path() == "/" && u.query().is_none() && u.fragment().is_none() { + let scheme = u.scheme(); + let host = u.host_str()?; + return Some(match u.port() { + Some(p) => format!("{scheme}://{host}:{p}"), + None => format!("{scheme}://{host}"), + }); + } + None +} + +/// Ontology item reference → canonical absolute URL on the slug host. +pub fn canonicalize_item(input: &str) -> String { + let s = input.trim(); + if s.is_empty() { + return String::new(); + } + + if let Some(rest) = s.strip_prefix("-/") { + let (host, tail) = rest + .split_once('/') + .map_or((rest, ""), |(h, t)| (h, t)); + let host = host.trim().to_lowercase(); + if host.is_empty() { + return String::new(); + } + let preserve_case = host_preserves_dash_path_case(&host); + return if tail.is_empty() { + finalize_external_identity_url(format!("https://{}", host)) + } else { + let path = tail + .trim_start_matches('/') + .trim_end_matches('/') + .split('/') + .filter_map(|seg| { + let t = seg.trim(); + if t.is_empty() { + None + } else if preserve_case { + Some(t.to_string()) + } else { + Some(t.to_lowercase()) + } + }) + .collect::>() + .join("/"); + finalize_external_identity_url(format!("https://{}/{}", host, path)) + }; + } + + if let Some(rest) = s.strip_prefix("https://") { + let (host, tail) = rest.split_once('/').map_or((rest, ""), |(h, t)| (h, t)); + let host = host.trim().to_lowercase(); + return finalize_external_identity_url(if tail.is_empty() { + format!("https://{}", host) + } else { + format!("https://{}/{}", host, tail) + }); + } + if let Some(rest) = s.strip_prefix("http://") { + let (host, tail) = rest.split_once('/').map_or((rest, ""), |(h, t)| (h, t)); + let host = host.trim().to_lowercase(); + return finalize_external_identity_url(if tail.is_empty() { + format!("http://{}", host) + } else { + format!("http://{}/{}", host, tail) + }); + } + + let is_tilde = s.starts_with("~/"); + let rest = s.strip_prefix("~/").or_else(|| s.strip_prefix("/")).unwrap_or(s); + + let tail = rest + .split('/') + .filter_map(|seg| { + let t = seg.trim(); + if t.is_empty() { + None + } else { + Some(t.to_lowercase()) + } + }) + .collect::>() + .join("/"); + + if is_tilde { + if tail.is_empty() { + return SLUG_TILDE_ONTOLOGY_ROOT.to_string(); + } + format!("https://slug.social/~/{}", tail) + } else if tail.is_empty() { + "https://slug.social".to_string() + } else { + format!("https://slug.social/{}", tail) + } +} + +pub fn item_path_segments(input: &str) -> Vec { + let canonical = canonicalize_item(input); + if canonical.is_empty() { + return vec![]; + } + + if let Some(rest) = canonical.strip_prefix("https://") { + let (host, tail) = rest.split_once('/').map_or((rest, ""), |(h, t)| (h, t)); + let mut out = vec![format!("https://{}", host)]; + out.extend(tail.split('/').filter(|s| !s.is_empty()).map(|s| s.to_string())); + return out; + } + if let Some(rest) = canonical.strip_prefix("http://") { + let (host, tail) = rest.split_once('/').map_or((rest, ""), |(h, t)| (h, t)); + let mut out = vec![format!("http://{}", host)]; + out.extend(tail.split('/').filter(|s| !s.is_empty()).map(|s| s.to_string())); + return out; + } + + canonical + .split('/') + .filter(|s| !s.is_empty()) + .map(|s| s.to_string()) + .collect() +} + +pub fn item_parent_path(input: &str) -> Option { + let segs = item_path_segments(input); + if segs.len() <= 1 { + return None; + } + Some(segs[..segs.len() - 1].join("/")) +} + +pub(crate) fn external_display_dash_prefix(host_and_path: &str) -> String { + let (host, path) = host_and_path + .split_once('/') + .map_or((host_and_path, ""), |(h, p)| (h, p)); + let host = host.trim().to_lowercase(); + let path = path + .trim_end_matches('/') + .split('/') + .filter_map(|seg| { + let t = seg.trim(); + if t.is_empty() { + None + } else { + Some(t.to_lowercase()) + } + }) + .collect::>() + .join("/"); + if path.is_empty() { + format!("-/{}", host) + } else { + format!("-/{}", format!("{}/{}", host, path)) + } +} diff --git a/types/src/lib.rs b/types/src/lib.rs index ceaa574cd32b7e7eb397c9a3191fcbc037b8c606..05bcfcfbf48f1dc7a4c05e32055bd12eb5cfd623 100644 --- a/types/src/lib.rs +++ b/types/src/lib.rs @@ -1,14 +1,20 @@ use serde::{Deserialize, Serialize}; pub mod url_normalize; +pub mod item_wire; +pub mod item_id; pub mod paths; pub mod timeago; +pub use item_id::ItemId; +pub use item_wire::{ + canonicalize_item, item_parent_path, item_path_segments, normalize_slug_ontology_storage_url, + SLUG_TILDE_ONTOLOGY_ROOT, +}; pub use paths::{ - canonicalize_item, canonicalize_tag, item_parent_path, item_path_segments, normalize_slug_ontology_storage_url, - CanonicalItemUrl, ForumThreadUrl, GardenItemUrl, RelativePath, SLUG_TILDE_ONTOLOGY_ROOT, + canonicalize_tag, ForumThreadUrl, GardenItemUrl, RelativePath, room_id_from_route_segment, room_route_segment, ROOM_SHORT_ID_LEN, - TildeHttpPathTail, TildeOntologyPath, TildePath, tilde_http_path_to_canonical, + TildeHttpPathTail, TildeOntologyPath, TildePath, tilde_http_path_to_item_id, }; pub use url_normalize::normalize_http_identity_url; diff --git a/types/src/paths.rs b/types/src/paths.rs index 5c60e762c2ca74d79c74237097d5bcc02ba74af2..a1d066d83e7fbcefc27707dd682e54dbcec1cc56 100644 --- a/types/src/paths.rs +++ b/types/src/paths.rs @@ -3,37 +3,23 @@ //! //! ## String kinds (parse in this module only) //! -//! - **[`canonicalize_item`] / [`CanonicalItemUrl`]** — graph storage key and DSL form; tilde +//! - **[`canonicalize_item`] / [`crate::ItemId`]** — graph storage key and DSL form; tilde //! ontology root is always [`SLUG_TILDE_ONTOLOGY_ROOT`] (no `…/~/` trailing slash only). //! - **[`TildeHttpPathTail`]** — capture from `GET /~/*path` or `…/r/{short}{slug}/~/…` (the `*path` segment). //! - **`-/…` wire form** — external items; see [`canonicalize_item`] dash branch. //! - **[`GardenItemUrl`], [`ForumThreadUrl`]** — JSON / browser href surfaces. //! - **[`ROOM_SHORT_ID_LEN`] / [`room_route_segment`]** — `/r/{short}{slug}` vs wire `short/slug`. -use std::borrow::Borrow; use std::fmt; use std::ops::Deref; use serde::{Deserialize, Serialize}; -use crate::url_normalize::{host_preserves_dash_path_case, normalize_http_identity_url}; - -// --------------------------------------------------------------------------- -// Slug tilde ontology (single storage form for `~/`) -// --------------------------------------------------------------------------- - -/// Canonical absolute URL for the tilde ontology **root** (`~/` in UI). Used as the -/// `item_children` parent key for top-level items and must match [`CanonicalItemUrl::ontology_root`]. -pub const SLUG_TILDE_ONTOLOGY_ROOT: &str = "https://slug.social/~"; - -/// Collapse legacy or parser variants of the ontology root to [`SLUG_TILDE_ONTOLOGY_ROOT`]. -pub fn normalize_slug_ontology_storage_url(s: &str) -> String { - if s == "https://slug.social/~/" { - SLUG_TILDE_ONTOLOGY_ROOT.to_string() - } else { - s.to_string() - } -} +use crate::item_id::ItemId; +pub use crate::item_wire::{ + canonicalize_item, item_parent_path, item_path_segments, normalize_slug_ontology_storage_url, + SLUG_TILDE_ONTOLOGY_ROOT, +}; // --------------------------------------------------------------------------- // Private room HTTP path (`/r/{short}{slug}`; wire id remains `short/slug`) @@ -84,310 +70,10 @@ pub fn canonicalize_tag(input: &str) -> String { input.trim().trim_start_matches('#').to_lowercase() } -fn finalize_external_identity_url(s: String) -> String { - if s.starts_with("https://slug.social/") { - return s; - } - let normalized = normalize_http_identity_url(&s).unwrap_or_else(|| s.clone()); - strip_redundant_root_slash(&normalized).unwrap_or(normalized) -} - -/// `url::Url` serializes bare hosts with a `/` path; we keep host-only items slash-free for stable -/// keys matching the pre-normalizer spellings. -fn strip_redundant_root_slash(s: &str) -> Option { - let u = url::Url::parse(s).ok()?; - if u.path() == "/" && u.query().is_none() && u.fragment().is_none() { - let scheme = u.scheme(); - let host = u.host_str()?; - return Some(match u.port() { - Some(p) => format!("{scheme}://{host}:{p}"), - None => format!("{scheme}://{host}"), - }); - } - None -} - -/// Ontology item reference → canonical absolute URL on the slug host. -pub fn canonicalize_item(input: &str) -> String { - let s = input.trim(); - if s.is_empty() { - return String::new(); - } - - // External scope: `-/host/path` is the universal alias for `https://host/path`. - if let Some(rest) = s.strip_prefix("-/") { - let (host, tail) = rest - .split_once('/') - .map_or((rest, ""), |(h, t)| (h, t)); - let host = host.trim().to_lowercase(); - if host.is_empty() { - return String::new(); - } - let preserve_case = host_preserves_dash_path_case(&host); - return if tail.is_empty() { - finalize_external_identity_url(format!("https://{}", host)) - } else { - let path = tail - .trim_start_matches('/') - .trim_end_matches('/') - .split('/') - .filter_map(|seg| { - let t = seg.trim(); - if t.is_empty() { - None - } else if preserve_case { - Some(t.to_string()) - } else { - Some(t.to_lowercase()) - } - }) - .collect::>() - .join("/"); - finalize_external_identity_url(format!("https://{}/{}", host, path)) - }; - } - - if let Some(rest) = s.strip_prefix("https://") { - let (host, tail) = rest.split_once('/').map_or((rest, ""), |(h, t)| (h, t)); - let host = host.trim().to_lowercase(); - return finalize_external_identity_url(if tail.is_empty() { - format!("https://{}", host) - } else { - format!("https://{}/{}", host, tail) - }); - } - if let Some(rest) = s.strip_prefix("http://") { - let (host, tail) = rest.split_once('/').map_or((rest, ""), |(h, t)| (h, t)); - let host = host.trim().to_lowercase(); - return finalize_external_identity_url(if tail.is_empty() { - format!("http://{}", host) - } else { - format!("http://{}/{}", host, tail) - }); - } - - let is_tilde = s.starts_with("~/"); - let rest = s.strip_prefix("~/").or_else(|| s.strip_prefix("/")).unwrap_or(s); - - let tail = rest - .split('/') - .filter_map(|seg| { - let t = seg.trim(); - if t.is_empty() { - None - } else { - Some(t.to_lowercase()) - } - }) - .collect::>() - .join("/"); - - if is_tilde { - if tail.is_empty() { - return SLUG_TILDE_ONTOLOGY_ROOT.to_string(); - } - format!("https://slug.social/~/{}", tail) - } else if tail.is_empty() { - "https://slug.social".to_string() - } else { - format!("https://slug.social/{}", tail) - } -} - -pub fn item_path_segments(input: &str) -> Vec { - let canonical = canonicalize_item(input); - if canonical.is_empty() { - return vec![]; - } - - if let Some(rest) = canonical.strip_prefix("https://") { - let (host, tail) = rest.split_once('/').map_or((rest, ""), |(h, t)| (h, t)); - let mut out = vec![format!("https://{}", host)]; - out.extend(tail.split('/').filter(|s| !s.is_empty()).map(|s| s.to_string())); - return out; - } - if let Some(rest) = canonical.strip_prefix("http://") { - let (host, tail) = rest.split_once('/').map_or((rest, ""), |(h, t)| (h, t)); - let mut out = vec![format!("http://{}", host)]; - out.extend(tail.split('/').filter(|s| !s.is_empty()).map(|s| s.to_string())); - return out; - } - - canonical - .split('/') - .filter(|s| !s.is_empty()) - .map(|s| s.to_string()) - .collect() -} - -pub fn item_parent_path(input: &str) -> Option { - let segs = item_path_segments(input); - if segs.len() <= 1 { - return None; - } - Some(segs[..segs.len() - 1].join("/")) -} - -fn external_display_dash_prefix(host_and_path: &str) -> String { - let (host, path) = host_and_path - .split_once('/') - .map_or((host_and_path, ""), |(h, p)| (h, p)); - let host = host.trim().to_lowercase(); - let path = path - .trim_end_matches('/') - .split('/') - .filter_map(|seg| { - let t = seg.trim(); - if t.is_empty() { - None - } else { - Some(t.to_lowercase()) - } - }) - .collect::>() - .join("/"); - if path.is_empty() { - format!("-/{}", host) - } else { - format!("-/{}", format!("{}/{}", host, path)) - } -} - // --------------------------------------------------------------------------- // Storage + input path newtypes // --------------------------------------------------------------------------- -/// Canonical item identifier as produced by [`canonicalize_item`]. -/// -/// Shared across all scopes; room is not embedded. Usually -/// `https://slug.social/~/…` or an external `http(s)://…` URL item. -#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] -pub struct CanonicalItemUrl(pub String); - -impl CanonicalItemUrl { - pub fn parse(input: &str) -> Option { - let c = canonicalize_item(input); - if c.is_empty() { - None - } else { - Some(Self(normalize_slug_ontology_storage_url(&c))) - } - } - - pub fn as_str(&self) -> &str { - &self.0 - } - - /// Collapses legacy slug ontology root spellings so [`HashMap`] keys match the reducer graph. - pub fn normalized_storage(self) -> Self { - Self(normalize_slug_ontology_storage_url(self.as_str())) - } - - pub fn tilde_tail(&self) -> Option<&str> { - if let Some(tail) = self.0.strip_prefix("https://slug.social/~/") { - return Some(tail); - } - if self.0 == SLUG_TILDE_ONTOLOGY_ROOT || self.0 == "https://slug.social/~/" { - return Some(""); - } - None - } - - pub fn last_segment(&self) -> &str { - self.0 - .rsplit('/') - .find(|s| !s.is_empty()) - .unwrap_or(self.0.as_str()) - } - - pub fn ontology_root() -> Self { - Self(SLUG_TILDE_ONTOLOGY_ROOT.to_string()) - } - - pub fn parent(&self) -> Option { - if self.tilde_tail().is_some() { - if self.tilde_tail().map(|t| t.is_empty()).unwrap_or(true) { - return None; - } - let last_slash = self.0.rfind('/')?; - let parent_str = &self.0[..last_slash]; - if parent_str.is_empty() { - None - } else { - Some(Self(parent_str.to_string())) - } - } else if let Some(rest) = self.0.strip_prefix("https://") { - Self::parent_http_url("https://", rest) - } else if let Some(rest) = self.0.strip_prefix("http://") { - Self::parent_http_url("http://", rest) - } else { - None - } - } - - fn parent_http_url(scheme: &'static str, rest: &str) -> Option { - let (host, path) = rest.split_once('/').map_or((rest, ""), |(h, p)| (h, p)); - let host = host.trim(); - let path = path.trim_end_matches('/'); - if path.is_empty() { - return None; - } - let parent_path = path.rsplit_once('/').map(|(p, _)| p).unwrap_or(""); - if parent_path.is_empty() { - Some(Self(format!("{scheme}{}", host))) - } else { - Some(Self(format!("{scheme}{}/{}", host, parent_path))) - } - } - - /// `-/` representation for external `https://…` items, `~/…` for slug ontology, else unchanged. - pub fn display_path(&self) -> String { - if let Some(tail) = self.tilde_tail() { - if tail.is_empty() { - return "~/".to_string(); - } - return format!("~/{}", tail); - } - if let Some(tail) = self.0.strip_prefix("https://") { - if tail.starts_with("slug.social") { - self.0.clone() - } else { - external_display_dash_prefix(tail) - } - } else if let Some(tail) = self.0.strip_prefix("http://") { - if tail.starts_with("slug.social") { - self.0.clone() - } else { - external_display_dash_prefix(tail) - } - } else { - self.0.clone() - } - } - - pub fn tilde_segments(&self) -> Vec<&str> { - match self.tilde_tail() { - Some(tail) if !tail.is_empty() => { - std::iter::once("~") - .chain(tail.split('/').filter(|s| !s.is_empty())) - .collect() - } - Some(_) => vec!["~"], - None => vec![], - } - } - - /// `~/…` list label for ontology items (paths index, CLI). - pub fn tilde_list_label(&self) -> TildeOntologyPath { - TildeOntologyPath::from_stored(self) - } - - /// Absolute href for JSON/RPC and browsers for this stored id in `room`. - pub fn json_href(&self, room_wire: &str) -> GardenItemUrl { - GardenItemUrl::from_stored(self, room_wire) - } -} - /// HTTP route capture: path segment after `~/` in `GET /~/*path` or `…/r/{short}{slug}/~/…` (empty = ontology root). #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct TildeHttpPathTail(pub String); @@ -401,13 +87,13 @@ impl TildeHttpPathTail { &self.0 } - pub fn to_canonical(&self) -> CanonicalItemUrl { - tilde_http_path_to_canonical(self.as_str()) + pub fn to_item_id(&self) -> ItemId { + tilde_http_path_to_item_id(self.as_str()) } } -/// Map the router's tilde tail (e.g. `topic/a`, or empty for root) to a [`CanonicalItemUrl`]. -pub fn tilde_http_path_to_canonical(path_segment: &str) -> CanonicalItemUrl { +/// Map the router's tilde tail (e.g. `topic/a`, or empty for root) to an [`ItemId`]. +pub fn tilde_http_path_to_item_id(path_segment: &str) -> ItemId { let p = path_segment.trim_start_matches('/'); let raw = if p.starts_with("http://") || p.starts_with("https://") { p.to_string() @@ -416,37 +102,7 @@ pub fn tilde_http_path_to_canonical(path_segment: &str) -> CanonicalItemUrl { } else { format!("~/{}", p) }; - CanonicalItemUrl::parse(&raw).unwrap_or_else(|| CanonicalItemUrl::ontology_root()) -} - -impl fmt::Display for CanonicalItemUrl { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - self.0.fmt(f) - } -} - -impl Borrow for CanonicalItemUrl { - fn borrow(&self) -> &str { - &self.0 - } -} - -impl PartialEq for CanonicalItemUrl { - fn eq(&self, other: &str) -> bool { - self.0 == other - } -} - -impl PartialEq<&str> for CanonicalItemUrl { - fn eq(&self, other: &&str) -> bool { - self.0 == *other - } -} - -impl PartialEq for CanonicalItemUrl { - fn eq(&self, other: &String) -> bool { - &self.0 == other - } + ItemId::parse(&raw).unwrap_or_else(ItemId::ontology_root) } #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] @@ -468,8 +124,8 @@ impl TildePath { &self.0 } - pub fn canonicalize(&self) -> Option { - CanonicalItemUrl::parse(&self.0) + pub fn canonicalize(&self) -> Option { + ItemId::parse(&self.0) } } @@ -496,7 +152,7 @@ impl RelativePath { &self.0 } - pub fn join_under_ontology_root(&self, root: &CanonicalItemUrl) -> Option { + pub fn join_under_ontology_root(&self, root: &ItemId) -> Option { let base = root.tilde_tail()?; let joined = if base.is_empty() { if self.0.is_empty() { @@ -509,7 +165,7 @@ impl RelativePath { } else { format!("~/{}/{}", base.trim_end_matches('/'), self.0) }; - CanonicalItemUrl::parse(&joined) + ItemId::parse(&joined) } } @@ -545,14 +201,17 @@ impl GardenItemUrl { self.0 } - /// Stored canonical id + RPC `room` field (`"public"` or `"short/slug"`). - pub fn from_stored(stored: &CanonicalItemUrl, room_wire: &str) -> Self { - Self(garden_href_string(stored.as_str(), room_wire)) + /// Stored item id + RPC `room` field (`"public"` or `"short/slug"`). + pub fn from_stored(stored: &ItemId, room_wire: &str) -> Self { + Self(garden_href_string(stored, room_wire)) } /// Like [`Self::from_stored`] but accepts a string that may already be canonical. pub fn from_storage_str(stored: &str, room_wire: &str) -> Self { - Self(garden_href_string(stored, room_wire)) + let Some(id) = ItemId::parse(stored) else { + return Self(api_path_or_url(stored)); + }; + Self(garden_href_string(&id, room_wire)) } } @@ -570,18 +229,15 @@ impl Deref for GardenItemUrl { } } -fn garden_href_string(item: &str, room_wire: &str) -> String { +fn garden_href_string(c: &ItemId, room_wire: &str) -> String { let room = room_wire.trim(); if room.is_empty() || room == "public" { - return api_path_or_url(item); + return api_path_or_url(c.as_str()); } let Some(room_seg) = room_route_segment(room) else { - return api_path_or_url(item); - }; - let Some(c) = CanonicalItemUrl::parse(item) else { - return api_path_or_url(item); + return api_path_or_url(c.as_str()); }; - let root = CanonicalItemUrl::ontology_root(); + let root = ItemId::ontology_root(); let item_norm = c.as_str().trim_end_matches('/'); let root_norm = root.as_str().trim_end_matches('/'); if let Some(tail) = c.tilde_tail() { @@ -600,7 +256,7 @@ fn garden_href_string(item: &str, room_wire: &str) -> String { let tail = tail.strip_prefix("-/").unwrap_or(tail.as_str()); return format!("https://slug.social/r/{room_seg}/-/{tail}"); } - api_path_or_url(item) + api_path_or_url(c.as_str()) } /// Forum thread URL for JSON (`/t/…` or `/r/…/t/…` on slug.social). @@ -650,7 +306,7 @@ impl Deref for ForumThreadUrl { pub struct TildeOntologyPath(pub String); impl TildeOntologyPath { - pub fn from_stored(c: &CanonicalItemUrl) -> Self { + pub fn from_stored(c: &ItemId) -> Self { Self(c.display_path()) } @@ -679,22 +335,22 @@ mod tests { #[test] fn canonical_parent_deep() { - let c = CanonicalItemUrl::parse("~/a/b/c").unwrap(); + let c = ItemId::parse("~/a/b/c").unwrap(); assert_eq!(c.parent().unwrap().as_str(), "https://slug.social/~/a/b"); } #[test] fn canonical_parent_one_level() { - let c = CanonicalItemUrl::parse("~/a").unwrap(); + let c = ItemId::parse("~/a").unwrap(); assert_eq!(c.parent().unwrap().as_str(), "https://slug.social/~"); } #[test] fn canonical_parent_root_is_none() { - let root = CanonicalItemUrl::parse("~/").unwrap(); + let root = ItemId::parse("~/").unwrap(); assert!(root.parent().is_none()); assert_eq!(root.as_str(), SLUG_TILDE_ONTOLOGY_ROOT); - assert_eq!(root, CanonicalItemUrl::ontology_root()); + assert_eq!(root, ItemId::ontology_root()); } #[test] @@ -705,49 +361,49 @@ mod tests { SLUG_TILDE_ONTOLOGY_ROOT.to_string() ); assert_eq!( - CanonicalItemUrl::parse("https://slug.social/~/") + ItemId::parse("https://slug.social/~/") .unwrap() .as_str(), SLUG_TILDE_ONTOLOGY_ROOT ); - let legacy = CanonicalItemUrl("https://slug.social/~/".to_string()); + let legacy = ItemId::opaque("https://slug.social/~/".to_string()); assert_eq!(legacy.normalized_storage().as_str(), SLUG_TILDE_ONTOLOGY_ROOT); } #[test] fn tilde_http_path_tail_maps_router_segment() { assert_eq!( - TildeHttpPathTail::new("").to_canonical(), - CanonicalItemUrl::ontology_root() + TildeHttpPathTail::new("").to_item_id(), + ItemId::ontology_root() ); assert_eq!( - tilde_http_path_to_canonical("topic/x").as_str(), + tilde_http_path_to_item_id("topic/x").as_str(), "https://slug.social/~/topic/x" ); } #[test] fn display_path_slug_ontology_root() { - let r = CanonicalItemUrl::ontology_root(); + let r = ItemId::ontology_root(); assert_eq!(r.display_path(), "~/"); assert_eq!(r.tilde_tail(), Some("")); } #[test] fn tilde_segments_deep() { - let c = CanonicalItemUrl::parse("~/a/b").unwrap(); + let c = ItemId::parse("~/a/b").unwrap(); assert_eq!(c.tilde_segments(), vec!["~", "a", "b"]); } #[test] fn tilde_segments_root() { - let c = CanonicalItemUrl::parse("~/").unwrap(); + let c = ItemId::parse("~/").unwrap(); assert_eq!(c.tilde_segments(), vec!["~"]); } #[test] fn tilde_segments_non_ontology_is_empty() { - let c = CanonicalItemUrl::parse("https://example.com/foo").unwrap(); + let c = ItemId::parse("https://example.com/foo").unwrap(); assert_eq!(c.tilde_segments(), Vec::<&str>::new()); } @@ -831,28 +487,28 @@ mod tests { } #[test] - fn canonical_item_url_parent_external_strips_last_segment() { - let c = CanonicalItemUrl::parse("https://spotify.com/track/1").unwrap(); + fn item_id_parent_external_strips_last_segment() { + let c = ItemId::parse("https://spotify.com/track/1").unwrap(); assert_eq!( c.parent().unwrap().as_str(), "https://spotify.com/track" ); assert_eq!( - CanonicalItemUrl::parse("https://github.com/iss/1") + ItemId::parse("https://github.com/iss/1") .unwrap() .parent() .unwrap() .as_str(), "https://github.com/iss" ); - assert!(CanonicalItemUrl::parse("https://github.com").unwrap().parent().is_none()); + assert!(ItemId::parse("https://github.com").unwrap().parent().is_none()); } #[test] fn display_path_roundtrips_dash_and_tilde() { - let ext = CanonicalItemUrl::parse("https://GitHub.com/org/Issue").unwrap(); + let ext = ItemId::parse("https://GitHub.com/org/Issue").unwrap(); assert_eq!(ext.display_path(), "-/github.com/org/issue"); - let tilde = CanonicalItemUrl::parse("~/Rust/Doc").unwrap(); + let tilde = ItemId::parse("~/Rust/Doc").unwrap(); assert_eq!(tilde.display_path(), "~/rust/doc"); }