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!(DevServerProjectId);
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(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 pemissions.
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 Into<proto::ChannelRole> for ChannelRole {
219 fn into(self) -> proto::ChannelRole {
220 match self {
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 Into<i32> for ChannelRole {
231 fn into(self) -> i32 {
232 let proto: proto::ChannelRole = self.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(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 Into<proto::ChannelVisibility> for ChannelVisibility {
260 fn into(self) -> proto::ChannelVisibility {
261 match self {
262 ChannelVisibility::Public => proto::ChannelVisibility::Public,
263 ChannelVisibility::Members => proto::ChannelVisibility::Members,
264 }
265 }
266}
267
268impl Into<i32> for ChannelVisibility {
269 fn into(self) -> i32 {
270 let proto: proto::ChannelVisibility = self.into();
271 proto.into()
272 }
273}
274
275#[derive(Copy, Clone, Debug, Serialize, PartialEq)]
276pub enum PrincipalId {
277 UserId(UserId),
278 DevServerId(DevServerId),
279}
280
281/// Indicate whether a [Buffer] has permissions to edit.
282#[derive(PartialEq, Clone, Copy, Debug)]
283pub enum Capability {
284 /// The buffer is a mutable replica.
285 ReadWrite,
286 /// The buffer is a read-only replica.
287 ReadOnly,
288}