twilight_http/request/channel/thread/
create_thread.rs1use crate::{
2 client::Client,
3 error::Error,
4 request::{self, AuditLogReason, Request, TryIntoRequest},
5 response::{Response, ResponseFuture},
6 routing::Route,
7};
8use serde::Serialize;
9use std::future::IntoFuture;
10use twilight_model::{
11 channel::{Channel, ChannelType, thread::AutoArchiveDuration},
12 id::{Id, marker::ChannelMarker},
13};
14use twilight_validate::{
15 channel::{ChannelValidationError, is_thread as validate_is_thread, name as validate_name},
16 request::{ValidationError, audit_reason as validate_audit_reason},
17};
18
19#[derive(Serialize)]
20struct CreateThreadFields<'a> {
21 #[serde(skip_serializing_if = "Option::is_none")]
22 auto_archive_duration: Option<AutoArchiveDuration>,
23 #[serde(skip_serializing_if = "Option::is_none")]
24 invitable: Option<bool>,
25 #[serde(rename = "type")]
26 kind: ChannelType,
27 name: &'a str,
28}
29
30#[must_use = "requests must be configured and executed"]
37pub struct CreateThread<'a> {
38 channel_id: Id<ChannelMarker>,
39 fields: Result<CreateThreadFields<'a>, ChannelValidationError>,
40 http: &'a Client,
41 reason: Result<Option<&'a str>, ValidationError>,
42}
43
44impl<'a> CreateThread<'a> {
45 pub(crate) fn new(
46 http: &'a Client,
47 channel_id: Id<ChannelMarker>,
48 name: &'a str,
49 kind: ChannelType,
50 ) -> Self {
51 let fields = Ok(CreateThreadFields {
52 auto_archive_duration: None,
53 invitable: None,
54 kind,
55 name,
56 })
57 .and_then(|fields| {
58 validate_name(name)?;
59 validate_is_thread(kind)?;
60
61 Ok(fields)
62 });
63
64 Self {
65 channel_id,
66 fields,
67 http,
68 reason: Ok(None),
69 }
70 }
71
72 pub const fn auto_archive_duration(
77 mut self,
78 auto_archive_duration: AutoArchiveDuration,
79 ) -> Self {
80 if let Ok(fields) = self.fields.as_mut() {
81 fields.auto_archive_duration = Some(auto_archive_duration);
82 }
83
84 self
85 }
86
87 pub const fn invitable(mut self, invitable: bool) -> Self {
89 if let Ok(fields) = self.fields.as_mut() {
90 fields.invitable = Some(invitable);
91 }
92
93 self
94 }
95}
96
97impl<'a> AuditLogReason<'a> for CreateThread<'a> {
98 fn reason(mut self, reason: &'a str) -> Self {
99 self.reason = validate_audit_reason(reason).and(Ok(Some(reason)));
100
101 self
102 }
103}
104
105impl IntoFuture for CreateThread<'_> {
106 type Output = Result<Response<Channel>, Error>;
107
108 type IntoFuture = ResponseFuture<Channel>;
109
110 fn into_future(self) -> Self::IntoFuture {
111 let http = self.http;
112
113 match self.try_into_request() {
114 Ok(request) => http.request(request),
115 Err(source) => ResponseFuture::error(source),
116 }
117 }
118}
119
120impl TryIntoRequest for CreateThread<'_> {
121 fn try_into_request(self) -> Result<Request, Error> {
122 let fields = self.fields.map_err(Error::validation)?;
123
124 let mut request = Request::builder(&Route::CreateThread {
125 channel_id: self.channel_id.get(),
126 })
127 .json(&fields);
128
129 if let Some(reason) = self.reason.map_err(Error::validation)? {
130 request = request.headers(request::audit_header(reason)?);
131 }
132
133 request.build()
134 }
135}