Can update client last usage

This commit is contained in:
Pierre HUBERT 2025-01-30 21:53:51 +01:00
parent 08f535c15c
commit 501520a9df
2 changed files with 23 additions and 2 deletions

View File

@ -1,4 +1,5 @@
use crate::user::{APIClient, APIClientID, UserConfig, UserID}; use crate::user::{APIClient, APIClientID, UserConfig, UserID};
use crate::utils::curr_time;
use actix_web::dev::Payload; use actix_web::dev::Payload;
use actix_web::{FromRequest, HttpRequest}; use actix_web::{FromRequest, HttpRequest};
use jwt_simple::common::VerificationOptions; use jwt_simple::common::VerificationOptions;
@ -114,9 +115,20 @@ impl APIClientAuth {
} }
// TODO : handle payload // TODO : handle payload
// TODO : update last use (if required)
// TODO : check for IP restriction // TODO : check for IP restriction
// Update last use (if needed)
if client.need_update_last_used() {
let mut user_up = user.clone();
match user_up.find_client_by_id_mut(&client.id) {
None => log::error!("Client ID disappeared!!!"),
Some(u) => u.used = curr_time().unwrap(),
}
if let Err(e) = user_up.save().await {
log::error!("Failed to update last token usage! {e}");
}
}
Ok(Self { Ok(Self {
client: client.clone(), client: client.clone(),
payload: None, payload: None,

View File

@ -80,6 +80,10 @@ impl APIClient {
pub fn fmt_used(&self) -> String { pub fn fmt_used(&self) -> String {
format_time(self.used).unwrap_or_default() format_time(self.used).unwrap_or_default()
} }
pub fn need_update_last_used(&self) -> bool {
self.used + 60 * 15 < curr_time().unwrap()
}
} }
impl APIClient { impl APIClient {
@ -97,7 +101,7 @@ impl APIClient {
} }
} }
#[derive(serde::Serialize, serde::Deserialize)] #[derive(serde::Serialize, serde::Deserialize, Clone)]
pub struct UserConfig { pub struct UserConfig {
/// Target user ID /// Target user ID
pub user_id: UserID, pub user_id: UserID,
@ -211,4 +215,9 @@ impl UserConfig {
pub fn find_client_by_id(&self, id: &APIClientID) -> Option<&APIClient> { pub fn find_client_by_id(&self, id: &APIClientID) -> Option<&APIClient> {
self.clients.iter().find(|c| &c.id == id) self.clients.iter().find(|c| &c.id == id)
} }
/// Find a client by its id and get a mutable reference
pub fn find_client_by_id_mut(&mut self, id: &APIClientID) -> Option<&mut APIClient> {
self.clients.iter_mut().find(|c| &c.id == id)
}
} }