only update last_seen every 12 hours

This commit is contained in:
Joe Ardent 2023-05-31 12:43:57 -07:00
parent ac0af7970d
commit 8c666c3f78

View file

@ -1,4 +1,7 @@
use std::fmt::Display; use std::{
fmt::Display,
time::{SystemTime, UNIX_EPOCH},
};
use axum::{extract::State, http::Request, middleware::Next, response::IntoResponse}; use axum::{extract::State, http::Request, middleware::Next, response::IntoResponse};
use axum_login::{secrecy::SecretVec, AuthUser}; use axum_login::{secrecy::SecretVec, AuthUser};
@ -78,7 +81,18 @@ pub async fn handle_update_last_seen<BodyT>(
next: Next<BodyT>, next: Next<BodyT>,
) -> impl IntoResponse { ) -> impl IntoResponse {
if let Some(user) = auth.current_user { if let Some(user) = auth.current_user {
if let Some(then) = user.last_seen {
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default()
.as_secs() as i64;
// The Nyquist frequency for 1-day tracking resolution is 12 hours.
if now - then > 12 * 3600 {
user.update_last_seen(&pool).await; user.update_last_seen(&pool).await;
} }
} else {
user.update_last_seen(&pool).await;
}
}
next.run(request).await next.run(request).await
} }