feat: implement session last_seen update

main
Jef Roosens 2025-03-15 20:59:00 +01:00
parent 330877c8c5
commit f00d842bad
No known key found for this signature in database
GPG Key ID: 21FD3D77D56BAF49
4 changed files with 35 additions and 5 deletions

View File

@ -181,4 +181,20 @@ impl gpodder::AuthStore for SqliteRepository {
.execute(&mut self.pool.get()?)
.map(|_| ())?)
}
fn refresh_session(
&self,
session: &gpodder::Session,
timestamp: DateTime<chrono::Utc>,
) -> Result<(), AuthErr> {
if diesel::update(sessions::table.filter(sessions::id.eq(session.id)))
.set(sessions::last_seen.eq(timestamp.timestamp()))
.execute(&mut self.pool.get()?)?
== 0
{
Err(AuthErr::UnknownSession)
} else {
Ok(())
}
}
}

View File

@ -52,6 +52,9 @@ pub trait AuthStore {
/// Remove the session with the given session ID
fn remove_session(&self, session_id: i64) -> Result<(), AuthErr>;
/// Update the session's timestamp
fn refresh_session(&self, session: &Session, timestamp: DateTime<Utc>) -> Result<(), AuthErr>;
}
pub trait DeviceRepository {

View File

@ -20,7 +20,7 @@ impl GpodderRepository {
}
}
pub fn validate_session(&self, session_id: i64) -> Result<models::Session, AuthErr> {
pub fn get_session(&self, session_id: i64) -> Result<models::Session, AuthErr> {
let session = self
.store
.get_session(session_id)?
@ -65,6 +65,12 @@ impl GpodderRepository {
Ok(session)
}
pub fn refresh_session(&self, session: &models::Session) -> Result<(), AuthErr> {
let now = Utc::now();
self.store.refresh_session(session, now)
}
pub fn remove_session(&self, session_id: i64) -> Result<(), AuthErr> {
self.store.remove_session(session_id)
}

View File

@ -47,8 +47,13 @@ pub async fn auth_middleware(State(ctx): State<Context>, mut req: Request, next:
.get(SESSION_ID_COOKIE)
.and_then(|c| c.value().parse::<i64>().ok())
{
let ctx_clone = ctx.clone();
match tokio::task::spawn_blocking(move || ctx_clone.store.validate_session(session_id))
let ctx = ctx.clone();
match tokio::task::spawn_blocking(move || {
let session = ctx.store.get_session(session_id)?;
ctx.store.refresh_session(&session)?;
Ok(session)
})
.await
.unwrap()
{