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
//! Cached message-related models.

use serde::Serialize;
use twilight_model::{
    application::interaction::InteractionType,
    channel::{
        message::{
            sticker::MessageSticker, Component, Embed, Message, MessageActivity,
            MessageApplication, MessageFlags, MessageInteraction, MessageReference, MessageType,
            Reaction, RoleSubscriptionData,
        },
        Attachment, ChannelMention,
    },
    guild::PartialMember,
    id::{
        marker::{
            ApplicationMarker, ChannelMarker, GuildMarker, InteractionMarker, MessageMarker,
            RoleMarker, UserMarker, WebhookMarker,
        },
        Id,
    },
    util::Timestamp,
};

/// Information about the message interaction.
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub struct CachedMessageInteraction {
    id: Id<InteractionMarker>,
    #[serde(rename = "type")]
    kind: InteractionType,
    name: String,
    user_id: Id<UserMarker>,
}

impl CachedMessageInteraction {
    /// ID of the interaction.
    pub const fn id(&self) -> Id<InteractionMarker> {
        self.id
    }

    /// Type of the interaction.
    pub const fn kind(&self) -> InteractionType {
        self.kind
    }

    /// Name of the interaction used.
    pub fn name(&self) -> &str {
        &self.name
    }

    /// ID of the user who invoked the interaction.
    pub const fn user_id(&self) -> Id<UserMarker> {
        self.user_id
    }

    /// Construct a cached message interaction from its [`twilight_model`] form.
    #[allow(clippy::missing_const_for_fn)]
    pub(crate) fn from_model(message_interaction: MessageInteraction) -> Self {
        // Reasons for dropping fields:
        //
        // - `member`: we have the user's ID from the `user_id` field
        let MessageInteraction {
            id,
            kind,
            member: _,
            name,
            user,
        } = message_interaction;

        Self {
            id,
            kind,
            name,
            user_id: user.id,
        }
    }
}

/// Represents a cached [`Message`].
///
/// [`Message`]: twilight_model::channel::Message
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub struct CachedMessage {
    activity: Option<MessageActivity>,
    application: Option<MessageApplication>,
    application_id: Option<Id<ApplicationMarker>>,
    pub(crate) attachments: Vec<Attachment>,
    author: Id<UserMarker>,
    channel_id: Id<ChannelMarker>,
    components: Vec<Component>,
    pub(crate) content: String,
    pub(crate) edited_timestamp: Option<Timestamp>,
    pub(crate) embeds: Vec<Embed>,
    flags: Option<MessageFlags>,
    guild_id: Option<Id<GuildMarker>>,
    id: Id<MessageMarker>,
    interaction: Option<CachedMessageInteraction>,
    kind: MessageType,
    member: Option<PartialMember>,
    mention_channels: Vec<ChannelMention>,
    pub(crate) mention_everyone: bool,
    pub(crate) mention_roles: Vec<Id<RoleMarker>>,
    pub(crate) mentions: Vec<Id<UserMarker>>,
    pub(crate) pinned: bool,
    pub(crate) reactions: Vec<Reaction>,
    reference: Option<MessageReference>,
    role_subscription_data: Option<RoleSubscriptionData>,
    sticker_items: Vec<MessageSticker>,
    thread_id: Option<Id<ChannelMarker>>,
    pub(crate) timestamp: Timestamp,
    pub(crate) tts: bool,
    webhook_id: Option<Id<WebhookMarker>>,
}

impl CachedMessage {
    /// For rich presence chat embeds, the activity object.
    pub const fn activity(&self) -> Option<&MessageActivity> {
        self.activity.as_ref()
    }

    /// For interaction responses, the ID of the interaction's application.
    pub const fn application(&self) -> Option<&MessageApplication> {
        self.application.as_ref()
    }

    /// Associated application's ID.
    ///
    /// Sent if the message is a response to an Interaction.
    pub const fn application_id(&self) -> Option<Id<ApplicationMarker>> {
        self.application_id
    }

    /// List of attached files.
    ///
    /// Refer to the documentation for [`Message::attachments`] for caveats with
    /// receiving the attachments of messages.
    ///
    /// [`Message::attachments`]: twilight_model::channel::Message::attachments
    pub fn attachments(&self) -> &[Attachment] {
        &self.attachments
    }

    /// ID of the message author.
    ///
    /// If the author is a webhook, this is its ID.
    pub const fn author(&self) -> Id<UserMarker> {
        self.author
    }

    /// ID of the channel the message was sent in.
    pub const fn channel_id(&self) -> Id<ChannelMarker> {
        self.channel_id
    }

    /// List of provided components, such as buttons.
    ///
    /// Refer to the documentation for [`Message::components`] for caveats with
    /// receiving the components of messages.
    ///
    /// [`Message::components`]: twilight_model::channel::Message::components
    pub fn components(&self) -> &[Component] {
        &self.components
    }

    /// Content of a message.
    ///
    /// Refer to the documentation for [`Message::content`] for caveats with
    /// receiving the content of messages.
    ///
    /// [`Message::content`]: twilight_model::channel::Message::content
    pub fn content(&self) -> &str {
        &self.content
    }

    /// [`Timestamp`] of the date the message was last edited.
    pub const fn edited_timestamp(&self) -> Option<Timestamp> {
        self.edited_timestamp
    }

