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 pub fn descendant_path_filter(&self) -> String {
44 format!("{}{}/%", self.parent_path, self.id)
45 }
46}
47
48impl ActiveModelBehavior for ActiveModel {}
49
50#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
51pub enum Relation {
52 #[sea_orm(has_one = "super::room::Entity")]
53 Room,
54 #[sea_orm(has_one = "super::buffer::Entity")]
55 Buffer,
56 #[sea_orm(has_many = "super::channel_member::Entity")]
57 Member,
58 #[sea_orm(has_many = "super::channel_buffer_collaborator::Entity")]
59 BufferCollaborators,
60 #[sea_orm(has_many = "super::channel_chat_participant::Entity")]
61 ChatParticipants,
62}
63
64impl Related<super::channel_member::Entity> for Entity {
65 fn to() -> RelationDef {
66 Relation::Member.def()
67 }
68}
69
70impl Related<super::room::Entity> for Entity {
71 fn to() -> RelationDef {
72 Relation::Room.def()
73 }
74}
75
76impl Related<super::buffer::Entity> for Entity {
77 fn to() -> RelationDef {
78 Relation::Buffer.def()
79 }
80}
81
82impl Related<super::channel_buffer_collaborator::Entity> for Entity {
83 fn to() -> RelationDef {
84 Relation::BufferCollaborators.def()
85 }
86}
87
88impl Related<super::channel_chat_participant::Entity> for Entity {
89 fn to() -> RelationDef {
90 Relation::ChatParticipants.def()
91 }
92}