user.rs

  1use crate::db::UserId;
  2use chrono::NaiveDateTime;
  3use sea_orm::entity::prelude::*;
  4use serde::Serialize;
  5
  6/// A user model.
  7#[derive(Clone, Debug, Default, PartialEq, Eq, DeriveEntityModel, Serialize)]
  8#[sea_orm(table_name = "users")]
  9pub struct Model {
 10    #[sea_orm(primary_key)]
 11    pub id: UserId,
 12    pub github_login: String,
 13    pub github_user_id: i32,
 14    pub github_user_created_at: Option<NaiveDateTime>,
 15    pub email_address: Option<String>,
 16    pub name: Option<String>,
 17    pub admin: bool,
 18    pub invite_code: Option<String>,
 19    pub invite_count: i32,
 20    pub inviter_id: Option<UserId>,
 21    pub connected_once: bool,
 22    pub metrics_id: Uuid,
 23    pub created_at: NaiveDateTime,
 24    pub accepted_tos_at: Option<NaiveDateTime>,
 25    pub custom_llm_monthly_allowance_in_cents: Option<i32>,
 26}
 27
 28#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
 29pub enum Relation {
 30    #[sea_orm(has_many = "super::access_token::Entity")]
 31    AccessToken,
 32    #[sea_orm(has_one = "super::room_participant::Entity")]
 33    RoomParticipant,
 34    #[sea_orm(has_many = "super::project::Entity")]
 35    HostedProjects,
 36    #[sea_orm(has_many = "super::channel_member::Entity")]
 37    ChannelMemberships,
 38    #[sea_orm(has_many = "super::user_feature::Entity")]
 39    UserFeatures,
 40    #[sea_orm(has_one = "super::contributor::Entity")]
 41    Contributor,
 42}
 43
 44impl Model {
 45    /// Returns the timestamp of when the user's account was created.
 46    ///
 47    /// This will be the earlier of the `created_at` and `github_user_created_at` timestamps.
 48    pub fn account_created_at(&self) -> NaiveDateTime {
 49        let mut account_created_at = self.created_at;
 50        if let Some(github_created_at) = self.github_user_created_at {
 51            account_created_at = account_created_at.min(github_created_at);
 52        }
 53
 54        account_created_at
 55    }
 56
 57    /// Returns the age of the user's account.
 58    pub fn account_age(&self) -> chrono::Duration {
 59        chrono::Utc::now().naive_utc() - self.account_created_at()
 60    }
 61}
 62
 63impl Related<super::access_token::Entity> for Entity {
 64    fn to() -> RelationDef {
 65        Relation::AccessToken.def()
 66    }
 67}
 68
 69impl Related<super::room_participant::Entity> for Entity {
 70    fn to() -> RelationDef {
 71        Relation::RoomParticipant.def()
 72    }
 73}
 74
 75impl Related<super::project::Entity> for Entity {
 76    fn to() -> RelationDef {
 77        Relation::HostedProjects.def()
 78    }
 79}
 80
 81impl Related<super::channel_member::Entity> for Entity {
 82    fn to() -> RelationDef {
 83        Relation::ChannelMemberships.def()
 84    }
 85}
 86
 87impl Related<super::user_feature::Entity> for Entity {
 88    fn to() -> RelationDef {
 89        Relation::UserFeatures.def()
 90    }
 91}
 92
 93impl ActiveModelBehavior for ActiveModel {}
 94
 95pub struct UserFlags;
 96
 97impl Linked for UserFlags {
 98    type FromEntity = Entity;
 99
100    type ToEntity = super::feature_flag::Entity;
101
102    fn link(&self) -> Vec<RelationDef> {
103        vec![
104            super::user_feature::Relation::User.def().rev(),
105            super::user_feature::Relation::Flag.def(),
106        ]
107    }
108}