Skip to main content

revolt_models/v0/
channels.rs

1#![allow(deprecated)]
2use super::{File, UserVoiceState};
3
4use revolt_permissions::{Override, OverrideField};
5use std::collections::{HashMap, HashSet};
6
7#[cfg(feature = "rocket")]
8use rocket::FromForm;
9
10auto_derived!(
11    /// Channel
12    #[serde(tag = "channel_type")]
13    pub enum Channel {
14        /// Personal "Saved Notes" channel which allows users to save messages
15        SavedMessages {
16            /// Unique Id
17            #[cfg_attr(feature = "serde", serde(rename = "_id"))]
18            id: String,
19            /// Id of the user this channel belongs to
20            user: String,
21        },
22        /// Direct message channel between two users
23        DirectMessage {
24            /// Unique Id
25            #[cfg_attr(feature = "serde", serde(rename = "_id"))]
26            id: String,
27
28            /// Whether this direct message channel is currently open on both sides
29            active: bool,
30            /// 2-tuple of user ids participating in direct message
31            recipients: Vec<String>,
32            /// Id of the last message sent in this channel
33            #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
34            last_message_id: Option<String>,
35        },
36        /// Group channel between 1 or more participants
37        Group {
38            /// Unique Id
39            #[cfg_attr(feature = "serde", serde(rename = "_id"))]
40            id: String,
41
42            /// Display name of the channel
43            name: String,
44            /// User id of the owner of the group
45            owner: String,
46            /// Channel description
47            #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
48            description: Option<String>,
49            /// Array of user ids participating in channel
50            recipients: Vec<String>,
51
52            /// Custom icon attachment
53            #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
54            icon: Option<File>,
55            /// Id of the last message sent in this channel
56            #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
57            last_message_id: Option<String>,
58
59            /// Permissions assigned to members of this group
60            /// (does not apply to the owner of the group)
61            #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
62            permissions: Option<i64>,
63
64            /// Whether this group is marked as not safe for work
65            #[cfg_attr(
66                feature = "serde",
67                serde(skip_serializing_if = "crate::if_false", default)
68            )]
69            nsfw: bool,
70        },
71        /// Text channel belonging to a server
72        TextChannel {
73            /// Unique Id
74            #[cfg_attr(feature = "serde", serde(rename = "_id"))]
75            id: String,
76            /// Id of the server this channel belongs to
77            server: String,
78
79            /// Display name of the channel
80            name: String,
81            /// Channel description
82            #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
83            description: Option<String>,
84
85            /// Custom icon attachment
86            #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
87            icon: Option<File>,
88            /// Id of the last message sent in this channel
89            #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
90            last_message_id: Option<String>,
91
92            /// Default permissions assigned to users in this channel
93            #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
94            default_permissions: Option<OverrideField>,
95            /// Permissions assigned based on role to this channel
96            #[cfg_attr(
97                feature = "serde",
98                serde(
99                    default = "HashMap::<String, OverrideField>::new",
100                    skip_serializing_if = "HashMap::<String, OverrideField>::is_empty"
101                )
102            )]
103            role_permissions: HashMap<String, OverrideField>,
104
105            /// Whether this channel is marked as not safe for work
106            #[cfg_attr(
107                feature = "serde",
108                serde(skip_serializing_if = "crate::if_false", default)
109            )]
110            nsfw: bool,
111
112            /// Voice Information for when this channel is also a voice channel
113            #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
114            voice: Option<VoiceInformation>,
115
116            /// The channel's slowmode delay in seconds
117            #[serde(skip_serializing_if = "Option::is_none")]
118            slowmode: Option<u64>,
119        },
120    }
121
122    /// Voice information for a channel
123    #[derive(Default)]
124    #[cfg_attr(feature = "validator", derive(validator::Validate))]
125    pub struct VoiceInformation {
126        /// Maximium amount of users allowed in the voice channel at once
127        #[cfg_attr(feature = "validator", validate(range(min = 1)))]
128        #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
129        pub max_users: Option<usize>,
130    }
131
132    /// Partial representation of a channel
133    #[derive(Default)]
134    pub struct PartialChannel {
135        #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
136        pub name: Option<String>,
137        #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
138        pub owner: Option<String>,
139        #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
140        pub description: Option<String>,
141        #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
142        pub icon: Option<File>,
143        #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
144        pub nsfw: Option<bool>,
145        #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
146        pub active: Option<bool>,
147        #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
148        pub permissions: Option<i64>,
149        #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
150        pub role_permissions: Option<HashMap<String, OverrideField>>,
151        #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
152        pub default_permissions: Option<OverrideField>,
153        #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
154        pub last_message_id: Option<String>,
155        #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
156        pub voice: Option<VoiceInformation>,
157        #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
158        pub slowmode: Option<u64>,
159    }
160
161    /// Optional fields on channel object
162    pub enum FieldsChannel {
163        Description,
164        Icon,
165        DefaultPermissions,
166        Voice,
167    }
168
169    /// New webhook information
170    #[cfg_attr(feature = "validator", derive(validator::Validate))]
171    pub struct DataEditChannel {
172        /// Channel name
173        #[cfg_attr(feature = "validator", validate(length(min = 1, max = 32)))]
174        pub name: Option<String>,
175
176        /// Channel description
177        #[cfg_attr(feature = "validator", validate(length(min = 0, max = 1024)))]
178        pub description: Option<String>,
179
180        /// Group owner
181        pub owner: Option<String>,
182
183        /// Icon
184        ///
185        /// Provide an Autumn attachment Id.
186        #[cfg_attr(feature = "validator", validate(length(min = 1, max = 128)))]
187        pub icon: Option<String>,
188
189        /// Whether this channel is age-restricted
190        pub nsfw: Option<bool>,
191
192        /// Whether this channel is archived
193        pub archived: Option<bool>,
194
195        /// Voice Information for voice channels
196        pub voice: Option<VoiceInformation>,
197
198        /// The channel's slow mode delay in seconds, up to 6 hours
199        #[cfg_attr(feature = "validator", validate(range(min = 0, max = 21600)))]
200        pub slowmode: Option<u64>,
201
202        /// Fields to remove from channel
203        #[cfg_attr(feature = "serde", serde(default))]
204        pub remove: Vec<FieldsChannel>,
205    }
206
207    /// Create new group
208    #[derive(Default)]
209    #[cfg_attr(feature = "validator", derive(validator::Validate))]
210    pub struct DataCreateGroup {
211        /// Group name
212        #[cfg_attr(feature = "validator", validate(length(min = 1, max = 32)))]
213        pub name: String,
214        /// Group description
215        #[cfg_attr(feature = "validator", validate(length(min = 0, max = 1024)))]
216        pub description: Option<String>,
217        /// Group icon
218        #[cfg_attr(feature = "validator", validate(length(min = 1, max = 128)))]
219        pub icon: Option<String>,
220        /// Array of user IDs to add to the group
221        ///
222        /// Must be friends with these users.
223        #[cfg_attr(feature = "validator", validate(length(min = 0, max = 49)))]
224        #[serde(default)]
225        pub users: HashSet<String>,
226        /// Whether this group is age-restricted
227        #[serde(skip_serializing_if = "Option::is_none")]
228        pub nsfw: Option<bool>,
229    }
230
231    /// Server Channel Type
232    #[derive(Default)]
233    pub enum LegacyServerChannelType {
234        /// Text Channel
235        #[default]
236        Text,
237        /// Voice Channel
238        Voice,
239    }
240
241    /// Create new server channel
242    #[derive(Default)]
243    #[cfg_attr(feature = "validator", derive(validator::Validate))]
244    pub struct DataCreateServerChannel {
245        /// Channel type
246        #[serde(rename = "type", default = "LegacyServerChannelType::default")]
247        pub channel_type: LegacyServerChannelType,
248        /// Channel name
249        #[cfg_attr(feature = "validator", validate(length(min = 1, max = 32)))]
250        pub name: String,
251        /// Channel description
252        #[cfg_attr(feature = "validator", validate(length(min = 0, max = 1024)))]
253        pub description: Option<String>,
254        /// Whether this channel is age restricted
255        #[serde(skip_serializing_if = "Option::is_none")]
256        pub nsfw: Option<bool>,
257
258        /// Voice Information for when this channel is also a voice channel
259        #[serde(skip_serializing_if = "Option::is_none")]
260        pub voice: Option<VoiceInformation>,
261    }
262
263    /// New default permissions
264    #[serde(untagged)]
265    pub enum DataDefaultChannelPermissions {
266        Value {
267            /// Permission values to set for members in a `Group`
268            permissions: u64,
269        },
270        Field {
271            /// Allow / deny values to set for members in this server channel
272            permissions: Override,
273        },
274    }
275
276    /// New role permissions
277    pub struct DataSetRolePermissions {
278        /// Allow / deny values to set for this role
279        pub permissions: Override,
280    }
281
282    /// Options when deleting a channel
283    #[cfg_attr(feature = "rocket", derive(FromForm))]
284    pub struct OptionsChannelDelete {
285        /// Whether to not send a leave message
286        pub leave_silently: Option<bool>,
287    }
288
289    /// Voice server token response
290    pub struct CreateVoiceUserResponse {
291        /// Token for authenticating with the voice server
292        pub token: String,
293        /// Url of the livekit server to connect to
294        pub url: String,
295    }
296
297    /// Voice state for a channel
298    pub struct ChannelVoiceState {
299        pub id: String,
300        /// The states of the users who are connected to the channel
301        pub participants: Vec<UserVoiceState>,
302    }
303
304    /// Join a voice channel
305    pub struct DataJoinCall {
306        /// Name of the node to join
307        pub node: Option<String>,
308        /// Whether to force disconnect any other existing voice connections
309        ///
310        /// Useful for disconnecting on another device and joining on a new.
311        pub force_disconnect: Option<bool>,
312        /// Users which should be notified of the call starting
313        ///
314        /// Only used when the user is the first one connected.
315        pub recipients: Option<Vec<String>>,
316    }
317
318    pub struct ChannelSlowmode {
319        pub channel_id: String,
320        pub duration: u64,
321        pub retry_after: u64,
322    }
323);
324
325impl Channel {
326    /// Get a reference to this channel's id
327    pub fn id(&self) -> &str {
328        match self {
329            Channel::DirectMessage { id, .. }
330            | Channel::Group { id, .. }
331            | Channel::SavedMessages { id, .. }
332            | Channel::TextChannel { id, .. } => id,
333        }
334    }
335
336    /// This returns a Result because the recipient name can't be determined here without a db call,
337    /// which can't be done since this is models, which can't reference the database crate.
338    ///
339    /// If it returns None, you need to fetch the name from the db.
340    pub fn name(&self) -> Option<&str> {
341        match self {
342            Channel::DirectMessage { .. } => None,
343            Channel::SavedMessages { .. } => Some("Saved Messages"),
344            Channel::TextChannel { name, .. } | Channel::Group { name, .. } => Some(name),
345        }
346    }
347}