channel.rs

 1use crate::db::{ChannelId, ChannelVisibility};
 2use sea_orm::entity::prelude::*;
 3
 4#[derive(Clone, Debug, Default, PartialEq, Eq, DeriveEntityModel)]
 5#[sea_orm(table_name = "channels")]
 6pub struct Model {
 7    #[sea_orm(primary_key)]
 8    pub id: ChannelId,
 9    pub name: String,
10    pub visibility: ChannelVisibility,
11    pub parent_path: String,
12    pub requires_zed_cla: bool,
13}
14
15impl Model {
16    pub fn parent_id(&self) -> Option<ChannelId> {
17        self.ancestors().last()
18    }
19
20    pub fn is_root(&self) -> bool {
21        self.parent_path.is_empty()
22    }
23
24    pub fn root_id(&self) -> ChannelId {
25        self.ancestors().next().unwrap_or(self.id)
26    }
27
28    pub fn ancestors(&self) -> impl Iterator<Item = ChannelId> + '_ {
29        self.parent_path
30            .trim_end_matches('/')
31            .split('/')
32            .filter_map(|id| Some(ChannelId::from_proto(id.parse().ok()?)))
33    }
34
35    pub fn ancestors_including_self(&self) -> impl Iterator<Item = ChannelId> + '_ {
36        self.ancestors().chain(Some(self.id))
37    }
38
39    pub fn path(&self) -> String {
40        format!("{}{}/", self.parent_path, self.id)
41    }
42}
43
44impl ActiveModelBehavior for ActiveModel {}
45
46#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
47pub enum Relation {
48    #[sea_orm(has_one = "super::room::Entity")]
49    Room,
50    #[sea_orm(has_one = "super::buffer::Entity")]
51    Buffer,
52    #[sea_orm(has_many = "super::channel_member::Entity")]
53    Member,
54    #[sea_orm(has_many = "super::channel_buffer_collaborator::Entity")]
55    BufferCollaborators,
56    #[sea_orm(has_many = "super::channel_chat_participant::Entity")]
57    ChatParticipants,
58}
59
60impl Related<super::channel_member::Entity> for Entity {
61    fn to() -> RelationDef {
62        Relation::Member.def()
63    }
64}
65
66impl Related<super::room::Entity> for Entity {
67    fn to() -> RelationDef {
68        Relation::Room.def()
69    }
70}
71
72impl Related<super::buffer::Entity> for Entity {
73    fn to() -> RelationDef {
74        Relation::Buffer.def()
75    }
76}
77
78impl Related<super::channel_buffer_collaborator::Entity> for Entity {
79    fn to() -> RelationDef {
80        Relation::BufferCollaborators.def()
81    }
82}
83
84impl Related<super::channel_chat_participant::Entity> for Entity {
85    fn to() -> RelationDef {
86        Relation::ChatParticipants.def()
87    }
88}