From b8eeecc5df5ce5d4cac1cb2c5c923e54f9479b19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Sun, 12 Jul 2026 04:46:19 +0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(meta):=20GET=20/time=20for=20c?= =?UTF-8?q?lient=20clock=20sync?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Session expiry and event times are judged by the server clock; frontends rendering countdowns or past/future checks must not trust the device clock. Fetch once, keep offset = now - Date.now(), add it back when the authoritative time is needed. --- src/lib.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index b37522d..8f6e96e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -71,6 +71,7 @@ pub fn build_router(state: AppState) -> Router { let (router, api) = OpenApiRouter::with_openapi(ApiDoc::openapi()) .route("/", get(health)) .routes(routes!(health)) + .routes(routes!(server_time)) .nest("/auth", web::auth::routes(&state.rate_limit)) .nest("/users", web::users::routes()) .nest("/notes", web::notes::routes()) @@ -141,3 +142,27 @@ fn cors_layer() -> CorsLayer { async fn health() -> Json { Json(json!({ "status": "ok" })) } + +/// The server's current time. All API timestamps are UTC unix-milliseconds +/// judged by this clock (session expiry included), so a frontend that renders +/// countdowns or "is this in the past?" logic should not trust the device +/// clock — fetch this once, keep `offset = now - Date.now()`, and add the +/// offset to `Date.now()` whenever it needs the authoritative time. +#[derive(serde::Serialize, utoipa::ToSchema)] +struct TimeResponse { + /// Current server time, UTC unix-milliseconds. + #[schema(example = 1_752_275_000_000_i64)] + now: i64, +} + +#[utoipa::path( + get, + path = "/time", + tag = "meta", + responses((status = 200, description = "Current server time (UTC unix-milliseconds)", body = TimeResponse)), +)] +async fn server_time() -> Json { + Json(TimeResponse { + now: domain::timestamp::Timestamp::now().as_millis(), + }) +}