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 /// Returns the age of the user's account.
60 pub fn account_age(&self) -> chrono::Duration {
61 chrono::Utc::now().naive_utc() - self.account_created_at()
62 }
63}
64
65impl Related<super::access_token::Entity> for Entity {
66 fn to() -> RelationDef {
67 Relation::AccessToken.def()
68 }
69}
70
71impl Related<super::billing_customer::Entity> for Entity {
72 fn to() -> RelationDef {
73 Relation::BillingCustomer.def()
74 }
75}
76
77impl Related<super::room_participant::Entity> for Entity {
78 fn to() -> RelationDef {
79 Relation::RoomParticipant.def()
80 }
81}
82
83impl Related<super::project::Entity> for Entity {
84 fn to() -> RelationDef {
85 Relation::HostedProjects.def()
86 }
87}
88
89impl Related<super::channel_member::Entity> for Entity {
90 fn to() -> RelationDef {
91 Relation::ChannelMemberships.def()
92 }
93}
94
95impl Related<super::user_feature::Entity> for Entity {
96 fn to() -> RelationDef {
97 Relation::UserFeatures.def()
98 }
99}
100
101impl ActiveModelBehavior for ActiveModel {}
102
103pub struct UserFlags;
104
105impl Linked for UserFlags {
106 type FromEntity = Entity;
107
108 type ToEntity = super::feature_flag::Entity;
109
110 fn link(&self) -> Vec<RelationDef> {
111 vec![
112 super::user_feature::Relation::User.def().rev(),
113 super::user_feature::Relation::Flag.def(),
114 ]
115 }
116}