add error/succ messages

This commit is contained in:
2025-08-03 12:11:02 +02:00
parent e3fd3bdfcc
commit 811d29b9ec
6 changed files with 272 additions and 116 deletions

124
Cargo.lock generated
View File

@@ -53,6 +53,17 @@ version = "1.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "69f7f8c3906b62b754cd5326047894316021dcfe5a194c8ea52bdd94934a3457" checksum = "69f7f8c3906b62b754cd5326047894316021dcfe5a194c8ea52bdd94934a3457"
[[package]]
name = "async-trait"
version = "0.1.88"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e539d3fca749fcee5236ab05e93a52867dd549cc157c8cb7f99595f3cedffdb5"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "atoi" name = "atoi"
version = "2.0.0" version = "2.0.0"
@@ -145,6 +156,22 @@ dependencies = [
"tower-service", "tower-service",
] ]
[[package]]
name = "axum-messages"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d67ce6e7bc1e1e71f2a4e86d418045a29c63c4ebb631f3d9bb2f81c4958ea391"
dependencies = [
"axum-core",
"http",
"parking_lot",
"serde",
"serde_json",
"tower",
"tower-sessions-core",
"tracing",
]
[[package]] [[package]]
name = "backtrace" name = "backtrace"
version = "0.3.75" version = "0.3.75"
@@ -381,6 +408,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c9e6a11ca8224451684bc0d7d5a7adbf8f2fd6887261a1cfc3c0432f9d4068e" checksum = "9c9e6a11ca8224451684bc0d7d5a7adbf8f2fd6887261a1cfc3c0432f9d4068e"
dependencies = [ dependencies = [
"powerfmt", "powerfmt",
"serde",
] ]
[[package]] [[package]]
@@ -481,6 +509,20 @@ dependencies = [
"percent-encoding", "percent-encoding",
] ]
[[package]]
name = "futures"
version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "65bc07b1a8bc7c85c5f2e110c476c7389b4554ba72af57d8445ea63a576b0876"
dependencies = [
"futures-channel",
"futures-core",
"futures-io",
"futures-sink",
"futures-task",
"futures-util",
]
[[package]] [[package]]
name = "futures-channel" name = "futures-channel"
version = "0.3.31" version = "0.3.31"
@@ -525,6 +567,17 @@ version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e5c1b78ca4aae1ac06c48a526a655760685149f0d465d21f37abfe57ce075c6" checksum = "9e5c1b78ca4aae1ac06c48a526a655760685149f0d465d21f37abfe57ce075c6"
[[package]]
name = "futures-macro"
version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "162ee34ebcb7c64a8abebc059ce0fee27c2262618d7b60ed8faf72fef13c3650"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "futures-sink" name = "futures-sink"
version = "0.3.31" version = "0.3.31"
@@ -545,6 +598,7 @@ checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81"
dependencies = [ dependencies = [
"futures-core", "futures-core",
"futures-io", "futures-io",
"futures-macro",
"futures-sink", "futures-sink",
"futures-task", "futures-task",
"memchr", "memchr",
@@ -1007,6 +1061,7 @@ checksum = "96936507f153605bddfcda068dd804796c84324ed2510809e5b2a624c81da765"
dependencies = [ dependencies = [
"autocfg", "autocfg",
"scopeguard", "scopeguard",
"serde",
] ]
[[package]] [[package]]
@@ -2135,6 +2190,22 @@ dependencies = [
"tracing", "tracing",
] ]
[[package]]
name = "tower-cookies"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "151b5a3e3c45df17466454bb74e9ecedecc955269bdedbf4d150dfa393b55a36"
dependencies = [
"axum-core",
"cookie",
"futures-util",
"http",
"parking_lot",
"pin-project-lite",
"tower-layer",
"tower-service",
]
[[package]] [[package]]
name = "tower-http" name = "tower-http"
version = "0.6.6" version = "0.6.6"
@@ -2173,6 +2244,57 @@ version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8df9b6e13f2d32c91b9bd719c00d1958837bc7dec474d94952798cc8e69eeec3" checksum = "8df9b6e13f2d32c91b9bd719c00d1958837bc7dec474d94952798cc8e69eeec3"
[[package]]
name = "tower-sessions"
version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43a05911f23e8fae446005fe9b7b97e66d95b6db589dc1c4d59f6a2d4d4927d3"
dependencies = [
"async-trait",
"http",
"time",
"tokio",
"tower-cookies",
"tower-layer",
"tower-service",
"tower-sessions-core",
"tower-sessions-memory-store",
"tracing",
]
[[package]]
name = "tower-sessions-core"
version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce8cce604865576b7751b7a6bc3058f754569a60d689328bb74c52b1d87e355b"
dependencies = [
"async-trait",
"axum-core",
"base64",
"futures",
"http",
"parking_lot",
"rand",
"serde",
"serde_json",
"thiserror",
"time",
"tokio",
"tracing",
]
[[package]]
name = "tower-sessions-memory-store"
version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fb05909f2e1420135a831dd5df9f5596d69196d0a64c3499ca474c4bd3d33242"
dependencies = [
"async-trait",
"time",
"tokio",
"tower-sessions-core",
]
[[package]] [[package]]
name = "tracing" name = "tracing"
version = "0.1.41" version = "0.1.41"
@@ -2421,6 +2543,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"axum", "axum",
"axum-extra", "axum-extra",
"axum-messages",
"chrono", "chrono",
"maud", "maud",
"rust-i18n", "rust-i18n",
@@ -2428,6 +2551,7 @@ dependencies = [
"sqlx", "sqlx",
"tokio", "tokio",
"tower-http", "tower-http",
"tower-sessions",
"uuid", "uuid",
] ]

