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!(AccessTokenId);
71id_type!(BufferId);
72id_type!(ChannelBufferCollaboratorId);
73id_type!(ChannelChatParticipantId);
74id_type!(ChannelId);
75id_type!(ChannelMemberId);
76id_type!(ContactId);
77id_type!(DevServerId);
78id_type!(ExtensionId);
79id_type!(FlagId);
80id_type!(FollowerId);
81id_type!(HostedProjectId);
82id_type!(MessageId);
83id_type!(NotificationId);
84id_type!(NotificationKindId);
85id_type!(ProjectCollaboratorId);
86id_type!(ProjectId);
87id_type!(ReplicaId);
88id_type!(RoomId);
89id_type!(RoomParticipantId);
90id_type!(ServerId);
91id_type!(SignupId);
92id_type!(UserId);
93
94/// ChannelRole gives you permissions for both channels and calls.
95#[derive(
96 Eq, PartialEq, Copy, Clone, Debug, EnumIter, DeriveActiveEnum, Default, Hash, Serialize,
97)]
98#[sea_orm(rs_type = "String", db_type = "String(None)")]
99pub enum ChannelRole {
100 /// Admin can read/write and change permissions.
101 #[sea_orm(string_value = "admin")]
102 Admin,
103 /// Member can read/write, but not change pemissions.
104 #[sea_orm(string_value = "member")]
105 #[default]
106 Member,
107 /// Talker can read, but not write.
108 /// They can use microphones and the channel chat
109 #[sea_orm(string_value = "talker")]
110 Talker,
111 /// Guest can read, but not write.
112 /// They can not use microphones but can use the chat.
113 #[sea_orm(string_value = "guest")]
114 Guest,
115 /// Banned may not read.
116 #[sea_orm(string_value = "banned")]
117 Banned,
118}
119
120impl ChannelRole {
121 /// Returns true if this role is more powerful than the other role.
122 pub fn should_override(&self, other: Self) -> bool {
123 use ChannelRole::*;
124 match self {
125 Admin => matches!(other, Member | Banned | Talker | Guest),
126 Member => matches!(other, Banned | Talker | Guest),
127 Talker => matches!(other, Guest),
128 Banned => matches!(other, Guest),
129 Guest => false,
130 }
131 }
132
133 /// Returns the maximal role between the two
134 pub fn max(&self, other: Self) -> Self {
135 if self.should_override(other) {
136 *self
137 } else {
138 other
139 }
140 }
141
142 pub fn can_see_channel(&self, visibility: ChannelVisibility) -> bool {
143 use ChannelRole::*;
144 match self {
145 Admin | Member => true,
146 Guest | Talker => visibility == ChannelVisibility::Public,
147 Banned => false,
148 }
149 }
150
151 /// True if the role allows access to all descendant channels
152 pub fn can_see_all_descendants(&self) -> bool {
153 use ChannelRole::*;
154 match self {
155 Admin | Member => true,
156 Guest | Talker | Banned => false,
157 }
158 }
159
160 /// True if the role only allows access to public descendant channels
161 pub fn can_only_see_public_descendants(&self) -> bool {
162 use ChannelRole::*;
163 match self {
164 Guest | Talker => true,
165 Admin | Member | Banned => false,
166 }
167 }
168
169 /// True if the role can share screen/microphone/projects into rooms.
170 pub fn can_use_microphone(&self) -> bool {
171 use ChannelRole::*;
172 match self {
173 Admin | Member | Talker => true,
174 Guest | Banned => false,
175 }
176 }
177
178 /// True if the role can edit shared projects.
179 pub fn can_edit_projects(&self) -> bool {
180 use ChannelRole::*;
181 match self {
182 Admin | Member => true,
183 Talker | Guest | Banned => false,
184 }
185 }
186
187 /// True if the role can read shared projects.
188 pub fn can_read_projects(&self) -> bool {
189 use ChannelRole::*;
190 match self {
191 Admin | Member | Guest | Talker => true,
192 Banned => false,
193 }
194 }
195
196 pub fn requires_cla(&self) -> bool {
197 use ChannelRole::*;
198 match self {
199 Admin | Member => true,
200 Banned | Guest | Talker => false,
201 }
202 }
203}
204
205impl From<proto::ChannelRole> for ChannelRole {
206 fn from(value: proto::ChannelRole) -> Self {
207 match value {
208 proto::ChannelRole::Admin => ChannelRole::Admin,
209 proto::ChannelRole::Member => ChannelRole::Member,
210 proto::ChannelRole::Talker => ChannelRole::Talker,
211 proto::ChannelRole::Guest => ChannelRole::Guest,
212 proto::ChannelRole::Banned => ChannelRole::Banned,
213 }
214 }
215}
216
217impl Into<proto::ChannelRole> for ChannelRole {
218 fn into(self) -> proto::ChannelRole {
219 match self {
220 ChannelRole::Admin => proto::ChannelRole::Admin,
221 ChannelRole::Member => proto::ChannelRole::Member,
222 ChannelRole::Talker => proto::ChannelRole::Talker,
223 ChannelRole::Guest => proto::ChannelRole::Guest,
224 ChannelRole::Banned => proto::ChannelRole::Banned,
225 }
226 }
227}
228
229impl Into<i32> for ChannelRole {
230 fn into(self) -> i32 {
231 let proto: proto::ChannelRole = self.into();
232 proto.into()
233 }
234}
235
236/// ChannelVisibility controls whether channels are public or private.
237#[derive(Eq, PartialEq, Copy, Clone, Debug, EnumIter, DeriveActiveEnum, Default, Hash)]
238#[sea_orm(rs_type = "String", db_type = "String(None)")]
239pub enum ChannelVisibility {
240 /// Public channels are visible to anyone with the link. People join with the Guest role by default.
241 #[sea_orm(string_value = "public")]
242 Public,
243 /// Members channels are only visible to members of this channel or its parents.
244 #[sea_orm(string_value = "members")]
245 #[default]
246 Members,
247}
248
249impl From<proto::ChannelVisibility> for ChannelVisibility {
250 fn from(value: proto::ChannelVisibility) -> Self {
251 match value {
252 proto::ChannelVisibility::Public => ChannelVisibility::Public,
253 proto::ChannelVisibility::Members => ChannelVisibility::Members,
254 }
255 }
256}
257
258impl Into<proto::ChannelVisibility> for ChannelVisibility {
259 fn into(self) -> proto::ChannelVisibility {
260 match self {
261 ChannelVisibility::Public => proto::ChannelVisibility::Public,
262 ChannelVisibility::Members => proto::ChannelVisibility::Members,
263 }
264 }
265}
266
267impl Into<i32> for ChannelVisibility {
268 fn into(self) -> i32 {
269 let proto: proto::ChannelVisibility = self.into();
270 proto.into()
271 }
272}