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        Slowmode,
168    }
169
170    /// New webhook information
171    #[cfg_attr(feature = "validator", derive(validator::Validate))]
172    pub struct DataEditChannel {
173        /// Channel name
174        #[cfg_attr(feature = "validator", validate(length(min = 1, max = 32)))]
175        pub name: Option<String>,
176
177        /// Channel description
178        #[cfg_attr(feature = "validator", validate(length(min = 0, max = 1024)))]
179        pub description: Option<String>,
180
181        /// Group owner
182        pub owner: Option<String>,
183
184        /// Icon
185        ///
186        /// Provide an Autumn attachment Id.
187        #[cfg_attr(feature = "validator", validate(length(min = 1, max = 128)))]
188        pub icon: Option<String>,
189
190        /// Whether this channel is age-restricted
191        pub nsfw: Option<bool>,
192
193        /// Whether this channel is archived
194        pub archived: Option<bool>,
195
196        /// Voice Information for voice channels
197        pub voice: Option<VoiceInformation>,
198
199        /// The channel's slow mode delay in seconds, up to 6 hours
200        #[cfg_attr(feature = "validator", validate(range(min = 0, max = 21600)))]
201        pub slowmode: Option<u64>,
202
203        /// Fields to remove from channel
204        #[cfg_attr(feature = "serde", serde(default))]
205        pub remove: Vec<FieldsChannel>,
206    }
207
208    /// Create new group
209    #[derive(Default)]
210    #[cfg_attr(feature = "validator", derive(validator::Validate))]
211    pub struct DataCreateGroup {
212        /// Group name
213        #[cfg_attr(feature = "validator", validate(length(min = 1, max = 32)))]
214        pub name: String,
215        /// Group description
216        #[cfg_attr(feature = "validator", validate(length(min = 0, max = 1024)))]
217        pub description: Option<String>,
218        /// Group icon
219        #[cfg_attr(feature = "validator", validate(length(min = 1, max = 128)))]
220        pub icon: Option<String>,
221        /// Array of user IDs to add to the group
222        ///
223        /// Must be friends with these users.
224        #[cfg_attr(feature = "validator", validate(length(min = 0, max = 49)))]
225        #[serde(default)]
226        pub users: HashSet<String>,
227        /// Whether this group is age-restricted
228        #[serde(skip_serializing_if = "Option::is_none")]
229        pub nsfw: Option<bool>,
230    }
231
232    /// Server Channel Type
233    #[derive(Default)]
234    pub enum LegacyServerChannelType {
235        /// Text Channel
236        #[default]
237        Text,
238        /// Voice Channel
239        Voice,
240    }
241
242    /// Create new server channel
243    #[derive(Default)]
244    #[cfg_attr(feature = "validator", derive(validator::Validate))]
245    pub struct DataCreateServerChannel {
246        /// Channel type
247        #[serde(rename = "type", default = "LegacyServerChannelType::default")]
248        pub channel_type: LegacyServerChannelType,
249        /// Channel name
250        #[cfg_attr(feature = "validator", validate(length(min = 1, max = 32)))]
251        pub name: String,
252        /// Channel description
253        #[cfg_attr(feature = "validator", validate(length(min = 0, max = 1024)))]
254        pub description: Option<String>,
255        /// Whether this channel is age restricted
256        #[serde(skip_serializing_if = "Option::is_none")]
257        pub nsfw: Option<bool>,
258
259        /// Voice Information for when this channel is also a voice channel
260        #[serde(skip_serializing_if = "Option::is_none")]
261        pub voice: Option<VoiceInformation>,
262    }
263
264    /// New default permissions
265    #[serde(untagged)]
266    pub enum DataDefaultChannelPermissions {
267        Value {
268            /// Permission values to set for members in a `Group`
269            permissions: u64,
270        },
271        Field {
272            /// Allow / deny values to set for members in this server channel
273            permissions: Override,
274        },
275    }
276
277    /// New role permissions
278    pub struct DataSetRolePermissions {
279        /// Allow / deny values to set for this role
280        pub permissions: Override,
281    }
282
283    /// Options when deleting a channel
284    #[cfg_attr(feature = "rocket", derive(FromForm))]
285    pub struct OptionsChannelDelete {
286        /// Whether to not send a leave message
287        pub leave_silently: Option<bool>,
288    }
289
290    /// Voice server token response
291    pub struct CreateVoiceUserResponse {
292        /// Token for authenticating with the voice server
293        pub token: String,
294        /// Url of the livekit server to connect to
295        pub url: String,
296    }
297
298    /// Voice state for a channel
299    pub struct ChannelVoiceState {
300        pub id: String,
301        /// The states of the users who are connected to the channel
302        pub participants: Vec<UserVoiceState>,
303    }
304
305    /// Join a voice channel
306    pub struct DataJoinCall {
307        /// Name of the node to join
308        pub node: Option<String>,
309        /// Whether to force disconnect any other existing voice connections
310        ///
311        /// Useful for disconnecting on another device and joining on a new.
312        pub force_disconnect: Option<bool>,
313        /// Users which should be notified of the call starting
314        ///
315        /// Only used when the user is the first one connected.
316        pub recipients: Option<Vec<String>>,
317    }
318
319    pub struct ChannelSlowmode {
320        pub channel_id: String,
321        pub duration: u64,
322        pub retry_after: u64,
323    }
324);
325
326impl Channel {
327    /// Get a reference to this channel's id
328    pub fn id(&self) -> &str {
329        match self {
330            Channel::DirectMessage { id, .. }
331            | Channel::Group { id, .. }
332            | Channel::SavedMessages { id, .. }
333            | Channel::TextChannel { id, .. } => id,
334        }
335    }
336
337    /// This returns a Result because the recipient name can't be determined here without a db call,
338    /// which can't be done since this is models, which can't reference the database crate.
339    ///
340    /// If it returns None, you need to fetch the name from the db.
341    pub fn name(&self) -> Option<&str> {
342        match self {
343            Channel::DirectMessage { .. } => None,
344            Channel::SavedMessages { .. } => Some("Saved Messages"),
345            Channel::TextChannel { name, .. } | Channel::Group { name, .. } => Some(name),
346        }
347    }
348}