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