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 #[allow(missing_docs)]
23 #[serde(transparent)]
24 pub struct $name(pub i32);
25
26 impl $name {
27 #[allow(unused)]
28 #[allow(missing_docs)]
29 pub const MAX: Self = Self(i32::MAX);
30
31 #[allow(unused)]
32 #[allow(missing_docs)]
33 pub fn from_proto(value: u64) -> Self {
34 Self(value as i32)
35 }
36
37 #[allow(unused)]
38 #[allow(missing_docs)]
39 pub fn to_proto(self) -> u64 {
40 self.0 as u64
41 }
42 }
43
44 impl std::fmt::Display for $name {
45 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
46 self.0.fmt(f)
47 }
48 }
49
50 impl sea_orm::TryFromU64 for $name {
51 fn try_from_u64(n: u64) -> Result<Self, DbErr> {
52 Ok(Self(n.try_into().map_err(|_| {
53 DbErr::ConvertFromU64(concat!(
54 "error converting ",
55 stringify!($name),
56 " to u64"
57 ))
58 })?))
59 }
60 }
61
62 impl sea_orm::sea_query::Nullable for $name {
63 fn null() -> Value {
64 Value::Int(None)
65 }
66 }
67 };
68}
69
70id_type!(BufferId);
71id_type!(AccessTokenId);
72id_type!(ChannelChatParticipantId);
73id_type!(ChannelId);
74id_type!(ChannelMemberId);
75id_type!(MessageId);
76id_type!(ContactId);
77id_type!(FollowerId);
78id_type!(RoomId);
79id_type!(RoomParticipantId);
80id_type!(ProjectId);
81id_type!(ProjectCollaboratorId);
82id_type!(ReplicaId);
83id_type!(ServerId);
84id_type!(SignupId);
85id_type!(UserId);
86id_type!(ChannelBufferCollaboratorId);
87id_type!(FlagId);
88id_type!(ExtensionId);
89id_type!(NotificationId);
90id_type!(NotificationKindId);
91
92/// ChannelRole gives you permissions for both channels and calls.
93#[derive(Eq, PartialEq, Copy, Clone, Debug, EnumIter, DeriveActiveEnum, Default, Hash)]
94#[sea_orm(rs_type = "String", db_type = "String(None)")]
95pub enum ChannelRole {
96 /// Admin can read/write and change permissions.
97 #[sea_orm(string_value = "admin")]
98 Admin,
99 /// Member can read/write, but not change pemissions.
100 #[sea_orm(string_value = "member")]
101 #[default]
102 Member,
103 /// Talker can read, but not write.
104 /// They can use microphones and the channel chat
105 #[sea_orm(string_value = "talker")]
106 Talker,
107 /// Guest can read, but not write.
108 /// They can not use microphones but can use the chat.
109 #[sea_orm(string_value = "guest")]
110 Guest,
111 /// Banned may not read.
112 #[sea_orm(string_value = "banned")]
113 Banned,
114}
115
116impl ChannelRole {
117 /// Returns true if this role is more powerful than the other role.
118 pub fn should_override(&self, other: Self) -> bool {
119 use ChannelRole::*;
120 match self {
121 Admin => matches!(other, Member | Banned | Talker | Guest),
122 Member => matches!(other, Banned | Talker | Guest),
123 Talker => matches!(other, Guest),
124 Banned => matches!(other, Guest),
125 Guest => false,
126 }
127 }
128
129 /// Returns the maximal role between the two
130 pub fn max(&self, other: Self) -> Self {
131 if self.should_override(other) {
132 *self
133 } else {
134 other
135 }
136 }
137
138 pub fn can_see_channel(&self, visibility: ChannelVisibility) -> bool {
139 use ChannelRole::*;
140 match self {
141 Admin | Member => true,
142 Guest | Talker => visibility == ChannelVisibility::Public,
143 Banned => false,
144 }
145 }
146
147 /// True if the role allows access to all descendant channels
148 pub fn can_see_all_descendants(&self) -> bool {
149 use ChannelRole::*;
150 match self {
151 Admin | Member => true,
152 Guest | Talker | Banned => false,
153 }
154 }
155
156 /// True if the role only allows access to public descendant channels
157 pub fn can_only_see_public_descendants(&self) -> bool {
158 use ChannelRole::*;
159 match self {
160 Guest | Talker => true,
161 Admin | Member | Banned => false,
162 }
163 }
164
165 /// True if the role can share screen/microphone/projects into rooms.
166 pub fn can_use_microphone(&self) -> bool {
167 use ChannelRole::*;
168 match self {
169 Admin | Member | Talker => true,
170 Guest | Banned => false,
171 }
172 }
173
174 /// True if the role can edit shared projects.
175 pub fn can_edit_projects(&self) -> bool {
176 use ChannelRole::*;
177 match self {
178 Admin | Member => true,
179 Talker | Guest | Banned => false,
180 }
181 }
182
183 /// True if the role can read shared projects.
184 pub fn can_read_projects(&self) -> bool {
185 use ChannelRole::*;
186 match self {
187 Admin | Member | Guest | Talker => true,
188 Banned => false,
189 }
190 }
191
192 pub fn requires_cla(&self) -> bool {
193 use ChannelRole::*;
194 match self {
195 Admin | Member => true,
196 Banned | Guest | Talker => false,
197 }
198 }
199}
200
201impl From<proto::ChannelRole> for ChannelRole {
202 fn from(value: proto::ChannelRole) -> Self {
203 match value {
204 proto::ChannelRole::Admin => ChannelRole::Admin,
205 proto::ChannelRole::Member => ChannelRole::Member,
206 proto::ChannelRole::Talker => ChannelRole::Talker,
207 proto::ChannelRole::Guest => ChannelRole::Guest,
208 proto::ChannelRole::Banned => ChannelRole::Banned,
209 }
210 }
211}
212
213impl Into<proto::ChannelRole> for ChannelRole {
214 fn into(self) -> proto::ChannelRole {
215 match self {
216 ChannelRole::Admin => proto::ChannelRole::Admin,
217 ChannelRole::Member => proto::ChannelRole::Member,
218 ChannelRole::Talker => proto::ChannelRole::Talker,
219 ChannelRole::Guest => proto::ChannelRole::Guest,
220 ChannelRole::Banned => proto::ChannelRole::Banned,
221 }
222 }
223}
224
225impl Into<i32> for ChannelRole {
226 fn into(self) -> i32 {
227 let proto: proto::ChannelRole = self.into();
228 proto.into()
229 }
230}
231
232/// ChannelVisibility controls whether channels are public or private.
233#[derive(Eq, PartialEq, Copy, Clone, Debug, EnumIter, DeriveActiveEnum, Default, Hash)]
234#[sea_orm(rs_type = "String", db_type = "String(None)")]
235pub enum ChannelVisibility {
236 /// Public channels are visible to anyone with the link. People join with the Guest role by default.
237 #[sea_orm(string_value = "public")]
238 Public,
239 /// Members channels are only visible to members of this channel or its parents.
240 #[sea_orm(string_value = "members")]
241 #[default]
242 Members,
243}
244
245impl From<proto::ChannelVisibility> for ChannelVisibility {
246 fn from(value: proto::ChannelVisibility) -> Self {
247 match value {
248 proto::ChannelVisibility::Public => ChannelVisibility::Public,
249 proto::ChannelVisibility::Members => ChannelVisibility::Members,
250 }
251 }
252}
253
254impl Into<proto::ChannelVisibility> for ChannelVisibility {
255 fn into(self) -> proto::ChannelVisibility {
256 match self {
257 ChannelVisibility::Public => proto::ChannelVisibility::Public,
258 ChannelVisibility::Members => proto::ChannelVisibility::Members,
259 }
260 }
261}
262
263impl Into<i32> for ChannelVisibility {
264 fn into(self) -> i32 {
265 let proto: proto::ChannelVisibility = self.into();
266 proto.into()
267 }
268}