Skip to main content

twilight_http/request/channel/
follow_news_channel.rs

1use 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::FollowedChannel,
12    id::{Id, marker::ChannelMarker},
13};
14use twilight_validate::request::{ValidationError, audit_reason as validate_audit_reason};
15
16#[derive(Serialize)]
17struct FollowNewsChannelFields {
18    webhook_channel_id: Id<ChannelMarker>,
19}
20
21/// Follow a news channel by [`Id<ChannelMarker>`]s.
22#[must_use = "requests must be configured and executed"]
23pub struct FollowNewsChannel<'a> {
24    channel_id: Id<ChannelMarker>,
25    fields: FollowNewsChannelFields,
26    http: &'a Client,
27    reason: Result<Option<&'a str>, ValidationError>,
28}
29
30impl<'a> FollowNewsChannel<'a> {
31    pub(crate) const fn new(
32        http: &'a Client,
33        channel_id: Id<ChannelMarker>,
34        webhook_channel_id: Id<ChannelMarker>,
35    ) -> Self {
36        Self {
37            channel_id,
38            http,
39            fields: FollowNewsChannelFields { webhook_channel_id },
40            reason: Ok(None),
41        }
42    }
43}
44
45impl<'a> AuditLogReason<'a> for FollowNewsChannel<'a> {
46    fn reason(mut self, reason: &'a str) -> Self {
47        self.reason = validate_audit_reason(reason).and(Ok(Some(reason)));
48
49        self
50    }
51}
52
53impl IntoFuture for FollowNewsChannel<'_> {
54    type Output = Result<Response<FollowedChannel>, Error>;
55
56    type IntoFuture = ResponseFuture<FollowedChannel>;
57
58    fn into_future(self) -> Self::IntoFuture {
59        let http = self.http;
60
61        match self.try_into_request() {
62            Ok(request) => http.request(request),
63            Err(source) => ResponseFuture::error(source),
64        }
65    }
66}
67
68impl TryIntoRequest for FollowNewsChannel<'_> {
69    fn try_into_request(self) -> Result<Request, Error> {
70        let mut request = Request::builder(&Route::FollowNewsChannel {
71            channel_id: self.channel_id.get(),
72        })
73        .json(&self.fields);
74
75        if let Some(reason) = self.reason.map_err(Error::validation)? {
76            request = request.headers(request::audit_header(reason)?);
77        }
78
79        request.build()
80    }
81}