View File

@@ -6,6 +6,7 @@ edition = "2024"
[dependencies] [dependencies]
axum = "0.8" axum = "0.8"
axum-extra = { version = "0.10", features = ["cookie"] } axum-extra = { version = "0.10", features = ["cookie"] }
axum-messages = "0.8.0"
chrono = { version = "0.4.41", features = ["serde"] } chrono = { version = "0.4.41", features = ["serde"] }
maud = { version = "0.27", features = ["axum"] } maud = { version = "0.27", features = ["axum"] }
rust-i18n = "3.1.5" rust-i18n = "3.1.5"
@@ -13,4 +14,5 @@ serde = { version = "1", features = ["derive"] }
sqlx = { version = "0.8", features = ["runtime-tokio-rustls", "sqlite", "macros", "chrono"] } sqlx = { version = "0.8", features = ["runtime-tokio-rustls", "sqlite", "macros", "chrono"] }
tokio = { version = "1", features = ["macros", "rt-multi-thread"] } tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
tower-http = { version = "0.6", features = ["fs"] } tower-http = { version = "0.6", features = ["fs"] }
tower-sessions = "0.14.0"
uuid = { version = "1.17", features = ["v4", "serde"] } uuid = { version = "1.17", features = ["v4", "serde"] }

View File

@@ -1,4 +1,4 @@
use crate::{language::language, page::new, Backend}; use crate::{language::language, page::Page, Backend, NameUpdateError};
use axum::{ use axum::{
extract::{Path, State}, extract::{Path, State},
http::HeaderMap, http::HeaderMap,
@@ -7,6 +7,7 @@ use axum::{
Form, Router, Form, Router,
}; };
use axum_extra::extract::CookieJar; use axum_extra::extract::CookieJar;
use axum_messages::Messages;
use maud::{html, Markup, PreEscaped}; use maud::{html, Markup, PreEscaped};
use serde::Deserialize; use serde::Deserialize;
use std::sync::Arc; use std::sync::Arc;
@@ -15,6 +16,7 @@ use uuid::Uuid;
async fn index( async fn index(
State(backend): State<Arc<Backend>>, State(backend): State<Arc<Backend>>,
cookies: CookieJar, cookies: CookieJar,
messages: Messages,
headers: HeaderMap, headers: HeaderMap,
) -> Response { ) -> Response {
let (cookies, req) = backend.client_full(cookies, &headers).await; let (cookies, req) = backend.client_full(cookies, &headers).await;
@@ -24,66 +26,65 @@ async fn index(
let amount_total_cameras = backend.amount_total_cameras().await; let amount_total_cameras = backend.amount_total_cameras().await;
let highscore = backend.highscore().await; let highscore = backend.highscore().await;
let markup = new( let mut page = Page::new(req.lang);
html! { page.messages(messages);
hgroup { let markup = page.content(html! {
h1 { "Who finds the most cameras?" } hgroup {
} h1 { "Who finds the most cameras?" }
p { }
mark { "TODO: Explanation of AEF / digital shadows / search game" } p {
} mark { "TODO: Explanation of AEF / digital shadows / search game" }
}
div.mb-sm { div.mb-sm {
(client.get_display_name()) (client.get_display_name())
", do you want to be named something different? No worries, change here 👇" ", do you want to be named something different? No worries, change here 👇"
} }
form action="/name" method="post" { form action="/name" method="post" {
fieldset role="group" { fieldset role="group" {
input input
name="name" name="name"
placeholder="✨ Your new name starts here ✨" placeholder="✨ Your new name starts here ✨"
aria-label="Name"; aria-label="Name";
input type="submit" value="Save"; input type="submit" value="Save";
}
}
p.mb-0 {
"You have found "
(sightings.len())
"/"
(amount_total_cameras)
" cameras:"
progress value=(sightings.len()) max=(amount_total_cameras);
}
ol.flex.small {
@for sighting in &sightings {
li {
(sighting.camera.name)
} }
} }
}
p.mb-0 { p {
"You have found " h2 { "Highscore" }
(sightings.len()) ul.iterated {
"/" @for rank in highscore {
(amount_total_cameras) li.card {
" cameras:" span {
progress value=(sightings.len()) max=(amount_total_cameras); span.font-headline.rank.text-muted { (rank.rank) "." }
} @if rank.client == client { (PreEscaped("<mark id='ranking'>")) }
(rank.client.get_display_name())
ol.flex.small { @if rank.client == client { (PreEscaped("</mark>")) }
@for (idx, sighting) in sightings.iter().enumerate() {
li {
(sighting.camera.name)
}
}
}
p {
h2 { "Highscore" }
ul.iterated {
@for rank in highscore {
li.card {
span {
span.font-headline.rank.text-muted { (rank.rank) "." }
@if rank.client == client { (PreEscaped("<mark id='ranking'>")) }
(rank.client.get_display_name())
@if rank.client == client { (PreEscaped("</mark>")) }
}
span.font-headline.font-lg { (rank.amount) (PreEscaped("&nbsp;")) "📸" }
} }
span.font-headline.font-lg { (rank.amount) (PreEscaped("&nbsp;")) "📸" }
} }
} }
} }
}, }
req.lang, });
);
(cookies, markup).into_response() (cookies, markup).into_response()
} }
@@ -92,6 +93,7 @@ async fn game(
State(backend): State<Arc<Backend>>, State(backend): State<Arc<Backend>>,
cookies: CookieJar, cookies: CookieJar,
headers: HeaderMap, headers: HeaderMap,
messages: Messages,
Path(uuid): Path<String>, Path(uuid): Path<String>,
) -> Result<Redirect, Response> { ) -> Result<Redirect, Response> {
let (cookies, client) = backend.client(cookies).await; let (cookies, client) = backend.client(cookies).await;
@@ -104,20 +106,22 @@ async fn game(
return Err(not_found(cookies, headers).await.into_response()); return Err(not_found(cookies, headers).await.into_response());
}; };
let succ = backend.client_found_camera(&client, &camera).await; if backend.client_found_camera(&client, &camera).await {
// TODO: show succ/err based on succ messages.info(format!("found-cam|{}", camera.name));
} else {
messages.info(format!(
"err|Try to find a new camera!|You have already collected this camera.|"
));
}
Ok(Redirect::to("/game")) Ok(Redirect::to("/game"))
} }
async fn not_found(cookies: CookieJar, headers: HeaderMap) -> Markup { async fn not_found(cookies: CookieJar, headers: HeaderMap) -> Markup {
let lang = language(&cookies, &headers); let lang = language(&cookies, &headers);
new( Page::new(lang).content(html! {
html! { h1 { "uups" }
h1 { "uups" } })
},
lang,
)
} }
#[derive(Deserialize)] #[derive(Deserialize)]
@@ -128,13 +132,16 @@ struct NameForm {
async fn set_name( async fn set_name(
State(backend): State<Arc<Backend>>, State(backend): State<Arc<Backend>>,
cookies: CookieJar, cookies: CookieJar,
messages: Messages,
Form(form): Form<NameForm>, Form(form): Form<NameForm>,
) -> Response { ) -> Response {
let (cookies, client) = backend.client(cookies).await; let (cookies, client) = backend.client(cookies).await;
// Update the client's name in the backend match backend.set_client_name(&client, &form.name).await {
// TODO: handle succ/err msg Ok(()) => messages.info("set-name-succ"),
let _ = backend.set_client_name(&client, &form.name).await; Err(NameUpdateError::TooShort(expected, actual)) => messages.info(format!("err|That's too little!|We need more information about you. Give us at least {expected} characters for you new name!|You sent us {actual} characters.")),
Err(NameUpdateError::TooLong(expected, actual)) => messages.info(format!("err|That's too much!|We only live in (20)25, so please use less than {expected} characters for your new name.|You sent us {actual} characters.")),
};
// Redirect back to the game page // Redirect back to the game page
(cookies, Redirect::to("/game")).into_response() (cookies, Redirect::to("/game")).into_response()

View File

@@ -8,10 +8,7 @@ pub(super) async fn index(cookies: CookieJar, headers: HeaderMap) -> Markup {
rust_i18n::set_locale(lang.to_locale()); rust_i18n::set_locale(lang.to_locale());
let mut page = Page::new(lang); let page = Page::new(lang);
page.succ("Yeah! That worked!".into());
page.err("Damn...".into());
page.content( page.content(
html! { html! {
h1 { (t!("digital_shadows")) } h1 { (t!("digital_shadows")) }

View File

@@ -1,9 +1,11 @@
use crate::model::client::Client; use crate::model::client::Client;
use axum::{http::HeaderMap, routing::get, Router}; use axum::{http::HeaderMap, routing::get, Router};
use axum_extra::extract::{cookie::Cookie, CookieJar}; use axum_extra::extract::{cookie::Cookie, CookieJar};
use axum_messages::MessagesManagerLayer;
use sqlx::{pool::PoolOptions, sqlite::SqliteConnectOptions, SqlitePool}; use sqlx::{pool::PoolOptions, sqlite::SqliteConnectOptions, SqlitePool};
use std::{fmt::Display, str::FromStr, sync::Arc}; use std::{fmt::Display, str::FromStr, sync::Arc};
use tower_http::services::ServeDir; use tower_http::services::ServeDir;
use tower_sessions::{MemoryStore, SessionManagerLayer};
use uuid::Uuid; use uuid::Uuid;
#[macro_use] #[macro_use]
@@ -68,6 +70,11 @@ struct Req {
lang: Language, lang: Language,
} }
pub(crate) enum NameUpdateError {
TooLong(usize, usize),
TooShort(usize, usize),
}
impl Backend { impl Backend {
async fn client(&self, cookies: CookieJar) -> (CookieJar, Client) { async fn client(&self, cookies: CookieJar) -> (CookieJar, Client) {
let existing_uuid = cookies let existing_uuid = cookies
@@ -91,12 +98,12 @@ impl Backend {
(cookies, Req { client, lang }) (cookies, Req { client, lang })
} }
async fn set_client_name(&self, client: &Client, name: &str) -> Result<(), String> { async fn set_client_name(&self, client: &Client, name: &str) -> Result<(), NameUpdateError> {
if name.len() > 25 { if name.len() > 25 {
return Err("Maximum 25 chars are allowed".into()); return Err(NameUpdateError::TooLong(25, name.len()));
} }
if name.len() < 3 { if name.len() < 3 {
return Err("Minimum of 3 chars needed".into()); return Err(NameUpdateError::TooShort(3, name.len()));
} }
match self { match self {
@@ -118,6 +125,9 @@ impl Backend {
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
let session_store = MemoryStore::default();
let session_layer = SessionManagerLayer::new(session_store).with_secure(false);
let connection_options = SqliteConnectOptions::from_str("sqlite://db.sqlite").unwrap(); let connection_options = SqliteConnectOptions::from_str("sqlite://db.sqlite").unwrap();
let db: SqlitePool = PoolOptions::new() let db: SqlitePool = PoolOptions::new()
.connect_with(connection_options) .connect_with(connection_options)
@@ -128,7 +138,9 @@ async fn main() {
.route("/", get(index::index)) .route("/", get(index::index))
.nest_service("/static", ServeDir::new("./static/serve")) .nest_service("/static", ServeDir::new("./static/serve"))
.merge(game::routes()) .merge(game::routes())
.with_state(Arc::new(Backend::Sqlite(db))); .with_state(Arc::new(Backend::Sqlite(db)))
.layer(MessagesManagerLayer)
.layer(session_layer);
// run our app with hyper, listening globally on port 3000 // run our app with hyper, listening globally on port 3000
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap(); let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();

View File

@@ -1,40 +1,49 @@
use crate::Language; use crate::Language;
use axum_messages::Messages;
use maud::{html, Markup, DOCTYPE}; use maud::{html, Markup, DOCTYPE};
// TODO: set dynamic meta lang attribute // TODO: set dynamic meta lang attribute
// TODO: remove function
#[deprecated]
pub fn new(content: Markup, lang: Language) -> Markup {
let page = Page::new(lang);
page.content(content)
}
pub(crate) struct Page { pub(crate) struct Page {
lang: Language, lang: Language,
succ: Option<String>, found_camera: Option<String>,
err: Option<String>, new_name: bool,
err: Option<(String, String, String)>,
} }
impl Page { impl Page {
pub fn new(lang: Language) -> Self { pub fn new(lang: Language) -> Self {
Self { Self {
lang, lang,
succ: None, found_camera: None,
new_name: false,
err: None, err: None,
} }
} }
pub fn succ(&mut self, msg: String) { pub fn messages(&mut self, messages: Messages) {
self.succ = Some(msg); for message in messages {
} let text = &message.to_string()[..];
match (message.level, text) {
pub fn err(&mut self, msg: String) { (_, "set-name-succ") => {
self.err = Some(msg); self.new_name = true;
} }
(_, msg) if msg.starts_with("found-cam|") => {
fn has_msg(&self) -> bool { let (_, name) = msg.split_once('|').expect("we just checked |");
self.succ.is_some() || self.err.is_some() self.found_camera = Some(name.into());
}
(_, msg) if msg.starts_with("err|") => {
let mut parts = msg.splitn(4, '|');
let _ = parts.next().expect("just checked |");
if let (Some(title), Some(body), Some(footer)) =
(parts.next(), parts.next(), parts.next())
{
self.err = Some((title.into(), body.into(), footer.into()));
}
}
(_, _) => {}
}
}
} }
pub fn content(self, content: Markup) -> Markup { pub fn content(self, content: Markup) -> Markup {
@@ -83,39 +92,44 @@ impl Page {
} }
main.container { main.container {
article class="succ w-full" { @if let Some(found_camera) = &self.found_camera {
header { article class="succ w-full" {
"Camera found" header {
} "Camera "
"✨ You are a star ✨ Star dust sprinkle, sprinkle. " (found_camera)
a href="#ranking" { " found"
"See your ranking" }
} "✨ You are a star ✨ Star dust sprinkle, sprinkle. "
footer { a href="#ranking" {
"Rear Exit Cam" "See your ranking"
}
footer {
"Rear Exit Cam"
}
} }
} }
article class="name w-full" { @if self.new_name {
header { article class="name w-full" {
"Camera found" header {
} "New name!"
"text" }
footer { "Welcome"
"Name of Camera"
} }
} }
article class="error w-full" { @if let Some(err) = &self.err {
header { article class="error w-full" {
"Camera found" header {
} (err.0)
"text" }
footer { (err.1)
"Name of Camera" footer {
(err.2)
}
} }
} }
section { (content) } section { (content) }
} }