ids.rs

  1use crate::Result;
  2use rpc::proto;
  3use sea_orm::{entity::prelude::*, DbErr};
  4use serde::{Deserialize, Serialize};
  5
  6macro_rules! id_type {
  7    ($name:ident) => {
  8        #[derive(
  9            Clone,
 10            Copy,
 11            Debug,
 12            Default,
 13            PartialEq,
 14            Eq,
 15            PartialOrd,
 16            Ord,
 17            Hash,
 18            Serialize,
 19            Deserialize,
 20            DeriveValueType,
 21        )]
 22        #[serde(transparent)]
 23        pub struct $name(pub i32);
 24
 25        impl $name {
 26            #[allow(unused)]
 27            pub const MAX: Self = Self(i32::MAX);
 28
 29            #[allow(unused)]
 30            pub fn from_proto(value: u64) -> Self {
 31                Self(value as i32)
 32            }
 33
 34            #[allow(unused)]
 35            pub fn to_proto(self) -> u64 {
 36                self.0 as u64
 37            }
 38        }
 39
 40        impl std::fmt::Display for $name {
 41            fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
 42                self.0.fmt(f)
 43            }
 44        }
 45
 46        impl sea_orm::TryFromU64 for $name {
 47            fn try_from_u64(n: u64) -> Result<Self, DbErr> {
 48                Ok(Self(n.try_into().map_err(|_| {
 49                    DbErr::ConvertFromU64(concat!(
 50                        "error converting ",
 51                        stringify!($name),
 52                        " to u64"
 53                    ))
 54                })?))
 55            }
 56        }
 57
 58        impl sea_orm::sea_query::Nullable for $name {
 59            fn null() -> Value {
 60                Value::Int(None)
 61            }
 62        }
 63    };
 64}
 65
 66id_type!(BufferId);
 67id_type!(AccessTokenId);
 68id_type!(ChannelChatParticipantId);
 69id_type!(ChannelId);
 70id_type!(ChannelMemberId);
 71id_type!(MessageId);
 72id_type!(ContactId);
 73id_type!(FollowerId);
 74id_type!(RoomId);
 75id_type!(RoomParticipantId);
 76id_type!(ProjectId);
 77id_type!(ProjectCollaboratorId);
 78id_type!(ReplicaId);
 79id_type!(ServerId);
 80id_type!(SignupId);
 81id_type!(UserId);
 82id_type!(ChannelBufferCollaboratorId);
 83id_type!(FlagId);
 84
 85#[derive(Eq, PartialEq, Copy, Clone, Debug, EnumIter, DeriveActiveEnum, Default, Hash)]
 86#[sea_orm(rs_type = "String", db_type = "String(None)")]
 87pub enum ChannelRole {
 88    #[sea_orm(string_value = "admin")]
 89    Admin,
 90    #[sea_orm(string_value = "member")]
 91    #[default]
 92    Member,
 93    #[sea_orm(string_value = "guest")]
 94    Guest,
 95    #[sea_orm(string_value = "banned")]
 96    Banned,
 97}
 98
 99impl ChannelRole {
100    pub fn should_override(&self, other: Self) -> bool {
101        use ChannelRole::*;
102        match self {
103            Admin => matches!(other, Member | Banned | Guest),
104            Member => matches!(other, Banned | Guest),
105            Banned => matches!(other, Guest),
106            Guest => false,
107        }
108    }
109
110    pub fn max(&self, other: Self) -> Self {
111        if self.should_override(other) {
112            *self
113        } else {
114            other
115        }
116    }
117
118    pub fn can_see_all_descendants(&self) -> bool {
119        use ChannelRole::*;
120        match self {
121            Admin | Member => true,
122            Guest | Banned => false,
123        }
124    }
125
126    pub fn can_only_see_public_descendants(&self) -> bool {
127        use ChannelRole::*;
128        match self {
129            Guest => true,
130            Admin | Member | Banned => false,
131        }
132    }
133}
134
135impl From<proto::ChannelRole> for ChannelRole {
136    fn from(value: proto::ChannelRole) -> Self {
137        match value {
138            proto::ChannelRole::Admin => ChannelRole::Admin,
139            proto::ChannelRole::Member => ChannelRole::Member,
140            proto::ChannelRole::Guest => ChannelRole::Guest,
141            proto::ChannelRole::Banned => ChannelRole::Banned,
142        }
143    }
144}
145
146impl Into<proto::ChannelRole> for ChannelRole {
147    fn into(self) -> proto::ChannelRole {
148        match self {
149            ChannelRole::Admin => proto::ChannelRole::Admin,
150            ChannelRole::Member => proto::ChannelRole::Member,
151            ChannelRole::Guest => proto::ChannelRole::Guest,
152            ChannelRole::Banned => proto::ChannelRole::Banned,
153        }
154    }
155}
156
157impl Into<i32> for ChannelRole {
158    fn into(self) -> i32 {
159        let proto: proto::ChannelRole = self.into();
160        proto.into()
161    }
162}
163
164#[derive(Eq, PartialEq, Copy, Clone, Debug, EnumIter, DeriveActiveEnum, Default, Hash)]
165#[sea_orm(rs_type = "String", db_type = "String(None)")]
166pub enum ChannelVisibility {
167    #[sea_orm(string_value = "public")]
168    Public,
169    #[sea_orm(string_value = "members")]
170    #[default]
171    Members,
172}
173
174impl From<proto::ChannelVisibility> for ChannelVisibility {
175    fn from(value: proto::ChannelVisibility) -> Self {
176        match value {
177            proto::ChannelVisibility::Public => ChannelVisibility::Public,
178            proto::ChannelVisibility::Members => ChannelVisibility::Members,
179        }
180    }
181}
182
183impl Into<proto::ChannelVisibility> for ChannelVisibility {
184    fn into(self) -> proto::ChannelVisibility {
185        match self {
186            ChannelVisibility::Public => proto::ChannelVisibility::Public,
187            ChannelVisibility::Members => proto::ChannelVisibility::Members,
188        }
189    }
190}
191
192impl Into<i32> for ChannelVisibility {
193    fn into(self) -> i32 {
194        let proto: proto::ChannelVisibility = self.into();
195        proto.into()
196    }
197}