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 ancestors(&self) -> impl Iterator<Item = ChannelId> + '_ {
21        self.parent_path
22            .trim_end_matches('/')
23            .split('/')
24            .filter_map(|id| Some(ChannelId::from_proto(id.parse().ok()?)))
25    }
26
27    pub fn ancestors_including_self(&self) -> impl Iterator<Item = ChannelId> + '_ {
28        self.ancestors().chain(Some(self.id))
29    }
30
31    pub fn path(&self) -> String {
32        format!("{}{}/", self.parent_path, self.id)
33    }
34}
35
36impl ActiveModelBehavior for ActiveModel {}
37
38#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
39pub enum Relation {
40    #[sea_orm(has_one = "super::room::Entity")]
41    Room,
42    #[sea_orm(has_one = "super::buffer::Entity")]
43    Buffer,
44    #[sea_orm(has_many = "super::channel_member::Entity")]
45    Member,
46    #[sea_orm(has_many = "super::channel_buffer_collaborator::Entity")]
47    BufferCollaborators,
48    #[sea_orm(has_many = "super::channel_chat_participant::Entity")]
49    ChatParticipants,
50}
51
52impl Related<super::channel_member::Entity> for Entity {
53    fn to() -> RelationDef {
54        Relation::Member.def()
55    }
56}
57
58impl Related<super::room::Entity> for Entity {
59    fn to() -> RelationDef {
60        Relation::Room.def()
61    }
62}
63
64impl Related<super::buffer::Entity> for Entity {
65    fn to() -> RelationDef {
66        Relation::Buffer.def()
67    }
68}
69
70impl Related<super::channel_buffer_collaborator::Entity> for Entity {
71    fn to() -> RelationDef {
72        Relation::BufferCollaborators.def()
73    }
74}
75
76impl Related<super::channel_chat_participant::Entity> for Entity {
77    fn to() -> RelationDef {
78        Relation::ChatParticipants.def()
79    }
80}