twilight_http/request/channel/webhook/
delete_webhook_message.rs

1use crate::{
2    client::Client,
3    error::Error,
4    request::{self, AuditLogReason, Request, TryIntoRequest},
5    response::{marker::EmptyBody, Response, ResponseFuture},
6    routing::Route,
7};
8use std::future::IntoFuture;
9use twilight_model::id::{
10    marker::{ChannelMarker, MessageMarker, WebhookMarker},
11    Id,
12};
13use twilight_validate::request::{audit_reason as validate_audit_reason, ValidationError};
14
15/// Delete a message created by a webhook.
16///
17/// # Examples
18///
19/// ```no_run
20/// # use twilight_http::Client;
21/// use twilight_http::request::AuditLogReason;
22/// use twilight_model::id::Id;
23///
24/// # #[tokio::main]
25/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
26/// # let client = Client::new("token".to_owned());
27/// client
28///     .delete_webhook_message(Id::new(1), "token here", Id::new(2))
29///     .reason("reason here")
30///     .await?;
31/// # Ok(()) }
32/// ```
33#[must_use = "requests must be configured and executed"]
34pub struct DeleteWebhookMessage<'a> {
35    http: &'a Client,
36    message_id: Id<MessageMarker>,
37    reason: Result<Option<&'a str>, ValidationError>,
38    thread_id: Option<Id<ChannelMarker>>,
39    token: &'a str,
40    webhook_id: Id<WebhookMarker>,
41}
42
43impl<'a> DeleteWebhookMessage<'a> {
44    pub(crate) const fn new(
45        http: &'a Client,
46        webhook_id: Id<WebhookMarker>,
47        token: &'a str,
48        message_id: Id<MessageMarker>,
49    ) -> Self {
50        Self {
51            http,
52            message_id,
53            reason: Ok(None),
54            thread_id: None,
55            token,
56            webhook_id,
57        }
58    }
59
60    /// Delete in a thread belonging to the channel instead of the channel
61    /// itself.
62    pub fn thread_id(mut self, thread_id: Id<ChannelMarker>) -> Self {
63        self.thread_id.replace(thread_id);
64
65        self
66    }
67}
68
69impl<'a> AuditLogReason<'a> for DeleteWebhookMessage<'a> {
70    fn reason(mut self, reason: &'a str) -> Self {
71        self.reason = validate_audit_reason(reason).and(Ok(Some(reason)));
72
73        self
74    }
75}
76
77impl IntoFuture for DeleteWebhookMessage<'_> {
78    type Output = Result<Response<EmptyBody>, Error>;
79
80    type IntoFuture = ResponseFuture<EmptyBody>;
81
82    fn into_future(self) -> Self::IntoFuture {
83        let http = self.http;
84
85        match self.try_into_request() {
86            Ok(request) => http.request(request),
87            Err(source) => ResponseFuture::error(source),
88        }
89    }
90}
91
92impl TryIntoRequest for DeleteWebhookMessage<'_> {
93    fn try_into_request(self) -> Result<Request, Error> {
94        let mut request = Request::builder(&Route::DeleteWebhookMessage {
95            message_id: self.message_id.get(),
96            thread_id: self.thread_id.map(Id::get),
97            token: self.token,
98            webhook_id: self.webhook_id.get(),
99        })
100        .use_authorization_token(false);
101
102        if let Some(reason) = self.reason.map_err(Error::validation)? {
103            request = request.headers(request::audit_header(reason)?);
104        }
105
106        request.build()
107    }
108}
109
110#[cfg(test)]
111mod tests {
112    use super::DeleteWebhookMessage;
113    use crate::{
114        client::Client,
115        request::{Request, TryIntoRequest},
116        routing::Route,
117    };
118    use twilight_model::id::Id;
119
120    #[test]
121    fn request() {
122        let client = Client::new("token".to_owned());
123        let builder = DeleteWebhookMessage::new(&client, Id::new(1), "token", Id::new(2));
124        let actual = builder
125            .try_into_request()
126            .expect("failed to create request");
127
128        let expected = Request::from_route(&Route::DeleteWebhookMessage {
129            message_id: 2,
130            thread_id: None,
131            token: "token",
132            webhook_id: 1,
133        });
134
135        assert_eq!(expected.body, actual.body);
136        assert_eq!(expected.path, actual.path);
137    }
138}