diff --git a/migrations/2025-03-15-145721_session_last_seen/down.sql b/migrations/2025-03-15-145721_session_last_seen/down.sql new file mode 100644 index 0000000..e794cf7 --- /dev/null +++ b/migrations/2025-03-15-145721_session_last_seen/down.sql @@ -0,0 +1,3 @@ +-- This file should undo anything in `up.sql` +alter table sessions + drop column last_seen; diff --git a/migrations/2025-03-15-145721_session_last_seen/up.sql b/migrations/2025-03-15-145721_session_last_seen/up.sql new file mode 100644 index 0000000..776d287 --- /dev/null +++ b/migrations/2025-03-15-145721_session_last_seen/up.sql @@ -0,0 +1,3 @@ +-- Your SQL goes here +alter table sessions + add column last_seen bigint not null default 0; diff --git a/src/db/models/session.rs b/src/db/models/session.rs index 10ee4a1..273550a 100644 --- a/src/db/models/session.rs +++ b/src/db/models/session.rs @@ -11,16 +11,21 @@ use crate::db::{schema::*, DbPool, DbResult}; pub struct Session { pub id: i64, pub user_id: i64, + pub last_seen: i64, } impl Session { - pub fn new_for_user(pool: &DbPool, user_id: i64) -> DbResult { + pub fn new_for_user(pool: &DbPool, user_id: i64, last_seen: i64) -> DbResult { let id: i64 = rand::thread_rng().gen(); - Ok(Self { id, user_id } - .insert_into(sessions::table) - .returning(Self::as_returning()) - .get_result(&mut pool.get()?)?) + Ok(Self { + id, + user_id, + last_seen, + } + .insert_into(sessions::table) + .returning(Self::as_returning()) + .get_result(&mut pool.get()?)?) } pub fn user_from_id(pool: &DbPool, id: i64) -> DbResult> { diff --git a/src/db/repository/auth.rs b/src/db/repository/auth.rs index ff30f09..453c1f5 100644 --- a/src/db/repository/auth.rs +++ b/src/db/repository/auth.rs @@ -77,6 +77,7 @@ impl gpodder::AuthRepository for SqliteRepository { let session_id = db::Session { id, user_id: user.id, + last_seen: chrono::Utc::now().timestamp(), } .insert_into(sessions::table) .returning(sessions::id) diff --git a/src/db/schema.rs b/src/db/schema.rs index fe21dfe..2f597f1 100644 --- a/src/db/schema.rs +++ b/src/db/schema.rs @@ -41,6 +41,7 @@ diesel::table! { sessions (id) { id -> BigInt, user_id -> BigInt, + last_seen -> BigInt, } }