use crate::db::{self, builds, model::*}; use crate::review::candidates; use crate::time; use super::auth; use super::render::*; use super::{Req, Res, State}; mod mutations; mod pages; pub(crate) use mutations::{ build_cancel, build_retry, notice_ack, package_rebuild, package_withdraw, review_approve, review_reject, reviews_bulk, sub_add, sub_pause, sub_remove, }; pub(crate) use pages::{ attention, audit, build_detail, build_log, build_log_stream, builds_page, builds_with, dashboard, metrics, package_detail, packages, panels_base, review_body, review_detail, review_rows, reviews, settings, subscriptions, }; pub(crate) fn gate(state: &State, req: &Req, sid: &str) -> Option { if !auth::auth_required(&state.cfg) { return None; } if auth::is_authenticated(sid) || req.path == "/login" { return None; } Some(Res::redirect("/login")) } pub(crate) fn login_form(state: &State, req: &Req) -> Res { let page = Page::new("Sign in", csrf(state, req)); page_to_res(Login { page, error: String::new() }, 200) } /// `pub(crate)`, not `pub`: the census counted it as test-only because the word appears in `tests/http.rs`, but only inside a string — a route path, a content type, the word "redirect" in an assertion message. Nothing in the test tier calls it. pub(crate) fn logout(req: &Req) -> Res { if let Some(sid) = req.cookie("sid") { auth::drop_session(&sid); } let mut res = Res::redirect("/login"); res.header("Set-Cookie", "sid=; Path=/; Max-Age=0"); res } pub fn login(state: &State, req: &Req) -> Res { let given = req.form_get("password").unwrap_or_default(); let page = Page::new("Sign in", csrf(state, req)); if auth::password_ok(&state.cfg, &given) { if let Some(old) = req.cookie("sid") { auth::drop_session(&old); } let sid = match auth::new_session() { Some(sid) => sid, None => return page_to_res(Login { page, error: "server: no entropy for a session".into() }, 500), }; auth::authenticate(&sid); let mut res = Res::redirect("/"); res.header("X-New-Session", &sid); res } else { page_to_res(Login { page, error: "wrong password".into() }, 401) } } /// `pub(crate)`, not `pub`: the census counted it as test-only because the word appears in `tests/http.rs`, but only inside a string — a route path, a content type, the word "redirect" in an assertion message. Nothing in the test tier calls it. pub(crate) fn route(state: &State, req: &Req, seg: &[&str]) -> Res { match (req.method.as_str(), seg) { ("GET", []) => dashboard(state, req), ("GET", ["reviews"]) => reviews(state, req), ("GET", ["reviews", id]) => review_detail(state, req, id), ("POST", ["reviews", id, "approve"]) => review_approve(state, req, id), ("POST", ["reviews", id, "reject"]) => review_reject(state, req, id), ("POST", ["reviews", "bulk"]) => reviews_bulk(state, req), ("POST", ["logout"]) => logout(req), ("GET", ["subscriptions"]) => subscriptions(state, req), ("POST", ["subscriptions", "add"]) => sub_add(state, req), ("POST", ["subscriptions", "pause"]) => sub_pause(state, req), ("POST", ["subscriptions", "remove"]) => sub_remove(state, req), ("GET", ["attention"]) => attention(state, req), ("POST", ["attention", "ack"]) => notice_ack(state, req), ("GET", ["packages"]) => packages(state, req), ("GET", ["packages", name]) => package_detail(state, req, name), ("POST", ["packages", name, "rebuild"]) => package_rebuild(state, req, name), ("POST", ["packages", name, "withdraw"]) => package_withdraw(state, req, name), ("GET", ["builds"]) => builds_page(state, req), ("GET", ["builds", id]) => build_detail(state, req, id), ("GET", ["builds", id, "log.txt"]) => build_log(state, id), ("GET", ["builds", id, "log.stream"]) => build_log_stream(state, req, id), ("POST", ["builds", id, "cancel"]) => build_cancel(state, req, id), ("POST", ["builds", id, "requeue"]) => build_retry(state, req, id), ("GET", ["audit"]) => audit(state, req), ("GET", ["settings"]) => settings(state, req), _ => not_found(state, req), } } fn not_found(state: &State, req: &Req) -> Res { let page = chrome(state, req, "Not found", "nav_packages"); page_to_res( ErrorPage { page, status: "404".into(), message: "no such page".into(), detail: String::new(), }, 404, ) } fn chrome(state: &State, req: &Req, title: &str, active: &str) -> Page { let db = &state.db; let mut page = Page::new(title, csrf(state, req)); page.pending = candidates::pending_candidates(db).map(|v| v.len() as i64).unwrap_or(0); page.running = builds::in_flight(db).map(|v| v as i64).unwrap_or(0) + builds::queue_depth(db).map(|v| v as i64).unwrap_or(0); page.attention = db::list_notices(db, false).map(|v| v.len() as i64).unwrap_or(0) + builds::blocked_builds(db).map(|v| v.len() as i64).unwrap_or(0); page.nav_reviews = if active == "nav_reviews" { "on" } else { "" }; page.nav_builds = if active == "nav_builds" { "on" } else { "" }; page.nav_attention = if active == "nav_attention" { "on" } else { "" }; page.nav_packages = if active == "nav_packages" { "on" } else { "" }; page.nav_subscriptions = if active == "nav_subscriptions" { "on" } else { "" }; page.nav_audit = if active == "nav_audit" { "on" } else { "" }; page.nav_settings = if active == "nav_settings" { "on" } else { "" }; page.gated = auth::auth_required(&state.cfg); page } fn csrf(_state: &State, req: &Req) -> String { req.cookie("sid").map(|s| auth::csrf_token(&s)).unwrap_or_default() } fn human_size(bytes: i64) -> String { const UNITS: [&str; 4] = ["B", "KiB", "MiB", "GiB"]; let mut v = bytes as f64; let mut i = 0; while v >= 1024.0 && i < UNITS.len() - 1 { v /= 1024.0; i += 1; } if i == 0 { format!("{bytes} {}", UNITS[0]) } else { format!("{v:.1} {}", UNITS[i]) } } fn short(id: &str) -> String { id.chars().take(10).collect() } fn age(detected_at: &str) -> String { let now = time::now(); format!("{detected_at} → {now}") } fn actor(state: &State, req: &Req) -> &'static str { let sid = req.cookie("sid").unwrap_or_default(); if !auth::auth_required(&state.cfg) || auth::is_authenticated(&sid) { "web" } else { "web-anon" } } fn flash_sub(state: &State, req: &Req, msg: &str, err: bool) -> Res { let page = chrome(state, req, "Subscriptions", "nav_subscriptions").flash(msg.into(), err); let pending = candidates::pending_candidates(&state.db).unwrap_or_default(); let rows = db::list_subscriptions(&state.db) .unwrap_or_default() .into_iter() .map(|s| SubRow { pending: pending.iter().filter(|c| c.pkgbase == s.pkgbase).count() as i64, name: s.pkgbase.clone(), state: s.state.to_string(), maintainer: s.maintainer.unwrap_or_else(|| "unknown".into()), last_checked: s.last_checked_at.unwrap_or_else(|| "never".into()), next_check: s.next_check_at.unwrap_or_else(|| "soon".into()), last_error: s.last_error.clone().unwrap_or_default(), has_error: s.last_error.is_some(), pause_label: if s.state == SubscriptionState::Paused { "resume" } else { "pause" }, }) .collect(); page_to_res(Subscriptions { page, rows }, 200) } // ------------------------------------------------------------ rendering -- fn page_to_res(t: T, status: u16) -> Res { match askama::Template::render(&t) { Ok(body) => Res::html(status, body), Err(e) => Res::text(500, format!("template error: {e}")), } }