    /// List of embeds.
    ///
    /// Refer to the documentation for [`Message::embeds`] for caveats with
    /// receiving the embeds of messages.
    ///
    /// [`Message::embeds`]: twilight_model::channel::Message::embeds
    pub fn embeds(&self) -> &[Embed] {
        &self.embeds
    }

    /// Message flags.
    pub const fn flags(&self) -> Option<MessageFlags> {
        self.flags
    }

    /// ID of the guild the message was sent in, if there is one.
    pub const fn guild_id(&self) -> Option<Id<GuildMarker>> {
        self.guild_id
    }

    /// ID of the message.
    pub const fn id(&self) -> Id<MessageMarker> {
        self.id
    }

    /// Information about the message interaction.
    pub const fn interaction(&self) -> Option<&CachedMessageInteraction> {
        self.interaction.as_ref()
    }

    /// Type of the message.
    pub const fn kind(&self) -> MessageType {
        self.kind
    }

    /// Member data for the author, if there is any.
    pub const fn member(&self) -> Option<&PartialMember> {
        self.member.as_ref()
    }

    /// Channels mentioned in the content.
    pub fn mention_channels(&self) -> &[ChannelMention] {
        &self.mention_channels
    }

    /// Whether or not '@everyone' or '@here' is mentioned in the content.
    pub const fn mention_everyone(&self) -> bool {
        self.mention_everyone
    }

    /// Roles mentioned in the content.
    pub fn mention_roles(&self) -> &[Id<RoleMarker>] {
        &self.mention_roles
    }

    /// Users mentioned in the content.
    pub fn mentions(&self) -> &[Id<UserMarker>] {
        &self.mentions
    }

    /// Whether or not the message is pinned.
    pub const fn pinned(&self) -> bool {
        self.pinned
    }

    /// Reactions to the message.
    pub fn reactions(&self) -> &[Reaction] {
        &self.reactions
    }

    /// Message reference.
    pub const fn reference(&self) -> Option<&MessageReference> {
        self.reference.as_ref()
    }

    /// Information about the role subscription purchase or renewal that
    /// prompted this message.
    pub const fn role_subscription_data(&self) -> Option<&RoleSubscriptionData> {
        self.role_subscription_data.as_ref()
    }

    /// Stickers within the message.
    pub fn sticker_items(&self) -> &[MessageSticker] {
        &self.sticker_items
    }

    /// ID of the thread the message was sent in.
    pub const fn thread_id(&self) -> Option<Id<ChannelMarker>> {
        self.thread_id
    }

    /// [`Timestamp`] of the date the message was sent.
    pub const fn timestamp(&self) -> Timestamp {
        self.timestamp
    }

    /// Whether the message is text-to-speech.
    pub const fn tts(&self) -> bool {
        self.tts
    }

    /// For messages sent by webhooks, the webhook ID.
    pub const fn webhook_id(&self) -> Option<Id<WebhookMarker>> {
        self.webhook_id
    }

    /// Construct a cached message from its [`twilight_model`] form.
    pub(crate) fn from_model(message: Message) -> Self {
        let Message {
            activity,
            application,
            application_id,
            attachments,
            author,
            channel_id,
            components,
            content,
            edited_timestamp,
            embeds,
            flags,
            guild_id,
            id,
            interaction,
            kind,
            member,
            mention_channels,
            mention_everyone,
            mention_roles,
            mentions,
            pinned,
            reactions,
            reference,
            referenced_message: _,
            role_subscription_data,
            sticker_items,
            timestamp,
            thread,
            tts,
            webhook_id,
        } = message;

        Self {
            id,
            activity,
            application,
            application_id,
            attachments,
            author: author.id,
            channel_id,
            components,
            content,
            edited_timestamp,
            embeds,
            flags,
            guild_id,
            interaction: interaction.map(CachedMessageInteraction::from_model),
            kind,
            member,
            mention_channels,
            mention_everyone,
            mention_roles,
            mentions: mentions.into_iter().map(|mention| mention.id).collect(),
            pinned,
            reactions,
            reference,
            role_subscription_data,
            sticker_items,
            thread_id: thread.map(|thread| thread.id),
            timestamp,
            tts,
            webhook_id,
        }
    }
}

impl From<Message> for CachedMessage {
    fn from(message: Message) -> Self {
        Self::from_model(message)
    }
}

#[cfg(test)]
mod tests {
    use super::{CachedMessage, CachedMessageInteraction};
    use serde::Serialize;
    use static_assertions::{assert_fields, assert_impl_all};
    use std::fmt::Debug;
    use twilight_model::channel::message::Message;

    assert_fields!(
        CachedMessage: activity,
        application,
        application_id,
        attachments,
        author,
        channel_id,
        components,
        content,
        edited_timestamp,
        embeds,
        flags,
        guild_id,
        id,
        interaction,
        kind,
        member,
        mention_channels,
        mention_everyone,
        mention_roles,
        mentions,
        pinned,
        reactions,
        reference,
        sticker_items,
        thread_id,
        timestamp,
        tts,
        webhook_id
    );
    assert_impl_all!(
        CachedMessage: Clone,
        Debug,
        Eq,
        From<Message>,
        PartialEq,
        Send,
        Serialize,
        Sync,
    );
    assert_fields!(CachedMessageInteraction: id, kind, name, user_id);
    assert_impl_all!(
        CachedMessageInteraction: Clone,
        Debug,
        Eq,
        PartialEq,
        Send,
        Serialize,
        Sync,
    );
}