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    }
117
118    /// Voice information for a channel
119    #[derive(Default)]
120    #[cfg_attr(feature = "validator", derive(validator::Validate))]
121    pub struct VoiceInformation {
122        /// Maximium amount of users allowed in the voice channel at once
123        #[cfg_attr(feature = "validator", validate(range(min = 1)))]
124        #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
125        pub max_users: Option<usize>,
126    }
127
128    /// Partial representation of a channel
129    #[derive(Default)]
130    pub struct PartialChannel {
131        #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
132        pub name: Option<String>,
133        #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
134        pub owner: Option<String>,
135        #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
136        pub description: Option<String>,
137        #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
138        pub icon: Option<File>,
139        #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
140        pub nsfw: Option<bool>,
141        #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
142        pub active: Option<bool>,
143        #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
144        pub permissions: Option<i64>,
145        #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
146        pub role_permissions: Option<HashMap<String, OverrideField>>,
147        #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
148        pub default_permissions: Option<OverrideField>,
149        #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
150        pub last_message_id: Option<String>,
151        #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
152        pub voice: Option<VoiceInformation>,
153    }
154
155    /// Optional fields on channel object
156    pub enum FieldsChannel {
157        Description,
158        Icon,
159        DefaultPermissions,
160        Voice,
161    }
162
163    /// New webhook information
164    #[cfg_attr(feature = "validator", derive(validator::Validate))]
165    pub struct DataEditChannel {
166        /// Channel name
167        #[cfg_attr(feature = "validator", validate(length(min = 1, max = 32)))]
168        pub name: Option<String>,
169
170        /// Channel description
171        #[cfg_attr(feature = "validator", validate(length(min = 0, max = 1024)))]
172        pub description: Option<String>,
173
174        /// Group owner
175        pub owner: Option<String>,
176
177        /// Icon
178        ///
179        /// Provide an Autumn attachment Id.
180        #[cfg_attr(feature = "validator", validate(length(min = 1, max = 128)))]
181        pub icon: Option<String>,
182
183        /// Whether this channel is age-restricted
184        pub nsfw: Option<bool>,
185
186        /// Whether this channel is archived
187        pub archived: Option<bool>,
188
189        /// Voice Information for voice channels
190        pub voice: Option<VoiceInformation>,
191
192        /// Fields to remove from channel
193        #[cfg_attr(feature = "serde", serde(default))]
194        pub remove: Vec<FieldsChannel>,
195    }
196
197    /// Create new group
198    #[derive(Default)]
199    #[cfg_attr(feature = "validator", derive(validator::Validate))]
200    pub struct DataCreateGroup {
201        /// Group name
202        #[cfg_attr(feature = "validator", validate(length(min = 1, max = 32)))]
203        pub name: String,
204        /// Group description
205        #[cfg_attr(feature = "validator", validate(length(min = 0, max = 1024)))]
206        pub description: Option<String>,
207        /// Group icon
208        #[cfg_attr(feature = "validator", validate(length(min = 1, max = 128)))]
209        pub icon: Option<String>,
210        /// Array of user IDs to add to the group
211        ///
212        /// Must be friends with these users.
213        #[cfg_attr(feature = "validator", validate(length(min = 0, max = 49)))]
214        #[serde(default)]
215        pub users: HashSet<String>,
216        /// Whether this group is age-restricted
217        #[serde(skip_serializing_if = "Option::is_none")]
218        pub nsfw: Option<bool>,
219    }
220
221    /// Server Channel Type
222    #[derive(Default)]
223    pub enum LegacyServerChannelType {
224        /// Text Channel
225        #[default]
226        Text,
227        /// Voice Channel
228        Voice,
229    }
230
231    /// Create new server channel
232    #[derive(Default)]
233    #[cfg_attr(feature = "validator", derive(validator::Validate))]
234    pub struct DataCreateServerChannel {
235        /// Channel type
236        #[serde(rename = "type", default = "LegacyServerChannelType::default")]
237        pub channel_type: LegacyServerChannelType,
238        /// Channel name
239        #[cfg_attr(feature = "validator", validate(length(min = 1, max = 32)))]
240        pub name: String,
241        /// Channel description
242        #[cfg_attr(feature = "validator", validate(length(min = 0, max = 1024)))]
243        pub description: Option<String>,
244        /// Whether this channel is age restricted
245        #[serde(skip_serializing_if = "Option::is_none")]
246        pub nsfw: Option<bool>,
247
248        /// Voice Information for when this channel is also a voice channel
249        #[serde(skip_serializing_if = "Option::is_none")]
250        pub voice: Option<VoiceInformation>,
251    }
252
253    /// New default permissions
254    #[serde(untagged)]
255    pub enum DataDefaultChannelPermissions {
256        Value {
257            /// Permission values to set for members in a `Group`
258            permissions: u64,
259        },
260        Field {
261            /// Allow / deny values to set for members in this server channel
262            permissions: Override,
263        },
264    }
265
266    /// New role permissions
267    pub struct DataSetRolePermissions {
268        /// Allow / deny values to set for this role
269        pub permissions: Override,
270    }
271
272    /// Options when deleting a channel
273    #[cfg_attr(feature = "rocket", derive(FromForm))]
274    pub struct OptionsChannelDelete {
275        /// Whether to not send a leave message
276        pub leave_silently: Option<bool>,
277    }
278
279    /// Voice server token response
280    pub struct CreateVoiceUserResponse {
281        /// Token for authenticating with the voice server
282        pub token: String,
283        /// Url of the livekit server to connect to
284        pub url: String,
285    }
286
287    /// Voice state for a channel
288    pub struct ChannelVoiceState {
289        pub id: String,
290        /// The states of the users who are connected to the channel
291        pub participants: Vec<UserVoiceState>,
292    }
293
294    /// Join a voice channel
295    pub struct DataJoinCall {
296        /// Name of the node to join
297        pub node: Option<String>,
298        /// Whether to force disconnect any other existing voice connections
299        ///
300        /// Useful for disconnecting on another device and joining on a new.
301        pub force_disconnect: Option<bool>,
302        /// Users which should be notified of the call starting
303        ///
304        /// Only used when the user is the first one connected.
305        pub recipients: Option<Vec<String>>,
306    }
307);
308
309impl Channel {
310    /// Get a reference to this channel's id
311    pub fn id(&self) -> &str {
312        match self {
313            Channel::DirectMessage { id, .. }
314            | Channel::Group { id, .. }
315            | Channel::SavedMessages { id, .. }
316            | Channel::TextChannel { id, .. } => id,
317        }
318    }
319
320    /// This returns a Result because the recipient name can't be determined here without a db call,
321    /// which can't be done since this is models, which can't reference the database crate.
322    ///
323    /// If it returns None, you need to fetch the name from the db.
324    pub fn name(&self) -> Option<&str> {
325        match self {
326            Channel::DirectMessage { .. } => None,
327            Channel::SavedMessages { .. } => Some("Saved Messages"),
328            Channel::TextChannel { name, .. } | Channel::Group { name, .. } => Some(name),
329        }
330    }
331}