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: [2fe70b0e] themes Side A — unified diff (full patch): diff --git a/server/src/api/auth.rs b/server/src/api/auth.rs index 559db65de14c6157690ffbf18eca0cf65b0a5202..b3631b06153d52f88348fef927e7a024b7b85ad6 100644 --- a/server/src/api/auth.rs +++ b/server/src/api/auth.rs @@ -1,7 +1,7 @@ use axum::{ body::Body, extract::{Path, Query, State}, - http::{header, HeaderMap, HeaderValue, StatusCode}, + http::{header, HeaderMap, HeaderValue, StatusCode, Uri}, response::{IntoResponse, Redirect, Response}, Form, Json, }; @@ -17,7 +17,7 @@ use crate::{ events::{Event, GrantAdded, TokenIssued, UserRegistered}, html::{ auth_complete_page, auth_signed_in_fragment, choose_username_error_fragment, - choose_username_page, JsBuilder, + choose_username_page, theme_cookie_header_from_jar, theme_from_jar, theme_next_from_uri, JsBuilder, }, identity::{parse_agent, parse_username}, reducer::ReducerState, @@ -48,15 +48,17 @@ fn js_form_error_fragment(session: &str, error: &str) -> Response { .into_response() } -fn js_signed_in_fragment(bearer: &str) -> Response { +fn js_signed_in_fragment(bearer: &str, jar: &CookieJar) -> Response { let mut response = JsBuilder::new() .id("choose-username-form") .morph_inner(auth_signed_in_fragment()) .redirect("/auth/complete") .into_response(); - response - .headers_mut() - .insert(header::SET_COOKIE, session_cookie_header_value(bearer)); + let headers = response.headers_mut(); + headers.append(header::SET_COOKIE, session_cookie_header_value(bearer)); + if let Some(theme) = theme_cookie_header_from_jar(jar) { + headers.append(header::SET_COOKIE, theme); + } response } @@ -69,13 +71,18 @@ pub fn optional_principal(headers: &HeaderMap, jar: &CookieJar, reduced: &Reduce verify_token(reduced, c.value()).ok() } -fn redirect_with_session_cookie(public_url: &str, path_and_query: &str, bearer: &str) -> Response { - Response::builder() +fn redirect_with_session_cookie(public_url: &str, path_and_query: &str, bearer: &str, jar: &CookieJar) -> Response { + let mut res = Response::builder() .status(StatusCode::TEMPORARY_REDIRECT) .header(header::LOCATION, format!("{public_url}{path_and_query}")) - .header(header::SET_COOKIE, session_cookie_header_value(bearer)) .body(Body::empty()) - .unwrap() + .unwrap(); + let headers = res.headers_mut(); + headers.append(header::SET_COOKIE, session_cookie_header_value(bearer)); + if let Some(theme) = theme_cookie_header_from_jar(jar) { + headers.append(header::SET_COOKIE, theme); + } + res } async fn apply_invite_redemption(state: &AppState, invite_token: &str, grantee_username: &str) -> Result<(), String> { @@ -286,7 +293,11 @@ pub struct AuthCallbackQuery { pub state: String, } -pub async fn get_auth_callback(Query(q): Query, State(state): State) -> impl IntoResponse { +pub async fn get_auth_callback( + Query(q): Query, + State(state): State, + jar: CookieJar, +) -> impl IntoResponse { let sessions = pending_sessions(&state); { let sessions_read = sessions.read().await; @@ -365,7 +376,7 @@ pub async fn get_auth_callback(Query(q): Query, State(state): } let cookie_bearer = bearer.clone(); s.complete = Some((username, bearer)); - return redirect_with_session_cookie(&public_url, "/", &cookie_bearer).into_response(); + return redirect_with_session_cookie(&public_url, "/", &cookie_bearer, &jar).into_response(); } } @@ -378,14 +389,20 @@ pub struct ChooseUsernameQuery { pub error: Option, } -pub async fn get_choose_username(Query(q): Query, State(state): State) -> impl IntoResponse { +pub async fn get_choose_username( + Query(q): Query, + State(state): State, + jar: CookieJar, + uri: Uri, +) -> impl IntoResponse { let sessions = pending_sessions(&state); let sessions_read = sessions.read().await; if !sessions_read.contains_key(&q.session) { return api_error(StatusCode::NOT_FOUND, "unknown session", None).into_response(); } drop(sessions_read); - choose_username_page(&q.session, q.error.as_deref()).into_response() + let next = theme_next_from_uri(&uri); + choose_username_page(&q.session, q.error.as_deref(), theme_from_jar(&jar), &next).into_response() } #[derive(Debug, Deserialize)] @@ -396,6 +413,7 @@ pub struct ChooseUsernameForm { pub async fn post_choose_username( State(state): State, + jar: CookieJar, Form(form): Form, ) -> impl IntoResponse { let canon_user = match parse_username(&form.username) { @@ -477,7 +495,7 @@ pub async fn post_choose_username( s.complete = Some((canon_user.clone(), bearer.clone())); } - js_signed_in_fragment(&bearer).into_response() + js_signed_in_fragment(&bearer, &jar).into_response() } /// Start a browser-only OAuth flow (no CLI polling). Sets session cookie on success. @@ -569,8 +587,9 @@ pub async fn get_pending_session( .into_response() } -pub async fn get_auth_complete() -> impl IntoResponse { - auth_complete_page() +pub async fn get_auth_complete(jar: CookieJar, uri: Uri) -> impl IntoResponse { + let next = theme_next_from_uri(&uri); + auth_complete_page(theme_from_jar(&jar), &next).into_response() } pub async fn get_whoami(State(state): State, headers: HeaderMap) -> impl IntoResponse { diff --git a/server/src/api/helpers.rs b/server/src/api/helpers.rs index 81e2a55fa3abb8609b4099f91a989480336e11eb..9b71491e9f9efc44a2a4beba09be8f64bd2ff2ee 100644 --- a/server/src/api/helpers.rs +++ b/server/src/api/helpers.rs @@ -39,6 +39,55 @@ pub fn item_path_for_api(item: &str) -> String { } } +/// Same as [`item_path_for_api`], but for private rooms ontology items are prefixed with +/// `/r/{short}/{slug}` so the URL matches the web app (`/r/…/~/…` routes). +pub fn item_path_for_api_in_room(item: &str, room_wire: &str) -> String { + let room = room_wire.trim(); + if room.is_empty() || room == "public" { + return item_path_for_api(item); + } + let Some((short, slug)) = room.split_once('/') else { + return item_path_for_api(item); + }; + if short.is_empty() || slug.is_empty() { + return item_path_for_api(item); + } + let Some(c) = CanonicalItemUrl::parse(item) else { + return item_path_for_api(item); + }; + let root = CanonicalItemUrl::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() { + return if tail.is_empty() { + format!("https://slug.social/r/{short}/{slug}/~") + } else { + format!("https://slug.social/r/{short}/{slug}/~/{}", tail) + }; + } + if item_norm == root_norm { + return format!("https://slug.social/r/{short}/{slug}/~"); + } + item_path_for_api(item) +} + +/// Absolute thread URL for forum JSON (`/t/…` vs `/r/…/t/…`). +pub fn forum_thread_web_url(room_wire: &str, thread_tag: &str) -> String { + let room = room_wire.trim(); + let tag = thread_tag.trim().trim_start_matches('#'); + if room.is_empty() || room == "public" { + format!("https://slug.social/t/{tag}") + } else if let Some((short, slug)) = room.split_once('/') { + if short.is_empty() || slug.is_empty() { + format!("https://slug.social/t/{tag}") + } else { + format!("https://slug.social/r/{short}/{slug}/t/{tag}") + } + } else { + format!("https://slug.social/t/{tag}") + } +} + /// Resolve an item path as a first-class canonical path. pub fn resolve_item(item: &str) -> Result { let canonical = canonicalize_item(item); @@ -188,3 +237,52 @@ pub fn vote_touches_path(a: &str, b: &str, parent_canon: &str) -> bool { let under = |item: &str| item == parent_canon || item.starts_with(&format!("{}/", parent_canon)); under(a) || under(b) } + +#[cfg(test)] +mod wire_url_tests { + use super::{forum_thread_web_url, item_path_for_api_in_room}; + + #[test] + fn public_room_unchanged() { + let u = "https://slug.social/~/a/b"; + assert_eq!(item_path_for_api_in_room(u, "public"), u); + } + + #[test] + fn private_room_prefixes_ontology() { + assert_eq!( + item_path_for_api_in_room("https://slug.social/~/topic/x", "9ab12cd/my-room"), + "https://slug.social/r/9ab12cd/my-room/~/topic/x" + ); + } + + #[test] + fn private_room_ontology_root() { + assert_eq!( + item_path_for_api_in_room("https://slug.social/~", "9ab12cd/my-room"), + "https://slug.social/r/9ab12cd/my-room/~" + ); + assert_eq!( + item_path_for_api_in_room("https://slug.social/~/", "9ab12cd/my-room"), + "https://slug.social/r/9ab12cd/my-room/~" + ); + } + + #[test] + fn external_url_untouched_in_private_room() { + let u = "https://example.com/z"; + assert_eq!(item_path_for_api_in_room(u, "9ab12cd/my-room"), u); + } + + #[test] + fn forum_web_public_vs_room() { + assert_eq!( + forum_thread_web_url("public", "debate"), + "https://slug.social/t/debate" + ); + assert_eq!( + forum_thread_web_url("9ab12cd/my-room", "#debate"), + "https://slug.social/r/9ab12cd/my-room/t/debate" + ); + } +} diff --git a/server/src/api/rpc.rs b/server/src/api/rpc.rs index 31f5fcfb4eaf4df0a9cbac532dd3dedfe3611810..5b91f5836625eedbb1cd9423168046e3fb576c17 100644 --- a/server/src/api/rpc.rs +++ b/server/src/api/rpc.rs @@ -27,8 +27,9 @@ use crate::{ use super::auth::verify_bearer_principal; use super::helpers::{ - compute_connectivity_stats, is_pair_voted, item_path_for_api, now_ms, paginate_rankings, - parse_parent_specs, pick_random_distinct, resolve_item, vote_touches_path, + compute_connectivity_stats, forum_thread_web_url, is_pair_voted, item_path_for_api, + item_path_for_api_in_room, now_ms, paginate_rankings, parse_parent_specs, pick_random_distinct, + resolve_item, vote_touches_path, }; use super::validate::{normalize_room_and_thread, validate_ingest_document}; @@ -148,6 +149,7 @@ fn compute_scope_rank_changes( parent: &str, before: &crate::scope_rank::ChildrenRankings, after: &crate::scope_rank::ChildrenRankings, + room_wire: &str, ) -> Option { fn build_positions(rankings: &crate::scope_rank::ChildrenRankings) -> HashMap> { let mut map = HashMap::new(); @@ -182,7 +184,7 @@ fn compute_scope_rank_changes( }; if changed { changes.push(RankChange { - item: item_path_for_api(&item), + item: item_path_for_api_in_room(&item, room_wire), before: b, after: a, }); @@ -204,7 +206,7 @@ fn compute_scope_rank_changes( parent: if parent.is_empty() { "/".to_string() } else { - item_path_for_api(parent) + item_path_for_api_in_room(parent, room_wire) }, changes, }) @@ -256,6 +258,7 @@ fn build_rank_response_for_content( offset: usize, limit: Option, want_percent: bool, + room_wire: &str, ) -> Result { let parent_owned = parent.map(|s| s.to_string()); let specs = parse_parent_specs(parent_owned.as_ref()); @@ -299,7 +302,7 @@ fn build_rank_response_for_content( .ranked .into_iter() .map(|r| RankRow { - item: item_path_for_api(r.item.as_str()), + item: item_path_for_api_in_room(r.item.as_str(), room_wire), percent: if want_percent { Some((r.score / max_score) * 100.0) } else { @@ -315,7 +318,7 @@ fn build_rank_response_for_content( let prefixed_unranked: Vec = rankings .unranked_items .into_iter() - .map(|s| item_path_for_api(s.as_str())) + .map(|s| item_path_for_api_in_room(s.as_str(), room_wire)) .collect(); let (components, unranked_items) = if offset > 0 || limit.is_some() { @@ -522,7 +525,7 @@ async fn rpc_post( .filter_map(|p| { let before = pre_rankings.get(p)?; let after = crate::scope_rank::build_children_rankings(content, p); - compute_scope_rank_changes(p.as_str(), before, &after) + compute_scope_rank_changes(p.as_str(), before, &after, &room_key) }) .collect(); if v.is_empty() { None } else { Some(v) } @@ -530,14 +533,28 @@ async fn rpc_post( None }; + let (pair_hint, rank_hint, web_url) = if room_key == "public" { + ( + "npx slugsocial public garden pair".to_string(), + "npx slugsocial public garden rank".to_string(), + forum_thread_web_url("public", &thread_id), + ) + } else { + ( + format!("npx slugsocial private {room_key} garden pair"), + format!("npx slugsocial private {room_key} garden rank"), + forum_thread_web_url(&room_key, &thread_id), + ) + }; + Ok(RpcResult::PostOk { events_appended, ranking_changes, threads: vec![format!("#{}", thread_id)], next: NextMoves { - pair: "npx slugsocial public garden pair".to_string(), - rank: "npx slugsocial public garden rank".to_string(), - web: format!("https://slug.social/t/{}", thread_id), + pair: pair_hint, + rank: rank_hint, + web: web_url, }, }) } @@ -609,7 +626,7 @@ async fn rpc_check( raw: v.raw_text.clone(), principal, delegate, - room_id: room_key, + room_id: room_key.clone(), thread_tag: thread_id.clone(), }); @@ -647,7 +664,7 @@ async fn rpc_check( .ranked .into_iter() .map(|r| RankRow { - item: item_path_for_api(r.item.as_str()), + item: item_path_for_api_in_room(r.item.as_str(), &room_key), score: r.score, percent: None, }) @@ -655,25 +672,35 @@ async fn rpc_check( }) .collect(); CheckScopeRanking { - parent: item_path_for_api(parent.as_str()), + parent: item_path_for_api_in_room(parent.as_str(), &room_key), components, unranked_items: scoped .unranked_items .into_iter() - .map(|it| item_path_for_api(it.as_str())) + .map(|it| item_path_for_api_in_room(it.as_str(), &room_key)) .collect(), } }) .collect(); + let check_next = if room_key == "public" { + vec![ + "npx slugsocial public forum post --delegate ".to_string(), + "npx slugsocial public forum list".to_string(), + forum_thread_web_url("public", &thread_id), + ] + } else { + vec![ + format!("npx slugsocial private {room_key} forum post --delegate "), + format!("npx slugsocial private {room_key} forum list"), + forum_thread_web_url(&room_key, &thread_id), + ] + }; + Ok(RpcResult::CheckOk { rankings, threads: vec![format!("#{}", thread_id)], - next: vec![ - "npx slugsocial public forum post --delegate ".to_string(), - "npx slugsocial public forum list".to_string(), - format!("https://slug.social/t/{}", thread_id), - ], + next: check_next, }) } @@ -690,7 +717,7 @@ fn rpc_list_forum_threads(reduced: &ReducerState, room: &str) -> ThreadsResponse .map(|((_, tag), ts)| ThreadSummary { thread: format!("#{tag}"), last_activity_ts: ts.last_activity_ts, - web: format!("https://slug.social/t/{}", tag), + web: forum_thread_web_url(room, tag), }) .collect(); out.sort_by(|a, b| b.last_activity_ts.cmp(&a.last_activity_ts)); @@ -1031,8 +1058,8 @@ async fn rpc_get_pair(state: &AppState, room: String, parent_path: String) -> Re .collect(); let cs = compute_connectivity_stats(&content.ranking_group, &pool); Ok(RpcResult::Pair(PairResponse { - left: item_path_for_api(&left), - right: item_path_for_api(&right), + left: item_path_for_api_in_room(&left, &room), + right: item_path_for_api_in_room(&right, &room), left_body: lb, right_body: rb, threads: th, @@ -1088,6 +1115,7 @@ pub async fn handle_rpc_batch( offset.unwrap_or(0), limit, percent.unwrap_or(false), + &room, ) { Ok(r) => line_ok(RpcResult::GardenRank(r)), Err((e, h)) => line_err(e, h), @@ -1109,7 +1137,7 @@ pub async fn handle_rpc_batch( if !content.items.contains(&item) { line_err( "item not found", - Some(format!("{} does not exist", item_path_for_api(&item_str))), + Some(format!("{} does not exist", item_path_for_api_in_room(&item_str, &room))), ) } else { const MAX_ITEM_BODY: usize = 10_000; @@ -1132,7 +1160,7 @@ pub async fn handle_rpc_batch( .map(|s| s.iter().cloned().collect()) .unwrap_or_default(); line_ok(RpcResult::GardenItem(ItemResponse { - item: item_str, + item: item_path_for_api_in_room(&item_str, &room), body, truncated, body_len, @@ -1525,7 +1553,11 @@ pub async fn handle_rpc_batch( let range = (top - bot).max(1e-12); for r in items { let pct = want_percent.then(|| ((r.score - bot) / range * 100.0).clamp(0.0, 100.0)); - ranked.push(RankRow { item: item_path_for_api(r.item.as_str()), score: r.score, percent: pct }); + ranked.push(RankRow { + item: item_path_for_api_in_room(r.item.as_str(), &room), + score: r.score, + percent: pct, + }); } } @@ -1542,7 +1574,7 @@ pub async fn handle_rpc_batch( let page: Vec = ranked .into_iter() .chain(unranked.into_iter().map(|it| RankRow { - item: item_path_for_api(&it), + item: item_path_for_api_in_room(&it, &room), score: 0.0, percent: want_percent.then_some(0.0), })) @@ -1586,7 +1618,7 @@ pub async fn handle_rpc_batch( if !content.items.contains(&item) { line_err( "item not found", - Some(format!("{} does not exist", item_path_for_api(&item_str))), + Some(format!("{} does not exist", item_path_for_api_in_room(&item_str, &room))), ) } else { let votes: Vec = content @@ -1597,8 +1629,8 @@ pub async fn handle_rpc_batch( .take(limit) .map(|v| VoteRow { ts: v.ts, - a: v.a.as_str().to_string(), - b: v.b.as_str().to_string(), + a: item_path_for_api_in_room(v.a.as_str(), &room), + b: item_path_for_api_in_room(v.b.as_str(), &room), ratio: format!("{}:{}", v.ratio_left, v.ratio_right), actor: Some(v.principal.clone()), body: v.body.clone(), @@ -1608,7 +1640,7 @@ pub async fn handle_rpc_batch( }) .unwrap_or_default(); line_ok(RpcResult::Matchup(MatchupResponse { - item: item_path_for_api(&item_str), + item: item_path_for_api_in_room(&item_str, &room), votes, })) } @@ -1635,8 +1667,8 @@ pub async fn handle_rpc_batch( if a == item_str || b == item_str { Some(VoteRow { ts: e.ts, - a: item_path_for_api(&a), - b: item_path_for_api(&b), + a: item_path_for_api_in_room(&a, &room), + b: item_path_for_api_in_room(&b, &room), ratio: format!("{}:{}", ratio_left, ratio_right), actor: reduced.ingests_by_id.get(&e.post_id).map(|ing| ing.principal.clone()), body: explanation, @@ -1672,7 +1704,7 @@ pub async fn handle_rpc_batch( } }).collect(); line_ok(RpcResult::RankHistory(RankHistoryResponse { - item: item_path_for_api(&item_str), + item: item_path_for_api_in_room(&item_str, &room), history, })) } @@ -1691,6 +1723,10 @@ pub async fn handle_rpc_batch( .map(|p| p.as_str().to_string()) .collect(); paths.sort(); + let paths: Vec = paths + .into_iter() + .map(|p| item_path_for_api_in_room(&p, &room)) + .collect(); line_ok(RpcResult::Leaves(LeavesResponse { paths })) } }, @@ -1707,10 +1743,21 @@ pub async fn handle_rpc_batch( 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 path_label = CanonicalItemUrl::parse(path.as_str()) + .and_then(|c| { + c.tilde_tail().map(|t| { + if t.is_empty() { + "~/".to_string() + } else { + format!("~/{}", t) + } + }) + }) + .unwrap_or_else(|| path.to_string()); PathSummary { - path: format!("~/{}", path), + path: path_label, children, - web: format!("https://slug.social/~/{}", path), + web: item_path_for_api_in_room(path.as_str(), &room), } }).collect(); v.sort_by(|a, b| a.path.cmp(&b.path)); @@ -1743,8 +1790,8 @@ pub async fn handle_rpc_batch( .take(limit) .map(|v| VoteRow { ts: v.ts, - a: v.a.as_str().to_string(), - b: v.b.as_str().to_string(), + a: item_path_for_api_in_room(v.a.as_str(), &room), + b: item_path_for_api_in_room(v.b.as_str(), &room), ratio: format!("{}:{}", v.ratio_left, v.ratio_right), actor: Some(v.principal.clone()), body: v.body.clone(), diff --git a/server/src/html/auth.rs b/server/src/html/auth.rs index d57b1c725ce3e06b1d904882b9388c3da48d85cb..5df77c76b89ed2bb11d936e1dbb804d50a0e79a1 100644 --- a/server/src/html/auth.rs +++ b/server/src/html/auth.rs @@ -23,7 +23,7 @@ fn form_inner(session: &str, error: Option<&str>) -> Markup { } } -pub fn choose_username_page(session: &str, error: Option<&str>) -> Markup { +pub fn choose_username_page(session: &str, error: Option<&str>, theme: &str, theme_next: &str) -> Markup { let body = html! { nav.breadcrumb { a href="/" { "slug.social" } @@ -36,7 +36,7 @@ pub fn choose_username_page(session: &str, error: Option<&str>) -> Markup { (form_inner(session, error)) } }; - super::layout("join — slug.social", "view-auth", body, None) + super::layout("join — slug.social", "view-auth", body, None, theme, theme_next) } /// Fragment returned to the poem JS on error — replaces the form's innerHTML. @@ -52,7 +52,7 @@ pub fn auth_signed_in_fragment() -> Markup { } } -pub fn auth_complete_page() -> Markup { +pub fn auth_complete_page(theme: &str, theme_next: &str) -> Markup { let body = html! { nav.breadcrumb { a href="/" { "slug.social" } @@ -63,5 +63,5 @@ pub fn auth_complete_page() -> Markup { p { "Return to your terminal — your agent is polling and will collect your token automatically." } p.auth-hint { "You can close this tab." } }; - super::layout("signed in — slug.social", "view-auth", body, None) + super::layout("signed in — slug.social", "view-auth", body, None, theme, theme_next) } diff --git a/server/src/html/editor.rs b/server/src/html/editor.rs index 07f6ab3b778f07f60765fa3f39060ebc5f72bc1a..ecdd226b1b17d5f11d1b79f99add5759ef918281 100644 --- a/server/src/html/editor.rs +++ b/server/src/html/editor.rs @@ -1,8 +1,10 @@ use axum::{ extract::State, + http::Uri, response::{Html, IntoResponse}, Form, }; +use axum_extra::extract::cookie::CookieJar; use maud::{html, Markup}; use serde::Deserialize; @@ -13,7 +15,7 @@ use crate::{ state::AppState, }; -use super::{bc_segment, layout}; +use super::{bc_segment, layout, theme_from_jar, theme_next_from_uri}; fn bc_try() -> Markup { html! { @@ -23,7 +25,7 @@ fn bc_try() -> Markup { } /// The interactive editor page — `/try`. -pub async fn editor_page() -> impl IntoResponse { +pub async fn editor_page(jar: CookieJar, uri: Uri) -> impl IntoResponse { let page = layout( "try — slug.social", "view-thread", @@ -68,6 +70,8 @@ pub async fn editor_page() -> impl IntoResponse { "#)) } }, None, + theme_from_jar(&jar), + &theme_next_from_uri(&uri), ); Html(page.into_string()) } diff --git a/server/src/html/forum.rs b/server/src/html/forum.rs index 5c5f9e2ba0e5cb1d661c113117988ec80c01deca..368b1016eb43d83e9ff39464ff6164fa9a8b558a 100644 --- a/server/src/html/forum.rs +++ b/server/src/html/forum.rs @@ -1,6 +1,6 @@ use axum::{ extract::{Path, Query, State}, - http::{HeaderMap, StatusCode}, + http::{HeaderMap, StatusCode, Uri}, response::{Html, IntoResponse}, }; use axum_extra::extract::cookie::CookieJar; @@ -19,7 +19,7 @@ use crate::{ use super::{ bc_segment, bc_threads, cli_panel, layout, now_ms, profile_href, recency_class, - render_linkified_with_embeds_in_scope, JsBuilder, + render_linkified_with_embeds_in_scope, theme_from_jar, theme_next_from_uri, JsBuilder, }; #[derive(Clone)] @@ -577,6 +577,7 @@ pub async fn home( State(state): State, headers: HeaderMap, jar: CookieJar, + uri: Uri, ) -> impl IntoResponse { let now = now_ms(); let reduced = state.reduced.read().await; @@ -624,6 +625,8 @@ pub async fn home( (cli_panel("npx slugsocial public forum list")) }, None, + theme_from_jar(&jar), + &theme_next_from_uri(&uri), ); Html(page.into_string()) } @@ -679,6 +682,7 @@ async fn thread_view_inner( nav: ThreadNav, headers: HeaderMap, jar: CookieJar, + uri: Uri, ) -> impl IntoResponse { let tag = canonicalize_tag(&tag); let scope = nav.scope(); @@ -778,6 +782,8 @@ async fn thread_view_inner( (cli_panel(&cli)) }, None, + theme_from_jar(&jar), + &theme_next_from_uri(&uri), ); Html(page.into_string()).into_response() } @@ -789,8 +795,9 @@ pub async fn thread_view( Query(q): Query, headers: HeaderMap, jar: CookieJar, + uri: Uri, ) -> impl IntoResponse { - thread_view_inner(state, tag, q, ThreadNav::public(), headers, jar).await + thread_view_inner(state, tag, q, ThreadNav::public(), headers, jar, uri).await } /// Room thread — `/r/:short/:slug/t/:tag` @@ -800,31 +807,39 @@ pub async fn room_thread_view( Query(q): Query, headers: HeaderMap, jar: CookieJar, + uri: Uri, ) -> impl IntoResponse { let room_id = format!("{room_short}/{room_slug}"); let reduced = state.reduced.read().await; let user = optional_principal(&headers, &jar, &reduced); if !user_can_view_room(&reduced, &room_id, user.as_deref()) { drop(reduced); - return room_not_found_page().into_response(); + return room_not_found_page(&jar, &uri).into_response(); } drop(reduced); let Some(nav) = ThreadNav::from_room_id(&room_id) else { return (StatusCode::NOT_FOUND, "bad room path").into_response(); }; - thread_view_inner(state, tag, q, nav, headers, jar) + thread_view_inner(state, tag, q, nav, headers, jar, uri) .await .into_response() } -fn room_not_found_page() -> impl IntoResponse { +fn room_not_found_page(jar: &CookieJar, uri: &Uri) -> impl IntoResponse { let body = html! { nav class="breadcrumb" { a href="/" { "slug.social" } } h1 { "not found" } p { "The requested page could not be found." } p { a href="/" { "home" } } }; - let page = layout("not found — slug.social", "view-thread", body, None); + let page = layout( + "not found — slug.social", + "view-thread", + body, + None, + theme_from_jar(jar), + &theme_next_from_uri(uri), + ); (StatusCode::NOT_FOUND, Html(page.into_string())) } @@ -834,6 +849,7 @@ pub async fn room_page( Path((room_short, room_slug)): Path<(String, String)>, headers: HeaderMap, jar: CookieJar, + uri: Uri, ) -> impl IntoResponse { let room_id = format!("{room_short}/{room_slug}"); let now = now_ms(); @@ -845,7 +861,7 @@ pub async fn room_page( let user = optional_principal(&headers, &jar, &reduced); if !user_can_view_room(&reduced, &room_id, user.as_deref()) { drop(reduced); - return room_not_found_page().into_response(); + return room_not_found_page(&jar, &uri).into_response(); } let scope = ScopeId::Room(room_id.clone()); let mut rows = collect_thread_rows_for_scope(&reduced, &scope, now); @@ -902,6 +918,8 @@ pub async fn room_page( (cli_panel(&audit_cli)) }, None, + theme_from_jar(&jar), + &theme_next_from_uri(&uri), ); Html(page.into_string()).into_response() } @@ -943,6 +961,8 @@ async fn thread_post_view_inner( index_str: String, nav: ThreadNav, viewer: Option, + jar: CookieJar, + uri: Uri, ) -> impl IntoResponse { let tag = canonicalize_tag(&tag); let index: usize = index_str.parse().unwrap_or(0); @@ -987,6 +1007,8 @@ async fn thread_post_view_inner( } }, None, + theme_from_jar(&jar), + &theme_next_from_uri(&uri), ); Html(page.into_string()).into_response() } @@ -996,11 +1018,12 @@ pub async fn thread_post_view( Path((tag, index_str)): Path<(String, String)>, headers: HeaderMap, jar: CookieJar, + uri: Uri, ) -> impl IntoResponse { let reduced = state.reduced.read().await; let viewer = optional_principal(&headers, &jar, &reduced); drop(reduced); - thread_post_view_inner(state, tag, index_str, ThreadNav::public(), viewer) + thread_post_view_inner(state, tag, index_str, ThreadNav::public(), viewer, jar, uri) .await } @@ -1009,19 +1032,20 @@ pub async fn room_thread_post_view( Path((room_short, room_slug, tag, index_str)): Path<(String, String, String, String)>, headers: HeaderMap, jar: CookieJar, + uri: Uri, ) -> impl IntoResponse { let room_id = format!("{room_short}/{room_slug}"); let reduced = state.reduced.read().await; let user = optional_principal(&headers, &jar, &reduced); if !user_can_view_room(&reduced, &room_id, user.as_deref()) { drop(reduced); - return room_not_found_page().into_response(); + return room_not_found_page(&jar, &uri).into_response(); } drop(reduced); let Some(nav) = ThreadNav::from_room_id(&room_id) else { return (StatusCode::NOT_FOUND, "bad room path").into_response(); }; - thread_post_view_inner(state, tag, index_str, nav, user) + thread_post_view_inner(state, tag, index_str, nav, user, jar, uri) .await .into_response() } @@ -1193,6 +1217,7 @@ pub async fn user_profile_page( Path(username): Path, headers: HeaderMap, jar: CookieJar, + uri: Uri, ) -> impl IntoResponse { let canon = match parse_username(&username) { Ok(u) => u, @@ -1270,6 +1295,8 @@ pub async fn user_profile_page( (cli_panel(&format!("npx slugsocial public forum list"))) }, None, + theme_from_jar(&jar), + &theme_next_from_uri(&uri), ); Html(page.into_string()).into_response() } @@ -1292,13 +1319,14 @@ pub async fn room_thread_post_expand( Path((room_short, room_slug, tag, index_str)): Path<(String, String, String, String)>, headers: HeaderMap, jar: CookieJar, + uri: Uri, ) -> impl IntoResponse { let room_id = format!("{room_short}/{room_slug}"); let reduced = state.reduced.read().await; let user = optional_principal(&headers, &jar, &reduced); if !user_can_view_room(&reduced, &room_id, user.as_deref()) { drop(reduced); - return room_not_found_page().into_response(); + return room_not_found_page(&jar, &uri).into_response(); } let Some(nav) = ThreadNav::from_room_id(&room_id) else { drop(reduced); @@ -1328,13 +1356,14 @@ pub async fn room_thread_post_expand_deleted( Path((room_short, room_slug, tag, index_str)): Path<(String, String, String, String)>, headers: HeaderMap, jar: CookieJar, + uri: Uri, ) -> impl IntoResponse { let room_id = format!("{room_short}/{room_slug}"); let reduced = state.reduced.read().await; let user = optional_principal(&headers, &jar, &reduced); if !user_can_view_room(&reduced, &room_id, user.as_deref()) { drop(reduced); - return room_not_found_page().into_response(); + return room_not_found_page(&jar, &uri).into_response(); } let Some(nav) = ThreadNav::from_room_id(&room_id) else { drop(reduced); @@ -1364,13 +1393,14 @@ pub async fn room_thread_post_collapse_deleted( Path((room_short, room_slug, tag, index_str)): Path<(String, String, String, String)>, headers: HeaderMap, jar: CookieJar, + uri: Uri, ) -> impl IntoResponse { let room_id = format!("{room_short}/{room_slug}"); let reduced = state.reduced.read().await; let user = optional_principal(&headers, &jar, &reduced); if !user_can_view_room(&reduced, &room_id, user.as_deref()) { drop(reduced); - return room_not_found_page().into_response(); + return room_not_found_page(&jar, &uri).into_response(); } let Some(nav) = ThreadNav::from_room_id(&room_id) else { drop(reduced); diff --git a/server/src/html/garden.rs b/server/src/html/garden.rs index e08f068ae04f5a2542ec2c43c240f931f87146bf..9b4789a79c3e293cbfc5f033a0eac8650320d94d 100644 --- a/server/src/html/garden.rs +++ b/server/src/html/garden.rs @@ -1,6 +1,6 @@ use axum::{ extract::{Path, State}, - http::{HeaderMap, StatusCode}, + http::{HeaderMap, StatusCode, Uri}, response::{Html, IntoResponse}, }; use axum_extra::extract::cookie::CookieJar; @@ -20,7 +20,8 @@ use crate::{ use super::{ bc_path, bc_segment, cli_panel, layout, now_ms, ratio_pct, - render_linkified_with_embeds_in_scope, breadcrumb_path::OntologyPath, + render_linkified_with_embeds_in_scope, theme_from_jar, theme_next_from_uri, + breadcrumb_path::OntologyPath, forum::ThreadNav, }; @@ -59,14 +60,21 @@ fn scoped_bc_path(path: &OntologyPath, nav: &ThreadNav) -> maud::Markup { } } -fn room_not_found_page() -> impl IntoResponse { +fn room_not_found_page(jar: &CookieJar, uri: &Uri) -> impl IntoResponse { let body = html! { nav class="breadcrumb" { a href="/" { "slug.social" } } h1 { "not found" } p { "The requested page could not be found." } p { a href="/" { "home" } } }; - let page = layout("not found — slug.social", "view-thread", body, None); + let page = layout( + "not found — slug.social", + "view-thread", + body, + None, + theme_from_jar(jar), + &theme_next_from_uri(uri), + ); (StatusCode::NOT_FOUND, Html(page.into_string())) } @@ -81,7 +89,11 @@ fn user_can_view_room(reduced: &ReducerState, room_id: &str, username: Option<&s } /// Ontology index — root-level paths. Private (UUID) roots are excluded. -pub async fn garden_index(State(state): State) -> impl IntoResponse { +pub async fn garden_index( + State(state): State, + jar: CookieJar, + uri: Uri, +) -> impl IntoResponse { let nav = ThreadNav::public(); let child_rankings = { let reduced = state.reduced.read().await; @@ -130,6 +142,8 @@ pub async fn garden_index(State(state): State) -> impl IntoResponse { (cli_panel("npx slugsocial garden tree")) }, None, + theme_from_jar(&jar), + &theme_next_from_uri(&uri), ); Html(page.into_string()) } @@ -138,9 +152,11 @@ pub async fn garden_index(State(state): State) -> impl IntoResponse { pub async fn ontology_path( State(state): State, Path(path): Path, + jar: CookieJar, + uri: Uri, ) -> impl IntoResponse { let path = OntologyPath::from_input(&path); - render_scope_view(state, path, ThreadNav::public()).await + render_scope_view(state, path, ThreadNav::public(), jar, uri).await } pub async fn room_garden_index( @@ -148,19 +164,20 @@ pub async fn room_garden_index( Path((room_short, room_slug)): Path<(String, String)>, headers: HeaderMap, jar: CookieJar, + uri: Uri, ) -> impl IntoResponse { let room_id = format!("{room_short}/{room_slug}"); let reduced = state.reduced.read().await; let user = optional_principal(&headers, &jar, &reduced); if !user_can_view_room(&reduced, &room_id, user.as_deref()) { drop(reduced); - return room_not_found_page().into_response(); + return room_not_found_page(&jar, &uri).into_response(); } drop(reduced); let Some(nav) = ThreadNav::from_room_id(&room_id) else { return (StatusCode::NOT_FOUND, "bad room path").into_response(); }; - render_scope_view(state, OntologyPath::root(), nav).await + render_scope_view(state, OntologyPath::root(), nav, jar, uri).await } pub async fn room_ontology_path( @@ -168,20 +185,21 @@ pub async fn room_ontology_path( Path((room_short, room_slug, path)): Path<(String, String, String)>, headers: HeaderMap, jar: CookieJar, + uri: Uri, ) -> impl IntoResponse { let room_id = format!("{room_short}/{room_slug}"); let reduced = state.reduced.read().await; let user = optional_principal(&headers, &jar, &reduced); if !user_can_view_room(&reduced, &room_id, user.as_deref()) { drop(reduced); - return room_not_found_page().into_response(); + return room_not_found_page(&jar, &uri).into_response(); } drop(reduced); let Some(nav) = ThreadNav::from_room_id(&room_id) else { return (StatusCode::NOT_FOUND, "bad room path").into_response(); }; let path = OntologyPath::from_input(&path); - render_scope_view(state, path, nav).await + render_scope_view(state, path, nav, jar, uri).await } #[derive(Debug, Clone)] @@ -375,6 +393,8 @@ async fn render_scope_view( state: AppState, path: OntologyPath, nav: ThreadNav, + jar: CookieJar, + uri: Uri, ) -> axum::response::Response { let scope = nav.scope(); let model = { @@ -525,6 +545,8 @@ async fn render_scope_view( (cli_panel(&cli)) }, None, + theme_from_jar(&jar), + &theme_next_from_uri(&uri), ); Html(page.into_string()).into_response() diff --git a/server/src/html/mod.rs b/server/src/html/mod.rs index a8fd73433d120b07351dd027faf0b25f8f101321..53041a0cf0b30e6f20749c92ecc15a4e9ac56e09 100644 --- a/server/src/html/mod.rs +++ b/server/src/html/mod.rs @@ -1,10 +1,13 @@ use axum::{ body::Body, extract::Path, - http::{header, StatusCode}, + http::{header, HeaderValue, StatusCode, Uri}, response::{IntoResponse, Response}, + Form, }; +use axum_extra::extract::cookie::CookieJar; use maud::{html, Markup, DOCTYPE}; +use serde::Deserialize; use std::collections::HashSet; mod auth; @@ -33,6 +36,75 @@ pub(crate) fn profile_href(username: &str) -> String { format!("/u/{username}") } +/// Cookie name for UI theme (must match document.cookie migration in [`layout`]). +pub const SLUG_THEME_COOKIE: &str = "slug-theme"; + +/// Normalize a requested theme id to a known stylesheet key. +pub fn normalize_theme(raw: &str) -> &'static str { + match raw { + "retro" => "retro", + "retro_craft" => "retro_craft", + _ => "default", + } +} + +/// Resolved theme for rendering and cookie re-issue. +pub fn theme_from_jar(jar: &CookieJar) -> &'static str { + jar.get(SLUG_THEME_COOKIE) + .map(|c| normalize_theme(c.value())) + .unwrap_or("default") +} + +/// `Path` + optional `?query` for round-tripping after `POST /theme`. +pub fn theme_next_from_uri(uri: &Uri) -> String { + uri.path_and_query() + .map(|pq| pq.as_str().to_string()) + .filter(|s| !s.is_empty()) + .unwrap_or_else(|| "/".to_string()) +} + +/// `Set-Cookie` for a validated theme (ASCII cookie value). +pub fn theme_cookie_header_value(theme: &str) -> HeaderValue { + let t = normalize_theme(theme); + let s = format!("{SLUG_THEME_COOKIE}={t}; Path=/; SameSite=Lax; Max-Age=31536000"); + HeaderValue::from_str(&s).expect("theme cookie must be ASCII") +} + +/// Re-issue theme cookie on responses that also set `slug_session`, so login does not drop theme. +pub fn theme_cookie_header_from_jar(jar: &CookieJar) -> Option { + let c = jar.get(SLUG_THEME_COOKIE)?; + Some(theme_cookie_header_value(c.value())) +} + +fn sanitize_theme_next(next: Option<&str>) -> String { + let s = next.unwrap_or("/").trim(); + if s.starts_with('/') && !s.starts_with("//") && s.len() < 8192 { + s.to_string() + } else { + "/".to_string() + } +} + +#[derive(Debug, Deserialize)] +pub struct ThemeForm { + theme: String, + next: Option, +} + +/// `POST /theme` — set theme cookie and redirect back (full navigation, no fetch). +pub async fn post_theme(Form(form): Form) -> impl IntoResponse { + let theme = normalize_theme(&form.theme); + let next = sanitize_theme_next(form.next.as_deref()); + let loc = HeaderValue::try_from(next.as_str()) + .unwrap_or_else(|_| HeaderValue::from_static("/")); + Response::builder() + .status(StatusCode::SEE_OTHER) + .header(header::LOCATION, loc) + .header(header::SET_COOKIE, theme_cookie_header_value(theme)) + .body(Body::empty()) + .expect("theme redirect response") +} + // Embed CSS files at compile time const THEME_DEFAULT_CSS: &str = include_str!("../../static/theme_default.css"); const THEME_RETRO_CSS: &str = include_str!("../../static/theme_retro.css"); @@ -173,7 +245,9 @@ impl JsQueryBuilder { } } -pub(super) fn layout(title: &str, view: &str, body: Markup, views: Option) -> Markup { +pub(super) fn layout(title: &str, view: &str, body: Markup, views: Option, theme: &str, theme_next: &str) -> Markup { + let theme = normalize_theme(theme); + let css_href = format!("/static/theme_{theme}.css"); html! { (DOCTYPE) html { @@ -181,7 +255,20 @@ pub(super) fn layout(title: &str, view: &str, body: Markup, views: Option) meta charset="utf-8"; meta name="viewport" content="width=device-width, initial-scale=1"; title { (title) } - link rel="stylesheet" href="/static/theme_default.css" id="theme-stylesheet"; + script { (maud::PreEscaped(r#" +(function() { + try { + var ls = localStorage.getItem('slug-theme'); + if (!ls) return; + var m = document.cookie.match(/(?:^|;\s*)slug-theme=([^;]+)/); + var c = m ? decodeURIComponent(m[1].replace(/\+/g, ' ')) : ''; + if (ls === c) { localStorage.removeItem('slug-theme'); return; } + document.cookie = 'slug-theme=' + encodeURIComponent(ls) + '; Path=/; SameSite=Lax; Max-Age=31536000'; + location.reload(); + } catch (e) {} +})(); +"#)) } + link rel="stylesheet" href=(css_href) id="theme-stylesheet"; script src="https://unpkg.com/idiomorph@0.3.0/dist/idiomorph.min.js" {} } body class=(view) { @@ -196,32 +283,21 @@ pub(super) fn layout(title: &str, view: &str, body: Markup, views: Option) input type="range" id="spread-slider" min="0" max="1" step="0.05" value="1"; } a id="search-btn" href="/search" { "search" } - div id="theme-switcher" { "theme" } + form id="slug-theme-form" method="post" action="/theme" { + input type="hidden" name="next" value=(theme_next); + select id="theme-select" name="theme" onchange="this.form.submit()" aria-label="Theme" { + @for (val, label) in [("default", "default"), ("retro", "retro"), ("retro_craft", "craft")] { + @if theme == val { + option value=(val) selected { (label) } + } @else { + option value=(val) { (label) } + } + } + } + } } script { (maud::PreEscaped(r#" (function() { - // Theme switching - const themes = ['default', 'retro', 'retro_craft']; - const themeLabel = { default: 'default', retro: 'retro', retro_craft: 'craft' }; - const storedTheme = localStorage.getItem('slug-theme') || 'default'; - const switcher = document.getElementById('theme-switcher'); - const stylesheet = document.getElementById('theme-stylesheet'); - - function setTheme(name) { - stylesheet.href = `/static/theme_${name}.css`; - localStorage.setItem('slug-theme', name); - switcher.textContent = themeLabel[name] || name; - setTimeout(() => setSpread(parseFloat(slider.value)), 50); - } - - setTheme(storedTheme); - - switcher.addEventListener('click', function() { - const current = themes.indexOf(localStorage.getItem('slug-theme') || 'default'); - const next = (current + 1) % themes.length; - setTheme(themes[next]); - }); - // Spread control const slider = document.getElementById('spread-slider'); const storedSpread = localStorage.getItem('slug-spread'); @@ -245,6 +321,7 @@ script { (maud::PreEscaped(r#" const f = e.target; if (!f || f.tagName !== 'FORM') return; if ((f.method || 'get').toLowerCase() !== 'post') return; + if (f.id === 'slug-theme-form') return; e.preventDefault(); const resp = await fetch(f.action, { method: 'POST', diff --git a/server/src/html/search.rs b/server/src/html/search.rs index 0871ddb7a87e3153e2cb6c1f827b46de2c9f0f3b..28ceaa53c3fcac6777311535e95fb771b19438f5 100644 --- a/server/src/html/search.rs +++ b/server/src/html/search.rs @@ -1,6 +1,6 @@ use axum::{ extract::{Query, State}, - http::HeaderMap, + http::{HeaderMap, Uri}, response::{Html, IntoResponse}, }; use axum_extra::extract::cookie::CookieJar; @@ -15,7 +15,7 @@ use crate::{ timeago, }; -use super::{authorship_address, bc_segment, cli_panel, layout, now_ms}; +use super::{authorship_address, bc_segment, cli_panel, layout, now_ms, theme_from_jar, theme_next_from_uri}; /// Escape HTML special chars for safe injection. fn escape_html(s: &str) -> String { @@ -396,6 +396,7 @@ pub async fn search_page( Query(params): Query, headers: HeaderMap, jar: CookieJar, + uri: Uri, ) -> impl IntoResponse { let query = params.q.clone().unwrap_or_default(); let results = if query.len() >= 2 { @@ -420,6 +421,8 @@ pub async fn search_page( (cli_panel("npx slugsocial search ")) }, None, + theme_from_jar(&jar), + &theme_next_from_uri(&uri), ); Html(page.into_string()) } diff --git a/server/src/lib.rs b/server/src/lib.rs index 43b63293c2a62b9700948767c618cb0614ddf6d9..b9d4b79791ba8dd3a3c26ff777943a2548092de5 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -33,6 +33,7 @@ pub fn create_app(state: AppState) -> Router { .route("/post", post(api::post_web_ingest)) .route("/post/redact", post(api::post_web_redact)) .route("/post/check", post(api::check_web_ingest)) + .route("/theme", post(crate::html::post_theme)) .route("/sse", get(api::get_html_stream)) .route("/stream", get(api::get_stream)) .route("/search", get(crate::html::search_page)) diff --git a/server/src/path_types.rs b/server/src/path_types.rs index 997597f6659c1ba2092bc5348e393bf7a8fa7ae0..cad58cac7da948f92d6ea2a5587d917ab21d57ea 100644 --- a/server/src/path_types.rs +++ b/server/src/path_types.rs @@ -8,6 +8,12 @@ //! //! This module adds lightweight newtypes so code can be explicit about what it //! expects without changing core storage formats. +//! +//! **Storage vs wire:** [`CanonicalItemUrl`] values are shared across scopes +//! (`https://slug.social/~/…`); which [`crate::reducer::ContentState`] they live in +//! is determined by scope, not by embedding the room id in the string. For JSON/RPC +//! and browser links in a private room, use [`crate::api::helpers::item_path_for_api_in_room`] +//! so ontology items become `https://slug.social/r/{short}/{slug}/~/…`. use std::borrow::Borrow; use std::fmt; diff --git a/server/static/theme_default.css b/server/static/theme_default.css index 2b06b12f3e77c9ed305272fe3ab47a8ad08504df..5e6304bcc36caed8aeda913fd241fc8fb9cf6d25 100644 --- a/server/static/theme_default.css +++ b/server/static/theme_default.css @@ -678,7 +678,10 @@ details > summary::-webkit-details-marker { display: none; } gap: 6px; } #spread-slider { accent-color: var(--ui); width: 80px; } -#theme-switcher { +#slug-theme-form { + display: contents; +} +#theme-select { background: var(--g4); border: var(--bv) solid; border-color: var(--hi) var(--lo) var(--lo) var(--hi); @@ -688,8 +691,8 @@ details > summary::-webkit-details-marker { display: none; } padding: 2px 10px; user-select: none; } -#theme-switcher:hover { color: var(--signal); } -#theme-switcher:active { +#theme-select:hover { color: var(--signal); } +#theme-select:active { border-color: var(--lo) var(--hi) var(--hi) var(--lo); transform: translate(1px, 1px); } diff --git a/server/static/theme_retro_craft.css b/server/static/theme_retro_craft.css index 508222443a3bb2f16d3034a07af53a7df37a0314..3fde7e2b77fa4e4f46418927eb0fb0e5cd195c75 100644 --- a/server/static/theme_retro_craft.css +++ b/server/static/theme_retro_craft.css @@ -35,7 +35,6 @@ h1, h2, h3 { font-weight: 600; letter-spacing: 0.14em; margin: 1.5rem 0 0.5rem; - text-transform: uppercase; } a { @@ -354,7 +353,10 @@ div.cli-panel { accent-color: var(--accent); width: 72px; } -#theme-switcher, +#slug-theme-form { + display: contents; +} +#theme-select, #search-btn, a#src-link { border: 1px solid var(--line); @@ -365,7 +367,7 @@ a#src-link { padding: 0.25rem 0.6rem; text-decoration: none; } -#theme-switcher:hover, +#theme-select:hover, #search-btn:hover, a#src-link:hover { border-color: var(--accent); Side B — contributor: tommy-mor Side B — commit message: [40b975bf] nice Side B — unified diff (full patch): diff --git a/Cargo.lock b/Cargo.lock index 266e876bb7ccbe788beb1d5bd53ad5b45ee5825b..2cea973082716e761ef6f5dd5886acc08ff9aac0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -222,6 +222,12 @@ dependencies = [ "syn", ] +[[package]] +name = "dotenvy" +version = "0.15.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1aaf95b3e5c8f23aa320147307562d361db0ae0d51242340f558153b4eb2439b" + [[package]] name = "encoding_rs" version = "0.8.35" @@ -1238,6 +1244,7 @@ version = "0.0.1" dependencies = [ "axum", "axum-extra", + "dotenvy", "maud", "reqwest", "serde", diff --git a/server/Cargo.toml b/server/Cargo.toml index 4677fedcb45292eebebe7e9cf6ce2f5738f18ddf..bd600138b613bd0f546bdec217a5334cdcb20aa5 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -17,6 +17,7 @@ tower-http = { version = "0.5", features = ["trace"] } tracing = "0.1" tracing-subscriber = { version = "0.3", features = ["env-filter"] } reqwest = { version = "0.12", features = ["json"] } +dotenvy = "0.15" [dev-dependencies] reqwest = { version = "0.12", features = ["json"] } diff --git a/server/src/api/ui_html.rs b/server/src/api/ui_html.rs index d2024bd4582bcc8482b461b2ba4fedbd8bff7c66..b33a84e8bb5e817b26592868d88090e6d664d950 100644 --- a/server/src/api/ui_html.rs +++ b/server/src/api/ui_html.rs @@ -6,7 +6,7 @@ use axum::{ use std::collections::HashMap; use crate::{ - html::{input_panel, js_string_literal, ranking_panel, JsBuilder}, + html::{entity_section, input_panel, js_string_literal, ranking_panel, JsBuilder}, parser::parse_reddit_url, path_types::ItemId, reddit::ensure_partial_tree, @@ -87,6 +87,20 @@ pub async fn post_ui_html( .into_response() } }, + HtmlUiAction::FetchEntity { item } => { + let id = parse_item_param(&item); + if id.is_root() { + return ui_js_warn("nothing to fetch for the root").into_response(); + } + state.queue_entity_fetch(id.clone()); + let tree = state.tree.read().await; + let empty = crate::reducer::NodeState::default(); + let node = tree.get(&id).unwrap_or(&empty); + let panel = entity_section(&id, node, true); + JsBuilder::new() + .morph_selector("#entity-section", panel) + .into_response() + }, } } diff --git a/server/src/events.rs b/server/src/events.rs index ed5be6b13b9d46e838831d6ce0f96f569b401730..07ce24b5e56cf72b0b442c3c3241efbf6c3b006a 100644 --- a/server/src/events.rs +++ b/server/src/events.rs @@ -1,4 +1,5 @@ use serde::{Deserialize, Serialize}; +use serde_json::Value; #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(tag = "type", rename_all = "snake_case")] @@ -18,4 +19,10 @@ pub enum Event { }, /// Register a node path in the fractal tree (no external fetch). NodeEnsured { id: String }, + /// Full upstream API payload for a node (domain-specific view derived at replay/render time). + EntityImported { + id: String, + ts: i64, + payload: Value, + }, } diff --git a/server/src/html/mod.rs b/server/src/html/mod.rs index df6505021d9f446c2b453e20e3eb3cf696a111f9..caf1309c8d93b47104499c57f9cc35ee7631fbb9 100644 --- a/server/src/html/mod.rs +++ b/server/src/html/mod.rs @@ -10,6 +10,7 @@ use crate::{ form_template::template_json_compact, path_types::ItemId, ranking::{top_bottom, RankedItem}, + reddit::is_fetchable, reducer::{GroupState, NodeState}, state::AppState, ui_action::UI_RPC_FIELD, @@ -151,7 +152,7 @@ pub fn breadcrumb_path(item: &ItemId) -> Markup { fn entity_panel(node: &NodeState) -> Markup { html! { @if let Some(data) = &node.data { - section id="entity-panel" class="demo-panel entity-card" { + div id="entity-panel" class="entity-card" { h2 { (data.title) } @if let Some(author) = &data.author { p class="muted small" { "by " (author) } @@ -164,6 +165,42 @@ fn entity_panel(node: &NodeState) -> Markup { } } +/// Reddit/API import control — only shown on fetchable pages; never auto-fires. +pub fn fetch_entity_panel(item: &ItemId, has_data: bool, fetching: bool) -> Markup { + if !is_fetchable(item) { + return html! {}; + } + let label = if fetching { + "Fetching…" + } else if has_data { + "Fetch more" + } else { + "Fetch from Reddit" + }; + let rpc = template_json_compact(&serde_json::json!({ + "action": "fetch_entity", + "item": item.as_str(), + })) + .expect("fetch_entity rpc template"); + html! { + form method="post" action="/ui" id="fetch-entity-form" class="fetch-entity-form" { + input type="hidden" name=(UI_RPC_FIELD) value=(rpc); + button type="submit" class="btn-secondary" disabled=(fetching) { (label) } + } + } +} + +/// Entity card + explicit fetch control (morphed as `#entity-section`). +pub fn entity_section(item: &ItemId, node: &NodeState, fetching: bool) -> Markup { + let has_data = node.data.is_some(); + html! { + section id="entity-section" class="demo-panel" { + (entity_panel(node)) + (fetch_entity_panel(item, has_data, fetching)) + } + } +} + fn rank_list(label: &str, items: &[RankedItem], start_rank: usize) -> Markup { html! { @if !items.is_empty() { @@ -260,7 +297,7 @@ async fn item_page(state: AppState, uri: Uri, item: ItemId) -> Markup { h1 { "sorter" } (input_panel("", None)) (breadcrumb_path(&item)) - (entity_panel(node)) + (entity_section(&item, node, false)) (ranking_panel(&item, group)) }; layout("sorter2", body, views) @@ -272,16 +309,5 @@ pub async fn home(State(state): State, uri: Uri) -> impl IntoResponse pub async fn browse(State(state): State, uri: Uri) -> impl IntoResponse { let item = ItemId::from_browse_uri(uri.path()).unwrap_or(ItemId::root()); - if item.as_str().starts_with("reddit.com") { - let needs_fetch = { - let tree = state.tree.read().await; - tree.get(&item) - .map(|n| n.data.is_none()) - .unwrap_or(true) - }; - if needs_fetch { - state.reddit.request_fetch(item.clone()); - } - } item_page(state, uri, item).await } diff --git a/server/src/main.rs b/server/src/main.rs index c22ec6c9f5358e5ec99fb83210dc351938505a93..1f0cddc39302b35b0cd6a6219f44c9d59202facf 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -2,6 +2,10 @@ use sorter2_server::state::AppConfig; #[tokio::main] async fn main() -> Result<(), Box> { + if std::env::var("SORTER2_SKIP_DOTENV").is_err() { + let _ = dotenvy::dotenv(); + } + tracing_subscriber::fmt() .with_env_filter( tracing_subscriber::EnvFilter::try_from_default_env() diff --git a/server/src/reddit.rs b/server/src/reddit.rs index 90053ad03b1d7c8e94f325dd4ee64c2b4f7da900..ff0f01e57b18af878eb5be3efc47204a7673589d 100644 --- a/server/src/reddit.rs +++ b/server/src/reddit.rs @@ -6,11 +6,15 @@ use std::time::{Duration, Instant}; use reqwest::{header, Client, StatusCode}; use serde::Deserialize; +use serde_json::Value; use tokio::sync::{mpsc, RwLock}; use crate::{ + event_log::EventLog, + events::Event, + html::now_ms, path_types::ItemId, - reducer::{EntityData, GlobalTree}, + reducer::GlobalTree, }; /// Bootstrap blank nodes along a URL path so breadcrumbs and voting work before fetch. @@ -20,6 +24,8 @@ pub fn ensure_partial_tree(tree: &mut GlobalTree, id: &ItemId) { pub struct RedditCommand { pub id: ItemId, + /// User-initiated fetch bypasses the in-memory "recently fetched" cache. + pub force: bool, } #[derive(Clone)] @@ -33,19 +39,31 @@ struct RedditCredentials { client_secret: String, } +#[derive(Clone)] +pub struct RedditApiConfig { + pub api_base: String, + pub oauth_base: String, + pub user_agent: String, + creds: Option, +} + struct OAuthToken { access_token: String, expires_at: Instant, } impl RedditBroker { - pub fn spawn(tree: Arc>, user_agent: &str) -> Self { + pub fn spawn( + tree: Arc>, + event_log: Arc, + config: RedditApiConfig, + ) -> Self { let (tx, rx) = mpsc::channel(100); let mut headers = header::HeaderMap::new(); headers.insert( header::USER_AGENT, - header::HeaderValue::from_str(user_agent).expect("valid user agent"), + header::HeaderValue::from_str(&config.user_agent).expect("valid user agent"), ); let client = Client::builder() @@ -54,22 +72,38 @@ impl RedditBroker { .build() .expect("reqwest client"); - let creds = RedditCredentials::from_env(); - tokio::spawn(reddit_worker(rx, tree, client, creds)); + tokio::spawn(reddit_worker(rx, tree, event_log, client, config)); Self { tx } } - /// Fire-and-forget: queue a fetch; worker updates the tree when done. - pub fn request_fetch(&self, id: ItemId) { - let _ = self.tx.try_send(RedditCommand { id }); + /// Queue a fetch; drops when the channel is full (backpressure). + pub fn request_fetch(&self, id: ItemId, force: bool) { + let _ = self.tx.try_send(RedditCommand { id, force }); + } +} + +impl RedditApiConfig { + pub fn from_env() -> Self { + Self { + api_base: reddit_api_base(), + oauth_base: reddit_oauth_base(), + user_agent: default_user_agent(), + creds: RedditCredentials::from_env(), + } } } impl RedditCredentials { + /// Reddit's OAuth docs call these "client id" and "client secret"; the app + /// registration UI often labels them "app id" / "app secret" — same values. fn from_env() -> Option { - let client_id = std::env::var("REDDIT_CLIENT_ID").ok()?; - let client_secret = std::env::var("REDDIT_CLIENT_SECRET").ok()?; + let client_id = std::env::var("REDDIT_CLIENT_ID") + .or_else(|_| std::env::var("REDDIT_APP_ID")) + .ok()?; + let client_secret = std::env::var("REDDIT_CLIENT_SECRET") + .or_else(|_| std::env::var("REDDIT_APP_SECRET")) + .ok()?; if client_id.is_empty() || client_secret.is_empty() { return None; } @@ -80,29 +114,63 @@ impl RedditCredentials { } } +pub fn reddit_api_base() -> String { + std::env::var("REDDIT_API_BASE").unwrap_or_else(|_| "https://www.reddit.com".into()) +} + +pub fn reddit_oauth_base() -> String { + std::env::var("REDDIT_OAUTH_BASE").unwrap_or_else(|_| "https://www.reddit.com".into()) +} + pub fn default_user_agent() -> String { std::env::var("REDDIT_USER_AGENT").unwrap_or_else(|_| { "web:sorter2.social:v0.0.1 (by /u/sorter2)".to_string() }) } +/// True when this node can be loaded from the Reddit JSON API. +pub fn is_fetchable(id: &ItemId) -> bool { + !map_item_to_reddit_api(id, "https://example.com").is_empty() +} + +/// Derive UI-facing fields from a stored payload (Reddit-specific when under reddit.com). +pub fn entity_view_from_payload(id: &ItemId, payload: &Value) -> Option { + if id.as_str().starts_with("reddit.com") { + return parse_reddit_view(id, payload); + } + None +} + +/// Apply a full API payload to the in-memory tree (view derived for known domains). +pub fn apply_entity_import(tree: &mut GlobalTree, id: &ItemId, payload: Value) { + let view = entity_view_from_payload(id, &payload); + tree.apply_entity_raw(id, payload, view); +} + async fn reddit_worker( mut rx: mpsc::Receiver, tree: Arc>, + event_log: Arc, client: Client, - creds: Option, + config: RedditApiConfig, ) { let mut in_flight = HashSet::new(); let mut recently_fetched: HashMap = HashMap::new(); let mut current_delay = Duration::from_secs(1); let mut oauth: Option = None; let cache_ttl = Duration::from_secs(300); + let creds = config.creds.clone(); + let api_base = config.api_base.clone(); + let oauth_base = config.oauth_base.clone(); while let Some(cmd) = rx.recv().await { let now = Instant::now(); recently_fetched.retain(|_, t| now.duration_since(*t) < cache_ttl); - if in_flight.contains(&cmd.id) || recently_fetched.contains_key(&cmd.id) { + if in_flight.contains(&cmd.id) { + continue; + } + if !cmd.force && recently_fetched.contains_key(&cmd.id) { continue; } @@ -112,18 +180,26 @@ async fn reddit_worker( tokio::time::sleep(current_delay).await; if let Some(c) = &creds { - oauth = ensure_oauth_token(&client, c, oauth.take()).await; + oauth = ensure_oauth_token(&client, &oauth_base, c, oauth.take()).await; } let token = oauth.as_ref().map(|t| t.access_token.as_str()); - let use_oauth = token.is_some(); - match do_fetch(&client, &fetch_id, use_oauth, token).await { - Ok(FetchOutcome::Entity(data)) => { - let mut w = tree.write().await; - w.set_entity_data(&fetch_id, data); - recently_fetched.insert(fetch_id.clone(), Instant::now()); - current_delay = Duration::from_millis(600); + match do_fetch(&client, &api_base, &fetch_id, token).await { + Ok(FetchOutcome::Payload(payload)) => { + let ts = now_ms(); + let event = Event::EntityImported { + id: fetch_id.as_str().to_string(), + ts, + payload: payload.clone(), + }; + if let Err(e) = event_log.append(&event).await { + tracing::warn!("event log append failed for {}: {}", fetch_id, e); + } else { + apply_entity_import(&mut *tree.write().await, &fetch_id, payload); + recently_fetched.insert(fetch_id.clone(), Instant::now()); + current_delay = Duration::from_millis(600); + } } Ok(FetchOutcome::NotFound) => { recently_fetched.insert(fetch_id.clone(), Instant::now()); @@ -149,13 +225,14 @@ async fn reddit_worker( } enum FetchOutcome { - Entity(EntityData), + Payload(Value), NotFound, RateLimited { reset_secs: u64 }, } async fn ensure_oauth_token( client: &Client, + oauth_base: &str, creds: &RedditCredentials, existing: Option, ) -> Option { @@ -165,8 +242,13 @@ async fn ensure_oauth_token( } } + let url = format!( + "{}/api/v1/access_token", + oauth_base.trim_end_matches('/') + ); + let resp = client - .post("https://www.reddit.com/api/v1/access_token") + .post(&url) .basic_auth(&creds.client_id, Some(&creds.client_secret)) .form(&[("grant_type", "client_credentials")]) .send() @@ -207,11 +289,11 @@ async fn ensure_oauth_token( async fn do_fetch( client: &Client, + api_base: &str, id: &ItemId, - use_oauth: bool, bearer: Option<&str>, ) -> Result { - let url = map_item_to_reddit_api(id, use_oauth); + let url = map_item_to_reddit_api(id, api_base); if url.is_empty() { return Ok(FetchOutcome::NotFound); } @@ -241,10 +323,8 @@ async fn do_fetch( return Ok(FetchOutcome::RateLimited { reset_secs: reset }); } - let bytes = resp.bytes().await.map_err(|e| e.to_string())?; - Ok(parse_reddit_json(id, &bytes) - .map(FetchOutcome::Entity) - .unwrap_or(FetchOutcome::NotFound)) + let payload: Value = resp.json().await.map_err(|e| e.to_string())?; + Ok(FetchOutcome::Payload(payload)) } fn rate_limit_remaining(resp: &reqwest::Response) -> Option { @@ -264,18 +344,14 @@ fn rate_limit_reset_secs(resp: &reqwest::Response) -> u64 { .unwrap_or(5) } -/// Map canonical item id to Reddit JSON API URL. -pub fn map_item_to_reddit_api(id: &ItemId, oauth: bool) -> String { +/// Map canonical item id to a Reddit JSON API URL under `api_base`. +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 base = if oauth { - "https://oauth.reddit.com" - } else { - "https://www.reddit.com" - }; + let base = api_base.trim_end_matches('/'); let segments: Vec<&str> = path.split('/').collect(); @@ -293,18 +369,17 @@ pub fn map_item_to_reddit_api(id: &ItemId, oauth: bool) -> String { String::new() } -fn parse_reddit_json(id: &ItemId, bytes: &[u8]) -> Option { - let v: serde_json::Value = serde_json::from_slice(bytes).ok()?; +fn parse_reddit_view(id: &ItemId, v: &Value) -> Option { let segments: Vec<&str> = id.as_str().split('/').collect(); if segments.iter().any(|&p| p == "comments") { - parse_post_listing(&v) + parse_post_listing(v) } else { - parse_subreddit_about(&v) + parse_subreddit_about(v) } } -fn parse_subreddit_about(v: &serde_json::Value) -> Option { +fn parse_subreddit_about(v: &Value) -> Option { let data = v.get("data")?; let title = data .get("title") @@ -323,7 +398,7 @@ fn parse_subreddit_about(v: &serde_json::Value) -> Option { .filter(|s| !s.is_empty()) .map(|s| s.to_string()); - Some(EntityData { + Some(crate::reducer::EntityData { title, author: None, body_html, @@ -331,10 +406,9 @@ fn parse_subreddit_about(v: &serde_json::Value) -> Option { }) } -fn parse_post_listing(v: &serde_json::Value) -> Option { +fn parse_post_listing(v: &Value) -> Option { let listing = v.as_array()?.first()?; - let child = listing - .pointer("/data/children/0/data")?; + let child = listing.pointer("/data/children/0/data")?; let title = child.get("title")?.as_str()?.to_string(); let author = child .get("author") @@ -352,7 +426,7 @@ fn parse_post_listing(v: &serde_json::Value) -> Option { .filter(|s| s.starts_with("http")) .map(|s| s.to_string()); - Some(EntityData { + Some(crate::reducer::EntityData { title, author, body_html, @@ -368,48 +442,51 @@ mod tests { fn map_subreddit_about_url() { let id = ItemId::parse("reddit.com/r/rust").unwrap(); assert_eq!( - map_item_to_reddit_api(&id, false), + map_item_to_reddit_api(&id, "https://www.reddit.com"), "https://www.reddit.com/r/rust/about.json?raw_json=1" ); assert_eq!( - map_item_to_reddit_api(&id, true), - "https://oauth.reddit.com/r/rust/about.json?raw_json=1" + map_item_to_reddit_api(&id, "http://127.0.0.1:9999"), + "http://127.0.0.1:9999/r/rust/about.json?raw_json=1" ); } #[test] fn map_post_url() { - let id = - ItemId::parse("reddit.com/r/amitheasshole/comments/1trnvdl").unwrap(); + let id = ItemId::parse("reddit.com/r/amitheasshole/comments/1trnvdl").unwrap(); assert_eq!( - map_item_to_reddit_api(&id, false), + map_item_to_reddit_api(&id, "https://www.reddit.com"), "https://www.reddit.com/r/amitheasshole/comments/1trnvdl.json?raw_json=1" ); } #[test] - fn map_non_reddit_empty() { - let id = ItemId::opaque("example.com/foo"); - assert!(map_item_to_reddit_api(&id, false).is_empty()); + fn is_fetchable_reddit_sub() { + let id = ItemId::parse("reddit.com/r/rust").unwrap(); + assert!(is_fetchable(&id)); + assert!(!is_fetchable(&ItemId::opaque("example.com/x"))); } #[test] fn parse_subreddit_fixture() { - let json = r#"{"kind":"t5","data":{"title":"Rust","display_name":"rust","public_description":"systems"}}"#; - let entity = parse_reddit_json( + 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(), - json.as_bytes(), + &v, ) .unwrap(); - assert_eq!(entity.title, "Rust"); + assert_eq!(entity.title, "The Rust Programming Language"); + assert!(entity.body_html.as_ref().is_some_and(|b| b.contains("Rust"))); } #[test] fn parse_post_fixture() { let json = r#"[{"kind":"Listing","data":{"children":[{"kind":"t3","data":{"title":"AITA","author":"op","selftext_html":"<p>hi</p>","thumbnail":"https://b.thumbs.redditmedia.com/x.jpg"}}]}}]"#; - let entity = parse_reddit_json( + let v: Value = serde_json::from_str(json).unwrap(); + let entity = entity_view_from_payload( &ItemId::parse("reddit.com/r/x/comments/abc").unwrap(), - json.as_bytes(), + &v, ) .unwrap(); assert_eq!(entity.title, "AITA"); diff --git a/server/src/reducer.rs b/server/src/reducer.rs index 077f700bf00ddefe18ffd004bb5288bdc7c4adaf..60db81a562e3590775012573225a76ba82a14563 100644 --- a/server/src/reducer.rs +++ b/server/src/reducer.rs @@ -1,6 +1,7 @@ use std::collections::{HashMap, HashSet, VecDeque}; use serde::{Deserialize, Serialize}; +use serde_json::Value; use crate::path_types::ItemId; @@ -128,6 +129,9 @@ pub struct EntityData { #[derive(Debug, Clone, Default)] pub struct NodeState { pub id: ItemId, + /// Full imported API JSON (persisted in the event log). + pub entity_raw: Option, + /// Domain-specific view derived from `entity_raw` (e.g. Reddit title/author). pub data: Option, pub children: HashSet, pub local_ranking: GroupState, @@ -197,10 +201,11 @@ impl GlobalTree { } } - pub fn set_entity_data(&mut self, id: &ItemId, data: EntityData) { + pub fn apply_entity_raw(&mut self, id: &ItemId, payload: Value, view: Option) { self.ensure_path(id); if let Some(node) = self.nodes.get_mut(id) { - node.data = Some(data); + node.entity_raw = Some(payload); + node.data = view; } } } diff --git a/server/src/state.rs b/server/src/state.rs index 8c03aa60c15aee803a534439400b69935b1a3d84..d71d1079a8f486cdab15795384aef0b81b32544d 100644 --- a/server/src/state.rs +++ b/server/src/state.rs @@ -7,7 +7,7 @@ use crate::{ events::Event, journal::JournalClient, path_types::ItemId, - reddit::{default_user_agent, RedditBroker}, + reddit::{apply_entity_import, RedditApiConfig, RedditBroker}, reducer::{GlobalTree, VoteData}, views::ViewStore, }; @@ -108,13 +108,18 @@ impl AppState { tree.ensure_path(&parsed); } } + Event::EntityImported { id, payload, .. } => { + if let Some(parsed) = ItemId::parse(&id).or_else(|| ItemId::from_url(&id)) { + apply_entity_import(&mut tree, &parsed, payload); + } + } } } } let tree = Arc::new(RwLock::new(tree)); let journal = JournalClient::spawn(tree.clone(), event_log.clone()); - let reddit = RedditBroker::spawn(tree.clone(), &default_user_agent()); + let reddit = RedditBroker::spawn(tree.clone(), event_log.clone(), RedditApiConfig::from_env()); Self { cfg: Arc::new(cfg), @@ -135,10 +140,14 @@ impl AppState { let mut w = self.tree.write().await; w.ensure_path(id); } - self.reddit.request_fetch(id.clone()); Ok(()) } + /// User-initiated Reddit/API import (via "Fetch more" — never on paste or navigate). + pub fn queue_entity_fetch(&self, id: ItemId) { + self.reddit.request_fetch(id, true); + } + pub async fn record_vote( &self, parent: &ItemId, @@ -169,6 +178,44 @@ impl AppState { #[cfg(test)] mod tests { use super::{normalize_scope, parse_item_param}; + use crate::{ + event_log::EventLog, + events::Event, + path_types::ItemId, + reddit::apply_entity_import, + reducer::GlobalTree, + }; + use serde_json::json; + + #[tokio::test] + async fn replay_entity_imported_restores_view() { + let tmp = tempfile::tempdir().unwrap(); + let log_path = tmp.path().join("events.jsonl"); + let log = EventLog::new(log_path.to_string_lossy().into_owned()); + let payload = json!({"kind":"t5","data":{"title":"Rust","display_name":"rust"}}); + log.append(&Event::EntityImported { + id: "reddit.com/r/rust".into(), + ts: 1, + payload: payload.clone(), + }) + .await + .unwrap(); + + let mut tree = GlobalTree::new(); + let (events, _) = log.load_all().await.unwrap(); + for ev in events { + if let Event::EntityImported { id, payload, .. } = ev { + let parsed = ItemId::parse(&id).unwrap(); + apply_entity_import(&mut tree, &parsed, payload); + } + } + let node = tree.get(&ItemId::parse("reddit.com/r/rust").unwrap()).unwrap(); + assert_eq!(node.data.as_ref().unwrap().title, "Rust"); + assert_eq!( + node.entity_raw.as_ref().unwrap()["data"]["display_name"], + "rust" + ); + } #[test] fn normalize_scope_strips_prefix_and_lowercases() { diff --git a/server/src/ui_action.rs b/server/src/ui_action.rs index d798874d1d94c0dfee59ec1ff703f9ee6ef432c0..3d6a49a2a3fb950752827efe1f5a049308f09baf 100644 --- a/server/src/ui_action.rs +++ b/server/src/ui_action.rs @@ -26,6 +26,10 @@ pub enum HtmlUiAction { ParseQuery { query: String, }, + /// Fetch upstream entity data for the current page (explicit user action only). + FetchEntity { + item: String, + }, } #[derive(Debug, Error)] diff --git a/server/static/sorter.css b/server/static/sorter.css index 36f68d415cea33c8562d5d02c0c9d4c5b225a782..1f9f5158414902d43a380dd899232c70d8cf5d63 100644 --- a/server/static/sorter.css +++ b/server/static/sorter.css @@ -52,6 +52,25 @@ body { filter: brightness(1.1); } +.btn-secondary { + background: transparent; + color: var(--accent); + border: 1px solid var(--border); + padding: 0.4rem 0.85rem; + font-size: 0.9rem; + cursor: pointer; + margin-top: 0.75rem; +} + +.btn-secondary:disabled { + opacity: 0.6; + cursor: wait; +} + +.fetch-entity-form { + margin-top: 0.5rem; +} + code { font-size: 0.85em; background: var(--bg); diff --git a/test/fixtures/reddit/r_rust_about.json b/test/fixtures/reddit/r_rust_about.json new file mode 100644 index 0000000000000000000000000000000000000000..bb7fd74ab00d63356d9b672a775996ed155b984d --- /dev/null +++ b/test/fixtures/reddit/r_rust_about.json @@ -0,0 +1,21 @@ +{ + "kind": "t5", + "data": { + "user_flair_background_color": null, + "submit_text_html": null, + "banner_img": "", + "user_is_banned": null, + "wiki_enabled": true, + "show_media": true, + "id": "2qh0i", + "display_name": "rust", + "title": "The Rust Programming Language", + "public_description": "A place for all things related to the Rust programming language.", + "public_description_html": "<!-- SC_OFF --><div class=\"md\"><p>A place for all things related to the Rust programming language.</p>\n</div><!-- SC_ON -->", + "icon_img": "https://styles.redditmedia.com/t5_2qh0i/styles/communityIcon_kfqpknb2j6j51.png", + "community_icon": "https://styles.redditmedia.com/t5_2qh0i/styles/communityIcon_kfqpknb2j6j51.png", + "subscribers": 350000, + "active_user_count": 1200, + "over18": false + } +} diff --git a/test/reddit_import.clj b/test/reddit_import.clj new file mode 100644 index 0000000000000000000000000000000000000000..54edaedeef08718c8f184d6aa468d8e6415068c7 --- /dev/null +++ b/test/reddit_import.clj @@ -0,0 +1,116 @@ +(ns test.reddit-import + (:require [babashka.process :as process] + [clojure.java.io :as io] + [clojure.string :as str] + [clojure.test :refer [deftest is testing]]) + (:import [com.sun.net.httpserver HttpServer HttpHandler HttpExchange] + [java.net InetSocketAddress])) + +(defn- repo-root [] + (.getCanonicalPath (io/file (System/getProperty "user.dir")))) + +(defn- pick-port [] + (with-open [s (java.net.ServerSocket. 0)] + (.getLocalPort s))) + +(defn- start-mock-reddit [port fixtures-dir] + (let [fixture (io/file fixtures-dir "r_rust_about.json") + body (.getBytes (slurp fixture) "UTF-8") + server (HttpServer/create (InetSocketAddress. "127.0.0.1" port) 0) + handler + (proxy [HttpHandler] [] + (handle [^HttpExchange exchange] + (.sendResponseHeaders exchange 200 (alength body)) + (let [out (.getResponseBody exchange)] + (.write out body) + (.close out))))] + (.createContext server "/" handler) + (.setExecutor server nil) + (.start server) + (fn stop [] + (.stop server 0)))) + +(defn- wait-health [base-url ms] + (let [deadline (+ (System/currentTimeMillis) ms) + url (str base-url "/healthz")] + (loop [] + (let [resp (try + (process/shell {:out :string :err :string} + "curl" "-sf" url) + (catch Exception _ nil))] + (if (and resp (zero? (:exit resp)) (= "ok" (str/trim (:out resp "")))) + true + (if (< (System/currentTimeMillis) deadline) + (do (Thread/sleep 200) (recur)) + false)))))) + +(defn- curl-post-ui [base rpc-json] + (process/shell {:out :string :err :string} + "curl" "-sf" "-X" "POST" (str base "/ui") + "--data-urlencode" (str "__rpc__=" rpc-json))) + +(defn- wait-event-log [path ms] + (let [deadline (+ (System/currentTimeMillis) ms)] + (loop [] + (if (.exists (io/file path)) + true + (if (< (System/currentTimeMillis) deadline) + (do (Thread/sleep 200) (recur)) + false))))) + +(deftest reddit-fetch-via-mock-api + (testing "Fetch more queues import; event log stores full payload; page shows title" + (let [root (repo-root) + fixtures (str root "/test/fixtures/reddit") + data-dir (.getAbsolutePath + (doto (io/file (System/getProperty "java.io.tmpdir") + (str "sorter2-reddit-" (System/currentTimeMillis))) + (.mkdirs))) + reddit-port (pick-port) + app-port (pick-port) + reddit-base (str "http://127.0.0.1:" reddit-port) + app-base (str "http://127.0.0.1:" app-port) + bin (str root "/target/release/sorter2-server") + stop-mock (start-mock-reddit reddit-port fixtures)] + (try + (is (zero? (:exit (process/shell {:dir root} + "cargo" "build" "--release" "--package" "sorter2-server"))) + "release build succeeds") + (let [proc (process/process {:dir root + :env (into (into {} (System/getenv)) + {"SORTER2_SKIP_DOTENV" "1" + "SORTER2_DATA_DIR" data-dir + "SORTER2_EVENT_LOG" (str data-dir "/events.jsonl") + "PORT" (str app-port) + "REDDIT_API_BASE" reddit-base + "REDDIT_OAUTH_BASE" reddit-base + "REDDIT_CLIENT_ID" "" + "REDDIT_CLIENT_SECRET" "" + "REDDIT_APP_ID" "" + "REDDIT_APP_SECRET" ""}) + :out :string + :err :string} + bin)] + (try + (is (wait-health app-base 20000) "app healthz") + (let [browse-url (str app-base "/~/https://reddit.com/r/rust") + before (:out (process/shell {:out :string :err :string} + "curl" "-sf" browse-url))] + (is (str/includes? before "Fetch from Reddit")) + (is (not (str/includes? before "The Rust Programming Language"))) + (let [rpc "{\"action\":\"fetch_entity\",\"item\":\"reddit.com/r/rust\"}" + post (curl-post-ui app-base rpc) + log-path (str data-dir "/events.jsonl")] + (is (zero? (:exit post)) "fetch_entity POST succeeds") + (is (wait-event-log log-path 10000) "event log written") + (let [after (:out (process/shell {:out :string :err :string} + "curl" "-sf" browse-url)) + log (slurp (io/file log-path))] + (is (str/includes? after "The Rust Programming Language")) + (is (str/includes? log "\"type\":\"entity_imported\"")) + (is (str/includes? log "\"subscribers\":350000")) + (is (str/includes? log "\"display_name\":\"rust\""))))) + (finally + (process/destroy proc)))) + (finally + (stop-mock))))))