ef0b83a
use sea_orm::entity::Set;
use sea_orm::entity::prelude::*;
use uuid::Uuid;
#[sea_orm::model]
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
#[sea_orm(table_name = "spaces")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub id: Uuid,
#[sea_orm(unique, column_type = "String(StringLen::N(32))")]
pub slug: String,
#[sea_orm(column_type = "String(StringLen::N(128))")]
pub name: String,
#[sea_orm(column_type = "String(StringLen::N(1024))")]
pub description: Option<String>,
#[sea_orm(column_type = "String(StringLen::N(512))")]
pub avatar_url: Option<String>,
pub status: super::SpaceStatus,
#[sea_orm(
column_type = "String(StringLen::N(16))",
default_value = "invite_only"
)]
pub join_policy: super::SpaceJoinPolicy,
#[sea_orm(default_value = false)]
pub allow_join: bool,
#[sea_orm(default_value = true)]
pub require_email_verified: bool,
#[sea_orm(default_value = false)]
pub require_mfa: bool,
#[sea_orm(column_type = "Text")]
pub welcome_message: Option<String>,
#[sea_orm(default_value = false)]
pub member_must_enable_2fa: bool,
#[sea_orm(default_value = true)]
pub allow_invite: bool,
#[sea_orm(default_value = 0)]
pub allow_join_level: i16,
// 唯一 OWNER。转移所有权时在同一事务内改写两行会籍。
#[sea_orm(indexed)]
pub owner_user_id: i64,
#[sea_orm(column_type = "String(StringLen::N(512))")]
pub suspended_reason: Option<String>,
#[sea_orm(default_expr = "Expr::current_timestamp()")]
pub created_at: DateTimeUtc,
#[sea_orm(default_expr = "Expr::current_timestamp()")]
pub updated_at: DateTimeUtc,
pub archived_at: Option<DateTimeUtc>,
pub deleted_at: Option<DateTimeUtc>,
// 成员/角色/权限变更的版本键。缓存键与乐观锁都用它,绝不复用 users.security_version:
// 那个字段推进会让用户全部令牌失效,组织层面的改动不该有这个副作用。
#[sea_orm(default_value = 1)]
pub version: i32,
#[sea_orm(has_many)]
pub members: HasMany<super::space_members::Entity>,
#[sea_orm(has_many)]
pub roles: HasMany<super::space_roles::Entity>,
#[sea_orm(has_many)]
pub invites: HasMany<super::space_invites::Entity>,
}
#[async_trait::async_trait]
impl ActiveModelBehavior for ActiveModel {
async fn before_save<C>(mut self, _db: &C, insert: bool) -> Result<Self, DbErr>
where
C: ConnectionTrait,
{
if insert && self.id.is_not_set() {
self.id = Set(Uuid::now_v7());
}
self.updated_at = Set(chrono::Utc::now());
Ok(self)
}
}