twilight_model/channel/thread/
metadata.rs

1use super::AutoArchiveDuration;
2use crate::util::Timestamp;
3use serde::{Deserialize, Serialize};
4
5/// The thread metadata object contains a number of thread-specific channel fields
6/// that are not needed by other channel types.
7#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
8pub struct ThreadMetadata {
9    pub archived: bool,
10    /// Duration without messages before the thread automatically archives.
11    ///
12    /// Automatic archive durations are not locked behind the guild's boost
13    /// level.
14    pub auto_archive_duration: AutoArchiveDuration,
15    pub archive_timestamp: Timestamp,
16    /// When the thread was created at.
17    ///
18    /// Only present if the Thread has been created after 2022-01-09.
19    #[serde(skip_serializing_if = "Option::is_none")]
20    pub create_timestamp: Option<Timestamp>,
21    #[serde(skip_serializing_if = "Option::is_none")]
22    pub invitable: Option<bool>,
23    #[serde(default)]
24    pub locked: bool,
25}
26
27#[cfg(test)]
28mod tests {
29    use super::{AutoArchiveDuration, ThreadMetadata};
30    use crate::util::datetime::{Timestamp, TimestampParseError};
31    use serde_test::Token;
32    use std::str::FromStr;
33
34    #[test]
35    fn thread_metadata() -> Result<(), TimestampParseError> {
36        const DATETIME: &str = "2021-09-19T14:17:32.000000+00:00";
37
38        let timestamp = Timestamp::from_str(DATETIME)?;
39
40        let value = ThreadMetadata {
41            archived: true,
42            auto_archive_duration: AutoArchiveDuration::Day,
43            archive_timestamp: timestamp,
44            create_timestamp: Some(timestamp),
45            invitable: Some(false),
46            locked: false,
47        };
48
49        serde_test::assert_tokens(
50            &value,
51            &[
52                Token::Struct {
53                    name: "ThreadMetadata",
54                    len: 6,
55                },
56                Token::Str("archived"),
57                Token::Bool(true),
58                Token::Str("auto_archive_duration"),
59                Token::U16(1440),
60                Token::Str("archive_timestamp"),
61                Token::Str(DATETIME),
62                Token::Str("create_timestamp"),
63                Token::Some,
64                Token::Str(DATETIME),
65                Token::Str("invitable"),
66                Token::Some,
67                Token::Bool(false),
68                Token::Str("locked"),
69                Token::Bool(false),
70                Token::StructEnd,
71            ],
72        );
73
74        Ok(())
75    }
76}