revolt_models/v0/
messages.rs

1use std::time::SystemTime;
2
3use indexmap::{IndexMap, IndexSet};
4use revolt_config::config;
5
6#[cfg(feature = "validator")]
7use validator::Validate;
8
9#[cfg(feature = "rocket")]
10use rocket::{FromForm, FromFormField};
11
12use iso8601_timestamp::Timestamp;
13
14use super::{Channel, Embed, File, Member, MessageWebhook, User, Webhook, RE_COLOUR};
15
16auto_derived_partial!(
17    /// Message
18    pub struct Message {
19        /// Unique Id
20        #[serde(rename = "_id")]
21        pub id: String,
22        /// Unique value generated by client sending this message
23        #[serde(skip_serializing_if = "Option::is_none")]
24        pub nonce: Option<String>,
25        /// Id of the channel this message was sent in
26        pub channel: String,
27        /// Id of the user or webhook that sent this message
28        pub author: String,
29        /// The user that sent this message
30        #[serde(skip_serializing_if = "Option::is_none")]
31        pub user: Option<User>,
32        /// The member that sent this message
33        #[serde(skip_serializing_if = "Option::is_none")]
34        pub member: Option<Member>,
35        /// The webhook that sent this message
36        #[serde(skip_serializing_if = "Option::is_none")]
37        pub webhook: Option<MessageWebhook>,
38        /// Message content
39        #[serde(skip_serializing_if = "Option::is_none")]
40        pub content: Option<String>,
41        /// System message
42        #[serde(skip_serializing_if = "Option::is_none")]
43        pub system: Option<SystemMessage>,
44        /// Array of attachments
45        #[serde(skip_serializing_if = "Option::is_none")]
46        pub attachments: Option<Vec<File>>,
47        /// Time at which this message was last edited
48        #[serde(skip_serializing_if = "Option::is_none")]
49        pub edited: Option<Timestamp>,
50        /// Attached embeds to this message
51        #[serde(skip_serializing_if = "Option::is_none")]
52        pub embeds: Option<Vec<Embed>>,
53        /// Array of user ids mentioned in this message
54        #[serde(skip_serializing_if = "Option::is_none")]
55        pub mentions: Option<Vec<String>>,
56        /// Array of role ids mentioned in this message
57        #[serde(skip_serializing_if = "Option::is_none")]
58        pub role_mentions: Option<Vec<String>>,
59        /// Array of message ids this message is replying to
60        #[serde(skip_serializing_if = "Option::is_none")]
61        pub replies: Option<Vec<String>>,
62        /// Hashmap of emoji IDs to array of user IDs
63        #[serde(skip_serializing_if = "IndexMap::is_empty", default)]
64        pub reactions: IndexMap<String, IndexSet<String>>,
65        /// Information about how this message should be interacted with
66        #[serde(skip_serializing_if = "Interactions::is_default", default)]
67        pub interactions: Interactions,
68        /// Name and / or avatar overrides for this message
69        #[serde(skip_serializing_if = "Option::is_none")]
70        pub masquerade: Option<Masquerade>,
71        /// Whether or not the message in pinned
72        #[serde(skip_serializing_if = "crate::if_option_false")]
73        pub pinned: Option<bool>,
74
75        /// Bitfield of message flags
76        ///
77        /// https://docs.rs/revolt-models/latest/revolt_models/v0/enum.MessageFlags.html
78        #[cfg_attr(
79            feature = "serde",
80            serde(skip_serializing_if = "crate::if_zero_u32", default)
81        )]
82        pub flags: u32,
83    },
84    "PartialMessage"
85);
86
87auto_derived!(
88    /// Bulk Message Response
89    #[serde(untagged)]
90    pub enum BulkMessageResponse {
91        JustMessages(
92            /// List of messages
93            Vec<Message>,
94        ),
95        MessagesAndUsers {
96            /// List of messages
97            messages: Vec<Message>,
98            /// List of users
99            users: Vec<User>,
100            /// List of members
101            #[serde(skip_serializing_if = "Option::is_none")]
102            members: Option<Vec<Member>>,
103        },
104    }
105
106    /// System Event
107    #[serde(tag = "type")]
108    pub enum SystemMessage {
109        #[serde(rename = "text")]
110        Text { content: String },
111        #[serde(rename = "user_added")]
112        UserAdded { id: String, by: String },
113        #[serde(rename = "user_remove")]
114        UserRemove { id: String, by: String },
115        #[serde(rename = "user_joined")]
116        UserJoined { id: String },
117        #[serde(rename = "user_left")]
118        UserLeft { id: String },
119        #[serde(rename = "user_kicked")]
120        UserKicked { id: String },
121        #[serde(rename = "user_banned")]
122        UserBanned { id: String },
123        #[serde(rename = "channel_renamed")]
124        ChannelRenamed { name: String, by: String },
125        #[serde(rename = "channel_description_changed")]
126        ChannelDescriptionChanged { by: String },
127        #[serde(rename = "channel_icon_changed")]
128        ChannelIconChanged { by: String },
129        #[serde(rename = "channel_ownership_changed")]
130        ChannelOwnershipChanged { from: String, to: String },
131        #[serde(rename = "message_pinned")]
132        MessagePinned { id: String, by: String },
133        #[serde(rename = "message_unpinned")]
134        MessageUnpinned { id: String, by: String },
135        #[serde(rename = "call_started")]
136        CallStarted { by: String, finished_at: Option<Timestamp> },
137    }
138
139    /// Name and / or avatar override information
140    #[cfg_attr(feature = "validator", derive(Validate))]
141    pub struct Masquerade {
142        /// Replace the display name shown on this message
143        #[serde(skip_serializing_if = "Option::is_none")]
144        #[validate(length(min = 1, max = 32))]
145        pub name: Option<String>,
146        /// Replace the avatar shown on this message (URL to image file)
147        #[serde(skip_serializing_if = "Option::is_none")]
148        #[validate(length(min = 1, max = 256))]
149        pub avatar: Option<String>,
150        /// Replace the display role colour shown on this message
151        ///
152        /// Must have `ManageRole` permission to use
153        #[serde(skip_serializing_if = "Option::is_none")]
154        #[validate(length(min = 1, max = 128), regex = "RE_COLOUR")]
155        pub colour: Option<String>,
156    }
157
158    /// Information to guide interactions on this message
159    #[derive(Default)]
160    pub struct Interactions {
161        /// Reactions which should always appear and be distinct
162        #[serde(skip_serializing_if = "Option::is_none", default)]
163        pub reactions: Option<IndexSet<String>>,
164        /// Whether reactions should be restricted to the given list
165        ///
166        /// Can only be set to true if reactions list is of at least length 1
167        #[serde(skip_serializing_if = "crate::if_false", default)]
168        pub restrict_reactions: bool,
169    }
170
171    /// Appended Information
172    pub struct AppendMessage {
173        /// Additional embeds to include in this message
174        #[serde(skip_serializing_if = "Option::is_none")]
175        pub embeds: Option<Vec<Embed>>,
176    }
177
178    /// Message Sort
179    ///
180    /// Sort used for retrieving messages
181    #[derive(Default)]
182    #[cfg_attr(feature = "rocket", derive(FromFormField))]
183    pub enum MessageSort {
184        /// Sort by the most relevant messages
185        #[default]
186        Relevance,
187        /// Sort by the newest messages first
188        Latest,
189        /// Sort by the oldest messages first
190        Oldest,
191    }
192
193    /// Push Notification
194    pub struct PushNotification {
195        /// Known author name
196        pub author: String,
197        /// URL to author avatar
198        pub icon: String,
199        /// URL to first matching attachment
200        #[serde(skip_serializing_if = "Option::is_none")]
201        pub image: Option<String>,
202        /// Message content or system message information
203        pub body: String,
204        /// Unique tag, usually the channel ID
205        pub tag: String,
206        /// Timestamp at which this notification was created
207        pub timestamp: u64,
208        /// URL to open when clicking notification
209        pub url: String,
210        /// The message object itself, to send to clients for processing
211        pub message: Message,
212        /// The channel object itself, for clients to process
213        pub channel: Channel,
214    }
215
216    /// Representation of a text embed before it is sent.
217    #[derive(Default)]
218    #[cfg_attr(feature = "validator", derive(Validate))]
219    pub struct SendableEmbed {
220        #[cfg_attr(feature = "validator", validate(length(min = 1, max = 256)))]
221        pub icon_url: Option<String>,
222        #[cfg_attr(feature = "validator", validate(length(min = 1, max = 256)))]
223        pub url: Option<String>,
224        #[cfg_attr(feature = "validator", validate(length(min = 1, max = 100)))]
225        pub title: Option<String>,
226        #[cfg_attr(feature = "validator", validate(length(min = 1, max = 2000)))]
227        pub description: Option<String>,
228        pub media: Option<String>,
229        #[cfg_attr(
230            feature = "validator",
231            validate(length(min = 1, max = 128), regex = "RE_COLOUR")
232        )]
233        pub colour: Option<String>,
234    }
235
236    /// What this message should reply to and how
237    pub struct ReplyIntent {
238        /// Message Id
239        pub id: String,
240        /// Whether this reply should mention the message's author
241        pub mention: bool,
242        /// Whether to error if the referenced message doesn't exist.
243        /// Otherwise, send a message without this reply.
244        /// Default is true.
245        pub fail_if_not_exists: Option<bool>,
246    }
247
248    /// Message to send
249    #[cfg_attr(feature = "validator", derive(Validate))]
250    pub struct DataMessageSend {
251        /// Unique token to prevent duplicate message sending
252        ///
253        /// **This is deprecated and replaced by `Idempotency-Key`!**
254        #[cfg_attr(feature = "validator", validate(length(min = 1, max = 64)))]
255        pub nonce: Option<String>,
256
257        /// Message content to send
258        #[cfg_attr(feature = "validator", validate(length(min = 0, max = 2000)))]
259        pub content: Option<String>,
260        /// Attachments to include in message
261        pub attachments: Option<Vec<String>>,
262        /// Messages to reply to
263        pub replies: Option<Vec<ReplyIntent>>,
264        /// Embeds to include in message
265        ///
266        /// Text embed content contributes to the content length cap
267        #[cfg_attr(feature = "validator", validate)]
268        pub embeds: Option<Vec<SendableEmbed>>,
269        /// Masquerade to apply to this message
270        #[cfg_attr(feature = "validator", validate)]
271        pub masquerade: Option<Masquerade>,
272        /// Information about how this message should be interacted with
273        pub interactions: Option<Interactions>,
274
275        /// Bitfield of message flags
276        ///
277        /// https://docs.rs/revolt-models/latest/revolt_models/v0/enum.MessageFlags.html
278        pub flags: Option<u32>,
279    }
280
281    /// Options for querying messages
282    #[cfg_attr(feature = "validator", derive(Validate))]
283    #[cfg_attr(feature = "rocket", derive(FromForm))]
284    pub struct OptionsQueryMessages {
285        /// Maximum number of messages to fetch
286        ///
287        /// For fetching nearby messages, this is \`(limit + 2)\`.
288        #[cfg_attr(feature = "validator", validate(range(min = 1, max = 100)))]
289        pub limit: Option<i64>,
290        /// Message id before which messages should be fetched
291        #[cfg_attr(feature = "validator", validate(length(min = 26, max = 26)))]
292        pub before: Option<String>,
293        /// Message id after which messages should be fetched
294        #[cfg_attr(feature = "validator", validate(length(min = 26, max = 26)))]
295        pub after: Option<String>,
296        /// Message sort direction
297        pub sort: Option<MessageSort>,
298        /// Message id to search around
299        ///
300        /// Specifying 'nearby' ignores 'before', 'after' and 'sort'.
301        /// It will also take half of limit rounded as the limits to each side.
302        /// It also fetches the message ID specified.
303        #[cfg_attr(feature = "validator", validate(length(min = 26, max = 26)))]
304        pub nearby: Option<String>,
305        /// Whether to include user (and member, if server channel) objects
306        pub include_users: Option<bool>,
307    }
308
309    /// Options for searching for messages
310    #[cfg_attr(feature = "validator", derive(Validate))]
311    pub struct DataMessageSearch {
312        /// Full-text search query
313        ///
314        /// See [MongoDB documentation](https://docs.mongodb.com/manual/text-search/#-text-operator) for more information.
315        #[cfg_attr(feature = "validator", validate(length(min = 1, max = 64)))]
316        pub query: Option<String>,
317        /// Whether to only search for pinned messages, cannot be sent with `query`.
318        pub pinned: Option<bool>,
319
320        /// Maximum number of messages to fetch
321        #[cfg_attr(feature = "validator", validate(range(min = 1, max = 100)))]
322        pub limit: Option<i64>,
323        /// Message id before which messages should be fetched
324        #[cfg_attr(feature = "validator", validate(length(min = 26, max = 26)))]
325        pub before: Option<String>,
326        /// Message id after which messages should be fetched
327        #[cfg_attr(feature = "validator", validate(length(min = 26, max = 26)))]
328        pub after: Option<String>,
329        /// Message sort direction
330        ///
331        /// By default, it will be sorted by latest.
332        #[cfg_attr(feature = "serde", serde(default = "MessageSort::default"))]
333        pub sort: MessageSort,
334        /// Whether to include user (and member, if server channel) objects
335        pub include_users: Option<bool>,
336    }
337
338    /// Changes to make to message
339    #[cfg_attr(feature = "validator", derive(Validate))]
340    pub struct DataEditMessage {
341        /// New message content
342        #[cfg_attr(feature = "validator", validate(length(min = 1, max = 2000)))]
343        pub content: Option<String>,
344        /// Embeds to include in the message
345        #[cfg_attr(feature = "validator", validate(length(min = 0, max = 10)))]
346        pub embeds: Option<Vec<SendableEmbed>>,
347    }
348
349    /// Options for bulk deleting messages
350    #[cfg_attr(
351        feature = "validator",
352        cfg_attr(feature = "validator", derive(Validate))
353    )]
354    pub struct OptionsBulkDelete {
355        /// Message IDs
356        #[validate(length(min = 1, max = 100))]
357        pub ids: Vec<String>,
358    }
359
360    /// Options for removing reaction
361    #[cfg_attr(feature = "rocket", derive(FromForm))]
362    pub struct OptionsUnreact {
363        /// Remove a specific user's reaction
364        pub user_id: Option<String>,
365        /// Remove all reactions
366        pub remove_all: Option<bool>,
367    }
368
369    /// Message flag bitfield
370    #[repr(u32)]
371    pub enum MessageFlags {
372        /// Message will not send push / desktop notifications
373        SuppressNotifications = 1,
374        /// Message will mention all users who can see the channel
375        MentionsEveryone = 2,
376        /// Message will mention all users who are online and can see the channel.
377        /// This cannot be true if MentionsEveryone is true
378        MentionsOnline = 3,
379    }
380
381    /// Optional fields on message
382    pub enum FieldsMessage {
383        Pinned,
384    }
385);
386
387/// Message Author Abstraction
388pub enum MessageAuthor<'a> {
389    User(&'a User),
390    Webhook(&'a Webhook),
391    System {
392        username: &'a str,
393        avatar: Option<&'a str>,
394    },
395}
396
397impl Interactions {
398    /// Check if default initialisation of fields
399    pub fn is_default(&self) -> bool {
400        !self.restrict_reactions && self.reactions.is_none()
401    }
402}
403
404impl MessageAuthor<'_> {
405    pub fn id(&self) -> &str {
406        match self {
407            MessageAuthor::User(user) => &user.id,
408            MessageAuthor::Webhook(webhook) => &webhook.id,
409            MessageAuthor::System { .. } => "00000000000000000000000000",
410        }
411    }
412
413    pub fn avatar(&self) -> Option<&str> {
414        match self {
415            MessageAuthor::User(user) => user.avatar.as_ref().map(|file| file.id.as_str()),
416            MessageAuthor::Webhook(webhook) => webhook.avatar.as_ref().map(|file| file.id.as_str()),
417            MessageAuthor::System { avatar, .. } => *avatar,
418        }
419    }
420
421    pub fn username(&self) -> &str {
422        match self {
423            MessageAuthor::User(user) => &user.username,
424            MessageAuthor::Webhook(webhook) => &webhook.name,
425            MessageAuthor::System { username, .. } => username,
426        }
427    }
428}
429
430impl From<SystemMessage> for String {
431    fn from(s: SystemMessage) -> String {
432        match s {
433            SystemMessage::Text { content } => content,
434            SystemMessage::UserAdded { .. } => "User added to the channel.".to_string(),
435            SystemMessage::UserRemove { .. } => "User removed from the channel.".to_string(),
436            SystemMessage::UserJoined { .. } => "User joined the channel.".to_string(),
437            SystemMessage::UserLeft { .. } => "User left the channel.".to_string(),
438            SystemMessage::UserKicked { .. } => "User kicked from the channel.".to_string(),
439            SystemMessage::UserBanned { .. } => "User banned from the channel.".to_string(),
440            SystemMessage::ChannelRenamed { .. } => "Channel renamed.".to_string(),
441            SystemMessage::ChannelDescriptionChanged { .. } => {
442                "Channel description changed.".to_string()
443            }
444            SystemMessage::ChannelIconChanged { .. } => "Channel icon changed.".to_string(),
445            SystemMessage::ChannelOwnershipChanged { .. } => {
446                "Channel ownership changed.".to_string()
447            }
448            SystemMessage::MessagePinned { .. } => "Message pinned.".to_string(),
449            SystemMessage::MessageUnpinned { .. } => "Message unpinned.".to_string(),
450            SystemMessage::CallStarted { .. } => "Call started.".to_string(),
451        }
452    }
453}
454
455impl PushNotification {
456    /// Create a new notification from a given message, author and channel ID
457    pub async fn from(msg: Message, author: Option<MessageAuthor<'_>>, channel: Channel) -> Self {
458        let config = config().await;
459
460        let icon = if let Some(author) = &author {
461            if let Some(avatar) = author.avatar() {
462                format!("{}/avatars/{}", config.hosts.autumn, avatar)
463            } else {
464                format!("{}/users/{}/default_avatar", config.hosts.api, author.id())
465            }
466        } else {
467            format!("{}/assets/logo.png", config.hosts.app)
468        };
469
470        let image = msg.attachments.as_ref().and_then(|attachments| {
471            attachments
472                .first()
473                .map(|v| format!("{}/attachments/{}", config.hosts.autumn, v.id))
474        });
475
476        let body = if let Some(ref sys) = msg.system {
477            sys.clone().into()
478        } else if let Some(ref text) = msg.content {
479            text.clone()
480        } else if let Some(text) = msg.embeds.as_ref().and_then(|embeds| match embeds.first() {
481            Some(Embed::Image(_)) => Some("Sent an image".to_string()),
482            Some(Embed::Video(_)) => Some("Sent a video".to_string()),
483            Some(Embed::Text(e)) => e
484                .description
485                .clone()
486                .or(e.title.clone().or(Some("Empty Embed".to_string()))),
487            Some(Embed::Website(e)) => e.title.clone().or(e
488                .description
489                .clone()
490                .or(e.site_name.clone().or(Some("Empty Embed".to_string())))),
491            Some(Embed::None) => Some("Empty Message".to_string()), // ???
492            None => Some("Empty Message".to_string()),              // ??
493        }) {
494            text
495        } else {
496            "Empty Message".to_string()
497        };
498
499        let timestamp = SystemTime::now()
500            .duration_since(SystemTime::UNIX_EPOCH)
501            .expect("Time went backwards")
502            .as_secs();
503
504        Self {
505            author: author
506                .map(|x| x.username().to_string())
507                .unwrap_or_else(|| "Revolt".to_string()),
508            icon,
509            image,
510            body,
511            tag: channel.id().to_string(),
512            timestamp,
513            url: format!("{}/channel/{}/{}", config.hosts.app, channel.id(), msg.id),
514            message: msg,
515            channel,
516        }
517    }
518}