essence 0.7.0

Essential models and database logic for the Adapt chat platform.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
use crate::models::ExtendedColor;
use crate::{
    models::{Message, PermissionPair},
    Error,
};
use serde::{Deserialize, Serialize};
use std::str::FromStr;
#[cfg(feature = "utoipa")]
use utoipa::{
    openapi::{Array, ArrayBuilder, KnownFormat, ObjectBuilder, SchemaFormat, SchemaType},
    ToSchema,
};

/// Either a fully resolved message or just its ID.
#[derive(Clone, Debug, Serialize)]
#[cfg_attr(feature = "client", derive(Deserialize))]
#[cfg_attr(feature = "utoipa", derive(ToSchema))]
#[cfg_attr(feature = "bincode", derive(bincode::Encode, bincode::Decode))]
#[serde(untagged)]
#[allow(clippy::large_enum_variant)]
pub enum MaybePartialMessage {
    /// The full message.
    Full(Message),
    /// Just the ID of the message.
    Id { id: u64 },
}

/// Represents common information found in text-based guild channels.
#[derive(Clone, Debug, Default, Serialize)]
#[cfg_attr(feature = "client", derive(Deserialize))]
#[cfg_attr(feature = "utoipa", derive(ToSchema))]
#[cfg_attr(feature = "bincode", derive(bincode::Encode, bincode::Decode))]
pub struct TextBasedGuildChannelInfo {
    /// The topic of the channel, if any.
    pub topic: Option<String>,
    /// Whether the channel is NSFW.
    pub nsfw: bool,
    /// Whether the channel is locked. Only people with the `MANAGE_CHANNELS` permission can
    /// send messages in locked channels.
    pub locked: bool,
    /// The slowmode delay of the channel, in **milliseconds**. This should be a value between
    /// `0` and `86_400_000` (24 hours). `0` indicates the absence of slowmode.
    pub slowmode: u32,
    /// The ID of the last message sent in this channel. This is `None` if no messages have been
    /// sent in this channel, and is sometimes always none in partial contexts.
    pub last_message: Option<Message>,
}

/// An intermediate representation of a channel's type. This is never used directly, but is used
/// to help deserialization.
#[derive(Copy, Clone, Debug, Deserialize, PartialEq, Eq)]
#[cfg_attr(feature = "client", derive(Serialize))]
#[cfg_attr(feature = "utoipa", derive(ToSchema))]
#[cfg_attr(feature = "bincode", derive(bincode::Encode, bincode::Decode))]
#[serde(rename_all = "snake_case")]
pub enum ChannelType {
    /// A text channel.
    Text,
    /// An announcement channel.
    Announcement,
    /// A voice channel.
    Voice,
    /// A category of channels.
    Category,
    /// Two or more channels merged together.
    Merged,
    /// A standard DM channel.
    Dm,
    /// A group DM channel.
    Group,
}

impl FromStr for ChannelType {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "text" => Ok(Self::Text),
            "announcement" => Ok(Self::Announcement),
            "voice" => Ok(Self::Voice),
            "category" => Ok(Self::Category),
            "merged" => Ok(Self::Merged),
            "dm" => Ok(Self::Dm),
            "group" => Ok(Self::Group),
            _ => Err(Error::InternalError {
                what: None,
                message: "Database returned invalid channel type".to_string(),
                debug: None,
            }),
        }
    }
}

impl ChannelType {
    /// Returns the channel type's name.
    #[inline]
    #[must_use]
    pub const fn name(&self) -> &'static str {
        match self {
            Self::Text => "text",
            Self::Announcement => "announcement",
            Self::Voice => "voice",
            Self::Category => "category",
            Self::Merged => "merged",
            Self::Dm => "dm",
            Self::Group => "group",
        }
    }

    /// Returns whether the channel type is a text-based channel in a guild.
    #[inline]
    #[must_use]
    pub const fn is_guild_text_based(&self) -> bool {
        matches!(self, Self::Text | Self::Announcement)
    }

    /// Returns whether the channel type is a text-based channel.
    #[inline]
    #[must_use]
    pub const fn is_text_based(&self) -> bool {
        self.is_guild_text_based() || self.is_dm()
    }

    /// Returns whether the channel type is a guild channel.
    #[inline]
    #[must_use]
    pub const fn is_guild(&self) -> bool {
        matches!(
            self,
            Self::Text | Self::Announcement | Self::Voice | Self::Category
        )
    }

    /// Returns whether the channel type is a DM channel.
    #[inline]
    #[must_use]
    pub const fn is_dm(&self) -> bool {
        matches!(self, Self::Dm | Self::Group)
    }
}

