diff --git a/src/handlers/accounts.rs b/src/handlers/accounts.rs index c1b5a15..2fa462c 100644 --- a/src/handlers/accounts.rs +++ b/src/handlers/accounts.rs @@ -9,6 +9,7 @@ use crate::{ db, error::AppError, models::user::{PreloginResponse, RegisterRequest, User}, + auth::Claims, }; #[worker::send] @@ -102,3 +103,27 @@ pub async fn register( pub async fn send_verification_email() -> String { "fixed-token-to-mock".to_string() } + +#[worker::send] +pub async fn revision_date( + claims: Claims, + State(env): State>, +) -> Result, AppError> { + let db = db::get_db(&env)? ; + + // get the user's updated_at timestamp + let updated_at: Option = db + .prepare("SELECT updated_at FROM users WHERE id = ?1") + .bind(&[claims.sub. into()])? + .first(Some("updated_at")) + .await + .map_err(|_| AppError::Database)?; + + // convert the timestamp to a millisecond-level Unix timestamp + let revision_date = updated_at + .and_then(|ts| chrono::DateTime::parse_from_rfc3339(&ts). ok()) + . map(|dt| dt.timestamp_millis()) + .unwrap_or_else(|| chrono::Utc::now().timestamp_millis()); + + Ok(Json(revision_date)) +} diff --git a/src/router.rs b/src/router.rs index 22fd302..4bfcf80 100644 --- a/src/router.rs +++ b/src/router.rs @@ -24,6 +24,8 @@ pub fn api_router(env: Env) -> Router { ) // Main data sync route .route("/api/sync", get(sync::get_sync_data)) + // For on-demand sync checks + .route("/api/accounts/revision-date", get(accounts::revision_date)) // Ciphers CRUD .route("/api/ciphers", post(ciphers::create_cipher_simple)) .route("/api/ciphers/create", post(ciphers::create_cipher))