revolt_models/v0/
channels.rs

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