/// Represents the type along with type-specific info of a guild channel.
#[derive(Clone, Debug, Serialize)]
#[cfg_attr(feature = "client", derive(Deserialize))]
#[cfg_attr(feature = "utoipa", derive(ToSchema))]
#[cfg_attr(feature = "bincode", derive(bincode::Encode, bincode::Decode))]
#[serde(tag = "type")]
#[serde(rename_all = "snake_case")]
pub enum GuildChannelInfo {
    /// A normal text channel.
    Text(TextBasedGuildChannelInfo),
    /// A text channel that has an announcement feed that can be subscribed to.
    Announcement(TextBasedGuildChannelInfo),
    /// A voice channel.
    Voice {
        /// The user limit of the channel. This should be a value between `0` and `500`. A value
        /// of `0` indicates the absence of a user limit.
        user_limit: u16,
    },
    /// A category of channels. This isn't really a channel, but it shares many of the same
    /// properties of one.
    Category,
    /// Two or more channels merged together.
    Merged(TextBasedGuildChannelInfo),
}

impl GuildChannelInfo {
    /// Returns the [`ChannelType`] of the channel.
    #[inline]
    #[must_use]
    pub const fn channel_type(&self) -> ChannelType {
        match self {
            Self::Text { .. } => ChannelType::Text,
            Self::Announcement { .. } => ChannelType::Announcement,
            Self::Voice { .. } => ChannelType::Voice,
            Self::Category => ChannelType::Category,
            Self::Merged { .. } => ChannelType::Merged,
        }
    }
}

/// Represents a permission overwrite.
#[derive(Clone, Debug, Deserialize, Serialize)]
#[cfg_attr(feature = "utoipa", derive(ToSchema))]
#[cfg_attr(feature = "bincode", derive(bincode::Encode, bincode::Decode))]
pub struct PermissionOverwrite {
    /// The ID of the role or user this overwrite applies to. The model type can be extracted from
    /// the ID.
    pub id: u64,
    /// The permissions this overwrite grants or denies.
    #[serde(flatten)]
    #[cfg_attr(feature = "bincode", bincode(with_serde))]
    pub permissions: PermissionPair,
}

/// Represents a channel in a guild.
#[derive(Clone, Debug, Serialize)]
#[cfg_attr(feature = "client", derive(Deserialize))]
#[cfg_attr(feature = "utoipa", derive(ToSchema))]
#[cfg_attr(feature = "bincode", derive(bincode::Encode, bincode::Decode))]
pub struct GuildChannel {
    /// The ID of the channel.
    pub id: u64,
    /// The ID of the guild that this channel is in.
    pub guild_id: u64,
    /// Information about the channel.
    #[serde(flatten)]
    pub info: GuildChannelInfo,
    /// The name of the channel.
    pub name: String,
    /// The accent color of the channel, used in the UI, or ``None`` if the channel has no color.
    pub color: Option<ExtendedColor>,
    /// The URL to the icon of the channel, if any.
    pub icon: Option<String>,
    /// The position of the channel in the channel list. A lower value means appearing "higher" in
    /// the UI, basically think of this as a 0-indexed listing of the channels from top-to-bottom.
    ///
    /// Positions are scoped per category, and categories have their own positions. Channels that
    /// lack a category will be shown above all categories. This is because no channels can be
    /// displayed in between or after categories - in the UI all non-category channels are displayed
    /// above any other category channels.
    ///
    /// For example:
    ///
    /// ```text
    /// [0] text-channel
    /// [1] voice-channel
    /// [2] another-text-channel
    /// [0] Category
    ///   [0] another-text-channel
    ///   [1] another-voice-channel
    ///   [0] Another Category
    ///     [1] nested-voice-channel
    ///     [2] nested-voice-channel-2
    /// [1] Yet Another Category
    ///   [0] another-text-channel
    /// ```
    pub position: u16,
    /// The permission overwrites for this channel.
    pub overwrites: Vec<PermissionOverwrite>,
    /// The ID of the parent category of the channel. This is `None` if the channel is not in a
    /// category. This is also used for merged channels.
    pub parent_id: Option<u64>,
}

impl Default for GuildChannel {
    fn default() -> Self {
        Self {
            id: 0,
            guild_id: 0,
            info: GuildChannelInfo::Text(TextBasedGuildChannelInfo::default()),
            name: "general".to_string(),
            color: None,
            icon: None,
            position: 0,
            overwrites: Vec::new(),
            parent_id: None,
        }
    }
}

#[cfg(feature = "utoipa")]
fn tuple_u64_u64() -> Array {
    ArrayBuilder::new()
        .items(
            ObjectBuilder::new()
                .schema_type(SchemaType::Integer)
                .format(Some(SchemaFormat::KnownFormat(KnownFormat::Int64))),
        )
        .min_items(Some(2))
        .max_items(Some(2))
        .build()
}

/// Represents extra information associated with DM channels.
#[derive(Clone, Debug, Deserialize, Serialize)]
#[cfg_attr(feature = "utoipa", derive(ToSchema))]
#[cfg_attr(feature = "bincode", derive(bincode::Encode, bincode::Decode))]
#[serde(tag = "type")]
#[serde(rename_all = "snake_case")]
pub enum DmChannelInfo {
    /// A normal DM channel.
    Dm {
        /// The two IDs of the recipients of the DM.
        #[cfg_attr(feature = "utoipa", schema(schema_with = tuple_u64_u64))]
        recipient_ids: (u64, u64),
    },
    /// A group chat consisting of multiple users.
    Group {
        /// The name of the group chat.
        name: String,
        /// The topic of the group chat, if any.
        topic: Option<String>,
        /// The URL of the group's icon, if any.
        icon: Option<String>,
        /// The ID of the owner of the group chat.
        owner_id: u64,
        /// A list of recipients in the group chat by user ID.
        recipient_ids: Vec<u64>,
    },
}

