Skip to main content

revolt_models/v0/
servers.rs

1use super::{Channel, File, RE_COLOUR};
2
3use revolt_permissions::{Override, OverrideField};
4use std::collections::HashMap;
5
6#[cfg(feature = "validator")]
7use validator::Validate;
8
9#[cfg(feature = "rocket")]
10use rocket::FromForm;
11
12auto_derived_partial!(
13    /// Server
14    pub struct Server {
15        /// Unique Id
16        #[cfg_attr(feature = "serde", serde(rename = "_id"))]
17        pub id: String,
18        /// User id of the owner
19        pub owner: String,
20
21        /// Name of the server
22        pub name: String,
23        /// Description for the server
24        #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
25        pub description: Option<String>,
26
27        /// Channels within this server
28        // TODO: investigate if this is redundant and can be removed
29        pub channels: Vec<String>,
30        /// Categories for this server
31        #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
32        pub categories: Option<Vec<Category>>,
33        /// Configuration for sending system event messages
34        #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
35        pub system_messages: Option<SystemMessageChannels>,
36
37        /// Roles for this server
38        #[cfg_attr(
39            feature = "serde",
40            serde(
41                default = "HashMap::<String, Role>::new",
42                skip_serializing_if = "HashMap::<String, Role>::is_empty"
43            )
44        )]
45        pub roles: HashMap<String, Role>,
46        /// Default set of server and channel permissions
47        pub default_permissions: i64,
48
49        /// Icon attachment
50        #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
51        pub icon: Option<File>,
52        /// Banner attachment
53        #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
54        pub banner: Option<File>,
55
56        /// Bitfield of server flags
57        #[cfg_attr(
58            feature = "serde",
59            serde(skip_serializing_if = "crate::if_zero_u32", default)
60        )]
61        pub flags: u32,
62
63        /// Whether this server is flagged as not safe for work
64        #[cfg_attr(
65            feature = "serde",
66            serde(skip_serializing_if = "crate::if_false", default)
67        )]
68        pub nsfw: bool,
69        /// Whether to enable analytics
70        #[cfg_attr(
71            feature = "serde",
72            serde(skip_serializing_if = "crate::if_false", default)
73        )]
74        pub analytics: bool,
75        /// Whether this server should be publicly discoverable
76        #[cfg_attr(
77            feature = "serde",
78            serde(skip_serializing_if = "crate::if_false", default)
79        )]
80        pub discoverable: bool,
81
82        /// Approximate amount of members in the server
83        pub approximate_member_count: usize,
84    },
85    "PartialServer"
86);
87
88auto_derived_partial!(
89    /// Role
90    pub struct Role {
91        /// Unique Id
92        #[cfg_attr(feature = "serde", serde(rename = "_id"))]
93        pub id: String,
94        /// Role name
95        pub name: String,
96        /// Permissions available to this role
97        pub permissions: OverrideField,
98        /// Colour used for this role
99        ///
100        /// This can be any valid CSS colour
101        #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
102        pub colour: Option<String>,
103        /// Whether this role should be shown separately on the member sidebar
104        #[cfg_attr(
105            feature = "serde",
106            serde(skip_serializing_if = "crate::if_false", default)
107        )]
108        pub hoist: bool,
109        /// Ranking of this role
110        #[cfg_attr(feature = "serde", serde(default))]
111        pub rank: i64,
112        /// Role icon
113        #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
114        pub icon: Option<File>,
115    },
116    "PartialRole"
117);
118
119auto_derived!(
120    /// Optional fields on server object
121    pub enum FieldsServer {
122        Description,
123        Categories,
124        SystemMessages,
125        Icon,
126        Banner,
127    }
128
129    /// Optional fields on server object
130    pub enum FieldsRole {
131        Colour,
132        Icon,
133    }
134
135    /// Channel category
136    #[cfg_attr(feature = "validator", derive(Validate))]
137    pub struct Category {
138        /// Unique ID for this category
139        #[cfg_attr(feature = "validator", validate(length(min = 1, max = 32)))]
140        pub id: String,
141        /// Title for this category
142        #[cfg_attr(feature = "validator", validate(length(min = 1, max = 32)))]
143        pub title: String,
144        /// Channels in this category
145        pub channels: Vec<String>,
146    }
147
148    /// System message channel assignments
149    pub struct SystemMessageChannels {
150        /// ID of channel to send user join messages in
151        #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
152        pub user_joined: Option<String>,
153        /// ID of channel to send user left messages in
154        #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
155        pub user_left: Option<String>,
156        /// ID of channel to send user kicked messages in
157        #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
158        pub user_kicked: Option<String>,
159        /// ID of channel to send user banned messages in
160        #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
161        pub user_banned: Option<String>,
162    }
163
164    /// Information about new server to create
165    #[derive(Default)]
166    #[cfg_attr(feature = "validator", derive(Validate))]
167    pub struct DataCreateServer {
168        /// Server name
169        #[cfg_attr(feature = "validator", validate(length(min = 1, max = 32)))]
170        pub name: String,
171        /// Server description
172        #[cfg_attr(feature = "validator", validate(length(min = 0, max = 1024)))]
173        pub description: Option<String>,
174        /// Whether this server is age-restricted
175        #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
176        pub nsfw: Option<bool>,
177    }
178
179    /// Information about new role to create
180    #[cfg_attr(feature = "validator", derive(Validate))]
181    pub struct DataCreateRole {
182        /// Role name
183        #[cfg_attr(feature = "validator", validate(length(min = 1, max = 32)))]
184        pub name: String,
185        /// Ranking position
186        ///
187        /// Smaller values take priority.
188        ///
189        /// **Removed** - no effect, use the edit server role positions route
190        pub rank: Option<i64>,
191    }
192
193    /// Response after creating new role
194    // TODO: remove this in favor of just Role
195    pub struct NewRoleResponse {
196        /// Id of the role
197        pub id: String,
198        /// New role
199        pub role: Role,
200    }
201
202    /// Information returned when creating server
203    pub struct CreateServerLegacyResponse {
204        /// Server object
205        pub server: Server,
206        /// Default channels
207        pub channels: Vec<Channel>,
208    }
209
210    /// Options when fetching server
211    #[cfg_attr(feature = "rocket", derive(FromForm))]
212    pub struct OptionsFetchServer {
213        /// Whether to include channels
214        pub include_channels: Option<bool>,
215    }
216
217    /// Fetch server information
218    #[serde(untagged)]
219    pub enum FetchServerResponse {
220        JustServer(Server),
221        ServerWithChannels {
222            #[serde(flatten)]
223            server: Server,
224            channels: Vec<Channel>,
225        },
226    }
227
228    /// New server information
229    #[cfg_attr(feature = "validator", derive(Validate))]
230    pub struct DataEditServer {
231        /// Server name
232        #[cfg_attr(feature = "validator", validate(length(min = 1, max = 32)))]
233        pub name: Option<String>,
234        /// Server description
235        #[cfg_attr(feature = "validator", validate(length(min = 0, max = 1024)))]
236        pub description: Option<String>,
237
238        /// Attachment Id for icon
239        pub icon: Option<String>,
240        /// Attachment Id for banner
241        pub banner: Option<String>,
242
243        /// Category structure for server
244        #[cfg_attr(feature = "validator", validate)]
245        pub categories: Option<Vec<Category>>,
246        /// System message configuration
247        pub system_messages: Option<SystemMessageChannels>,
248
249        /// Bitfield of server flags
250        #[cfg_attr(feature = "validator", serde(skip_serializing_if = "Option::is_none"))]
251        pub flags: Option<i32>,
252
253        // Whether this server is age-restricted
254        // nsfw: Option<bool>,
255        /// Whether this server is public and should show up on [Revolt Discover](https://rvlt.gg)
256        pub discoverable: Option<bool>,
257        /// Whether analytics should be collected for this server
258        ///
259        /// Must be enabled in order to show up on [Revolt Discover](https://rvlt.gg).
260        pub analytics: Option<bool>,
261
262        /// User id of the new owner
263        pub owner: Option<String>,
264
265        /// Fields to remove from server object
266        #[cfg_attr(feature = "serde", serde(default))]
267        pub remove: Vec<FieldsServer>,
268    }
269
270    /// New role information
271    #[cfg_attr(feature = "validator", derive(Validate))]
272    pub struct DataEditRole {
273        /// Role name
274        #[cfg_attr(feature = "validator", validate(length(min = 1, max = 32)))]
275        pub name: Option<String>,
276        /// Role colour
277        #[cfg_attr(
278            feature = "validator",
279            validate(length(min = 1, max = 128), regex = "RE_COLOUR")
280        )]
281        pub colour: Option<String>,
282        /// Whether this role should be displayed separately
283        pub hoist: Option<bool>,
284        /// Ranking position
285        ///
286        /// **Removed** - no effect, use the edit server role positions route
287        pub rank: Option<i64>,
288        /// Role icon
289        ///
290        /// Provide an Autumn attachment Id.
291        #[cfg_attr(feature = "validator", validate(length(min = 1, max = 128)))]
292        pub icon: Option<String>,
293        /// Fields to remove from role object
294        #[cfg_attr(feature = "serde", serde(default))]
295        pub remove: Vec<FieldsRole>,
296    }
297
298    /// New role permissions
299    pub struct DataSetServerRolePermission {
300        /// Allow / deny values for the role in this server.
301        pub permissions: Override,
302    }
303
304    /// Options when leaving a server
305    #[cfg_attr(feature = "rocket", derive(FromForm))]
306    pub struct OptionsServerDelete {
307        /// Whether to not send a leave message
308        pub leave_silently: Option<bool>,
309    }
310
311    /// New role positions
312    pub struct DataEditRoleRanks {
313        pub ranks: Vec<String>,
314    }
315);