Skip to main content

twilight_http/request/channel/webhook/
delete_webhook_message.rs

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