From 7dab7e29ccfadb484888cc61d7782e1190659481 Mon Sep 17 00:00:00 2001 From: Awiteb Date: Wed, 24 Jul 2024 08:40:56 +0300 Subject: [PATCH 1/2] feat: Add `last_logout` column to `users` table Signed-off-by: Awiteb --- crates/oxidetalis/src/database/user.rs | 2 ++ crates/oxidetalis_entities/src/users.rs | 8 +++++--- crates/oxidetalis_migrations/src/create_users_table.rs | 6 ++++++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/crates/oxidetalis/src/database/user.rs b/crates/oxidetalis/src/database/user.rs index d43301d..d4536a6 100644 --- a/crates/oxidetalis/src/database/user.rs +++ b/crates/oxidetalis/src/database/user.rs @@ -16,6 +16,7 @@ //! Functions for interacting with the user table in the database. +use chrono::Utc; use logcall::logcall; use oxidetalis_core::types::PublicKey; use oxidetalis_entities::prelude::*; @@ -47,6 +48,7 @@ impl UserTableExt for DatabaseConnection { if let Err(err) = (UserActiveModel { public_key: Set(*public_key), is_admin: Set(is_admin), + last_logout: Set(Utc::now()), ..Default::default() }) .save(self) diff --git a/crates/oxidetalis_entities/src/users.rs b/crates/oxidetalis_entities/src/users.rs index 66e3db9..8489585 100644 --- a/crates/oxidetalis_entities/src/users.rs +++ b/crates/oxidetalis_entities/src/users.rs @@ -21,6 +21,7 @@ //! Entity for `users` table +use chrono::Utc; use oxidetalis_core::types::PublicKey; use sea_orm::entity::prelude::*; @@ -30,9 +31,10 @@ use crate::prelude::*; #[sea_orm(table_name = "users")] pub struct Model { #[sea_orm(primary_key)] - pub id: UserId, - pub public_key: PublicKey, - pub is_admin: bool, + pub id: UserId, + pub public_key: PublicKey, + pub last_logout: chrono::DateTime, + pub is_admin: bool, } #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] diff --git a/crates/oxidetalis_migrations/src/create_users_table.rs b/crates/oxidetalis_migrations/src/create_users_table.rs index 9a52372..fef238f 100644 --- a/crates/oxidetalis_migrations/src/create_users_table.rs +++ b/crates/oxidetalis_migrations/src/create_users_table.rs @@ -47,6 +47,11 @@ impl MigrationTrait for Migration { .not_null() .unique_key(), ) + .col( + ColumnDef::new(Users::LastLogout) + .timestamp_with_time_zone() + .not_null(), + ) .col( ColumnDef::new(Users::IsAdmin) .boolean() @@ -64,5 +69,6 @@ pub enum Users { Table, Id, PublicKey, + LastLogout, IsAdmin, } -- 2.45.2 From c9480d60ea432e91ed61e7f937f2c4f878e14c85 Mon Sep 17 00:00:00 2001 From: Awiteb Date: Wed, 24 Jul 2024 08:42:02 +0300 Subject: [PATCH 2/2] feat: Save user logout time Signed-off-by: Awiteb --- crates/oxidetalis/src/websocket/mod.rs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/crates/oxidetalis/src/websocket/mod.rs b/crates/oxidetalis/src/websocket/mod.rs index 5986203..9f93b2a 100644 --- a/crates/oxidetalis/src/websocket/mod.rs +++ b/crates/oxidetalis/src/websocket/mod.rs @@ -177,7 +177,7 @@ async fn handle_socket( } }; } - user_disconnected(&conn_id, &user_public_key).await; + user_disconnected(&db_conn, &conn_id, &user_public_key, user).await; }; tokio_spawn(fut); } @@ -225,8 +225,21 @@ async fn handle_events( } /// Handle user disconnected -async fn user_disconnected(conn_id: &Uuid, public_key: &PublicKey) { +async fn user_disconnected( + db_conn: &DatabaseConnection, + conn_id: &Uuid, + public_key: &PublicKey, + user: Option, +) { ONLINE_USERS.remove_user(conn_id).await; + if ONLINE_USERS.is_online(public_key).await.is_none() { + if let Some(mut user) = user.map(IntoActiveModel::into_active_model) { + user.last_logout = Set(Utc::now()); + if let Err(err) = user.update(db_conn).await { + log::error!("{err}"); + } + } + } log::debug!("User disconnect: ConnId(={conn_id}) PublicKey(={public_key})"); } -- 2.45.2