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