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::billing_customer::Entity")]
 33    BillingCustomer,
 34    #[sea_orm(has_one = "super::room_participant::Entity")]
 35    RoomParticipant,
 36    #[sea_orm(has_many = "super::project::Entity")]
 37    HostedProjects,
 38    #[sea_orm(has_many = "super::channel_member::Entity")]
 39    ChannelMemberships,
 40    #[sea_orm(has_many = "super::user_feature::Entity")]
 41    UserFeatures,
 42    #[sea_orm(has_one = "super::contributor::Entity")]
 43    Contributor,
 44}
 45
 46impl Model {
 47    /// Returns the timestamp of when the user's account was created.
 48    ///
 49    /// This will be the earlier of the `created_at` and `github_user_created_at` timestamps.
 50    pub fn account_created_at(&self) -> NaiveDateTime {
 51        let mut account_created_at = self.created_at;
 52        if let Some(github_created_at) = self.github_user_created_at {
 53            account_created_at = account_created_at.min(github_created_at);
 54        }
 55
 56        account_created_at
 57    }
 58}
 59
 60impl Related<super::access_token::Entity> for Entity {
 61    fn to() -> RelationDef {
 62        Relation::AccessToken.def()
 63    }
 64}
 65
 66impl Related<super::billing_customer::Entity> for Entity {
 67    fn to() -> RelationDef {
 68        Relation::BillingCustomer.def()
 69    }
 70}
 71
 72impl Related<super::room_participant::Entity> for Entity {
 73    fn to() -> RelationDef {
 74        Relation::RoomParticipant.def()
 75    }
 76}
 77
 78impl Related<super::project::Entity> for Entity {
 79    fn to() -> RelationDef {
 80        Relation::HostedProjects.def()
 81    }
 82}
 83
 84impl Related<super::channel_member::Entity> for Entity {
 85    fn to() -> RelationDef {
 86        Relation::ChannelMemberships.def()
 87    }
 88}
 89
 90impl Related<super::user_feature::Entity> for Entity {
 91    fn to() -> RelationDef {
 92        Relation::UserFeatures.def()
 93    }
 94}
 95
 96impl ActiveModelBehavior for ActiveModel {}
 97
 98pub struct UserFlags;
 99
100impl Linked for UserFlags {
101    type FromEntity = Entity;
102
103    type ToEntity = super::feature_flag::Entity;
104
105    fn link(&self) -> Vec<RelationDef> {
106        vec![
107            super::user_feature::Relation::User.def().rev(),
108            super::user_feature::Relation::Flag.def(),
109        ]
110    }
111}