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
use crate::{
    channel::Attachment,
    id::{marker::GuildMarker, Id},
    util::Timestamp,
};

use super::{Embed, MessageFlags};

use serde::{Deserialize, Serialize};

/// The snap-shot of a message.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct MessageSnapshot {
    /// Subset of fields in the message object.
    message: MessageSnapshotFields,
    /// ID of the origin message's guild.
    #[serde(skip_serializing_if = "Option::is_none")]
    guild_id: Option<Id<GuildMarker>>,
}

/// A subset of the fields for a message that has been snap-shotted.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct MessageSnapshotFields {
    /// List of attachments from the message snapshot.
    pub attachments: Vec<Attachment>,
    /// Content of the message snapshot.
    pub content: String,
    /// When the message was last edited.
    pub edited_timestamp: Option<Timestamp>,
    /// List of embeds from the message snapshot.
    pub embeds: Vec<Embed>,
    /// Flags of the message.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub flags: Option<MessageFlags>,
    /// Timestamp of when the message was created.
    pub timestamp: Timestamp,
}

#[cfg(test)]
mod tests {
    use super::{MessageSnapshot, MessageSnapshotFields};
    use crate::{channel::Attachment, id::Id, util::Timestamp};
    use serde_test::Token;

    #[test]
    fn test_message_snapshot() {
        let value = MessageSnapshot {
            message: MessageSnapshotFields {
                attachments: vec![Attachment {
                    content_type: None,
                    description: None,
                    duration_secs: None,
                    ephemeral: false,
                    filename: "file.jpg".to_owned(),
                    flags: None,
                    height: Some(100),
                    id: Id::new(1),
                    proxy_url: "https://example.com".to_owned(),
                    size: 1000,
                    title: None,
                    url: "https://example.com".to_owned(),
                    waveform: None,
                    width: Some(100),
                }],
                content: "test".to_owned(),
                edited_timestamp: Some(Timestamp::from_secs(1_571_573_184).unwrap()),
                embeds: Vec::new(),
                flags: None,
                timestamp: Timestamp::from_secs(1_571_573_184).unwrap(),
            },
            guild_id: Some(Id::new(1)),
        };

        serde_test::assert_tokens(
            &value,
            &[
                Token::Struct {
                    name: "MessageSnapshot",
                    len: 2,
                },
                Token::Str("message"),
                Token::Struct {
                    name: "MessageSnapshotFields",
                    len: 5,
                },
                Token::Str("attachments"),
                Token::Seq { len: Some(1) },
                Token::Struct {
                    name: "Attachment",
                    len: 8,
                },
                Token::Str("content_type"),
                Token::None,
                // Token::Str("ephemeral"),
                // Token::Bool(false),
                Token::Str("filename"),
                Token::Str("file.jpg"),
                Token::Str("height"),
                Token::Some,
                Token::U64(100),
                Token::Str("id"),
                Token::NewtypeStruct { name: "Id" },
                Token::Str("1"),
                Token::Str("proxy_url"),
                Token::Str("https://example.com"),
                Token::Str("size"),
                Token::U64(1000),
                Token::Str("url"),
                Token::Str("https://example.com"),
                Token::Str("width"),
                Token::Some,
                Token::U64(100),
                Token::StructEnd,
                Token::SeqEnd,
                Token::Str("content"),
                Token::Str("test"),
                Token::Str("edited_timestamp"),
                Token::Some,
                Token::Str("2019-10-20T12:06:24.000000+00:00"),
                Token::Str("embeds"),
                Token::Seq { len: Some(0) },
                Token::SeqEnd,
                Token::Str("timestamp"),
                Token::Str("2019-10-20T12:06:24.000000+00:00"),
                Token::StructEnd,
                Token::Str("guild_id"),
                Token::Some,
                Token::NewtypeStruct { name: "Id" },
                Token::Str("1"),
                Token::StructEnd,
            ],
        );
    }
}