feat: implement session last_seen update
parent
330877c8c5
commit
f00d842bad
|
@ -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(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue