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 /// The order of this channel relative to its siblings within the same parent.
14 /// Lower values appear first. Channels are sorted by parent_path first, then by channel_order.
15 pub channel_order: i32,
16}
17
18impl Model {
19 pub fn parent_id(&self) -> Option<ChannelId> {
20 self.ancestors().last()
21 }
22
23 pub fn is_root(&self) -> bool {
24 self.parent_path.is_empty()
25 }
26
27 pub fn root_id(&self) -> ChannelId {
28 self.ancestors().next().unwrap_or(self.id)
29 }
30
31 pub fn ancestors(&self) -> impl Iterator<Item = ChannelId> + '_ {
32 self.parent_path
33 .trim_end_matches('/')
34 .split('/')
35 .filter_map(|id| Some(ChannelId::from_proto(id.parse().ok()?)))
36 }
37
38 pub fn ancestors_including_self(&self) -> impl Iterator<Item = ChannelId> + '_ {
39 self.ancestors().chain(Some(self.id))
40 }
41
42 pub fn path(&self) -> String {
43 format!("{}{}/", self.parent_path, self.id)
44 }
45
46 pub fn descendant_path_filter(&self) -> String {
47 format!("{}{}/%", self.parent_path, self.id)
48 }
49}
50
51impl ActiveModelBehavior for ActiveModel {}
52
53#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
54pub enum Relation {
55 #[sea_orm(has_one = "super::room::Entity")]
56 Room,
57 #[sea_orm(has_one = "super::buffer::Entity")]
58 Buffer,
59 #[sea_orm(has_many = "super::channel_member::Entity")]
60 Member,
61 #[sea_orm(has_many = "super::channel_buffer_collaborator::Entity")]
62 BufferCollaborators,
63 #[sea_orm(has_many = "super::channel_chat_participant::Entity")]
64 ChatParticipants,
65}
66
67impl Related<super::channel_member::Entity> for Entity {
68 fn to() -> RelationDef {
69 Relation::Member.def()
70 }
71}
72
73impl Related<super::room::Entity> for Entity {
74 fn to() -> RelationDef {
75 Relation::Room.def()
76 }
77}
78
79impl Related<super::buffer::Entity> for Entity {
80 fn to() -> RelationDef {
81 Relation::Buffer.def()
82 }
83}
84
85impl Related<super::channel_buffer_collaborator::Entity> for Entity {
86 fn to() -> RelationDef {
87 Relation::BufferCollaborators.def()
88 }
89}
90
91impl Related<super::channel_chat_participant::Entity> for Entity {
92 fn to() -> RelationDef {
93 Relation::ChatParticipants.def()
94 }
95}