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    "PartialServer"
83);
84
85auto_derived_partial!(
86    /// Role
87    pub struct Role {
88        /// Role name
89        pub name: String,
90        /// Permissions available to this role
91        pub permissions: OverrideField,
92        /// Colour used for this role
93        ///
94        /// This can be any valid CSS colour
95        #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
96        pub colour: Option<String>,
97        /// Whether this role should be shown separately on the member sidebar
98        #[cfg_attr(
99            feature = "serde",
100            serde(skip_serializing_if = "crate::if_false", default)
101        )]
102        pub hoist: bool,
103        /// Ranking of this role
104        #[cfg_attr(feature = "serde", serde(default))]
105        pub rank: i64,
106    },
107    "PartialRole"
108);
109
110auto_derived!(
111    /// Optional fields on server object
112    pub enum FieldsServer {
113        Description,
114        Categories,
115        SystemMessages,
116        Icon,
117        Banner,
118    }
119
120    /// Optional fields on server object
121    pub enum FieldsRole {
122        Colour,
123    }
124
125    /// Channel category
126    #[cfg_attr(feature = "validator", derive(Validate))]
127    pub struct Category {
128        /// Unique ID for this category
129        #[cfg_attr(feature = "validator", validate(length(min = 1, max = 32)))]
130        pub id: String,
131        /// Title for this category
132        #[cfg_attr(feature = "validator", validate(length(min = 1, max = 32)))]
133        pub title: String,
134        /// Channels in this category
135        pub channels: Vec<String>,
136    }
137
138    /// System message channel assignments
139    pub struct SystemMessageChannels {
140        /// ID of channel to send user join messages in
141        #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
142        pub user_joined: Option<String>,
143        /// ID of channel to send user left messages in
144        #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
145        pub user_left: Option<String>,
146        /// ID of channel to send user kicked messages in
147        #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
148        pub user_kicked: Option<String>,
149        /// ID of channel to send user banned messages in
150        #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
151        pub user_banned: Option<String>,
152    }
153
154    /// Information about new server to create
155    #[derive(Default)]
156    #[cfg_attr(feature = "validator", derive(Validate))]
157    pub struct DataCreateServer {
158        /// Server name
159        #[cfg_attr(feature = "validator", validate(length(min = 1, max = 32)))]
160        pub name: String,
161        /// Server description
162        #[cfg_attr(feature = "validator", validate(length(min = 0, max = 1024)))]
163        pub description: Option<String>,
164        /// Whether this server is age-restricted
165        #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
166        pub nsfw: Option<bool>,
167    }
168
169    /// Information about new role to create
170    #[cfg_attr(feature = "validator", derive(Validate))]
171    pub struct DataCreateRole {
172        /// Role name
173        #[cfg_attr(feature = "validator", validate(length(min = 1, max = 32)))]
174        pub name: String,
175        /// Ranking position
176        ///
177        /// Smaller values take priority.
178        pub rank: Option<i64>,
179    }
180
181    /// Response after creating new role
182    pub struct NewRoleResponse {
183        /// Id of the role
184        pub id: String,
185        /// New role
186        pub role: Role,
187    }
188
189    /// Information returned when creating server
190    pub struct CreateServerLegacyResponse {
191        /// Server object
192        pub server: Server,
193        /// Default channels
194        pub channels: Vec<Channel>,
195    }
196
197    /// Options when fetching server
198    #[cfg_attr(feature = "rocket", derive(FromForm))]
199    pub struct OptionsFetchServer {
200        /// Whether to include channels
201        pub include_channels: Option<bool>,
202    }
203
204    /// Fetch server information
205    #[serde(untagged)]
206    pub enum FetchServerResponse {
207        JustServer(Server),
208        ServerWithChannels {
209            #[serde(flatten)]
210            server: Server,
211            channels: Vec<Channel>,
212        },
213    }
214
215    /// New server information
216    #[cfg_attr(feature = "validator", derive(Validate))]
217    pub struct DataEditServer {
218        /// Server name
219        #[cfg_attr(feature = "validator", validate(length(min = 1, max = 32)))]
220        pub name: Option<String>,
221        /// Server description
222        #[cfg_attr(feature = "validator", validate(length(min = 0, max = 1024)))]
223        pub description: Option<String>,
224
225        /// Attachment Id for icon
226        pub icon: Option<String>,
227        /// Attachment Id for banner
228        pub banner: Option<String>,
229
230        /// Category structure for server
231        #[cfg_attr(feature = "validator", validate)]
232        pub categories: Option<Vec<Category>>,
233        /// System message configuration
234        pub system_messages: Option<SystemMessageChannels>,
235
236        /// Bitfield of server flags
237        #[cfg_attr(feature = "validator", serde(skip_serializing_if = "Option::is_none"))]
238        pub flags: Option<i32>,
239
240        // Whether this server is age-restricted
241        // nsfw: Option<bool>,
242        /// Whether this server is public and should show up on [Revolt Discover](https://rvlt.gg)
243        pub discoverable: Option<bool>,
244        /// Whether analytics should be collected for this server
245        ///
246        /// Must be enabled in order to show up on [Revolt Discover](https://rvlt.gg).
247        pub analytics: Option<bool>,
248
249        /// Fields to remove from server object
250        #[cfg_attr(feature = "validator", validate(length(min = 1)))]
251        pub remove: Option<Vec<FieldsServer>>,
252    }
253
254    /// New role information
255    #[cfg_attr(feature = "validator", derive(Validate))]
256    pub struct DataEditRole {
257        /// Role name
258        #[cfg_attr(feature = "validator", validate(length(min = 1, max = 32)))]
259        pub name: Option<String>,
260        /// Role colour
261        #[cfg_attr(
262            feature = "validator",
263            validate(length(min = 1, max = 128), regex = "RE_COLOUR")
264        )]
265        pub colour: Option<String>,
266        /// Whether this role should be displayed separately
267        pub hoist: Option<bool>,
268        /// Ranking position
269        ///
270        /// **Removed** - no effect, use the edit server role positions route
271        pub rank: Option<i64>,
272        /// Fields to remove from role object
273        #[cfg_attr(feature = "validator", validate(length(min = 1)))]
274        pub remove: Option<Vec<FieldsRole>>,
275    }
276
277    /// New role permissions
278    pub struct DataSetServerRolePermission {
279        /// Allow / deny values for the role in this server.
280        pub permissions: Override,
281    }
282
283    /// Options when leaving a server
284    #[cfg_attr(feature = "rocket", derive(FromForm))]
285    pub struct OptionsServerDelete {
286        /// Whether to not send a leave message
287        pub leave_silently: Option<bool>,
288    }
289
290    /// New role positions
291    pub struct DataEditRoleRanks {
292        pub ranks: Vec<String>,
293    }
294);