project.rs

 1use crate::db::{HostedProjectId, ProjectId, Result, RoomId, ServerId, UserId};
 2use anyhow::anyhow;
 3use rpc::ConnectionId;
 4use sea_orm::entity::prelude::*;
 5
 6#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
 7#[sea_orm(table_name = "projects")]
 8pub struct Model {
 9    #[sea_orm(primary_key)]
10    pub id: ProjectId,
11    pub room_id: Option<RoomId>,
12    pub host_user_id: Option<UserId>,
13    pub host_connection_id: Option<i32>,
14    pub host_connection_server_id: Option<ServerId>,
15    pub hosted_project_id: Option<HostedProjectId>,
16}
17
18impl Model {
19    pub fn host_connection(&self) -> Result<ConnectionId> {
20        let host_connection_server_id = self
21            .host_connection_server_id
22            .ok_or_else(|| anyhow!("empty host_connection_server_id"))?;
23        let host_connection_id = self
24            .host_connection_id
25            .ok_or_else(|| anyhow!("empty host_connection_id"))?;
26        Ok(ConnectionId {
27            owner_id: host_connection_server_id.0 as u32,
28            id: host_connection_id as u32,
29        })
30    }
31}
32
33#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
34pub enum Relation {
35    #[sea_orm(
36        belongs_to = "super::user::Entity",
37        from = "Column::HostUserId",
38        to = "super::user::Column::Id"
39    )]
40    HostUser,
41    #[sea_orm(
42        belongs_to = "super::room::Entity",
43        from = "Column::RoomId",
44        to = "super::room::Column::Id"
45    )]
46    Room,
47    #[sea_orm(has_many = "super::worktree::Entity")]
48    Worktrees,
49    #[sea_orm(has_many = "super::project_collaborator::Entity")]
50    Collaborators,
51    #[sea_orm(has_many = "super::language_server::Entity")]
52    LanguageServers,
53}
54
55impl Related<super::user::Entity> for Entity {
56    fn to() -> RelationDef {
57        Relation::HostUser.def()
58    }
59}
60
61impl Related<super::room::Entity> for Entity {
62    fn to() -> RelationDef {
63        Relation::Room.def()
64    }
65}
66
67impl Related<super::worktree::Entity> for Entity {
68    fn to() -> RelationDef {
69        Relation::Worktrees.def()
70    }
71}
72
73impl Related<super::project_collaborator::Entity> for Entity {
74    fn to() -> RelationDef {
75        Relation::Collaborators.def()
76    }
77}
78
79impl Related<super::language_server::Entity> for Entity {
80    fn to() -> RelationDef {
81        Relation::LanguageServers.def()
82    }
83}
84
85impl ActiveModelBehavior for ActiveModel {}