From 7251a3e92ce493f2625bc48c893f713c5884cc5c Mon Sep 17 00:00:00 2001 From: alemi Date: Sun, 24 Mar 2024 04:03:22 +0100 Subject: [PATCH] feat: added sessions with credentials --- ...m20240323_000002_add_simple_credentials.rs | 31 +++++++++++++++++-- src/model/session.rs | 29 +++++++++++++++++ src/model/user.rs | 10 +++++- 3 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 src/model/session.rs diff --git a/src/migrations/m20240323_000002_add_simple_credentials.rs b/src/migrations/m20240323_000002_add_simple_credentials.rs index ea0db292..d112a567 100644 --- a/src/migrations/m20240323_000002_add_simple_credentials.rs +++ b/src/migrations/m20240323_000002_add_simple_credentials.rs @@ -16,8 +16,26 @@ impl MigrationTrait for Migration { .not_null() .primary_key() ) - .col(ColumnDef::new(Credentials::Email).boolean().not_null()) - .col(ColumnDef::new(Credentials::Password).boolean().not_null()) + .col(ColumnDef::new(Credentials::Email).string().not_null()) + .col(ColumnDef::new(Credentials::Password).string().not_null()) + .to_owned() + ) + .await?; + + manager + .create_table( + Table::create() + .table(Sessions::Table) + .col( + ColumnDef::new(Sessions::Id) + .integer() + .not_null() + .auto_increment() + .primary_key() + ) + .col(ColumnDef::new(Sessions::Actor).string().not_null()) + .col(ColumnDef::new(Sessions::Session).string().not_null()) + .col(ColumnDef::new(Sessions::Expires).date_time().not_null()) .to_owned() ) .await?; @@ -41,3 +59,12 @@ enum Credentials { Email, Password, } + +#[derive(DeriveIden)] +enum Sessions { + Table, + Id, // TODO here ID is a number but in Credentials it's the actor ID (String) ??? weird!! + Actor, + Session, + Expires, +} diff --git a/src/model/session.rs b/src/model/session.rs new file mode 100644 index 00000000..598a6387 --- /dev/null +++ b/src/model/session.rs @@ -0,0 +1,29 @@ +use sea_orm::entity::prelude::*; + +#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] +#[sea_orm(table_name = "sessions")] +pub struct Model { + #[sea_orm(primary_key)] + pub id: i64, + pub actor: String, + pub session: String, + pub expires: ChronoDateTimeUtc, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation { + #[sea_orm( + belongs_to = "super::user::Entity", + from = "Column::Actor", + to = "super::user::Column::Id" + )] + User, +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::User.def() + } +} + +impl ActiveModelBehavior for ActiveModel {} diff --git a/src/model/user.rs b/src/model/user.rs index 2d899024..e61eab50 100644 --- a/src/model/user.rs +++ b/src/model/user.rs @@ -7,7 +7,6 @@ use crate::{activitypub, activitystream::object::{collection::Collection, actor: #[sea_orm(table_name = "users")] pub struct Model { #[sea_orm(primary_key)] - /// must be full AP ID, since they are unique over the network pub id: String, pub domain: String, pub actor_type: ActorType, @@ -77,6 +76,9 @@ pub enum Relation { #[sea_orm(has_one = "super::credential::Entity")] Credential, + + #[sea_orm(has_many = "super::session::Entity")] + Session, } impl Related for Entity { @@ -103,4 +105,10 @@ impl Related for Entity { } } +impl Related for Entity { + fn to() -> RelationDef { + Relation::Session.def() + } +} + impl ActiveModelBehavior for ActiveModel {}