- The browser's own form checks (required, minLength, e-mail format) show Hungarian text on every page (ValidationMessages in the root layout). - User-facing backend errors are Hungarian: e-mail/password login, password length, links, invitations, sharing, questions, limits. Google/Apple token failures log the reason and tell the user to retry. - errorText covers not_found, horoscope_unavailable and internal errors. - Input fields: lighter background and gold caret on focus, the example text steps aside, so a click visibly lands in the field. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
118 lines
4.3 KiB
Rust
118 lines
4.3 KiB
Rust
//! The public birth chart (`/szuletesi-horoszkop`): the whole natal chart from
|
||
//! birth data, without an account, plus the country list and place search it
|
||
//! needs. A calculation only: no AI, nothing is stored. Each visitor address
|
||
//! may make a limited number of calculations and place searches an hour, so
|
||
//! the endpoints cannot serve as a free calculation service for other apps.
|
||
|
||
use axum::Json;
|
||
use axum::extract::{Query, State};
|
||
use axum::http::HeaderMap;
|
||
use axum_extra::extract::WithRejection;
|
||
use serde::Deserialize;
|
||
use serde_json::{Value, json};
|
||
|
||
use super::{Body, St, client_ip};
|
||
use crate::error::{ApiError, ApiResult};
|
||
use crate::views::chart_highlights;
|
||
|
||
/// The limiter key of the visitor (all unknown addresses share one).
|
||
fn visitor(headers: &HeaderMap) -> String {
|
||
client_ip(headers).map_or_else(|| "unknown".to_string(), |ip| format!("ip:{ip}"))
|
||
}
|
||
|
||
fn valid_country(country: &str) -> bool {
|
||
country.len() == 2 && country.chars().all(|c| c.is_ascii_uppercase())
|
||
}
|
||
|
||
pub async fn countries(State(state): St) -> ApiResult<Json<Value>> {
|
||
Ok(Json(state.horoscope.countries().await?))
|
||
}
|
||
|
||
#[derive(Deserialize)]
|
||
pub struct PlacesQuery {
|
||
country: String,
|
||
q: String,
|
||
limit: Option<u32>,
|
||
}
|
||
|
||
pub async fn places(
|
||
State(state): St,
|
||
headers: HeaderMap,
|
||
WithRejection(Query(query), _): WithRejection<Query<PlacesQuery>, ApiError>,
|
||
) -> ApiResult<Json<Value>> {
|
||
let q = query.q.trim();
|
||
if !valid_country(&query.country) || q.chars().count() < 2 || q.chars().count() > 80 {
|
||
return Err(ApiError::BadRequest("Válassz országot, és írj be 2–80 karaktert a település nevéből.".into()));
|
||
}
|
||
state.places_limiter.hit(&visitor(&headers))?;
|
||
Ok(Json(state.horoscope.places(&query.country, q, query.limit.unwrap_or(8).min(10)).await?))
|
||
}
|
||
|
||
#[derive(Deserialize)]
|
||
#[serde(deny_unknown_fields)]
|
||
pub struct ChartRequest {
|
||
date: String,
|
||
/// "HH:MM"; without it there is no Ascendant.
|
||
time: Option<String>,
|
||
country: String,
|
||
geonames_id: i64,
|
||
/// "earlier" or "later", for a time that occurred twice (clocks set back).
|
||
ambiguous_time: Option<String>,
|
||
}
|
||
|
||
/// Whether a point may have been in the neighbouring sign at the start or the
|
||
/// end of the birth day (when the birth time is unknown).
|
||
fn uncertain(point: &Value, default_speed: f64) -> bool {
|
||
let speed = point["speed"].as_f64().unwrap_or(default_speed).abs();
|
||
point["degree_in_sign"].as_f64().is_some_and(|d| d < speed / 2.0 || 30.0 - d < speed / 2.0)
|
||
}
|
||
|
||
/// The natal chart of the birth data, as the chart pages show it.
|
||
pub async fn chart(
|
||
State(state): St,
|
||
headers: HeaderMap,
|
||
WithRejection(Json(req), _): Body<ChartRequest>,
|
||
) -> ApiResult<Json<Value>> {
|
||
if !valid_country(&req.country) {
|
||
return Err(ApiError::BadRequest("invalid country code".into()));
|
||
}
|
||
if req.ambiguous_time.as_deref().is_some_and(|a| a != "earlier" && a != "later") {
|
||
return Err(ApiError::BadRequest("ambiguous_time is 'earlier' or 'later'".into()));
|
||
}
|
||
state.calculator_limiter.hit(&visitor(&headers))?;
|
||
let mut birth = json!({ "date": req.date, "country": req.country, "geonames_id": req.geonames_id });
|
||
if let Some(time) = &req.time {
|
||
birth["time"] = json!(time);
|
||
}
|
||
if let Some(ambiguous) = &req.ambiguous_time {
|
||
birth["ambiguous_time"] = json!(ambiguous);
|
||
}
|
||
let chart = state.horoscope.natal(&birth).await?;
|
||
let time_known = req.time.is_some();
|
||
let point = |id: &str| {
|
||
chart["points"]
|
||
.as_array()
|
||
.and_then(|points| points.iter().find(|p| p["id"] == id))
|
||
.cloned()
|
||
.unwrap_or(Value::Null)
|
||
};
|
||
let uncertain = json!({
|
||
"sun": !time_known && uncertain(&point("sun"), 1.0),
|
||
"moon": !time_known && uncertain(&point("moon"), 13.2),
|
||
});
|
||
Ok(Json(json!({ "highlights": chart_highlights(&chart), "uncertain": uncertain, "chart": chart })))
|
||
}
|
||
|
||
#[cfg(test)]
|
||
mod tests {
|
||
use super::*;
|
||
|
||
#[test]
|
||
fn signs_near_a_border_are_uncertain() {
|
||
assert!(uncertain(&json!({"degree_in_sign": 3.2, "speed": 12.6}), 13.2));
|
||
assert!(!uncertain(&json!({"degree_in_sign": 15.0, "speed": 12.6}), 13.2));
|
||
assert!(uncertain(&json!({"degree_in_sign": 29.8}), 1.0));
|
||
assert!(!uncertain(&json!({"degree_in_sign": 29.0}), 1.0));
|
||
}
|
||
}
|