impl DmChannelInfo {
    /// Returns the [`ChannelType`] of the DM channel.
    #[inline]
    #[must_use]
    pub const fn channel_type(&self) -> ChannelType {
        match self {
            Self::Dm { .. } => ChannelType::Dm,
            Self::Group { .. } => ChannelType::Group,
        }
    }
}

/// Represents a direct-message-like channel that does not belong in a guild.
#[derive(Clone, Debug, Serialize)]
#[cfg_attr(feature = "client", derive(Deserialize))]
#[cfg_attr(feature = "utoipa", derive(ToSchema))]
#[cfg_attr(feature = "bincode", derive(bincode::Encode, bincode::Decode))]
pub struct DmChannel {
    /// The ID of the channel.
    pub id: u64,
    /// Information about the channel.
    #[serde(flatten)]
    pub info: DmChannelInfo,
    /// The last message sent in this channel. This is `None` if no messages have been sent in this
    /// channel, and is sometimes always none in partial contexts.
    pub last_message: Option<Message>,
}

/// Represents any channel.
#[derive(Clone, Debug, Serialize)]
#[cfg_attr(feature = "client", derive(Deserialize))]
#[cfg_attr(feature = "utoipa", derive(ToSchema))]
#[cfg_attr(feature = "bincode", derive(bincode::Encode, bincode::Decode))]
#[serde(untagged)]
#[allow(clippy::large_enum_variant)]
pub enum Channel {
    /// A guild channel.
    Guild(GuildChannel),
    /// A DM channel.
    Dm(DmChannel),
}

impl Channel {
    /// Returns the name of the channel. Returns `None` if the channel is a standard DM channel.
    #[must_use]
    pub fn name(&self) -> Option<&str> {
        match self {
            Self::Guild(channel) => Some(&channel.name),
            Self::Dm(channel) => {
                if let DmChannelInfo::Group { ref name, .. } = channel.info {
                    Some(name)
                } else {
                    None
                }
            }
        }
    }

    /// Sets the name of the channel to the given name. If the channel is a standard DM channel,
    /// nothing happens.
    pub fn set_name(&mut self, name: String) {
        match self {
            Self::Guild(channel) => channel.name = name,
            Self::Dm(channel) => {
                if let DmChannelInfo::Group {
                    name: ref mut group_name,
                    ..
                } = channel.info
                {
                    *group_name = name;
                }
            }
        }
    }

    /// Returns the topic of the channel.
    #[must_use]
    pub fn topic(&self) -> Option<&str> {
        match self {
            Self::Guild(channel) => {
                if let GuildChannelInfo::Text(ref info) | GuildChannelInfo::Announcement(ref info) =
                    channel.info
                {
                    info.topic.as_deref()
                } else {
                    None
                }
            }
            Self::Dm(channel) => {
                if let DmChannelInfo::Group { ref topic, .. } = channel.info {
                    topic.as_deref()
                } else {
                    None
                }
            }
        }
    }

    /// Sets the topic of the channel to the given topic.
    pub fn set_topic(&mut self, topic: Option<String>) {
        match self {
            Self::Guild(channel) => {
                if let GuildChannelInfo::Text(ref mut info)
                | GuildChannelInfo::Announcement(ref mut info) = channel.info
                {
                    info.topic = topic;
                }
            }
            Self::Dm(channel) => {
                if let DmChannelInfo::Group {
                    topic: ref mut group_topic,
                    ..
                } = channel.info
                {
                    *group_topic = topic;
                }
            }
        }
    }

    /// Returns the icon of the channel.
    #[must_use]
    pub fn icon(&self) -> Option<&str> {
        match self {
            Self::Guild(_) => None, // TODO: icons for guild channels
            Self::Dm(channel) => {
                if let DmChannelInfo::Group { ref icon, .. } = channel.info {
                    icon.as_deref()
                } else {
                    None
                }
            }
        }
    }

    /// Sets the icon of the channel to the given icon.
    pub fn set_icon(&mut self, icon: Option<String>) {
        match self {
            Self::Guild(_) => (), // TODO: icons for guild channels
            Self::Dm(channel) => {
                if let DmChannelInfo::Group {
                    icon: ref mut group_icon,
                    ..
                } = channel.info
                {
                    *group_icon = icon;
                }
            }
        }
    }
}

/// Represents any channel info.
#[derive(Clone, Debug, Serialize)]
#[cfg_attr(feature = "client", derive(Deserialize))]
#[allow(clippy::large_enum_variant)]
#[serde(untagged)]
pub enum ChannelInfo {
    /// A guild channel.
    Guild(GuildChannelInfo),
    /// A DM channel.
    Dm(DmChannelInfo),
}