feat: Add last_logout column to users table
All checks were successful
Write changelog / write-changelog (push) Successful in 4s
Update Contributors / Update Contributors (push) Successful in 4s
Rust CI / Rust CI (push) Successful in 4m26s

Reviewed-on: #31
Reviewed-by: Amjad Alsharafi <me@amjad.alsharafi.dev>
Signed-off-by: Awiteb <a@4rs.nl>
This commit is contained in:
Awiteb 2024-07-24 11:49:39 +03:00
parent 94e3e527ea
commit b79587f537
Signed by: awiteb
GPG key ID: 3F6B55640AA6682F
4 changed files with 28 additions and 5 deletions

View file

@ -16,6 +16,7 @@
//! Functions for interacting with the user table in the database. //! Functions for interacting with the user table in the database.
use chrono::Utc;
use logcall::logcall; use logcall::logcall;
use oxidetalis_core::types::PublicKey; use oxidetalis_core::types::PublicKey;
use oxidetalis_entities::prelude::*; use oxidetalis_entities::prelude::*;
@ -47,6 +48,7 @@ impl UserTableExt for DatabaseConnection {
if let Err(err) = (UserActiveModel { if let Err(err) = (UserActiveModel {
public_key: Set(*public_key), public_key: Set(*public_key),
is_admin: Set(is_admin), is_admin: Set(is_admin),
last_logout: Set(Utc::now()),
..Default::default() ..Default::default()
}) })
.save(self) .save(self)

View file

@ -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); tokio_spawn(fut);
} }
@ -225,8 +225,21 @@ async fn handle_events(
} }
/// Handle user disconnected /// 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<UserModel>,
) {
ONLINE_USERS.remove_user(conn_id).await; 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})"); log::debug!("User disconnect: ConnId(={conn_id}) PublicKey(={public_key})");
} }

View file

@ -21,6 +21,7 @@
//! Entity for `users` table //! Entity for `users` table
use chrono::Utc;
use oxidetalis_core::types::PublicKey; use oxidetalis_core::types::PublicKey;
use sea_orm::entity::prelude::*; use sea_orm::entity::prelude::*;
@ -30,9 +31,10 @@ use crate::prelude::*;
#[sea_orm(table_name = "users")] #[sea_orm(table_name = "users")]
pub struct Model { pub struct Model {
#[sea_orm(primary_key)] #[sea_orm(primary_key)]
pub id: UserId, pub id: UserId,
pub public_key: PublicKey, pub public_key: PublicKey,
pub is_admin: bool, pub last_logout: chrono::DateTime<Utc>,
pub is_admin: bool,
} }
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]

View file

@ -47,6 +47,11 @@ impl MigrationTrait for Migration {
.not_null() .not_null()
.unique_key(), .unique_key(),
) )
.col(
ColumnDef::new(Users::LastLogout)
.timestamp_with_time_zone()
.not_null(),
)
.col( .col(
ColumnDef::new(Users::IsAdmin) ColumnDef::new(Users::IsAdmin)
.boolean() .boolean()
@ -64,5 +69,6 @@ pub enum Users {
Table, Table,
Id, Id,
PublicKey, PublicKey,
LastLogout,
IsAdmin, IsAdmin,
} }