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
mod message;

pub use self::message::CreateForumThreadMessage;

use self::message::CreateForumThreadMessageFields;
use crate::{
    client::Client,
    error::Error,
    request::{attachment::AttachmentManager, Nullable, Request},
    response::ResponseFuture,
    routing::Route,
};
use serde::{Deserialize, Serialize};
use twilight_model::{
    channel::{thread::AutoArchiveDuration, Channel, Message},
    id::{
        marker::{ChannelMarker, TagMarker},
        Id,
    },
};

#[derive(Deserialize, Serialize)]
pub struct ForumThread {
    #[serde(flatten)]
    pub channel: Channel,
    pub message: Message,
}

#[derive(Serialize)]
struct CreateForumThreadFields<'a> {
    #[serde(skip_serializing_if = "Option::is_none")]
    applied_tags: Option<&'a [Id<TagMarker>]>,
    #[serde(skip_serializing_if = "Option::is_none")]
    auto_archive_duration: Option<AutoArchiveDuration>,
    message: CreateForumThreadMessageFields<'a>,
    name: &'a str,
    #[serde(skip_serializing_if = "Option::is_none")]
    rate_limit_per_user: Option<u16>,
}

/// Creates a new thread in a forum channel.
///
/// Requires the [`SEND_MESSAGES`] permission.
///
/// [`SEND_MESSAGES`]: twilight_model::guild::Permissions::SEND_MESSAGES
#[must_use = "requests must be configured and executed"]
pub struct CreateForumThread<'a> {
    attachment_manager: AttachmentManager<'a>,
    channel_id: Id<ChannelMarker>,
    fields: CreateForumThreadFields<'a>,
    http: &'a Client,
}

impl<'a> CreateForumThread<'a> {
    pub(crate) const fn new(
        http: &'a Client,
        channel_id: Id<ChannelMarker>,
        name: &'a str,
    ) -> Self {
        Self {
            attachment_manager: AttachmentManager::new(),
            channel_id,
            fields: CreateForumThreadFields {
                applied_tags: None,
                auto_archive_duration: None,
                message: CreateForumThreadMessageFields {
                    allowed_mentions: None,
                    attachments: None,
                    components: None,
                    content: None,
                    embeds: None,
                    flags: None,
                    payload_json: None,
                    sticker_ids: None,
                },
                name,
                rate_limit_per_user: None,
            },
            http,
        }
    }

    /// Set the forum thread's applied tags.
    pub const fn applied_tags(mut self, applied_tags: &'a [Id<TagMarker>]) -> Self {
        self.fields.applied_tags = Some(applied_tags);

        self
    }

    /// Set the default auto archive duration for newly created threads in the
    /// channel.
    ///
    /// Automatic archive durations are not locked behind the guild's boost
    /// level.
    pub const fn auto_archive_duration(
        mut self,
        auto_archive_duration: AutoArchiveDuration,
    ) -> Self {
        self.fields.auto_archive_duration = Some(auto_archive_duration);

        self
    }

    pub const fn message(self) -> CreateForumThreadMessage<'a> {
        CreateForumThreadMessage::new(self)
    }

    /// Execute the request, returning a future resolving to a [`Response`].
    ///
    /// [`Response`]: crate::response::Response
    fn exec(self) -> ResponseFuture<ForumThread> {
        let http = self.http;

        match self.try_into_request() {
            Ok(request) => http.request(request),
            Err(source) => ResponseFuture::error(source),
        }
    }

    fn try_into_request(mut self) -> Result<Request, Error> {
        let mut request = Request::builder(&Route::CreateForumThread {
            channel_id: self.channel_id.get(),
        });

        // Set the default allowed mentions if required.
        if self.fields.message.allowed_mentions.is_none() {
            if let Some(allowed_mentions) = self.http.default_allowed_mentions() {
                self.fields.message.allowed_mentions = Some(Nullable(Some(allowed_mentions)));
            }
        }

        // Determine whether we need to use a multipart/form-data body or a JSON
        // body.
        if !self.attachment_manager.is_empty() {
            let form = if let Some(payload_json) = self.fields.message.payload_json {
                self.attachment_manager.build_form(payload_json)
            } else {
                self.fields.message.attachments =
                    Some(self.attachment_manager.get_partial_attachments());

                let fields = crate::json::to_vec(&self.fields).map_err(Error::json)?;

                self.attachment_manager.build_form(fields.as_ref())
            };

            request = request.form(form);
        } else if let Some(payload_json) = self.fields.message.payload_json {
            request = request.body(payload_json.to_vec());
        } else {
            request = request.json(&self.fields)?;
        }

        Ok(request.build())
    }
}