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