From 8c666c3f781156e9321439ea279ddf83323bcd06 Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Wed, 31 May 2023 12:43:57 -0700 Subject: [PATCH] only update last_seen every 12 hours --- src/users.rs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/users.rs b/src/users.rs index e54f04b..26f2dfd 100644 --- a/src/users.rs +++ b/src/users.rs @@ -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_login::{secrecy::SecretVec, AuthUser}; @@ -78,7 +81,18 @@ pub async fn handle_update_last_seen( next: Next, ) -> impl IntoResponse { if let Some(user) = auth.current_user { - user.update_last_seen(&pool).await; + 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; + } + } else { + user.update_last_seen(&pool).await; + } } next.run(request).await }