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: [52f5c51c] Add Reddit OAuth linking and make UUID the only account identity. OAuth providers only attach to a session UUID (first link creates the principal); linked providers stay private on the account page. Co-authored-by: Cursor Side B — unified diff (full patch): diff --git a/AGENTS.md b/AGENTS.md index 6e0fd8ebb65d665c9c1438e3275971d62b98fd95..e9cc3173dbeb21ad0fc090ca7b407b027c7820a9 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -35,8 +35,11 @@ Environment variables (defaults in `server/src/state.rs`): - `SORTER2_DATA_DIR` — default `./data` (created on startup) - `SORTER2_EVENT_LOG` — default `{data_dir}/events.jsonl` - `SORTER2_BASE_URL` — public origin (also drives Secure cookies when `https://`) -- `GITHUB_CLIENT_ID` / `GITHUB_CLIENT_SECRET` — GitHub OAuth (optional; login disabled if unset) -- `SORTER2_ALLOW_MOCK_OAUTH=1` — allow `mock_user` on `/auth/github` (tests only) +- `GITHUB_CLIENT_ID` / `GITHUB_CLIENT_SECRET` — GitHub OAuth linking (optional) +- `REDDIT_CLIENT_ID` / `REDDIT_CLIENT_SECRET` (or `REDDIT_APP_*`) — Reddit API import + OAuth linking (optional) +- `SORTER2_ALLOW_MOCK_OAUTH=1` — allow `mock_user` on `/auth/github` and `/auth/reddit` (tests only) + +Identity: UUID is canonical. OAuth providers only *link* to a UUID (first link creates the principal). Linked providers are private to the account owner. Health check: `GET /healthz` → `ok`. diff --git a/server/src/auth/mod.rs b/server/src/auth/mod.rs index 5906f93b13853421e96a3c37bc9d8202a47842bf..c706ae8045a811e5941f5f6c72da88f42a403a82 100644 --- a/server/src/auth/mod.rs +++ b/server/src/auth/mod.rs @@ -1,4 +1,8 @@ -//! GitHub OAuth login, session cookies, and vote actor resolution. +//! OAuth linking, session cookies, and vote actor resolution. +//! +//! Canonical identity is a UUID. OAuth providers only *link* to that UUID +//! (first link creates the principal; later links attach while logged in). +//! Which providers are linked is private to the account owner. pub mod config; pub mod identity; @@ -22,7 +26,9 @@ use crate::{ form_template::template_json_compact, html::layout, state::AppState, - storage_schema::{oauth_link_owner, pseudonym_owner, Store, StoreFields}, + storage_schema::{ + linked_providers_for_uuid, oauth_link_owner, pseudonym_owner, Store, StoreFields, + }, ui_action::UI_RPC_FIELD, }; @@ -53,10 +59,12 @@ fn new_actor_uuid() -> String { pub struct LoginQuery { #[serde(default)] pub return_to: Option, + #[serde(default)] + pub error: Option, } #[derive(Debug, Deserialize)] -pub struct GitHubStartQuery { +pub struct OAuthStartQuery { #[serde(default)] pub return_to: Option, #[serde(default)] @@ -72,15 +80,22 @@ fn return_from_query_or_jar(jar: &CookieJar, query: Option<&str>) -> String { .unwrap_or_else(|| "/".to_string()) } -fn oauth_providers(base_url: &str, return_to: &str) -> Vec<(&'static str, String)> { +/// Available OAuth link targets: `(provider_key, label, start_href)`. +fn oauth_providers(base_url: &str, return_to: &str) -> Vec<(&'static str, &'static str, String)> { let mut out = Vec::new(); + let enc = urlencoding::encode(return_to); if oauth::GitHubConfig::from_env(base_url).is_some() { out.push(( - "GitHub", - format!( - "/auth/github?return_to={}", - urlencoding::encode(return_to) - ), + "github", + oauth::provider_label("github"), + format!("/auth/github?return_to={enc}"), + )); + } + if oauth::RedditConfig::from_env(base_url).is_some() { + out.push(( + "reddit", + oauth::provider_label("reddit"), + format!("/auth/reddit?return_to={enc}"), )); } out @@ -125,23 +140,41 @@ fn alias_claim_forms(return_to: &str, submit_label: &str) -> Result Markup { +fn login_error_message(code: Option<&str>) -> Option<&'static str> { + match code { + Some("oauth_taken") => { + Some("that OAuth account is already linked to a different sorter2 account") + } + Some("oauth_failed") => Some("OAuth failed — try again"), + _ => None, + } +} + +fn signed_out_body( + providers: &[(&str, &str, String)], + error: Option<&str>, +) -> Markup { html! { main class="panel login-page" { section class="login-section" { h1 { "sign in" } - p class="muted" { "link an account to vote under a lasting alias" } + p class="muted" { + "link an OAuth account to create your identity, then claim an alias to vote" + } + @if let Some(msg) = login_error_message(error) { + p class="alias-bad" data-testid="login-error" { (msg) } + } @if providers.is_empty() { p class="muted" { - "OAuth is not configured. Set GITHUB_CLIENT_ID and GITHUB_CLIENT_SECRET." + "OAuth is not configured. Set GitHub and/or Reddit client credentials." } } @else { ul class="oauth-provider-list" { - @for (name, href) in providers { + @for (key, label, href) in providers { li { a href=(href) class="btn-primary oauth-provider" - data-testid=(format!("oauth-{}", name.to_lowercase())) { - (format!("Continue with {name}")) + data-testid=(format!("oauth-{key}")) { + (format!("Link {label}")) } } } @@ -156,7 +189,10 @@ fn signed_out_body(providers: &[(&str, String)]) -> Markup { fn account_body( actor: &session::SessionActor, aliases: &[String], - providers: &[(&str, String)], + // Provider keys already linked to this UUID (private). + linked: &[String], + // Providers available to link: not yet attached. + unlinkable: &[(&str, &str, String)], claim_forms: Markup, ) -> Markup { let current = actor.pseudonym.trim(); @@ -212,16 +248,29 @@ fn account_body( (claim_forms) } - @if !providers.is_empty() { - section class="login-section" { - h2 { "linked sign-in" } - p class="muted small" { "sign in again with the same provider to return to this account" } + section class="login-section" { + h2 { "linked sign-in" } + p class="muted small" { + "private to you — linking more providers raises trust weight without publishing which accounts you use" + } + @if linked.is_empty() { + p class="muted" data-testid="linked-providers-empty" { "none yet" } + } @else { + ul class="linked-provider-list" data-testid="linked-providers" { + @for key in linked { + li data-testid=(format!("linked-{key}")) { + (oauth::provider_label(key)) + } + } + } + } + @if !unlinkable.is_empty() { ul class="oauth-provider-list" { - @for (name, href) in providers { + @for (key, label, href) in unlinkable { li { a href=(href) class="btn-secondary oauth-provider" - data-testid=(format!("oauth-relink-{}", name.to_lowercase())) { - (format!("Re-link {name}")) + data-testid=(format!("oauth-link-{key}")) { + (format!("Link {label}")) } } } @@ -243,12 +292,21 @@ fn account_body( fn login_body( session: Option<&session::SessionActor>, aliases: &[String], - providers: &[(&str, String)], + linked: &[String], + providers: &[(&str, &str, String)], claim_forms: Option, + error: Option<&str>, ) -> Markup { match (session, claim_forms) { - (Some(actor), Some(forms)) => account_body(actor, aliases, providers, forms), - _ => signed_out_body(providers), + (Some(actor), Some(forms)) => { + let unlinkable: Vec<_> = providers + .iter() + .filter(|(key, _, _)| !linked.iter().any(|p| p == key)) + .cloned() + .collect(); + account_body(actor, aliases, linked, &unlinkable, forms) + } + _ => signed_out_body(providers, error), } } @@ -268,6 +326,10 @@ pub async fn login_page( .as_ref() .map(|s| alias_list(db, &s.uuid)) .unwrap_or_default(); + let linked = session + .as_ref() + .map(|s| linked_providers_for_uuid(db, &s.uuid).unwrap_or_default()) + .unwrap_or_default(); let providers = oauth_providers(&base_url_from_env(state.cfg.port), &return_to); let claim_forms = if session.is_some() { @@ -282,7 +344,14 @@ pub async fn login_page( } else { "login · sorter2" }, - login_body(session.as_ref(), &aliases, &providers, claim_forms), + login_body( + session.as_ref(), + &aliases, + &linked, + &providers, + claim_forms, + query.error.as_deref(), + ), state.views.get_views("/login"), session .as_ref() @@ -302,7 +371,6 @@ pub async fn alias_page( let db = state.projection_store.db(); let session = session::load_valid_session(db, &session_id).ok_or(StatusCode::UNAUTHORIZED)?; if session::session_has_pseudonym(&session) { - // Already onboarded — manage aliases on the account page. return Ok(Redirect::to("/login").into_response()); } @@ -331,7 +399,7 @@ pub async fn alias_page( pub async fn github_start( State(state): State, jar: CookieJar, - Query(query): Query, + Query(query): Query, ) -> Result { let cfg = oauth::GitHubConfig::from_env(&base_url_from_env(state.cfg.port)) .ok_or(StatusCode::SERVICE_UNAVAILABLE)?; @@ -342,7 +410,28 @@ pub async fn github_start( } else { None }; - let url = oauth::authorize_url(&cfg, &state_token, mock_user); + let url = oauth::github_authorize_url(&cfg, &state_token, mock_user); + let jar = jar + .add(session::oauth_state_cookie_value(&state_token)) + .add(session::auth_return_cookie_value(&return_to)); + Ok((jar, Redirect::temporary(&url)).into_response()) +} + +pub async fn reddit_start( + State(state): State, + jar: CookieJar, + Query(query): Query, +) -> Result { + let cfg = oauth::RedditConfig::from_env(&base_url_from_env(state.cfg.port)) + .ok_or(StatusCode::SERVICE_UNAVAILABLE)?; + let return_to = return_from_query_or_jar(&jar, query.return_to.as_deref()); + let state_token = session::new_oauth_state(); + let mock_user = if config::mock_oauth_allowed() { + query.mock_user.as_deref() + } else { + None + }; + let url = oauth::reddit_authorize_url(&cfg, &state_token, mock_user); let jar = jar .add(session::oauth_state_cookie_value(&state_token)) .add(session::auth_return_cookie_value(&return_to)); @@ -355,6 +444,13 @@ pub struct OAuthCallbackQuery { pub state: String, } +/// Link `provider:provider_id` to a UUID. +/// +/// - Logged in + new provider → attach to session UUID +/// - Logged in + already ours → no-op +/// - Logged in + owned by someone else → conflict +/// - Logged out + known link → resume that UUID +/// - Logged out + unknown → create principal + first link async fn finish_oauth_login( state: &AppState, jar: CookieJar, @@ -364,11 +460,41 @@ async fn finish_oauth_login( let db = state.projection_store.db(); let return_to = return_from_query_or_jar(&jar, None); - let uuid = match oauth_link_owner(db, provider, &provider_id) - .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)? - { - Some(existing) => existing, - None => { + let existing_owner = oauth_link_owner(db, provider, &provider_id) + .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; + + let session_uuid = session::session_id_from_jar(&jar) + .as_deref() + .and_then(|id| session::load_valid_session(db, id)) + .map(|s| s.uuid); + let linking_while_logged_in = session_uuid.is_some(); + + let uuid = match (session_uuid, existing_owner) { + (Some(session_uuid), Some(owner)) if owner == session_uuid => session_uuid, + (Some(_), Some(_)) => { + return Ok(( + jar.add(session::clear_oauth_state_cookie()), + "/login?error=oauth_taken".into(), + )); + } + (Some(session_uuid), None) => { + let ts = now_ms(); + state + .append_identity_events(vec![Event::OauthLinked { + uuid: session_uuid.clone(), + provider: provider.to_string(), + provider_id, + ts, + }]) + .await + .map_err(|e| { + tracing::warn!(err = %e, "oauth link append failed"); + StatusCode::INTERNAL_SERVER_ERROR + })?; + session_uuid + } + (None, Some(owner)) => owner, + (None, None) => { let uuid = new_actor_uuid(); let ts = now_ms(); state @@ -409,6 +535,9 @@ async fn finish_oauth_login( "/login/alias?return_to={}", urlencoding::encode(&return_to) ) + } else if linking_while_logged_in { + // Additional link while already in an account → stay on account page. + "/login".to_string() } else { return_to }; @@ -434,22 +563,57 @@ pub async fn github_callback( .build() .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; - let token = oauth::exchange_code(&client, &cfg, &query.code) + let token = oauth::github_exchange_code(&client, &cfg, &query.code) .await .map_err(|e| { tracing::warn!(err = %e, "github oauth token exchange failed"); StatusCode::BAD_GATEWAY })?; - let user = oauth::fetch_user(&client, &cfg.api_base, &token) + let user = oauth::github_fetch_user(&client, &cfg.api_base, &token) .await .map_err(|e| { tracing::warn!(err = %e, "github user fetch failed"); StatusCode::BAD_GATEWAY })?; - let provider = "github"; - let provider_id = oauth::provider_id(&user); - let (jar, dest) = finish_oauth_login(&state, jar, provider, provider_id).await?; + let (jar, dest) = + finish_oauth_login(&state, jar, "github", oauth::github_provider_id(&user)).await?; + Ok((jar, Redirect::to(&dest)).into_response()) +} + +pub async fn reddit_callback( + State(state): State, + jar: CookieJar, + Query(query): Query, +) -> Result { + let cfg = oauth::RedditConfig::from_env(&base_url_from_env(state.cfg.port)) + .ok_or(StatusCode::SERVICE_UNAVAILABLE)?; + + let expected_state = session::oauth_state_from_jar(&jar).ok_or(StatusCode::BAD_REQUEST)?; + if expected_state != query.state { + return Err(StatusCode::BAD_REQUEST); + } + + let client = Client::builder() + .timeout(std::time::Duration::from_secs(15)) + .build() + .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; + + let token = oauth::reddit_exchange_code(&client, &cfg, &query.code) + .await + .map_err(|e| { + tracing::warn!(err = %e, "reddit oauth token exchange failed"); + StatusCode::BAD_GATEWAY + })?; + let user = oauth::reddit_fetch_user(&client, &cfg, &token) + .await + .map_err(|e| { + tracing::warn!(err = %e, "reddit user fetch failed"); + StatusCode::BAD_GATEWAY + })?; + + let (jar, dest) = + finish_oauth_login(&state, jar, "reddit", oauth::reddit_provider_id(&user)).await?; Ok((jar, Redirect::to(&dest)).into_response()) } diff --git a/server/src/auth/oauth.rs b/server/src/auth/oauth.rs index b80078fee0c5acd905b9125d29454e46c4e0d066..369b1527d9032cf312a07819279e0f988ef4a36e 100644 --- a/server/src/auth/oauth.rs +++ b/server/src/auth/oauth.rs @@ -1,8 +1,15 @@ -//! GitHub OAuth (raw reqwest, same style as reddit.rs). +//! OAuth providers (GitHub + Reddit). Provider accounts only *link* to a UUID; +//! the UUID is the canonical identity. Which providers are linked is private. use reqwest::Client; use serde::Deserialize; +use crate::reddit::{ + default_user_agent, reddit_oauth_api_base, reddit_oauth_token_base, +}; + +// ── GitHub ────────────────────────────────────────────────────────────────── + #[derive(Debug, Clone)] pub struct GitHubConfig { pub client_id: String, @@ -40,7 +47,7 @@ impl GitHubConfig { } #[derive(Debug, Deserialize)] -struct TokenResponse { +struct GitHubTokenResponse { access_token: String, } @@ -50,7 +57,7 @@ pub struct GitHubUser { pub login: String, } -pub fn authorize_url(cfg: &GitHubConfig, state: &str, mock_user: Option<&str>) -> String { +pub fn github_authorize_url(cfg: &GitHubConfig, state: &str, mock_user: Option<&str>) -> String { let mut url = format!( "{}/login/oauth/authorize?client_id={}&redirect_uri={}&scope=read:user&state={}", cfg.oauth_base.trim_end_matches('/'), @@ -65,7 +72,7 @@ pub fn authorize_url(cfg: &GitHubConfig, state: &str, mock_user: Option<&str>) - url } -pub async fn exchange_code( +pub async fn github_exchange_code( client: &Client, cfg: &GitHubConfig, code: &str, @@ -90,14 +97,14 @@ pub async fn exchange_code( return Err(format!("github token HTTP {}", resp.status())); } - let body: TokenResponse = resp + let body: GitHubTokenResponse = resp .json() .await .map_err(|e| format!("github token parse failed: {e}"))?; Ok(body.access_token) } -pub async fn fetch_user( +pub async fn github_fetch_user( client: &Client, api_base: &str, access_token: &str, @@ -120,10 +127,149 @@ pub async fn fetch_user( .map_err(|e| format!("github user parse failed: {e}")) } -pub fn provider_id(user: &GitHubUser) -> String { +pub fn github_provider_id(user: &GitHubUser) -> String { user.id.to_string() } +// ── Reddit ────────────────────────────────────────────────────────────────── + +#[derive(Debug, Clone)] +pub struct RedditConfig { + pub client_id: String, + pub client_secret: String, + pub redirect_uri: String, + /// Host for `/api/v1/authorize` (www.reddit.com in production). + pub authorize_base: String, + /// Host for `POST /api/v1/access_token`. + pub token_base: String, + /// Host for bearer `GET /api/v1/me` (oauth.reddit.com). + pub api_base: String, + pub user_agent: String, +} + +/// Authorize page base; defaults to the same host as token POSTs. +pub fn reddit_authorize_base() -> String { + std::env::var("REDDIT_OAUTH_AUTHORIZE_BASE") + .or_else(|_| std::env::var("REDDIT_OAUTH_BASE")) + .unwrap_or_else(|_| "https://www.reddit.com".into()) +} + +impl RedditConfig { + pub fn from_env(base_url: &str) -> Option { + 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; + } + let base = base_url.trim_end_matches('/'); + Some(Self { + client_id, + client_secret, + redirect_uri: format!("{base}/auth/reddit/callback"), + authorize_base: reddit_authorize_base(), + token_base: reddit_oauth_token_base(), + api_base: reddit_oauth_api_base(), + user_agent: default_user_agent(), + }) + } +} + +#[derive(Debug, Deserialize)] +struct RedditTokenResponse { + access_token: String, +} + +#[derive(Debug, Deserialize)] +pub struct RedditUser { + /// Stable id (`t2_…`); never use `name` as identity. + pub id: String, + pub name: String, +} + +pub fn reddit_authorize_url(cfg: &RedditConfig, state: &str, mock_user: Option<&str>) -> String { + let mut url = format!( + "{}/api/v1/authorize?client_id={}&response_type=code&state={}&redirect_uri={}&duration=temporary&scope=identity", + cfg.authorize_base.trim_end_matches('/'), + urlencoding::encode(&cfg.client_id), + urlencoding::encode(state), + urlencoding::encode(&cfg.redirect_uri), + ); + if let Some(user) = mock_user { + url.push_str("&mock_user="); + url.push_str(&urlencoding::encode(user)); + } + url +} + +pub async fn reddit_exchange_code( + client: &Client, + cfg: &RedditConfig, + code: &str, +) -> Result { + let resp = client + .post(format!( + "{}/api/v1/access_token", + cfg.token_base.trim_end_matches('/') + )) + .header("User-Agent", &cfg.user_agent) + .basic_auth(&cfg.client_id, Some(&cfg.client_secret)) + .form(&[ + ("grant_type", "authorization_code"), + ("code", code), + ("redirect_uri", cfg.redirect_uri.as_str()), + ]) + .send() + .await + .map_err(|e| format!("reddit token request failed: {e}"))?; + + if !resp.status().is_success() { + let status = resp.status(); + let body = resp.text().await.unwrap_or_default(); + return Err(format!("reddit token HTTP {status}: {body}")); + } + + let body: RedditTokenResponse = resp + .json() + .await + .map_err(|e| format!("reddit token parse failed: {e}"))?; + Ok(body.access_token) +} + +pub async fn reddit_fetch_user( + client: &Client, + cfg: &RedditConfig, + access_token: &str, +) -> Result { + let resp = client + .get(format!( + "{}/api/v1/me", + cfg.api_base.trim_end_matches('/') + )) + .header("User-Agent", &cfg.user_agent) + .bearer_auth(access_token) + .send() + .await + .map_err(|e| format!("reddit user request failed: {e}"))?; + + if !resp.status().is_success() { + return Err(format!("reddit user HTTP {}", resp.status())); + } + + resp.json() + .await + .map_err(|e| format!("reddit user parse failed: {e}")) +} + +pub fn reddit_provider_id(user: &RedditUser) -> String { + user.id.clone() +} + +// ── Shared helpers ────────────────────────────────────────────────────────── + pub fn validate_pseudonym(raw: &str) -> Result { let trimmed = raw.trim(); if trimmed.is_empty() { @@ -144,3 +290,12 @@ pub fn validate_pseudonym(raw: &str) -> Result { pub fn sanitize_pseudonym(login: &str) -> String { validate_pseudonym(login).unwrap_or_else(|_| "user".to_string()) } + +/// Display name for a provider key (`github` → `GitHub`). Never show provider ids. +pub fn provider_label(provider: &str) -> &'static str { + match provider { + "github" => "GitHub", + "reddit" => "Reddit", + _ => "OAuth", + } +} diff --git a/server/src/lib.rs b/server/src/lib.rs index 84f2565b105fb302241b949af64bd5e49916eab2..0d948af1457504fdbd34d0b14261f65ac58da0ec 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -47,6 +47,8 @@ pub fn create_app(state: AppState) -> Router { .route("/login/alias", get(crate::auth::alias_page)) .route("/auth/github", get(crate::auth::github_start)) .route("/auth/github/callback", get(crate::auth::github_callback)) + .route("/auth/reddit", get(crate::auth::reddit_start)) + .route("/auth/reddit/callback", get(crate::auth::reddit_callback)) .route("/auth/logout", post(crate::auth::logout)) .route("/auth/switch", post(crate::auth::switch_pseudonym)) .route("/ui", post(crate::api::ui_html::post_ui_html)) diff --git a/server/src/projection_apply.rs b/server/src/projection_apply.rs index 4fb4b48ee0a53b7f673fae0c9731eed5acc33332..f50458c2ff3c447a3cfd28adb298e6883a2da4e9 100644 --- a/server/src/projection_apply.rs +++ b/server/src/projection_apply.rs @@ -4,6 +4,8 @@ //! child links, recent-vote appends) plus a cursor advance, all committed in //! one atomic `DisableWal` batch. +use std::collections::HashMap; + use crate::{ event_log::EventLogError, events::{Event, EventRecord}, @@ -44,6 +46,8 @@ pub fn apply_records( let db = projection_store.db(); let mut batch = db.batch(); let mut last_seq = 0u64; + // Weight reads must see earlier writes in this same batch. + let mut pending_weights: HashMap = HashMap::new(); for record in records { match &record.event { @@ -85,6 +89,7 @@ pub fn apply_records( ensure_path_writes(&mut batch, &parsed); } Event::PrincipalCreated { uuid, .. } => { + pending_weights.insert(uuid.clone(), BASE_TRUST_WEIGHT); batch.write( Store::root() .user_weights() @@ -112,17 +117,25 @@ pub fn apply_records( } } else { batch.write(Store::root().oauth_links().key(&link_key).set(uuid)); - let current = Store::root() - .user_weights() - .key(&uuid.clone()) - .get(db) - .map_err(|e| EventLogError::Apply(e.to_string()))? + let current = pending_weights + .get(uuid) + .copied() + .or_else(|| { + Store::root() + .user_weights() + .key(&uuid.clone()) + .get(db) + .ok() + .flatten() + }) .unwrap_or(BASE_TRUST_WEIGHT); + let next = trust_weight_after_link(current); + pending_weights.insert(uuid.clone(), next); batch.write( Store::root() .user_weights() .key(&uuid.clone()) - .set(&trust_weight_after_link(current)), + .set(&next), ); } } @@ -205,6 +218,15 @@ mod tests { ), record( 3, + Event::OauthLinked { + uuid: uuid.into(), + provider: "reddit".into(), + provider_id: "t2_abc".into(), + ts, + }, + ), + record( + 4, Event::PseudonymClaimed { uuid: uuid.into(), pseudonym: "octocat".into(), @@ -219,8 +241,12 @@ mod tests { oauth_link_owner(store.db(), "github", "42").unwrap(), Some(uuid.to_string()) ); + assert_eq!( + crate::storage_schema::linked_providers_for_uuid(store.db(), uuid).unwrap(), + vec!["github".to_string(), "reddit".to_string()] + ); assert_eq!(resolve_actor_uuid(store.db(), "octocat").unwrap(), uuid); - assert_eq!(user_trust_weight(store.db(), uuid).unwrap(), 1.5); + assert_eq!(user_trust_weight(store.db(), uuid).unwrap(), 2.0); let aliases = Store::root() .user_pseudonyms() .key(&uuid.to_string()) diff --git a/server/src/storage_schema.rs b/server/src/storage_schema.rs index b942810afd96d3bf01b7765718303a39be4ef8d2..dac6f8e6080e3f5683c24dbb8861f5a981d456c4 100644 --- a/server/src/storage_schema.rs +++ b/server/src/storage_schema.rs @@ -103,6 +103,25 @@ pub fn oauth_link_owner(db: &Db, provider: &str, provider_id: &str) -> durable:: .get(db) } +/// Provider names linked to a UUID (`github`, `reddit`, …). Private — for the +/// account owner's page only; never expose which providers are linked publicly. +pub fn linked_providers_for_uuid(db: &Db, uuid: &str) -> durable::Result> { + let mut providers = Vec::new(); + for (key, owner) in Store::root().oauth_links().iter(db)? { + if owner != uuid { + continue; + } + let Some((provider, _)) = key.split_once(':') else { + continue; + }; + if !providers.iter().any(|p| p == provider) { + providers.push(provider.to_string()); + } + } + providers.sort(); + Ok(providers) +} + pub const RECENT_VOTES_CAP: u64 = 200; fn id_key(id: &ItemId) -> String { diff --git a/test/support/harness.clj b/test/support/harness.clj index 4505ece5aa193e826ca61c52f1467d252080d66b..741024aab1d14269e225791189633287980ae2e4 100644 --- a/test/support/harness.clj +++ b/test/support/harness.clj @@ -48,12 +48,15 @@ "GITHUB_CLIENT_SECRET" "test-secret" "GITHUB_OAUTH_BASE" (str "http://127.0.0.1:" oauth-port) "GITHUB_API_BASE" (str "http://127.0.0.1:" oauth-port) + ;; Reddit import fixtures + Reddit OAuth on reddit-port. "REDDIT_API_BASE" (str "http://127.0.0.1:" reddit-port) + "REDDIT_CLIENT_ID" "test-reddit" + "REDDIT_CLIENT_SECRET" "test-reddit-secret" "REDDIT_OAUTH_BASE" (str "http://127.0.0.1:" reddit-port) - "REDDIT_CLIENT_ID" "" - "REDDIT_CLIENT_SECRET" "" - "REDDIT_APP_ID" "" - "REDDIT_APP_SECRET" ""})) + "REDDIT_OAUTH_AUTHORIZE_BASE" (str "http://127.0.0.1:" reddit-port) + "REDDIT_OAUTH_TOKEN_BASE" (str "http://127.0.0.1:" reddit-port) + "REDDIT_OAUTH_API_BASE" (str "http://127.0.0.1:" reddit-port) + "REDDIT_USER_AGENT" "web:sorter2-test:v0 (by /u/test)"})) (defn with-auth-servers "Start mock Reddit + mock OAuth + release sorter2-server. diff --git a/test/support/mock_oauth.clj b/test/support/mock_oauth.clj index 909d7a7be8b159ea54a122d69c46af3db3318118..5ba7e9be3cf6d2226648e9609a09ed45f306930f 100644 --- a/test/support/mock_oauth.clj +++ b/test/support/mock_oauth.clj @@ -1,5 +1,5 @@ (ns test.support.mock-oauth - "In-process HTTP stub for GitHub OAuth (authorize, token, /user)." + "In-process HTTP stub for GitHub + Reddit OAuth (authorize, token, user)." (:require [clojure.string :as str]) (:import [com.sun.net.httpserver HttpServer HttpHandler HttpExchange] [java.net InetSocketAddress URLDecoder])) @@ -12,11 +12,14 @@ (URLDecoder/decode (or v "") "UTF-8")))) (str/split query #"&")))) -(defn- parse-mock-user [raw] +(defn- parse-mock-user + "GitHub-style `id:login` (numeric id). Reddit-style `t2_xxx:name`." + [raw] (let [s (or raw "1002:newbie") [id login] (str/split s #":" 2)] - {:id (Long/parseLong id) - :login (or login "newbie")})) + {:id id + :login (or login "newbie") + :numeric? (re-matches #"\d+" id)})) (defn- send-json [^HttpExchange ex status body] (let [bytes (.getBytes body "UTF-8")] @@ -31,9 +34,10 @@ (.sendResponseHeaders ex 302 -1) (.close (.getResponseBody ex))) -(defn- read-form-code [^HttpExchange ex] +(defn- read-form [^HttpExchange ex] (let [body (slurp (.getInputStream ex))] - (query-param body "code"))) + {:code (query-param body "code") + :grant (query-param body "grant_type")})) (defn- bearer-token [^HttpExchange ex] (some-> (.getRequestHeaders ex) @@ -44,8 +48,18 @@ (when (str/starts-with? token "mock:") (parse-mock-user (subs token 5)))) +(defn- authorize-redirect [exchange query] + (let [redirect-uri (query-param query "redirect_uri") + state (query-param query "state") + mock-user (query-param query "mock_user") + user (parse-mock-user mock-user) + code (str "mock:" (:id user) ":" (:login user)) + loc (str redirect-uri "?code=" (java.net.URLEncoder/encode code "UTF-8") + "&state=" (java.net.URLEncoder/encode state "UTF-8"))] + (send-redirect exchange loc))) + (defn start-mock-oauth - "Start mock GitHub OAuth on `port`. Returns a zero-arg `stop` function." + "Start mock GitHub + Reddit OAuth on `port`. Returns a zero-arg `stop` function." [port] (let [server (HttpServer/create (InetSocketAddress. "127.0.0.1" port) 0) handler @@ -53,28 +67,45 @@ (handle [^HttpExchange exchange] (let [uri (.getRequestURI exchange) path (.getPath uri) - query (.getQuery uri)] + query (.getQuery uri) + method (.getRequestMethod exchange)] (cond + ;; GitHub authorize (str/ends-with? path "/login/oauth/authorize") - (let [redirect-uri (query-param query "redirect_uri") - state (query-param query "state") - mock-user (query-param query "mock_user") - user (parse-mock-user mock-user) - code (str "mock:" (:id user) ":" (:login user)) - loc (str redirect-uri "?code=" (java.net.URLEncoder/encode code "UTF-8") - "&state=" (java.net.URLEncoder/encode state "UTF-8"))] - (send-redirect exchange loc)) + (authorize-redirect exchange query) + + ;; Reddit authorize + (str/ends-with? path "/api/v1/authorize") + (authorize-redirect exchange query) - (str/ends-with? path "/login/oauth/access_token") - (let [code (or (read-form-code exchange) "mock:1002:newbie")] + ;; GitHub token + (and (= method "POST") (str/ends-with? path "/login/oauth/access_token")) + (let [code (or (:code (read-form exchange)) "mock:1002:newbie")] (send-json exchange 200 (str "{\"access_token\":\"" code "\",\"token_type\":\"bearer\"}"))) + ;; Reddit token (client_credentials for import + authorization_code for login) + (and (= method "POST") (str/ends-with? path "/api/v1/access_token")) + (let [form (read-form exchange) + grant (or (:grant form) "") + code (or (:code form) "mock:t2_test:redditor")] + (if (= grant "client_credentials") + (send-json exchange 200 "{\"access_token\":\"app-token\",\"token_type\":\"bearer\",\"expires_in\":3600}") + (send-json exchange 200 (str "{\"access_token\":\"" code "\",\"token_type\":\"bearer\",\"expires_in\":3600}")))) + + ;; GitHub user (= path "/user") (let [token (bearer-token exchange) - user (or (parse-token-user token) {:id 1002 :login "newbie"})] + user (or (parse-token-user token) {:id "1002" :login "newbie" :numeric? true})] (send-json exchange 200 (str "{\"id\":" (:id user) ",\"login\":\"" (:login user) "\"}"))) + ;; Reddit /api/v1/me + (str/ends-with? path "/api/v1/me") + (let [token (bearer-token exchange) + user (or (parse-token-user token) {:id "t2_test" :login "redditor"})] + (send-json exchange 200 + (str "{\"id\":\"" (:id user) "\",\"name\":\"" (:login user) "\"}"))) + :else (send-json exchange 404 "{\"error\":\"not found\"}")))))] (.createContext server "/" handler) diff --git a/test/support/mock_reddit.clj b/test/support/mock_reddit.clj index 5efa92db3e1f79b8f423a2f1adcbda959123c1ad..a630cf0938722193e9af88382d60e777ff371be4 100644 --- a/test/support/mock_reddit.clj +++ b/test/support/mock_reddit.clj @@ -1,14 +1,56 @@ (ns test.support.mock-reddit - "In-process HTTP stub for Reddit API fixtures (`test/fixtures/reddit/`)." + "In-process HTTP stub for Reddit API fixtures + OAuth login endpoints." (:require [clojure.java.io :as io] [clojure.string :as str]) (:import [com.sun.net.httpserver HttpServer HttpHandler HttpExchange] - [java.net InetSocketAddress])) + [java.net InetSocketAddress URLDecoder])) (defn fixtures-dir ([] (fixtures-dir (System/getProperty "user.dir"))) ([root] (str root "/test/fixtures/reddit"))) +(defn- query-param [query key] + (when query + (some (fn [pair] + (let [[k v] (str/split pair "=" 2)] + (when (= k key) + (URLDecoder/decode (or v "") "UTF-8")))) + (str/split query #"&")))) + +(defn- parse-mock-user [raw] + (let [s (or raw "t2_test:redditor") + [id login] (str/split s #":" 2)] + {:id id :login (or login "redditor")})) + +(defn- send-bytes [^HttpExchange ex status ^bytes body content-type] + (.set (.getResponseHeaders ex) "Content-Type" content-type) + (.sendResponseHeaders ex status (alength body)) + (doto (.getResponseBody ex) + (.write body) + (.close))) + +(defn- send-json [^HttpExchange ex status body] + (send-bytes ex status (.getBytes body "UTF-8") "application/json")) + +(defn- send-redirect [^HttpExchange ex location] + (.set (.getResponseHeaders ex) "Location" location) + (.sendResponseHeaders ex 302 -1) + (.close (.getResponseBody ex))) + +(defn- read-form [^HttpExchange ex] + (let [body (slurp (.getInputStream ex))] + {:code (query-param body "code") + :grant (query-param body "grant_type")})) + +(defn- bearer-token [^HttpExchange ex] + (some-> (.getRequestHeaders ex) + (.getFirst "Authorization") + (str/replace #"^[Bb]earer " ""))) + +(defn- parse-token-user [token] + (when (str/starts-with? token "mock:") + (parse-mock-user (subs token 5)))) + (defn start-mock-reddit "Start a mock Reddit API on `port`. Returns a zero-arg `stop` function." ([port] (start-mock-reddit port (fixtures-dir))) @@ -19,15 +61,38 @@ handler (proxy [HttpHandler] [] (handle [^HttpExchange exchange] - ;; `/r//about.json` → subreddit entity; `/r/.json` → listing. - (let [path (.getPath (.getRequestURI exchange)) - body (if (str/includes? path "/about") - about - listing)] - (.sendResponseHeaders exchange 200 (alength body)) - (let [out (.getResponseBody exchange)] - (.write out body) - (.close out)))))] + (let [uri (.getRequestURI exchange) + path (.getPath uri) + query (.getQuery uri) + method (.getRequestMethod exchange)] + (cond + (str/ends-with? path "/api/v1/authorize") + (let [redirect-uri (query-param query "redirect_uri") + state (query-param query "state") + user (parse-mock-user (query-param query "mock_user")) + code (str "mock:" (:id user) ":" (:login user)) + loc (str redirect-uri "?code=" (java.net.URLEncoder/encode code "UTF-8") + "&state=" (java.net.URLEncoder/encode state "UTF-8"))] + (send-redirect exchange loc)) + + (and (= method "POST") (str/ends-with? path "/api/v1/access_token")) + (let [form (read-form exchange) + grant (or (:grant form) "") + code (or (:code form) "mock:t2_test:redditor")] + (if (= grant "client_credentials") + (send-json exchange 200 "{\"access_token\":\"app-token\",\"token_type\":\"bearer\",\"expires_in\":3600}") + (send-json exchange 200 (str "{\"access_token\":\"" code "\",\"token_type\":\"bearer\",\"expires_in\":3600}")))) + + (str/ends-with? path "/api/v1/me") + (let [user (or (parse-token-user (bearer-token exchange)) + {:id "t2_test" :login "redditor"})] + (send-json exchange 200 + (str "{\"id\":\"" (:id user) "\",\"name\":\"" (:login user) "\"}"))) + + ;; `/r//about.json` → subreddit entity; `/r/.json` → listing. + :else + (let [body (if (str/includes? path "/about") about listing)] + (send-bytes exchange 200 body "application/json"))))))] (.createContext server "/" handler) (.setExecutor server nil) (.start server)