titanium_model/
thread.rs

1//! Thread types for Discord's thread channels.
2//!
3//! Threads are sub-channels within text or forum channels.
4
5use crate::Snowflake;
6use crate::TitanString;
7use serde::{Deserialize, Serialize};
8
9/// Thread metadata.
10#[derive(Debug, Clone, Deserialize, Serialize)]
11pub struct ThreadMetadata<'a> {
12    /// Whether the thread is archived.
13    pub archived: bool,
14
15    /// The thread will stop showing in the channel list after `auto_archive_duration` minutes of inactivity.
16    pub auto_archive_duration: u32,
17
18    /// Timestamp when the thread's archive status was last changed (ISO8601).
19    pub archive_timestamp: TitanString<'a>,
20
21    /// Whether the thread is locked.
22    #[serde(default)]
23    pub locked: bool,
24
25    /// Whether non-moderators can add other non-moderators to the thread.
26    #[serde(default)]
27    pub invitable: Option<bool>,
28}
29
30/// A member of a thread.
31#[derive(Debug, Clone, Deserialize, Serialize)]
32pub struct ThreadMember<'a> {
33    /// The ID of the thread.
34    #[serde(default)]
35    pub id: Option<Snowflake>,
36
37    /// The ID of the user.
38    #[serde(default)]
39    pub user_id: Option<Snowflake>,
40
41    /// Time the user last joined the thread.
42    pub join_timestamp: TitanString<'a>,
43
44    /// Any user-thread flags.
45    pub flags: u64,
46
47    /// Additional member information (if requested).
48    #[serde(default)]
49    pub member: Option<super::member::GuildMember<'a>>,
50}
51
52/// A tag in a forum channel.
53#[derive(Debug, Clone, Deserialize, Serialize)]
54pub struct ForumTag<'a> {
55    /// The ID of the tag.
56    pub id: Snowflake,
57
58    /// The name of the tag (0-20 characters).
59    pub name: TitanString<'a>,
60
61    /// Whether this tag can only be added/removed by moderators.
62    pub moderated: bool,
63
64    /// The ID of a guild's custom emoji.
65    pub emoji_id: Option<Snowflake>,
66
67    /// The unicode character of the emoji.
68    #[serde(default)]
69    pub emoji_name: Option<TitanString<'a>>,
70}
71
72/// Default reaction for a forum channel.
73#[derive(Debug, Clone, Deserialize, Serialize)]
74pub struct DefaultReaction<'a> {
75    /// The ID of a guild's custom emoji.
76    pub emoji_id: Option<Snowflake>,
77
78    /// The unicode character of the emoji.
79    #[serde(default)]
80    pub emoji_name: Option<TitanString<'a>>,
81}
82
83/// Event data for THREAD_DELETE.
84#[derive(Debug, Clone, Deserialize, Serialize)]
85pub struct ThreadDeleteEvent {
86    /// The ID of the thread.
87    pub id: Snowflake,
88
89    /// The ID of the guild.
90    pub guild_id: Snowflake,
91
92    /// The ID of the parent channel.
93    pub parent_id: Option<Snowflake>,
94
95    /// The type of the thread.
96    #[serde(rename = "type")]
97    pub channel_type: u8,
98}
99
100/// Event data for THREAD_LIST_SYNC.
101#[derive(Debug, Clone, Deserialize, Serialize)]
102pub struct ThreadListSyncEvent<'a> {
103    /// The ID of the guild.
104    pub guild_id: Snowflake,
105
106    /// The parent channel IDs whose threads are being synced.
107    #[serde(default)]
108    pub channel_ids: Option<Vec<Snowflake>>,
109
110    /// All active threads in the given channels.
111    pub threads: Vec<super::Channel<'a>>,
112
113    /// All thread members for the current user in each of the threads.
114    pub members: Vec<ThreadMember<'a>>,
115}
116
117/// Event data for THREAD_MEMBER_UPDATE.
118#[derive(Debug, Clone, Deserialize, Serialize)]
119pub struct ThreadMemberUpdateEvent<'a> {
120    #[serde(flatten)]
121    pub member: ThreadMember<'a>,
122    pub guild_id: Snowflake,
123}
124
125/// Event data for THREAD_MEMBERS_UPDATE.
126#[derive(Debug, Clone, Deserialize, Serialize)]
127pub struct ThreadMembersUpdateEvent<'a> {
128    /// The ID of the thread.
129    pub id: Snowflake,
130
131    /// The ID of the guild.
132    pub guild_id: Snowflake,
133
134    /// The approximate number of members in the thread.
135    pub member_count: u32,
136
137    /// The users who were added to the thread.
138    #[serde(default)]
139    pub added_members: Option<Vec<ThreadMember<'a>>>,
140
141    /// The ID of the users who were removed from the thread.
142    #[serde(default)]
143    pub removed_member_ids: Option<Vec<Snowflake>>,
144}
145
146#[cfg(test)]
147mod tests {
148    use super::*;
149
150    #[test]
151    fn test_thread_metadata() {
152        let json = r#"{
153            "archived": false,
154            "auto_archive_duration": 1440,
155            "archive_timestamp": "2021-01-01T00:00:00.000Z",
156            "locked": false
157        }"#;
158
159        let metadata: ThreadMetadata = crate::json::from_str(json).unwrap();
160        assert!(!metadata.archived);
161        assert_eq!(metadata.auto_archive_duration, 1440);
162    }
163}