Skip to main content

twilight_http/request/channel/
get_pins.rs

1#[cfg(not(target_os = "wasi"))]
2use crate::response::{Response, ResponseFuture};
3use crate::{
4    client::Client,
5    error::Error,
6    request::{Request, TryIntoRequest},
7    routing::Route,
8};
9use std::future::IntoFuture;
10use twilight_model::channel::message::PinsListing;
11use twilight_model::id::{Id, marker::ChannelMarker};
12use twilight_model::util::Timestamp;
13use twilight_validate::request::{ValidationError, pin_limit as validate_pin_limit};
14
15pub struct GetPinsQueryFields {
16    before: Option<Timestamp>,
17    limit: Option<u16>,
18}
19
20/// Get the pins of a channel.
21#[must_use = "requests must be configured and executed"]
22pub struct GetPins<'a> {
23    channel_id: Id<ChannelMarker>,
24    fields: Result<GetPinsQueryFields, ValidationError>,
25    http: &'a Client,
26}
27
28impl<'a> GetPins<'a> {
29    pub(crate) const fn new(http: &'a Client, channel_id: Id<ChannelMarker>) -> Self {
30        Self {
31            channel_id,
32            fields: Ok(GetPinsQueryFields {
33                before: None,
34                limit: None,
35            }),
36            http,
37        }
38    }
39
40    /// Sets the timestamp filter to only retrieve pins before the provided timestamp.
41    ///
42    /// [docs]
43    ///
44    /// [docs]: https://discord.com/developers/docs/resources/message#get-channel-pins-query-string-params
45    pub const fn before(mut self, timestamp: Timestamp) -> Self {
46        if let Ok(fields) = self.fields.as_mut() {
47            fields.before = Some(timestamp);
48        }
49
50        self
51    }
52
53    /// Sets the limit of pins to retrieve in this request. (1-50) (default: 50)
54    ///
55    /// [docs]
56    ///
57    /// [docs]: https://discord.com/developers/docs/resources/message#get-channel-pins-query-string-params
58    pub fn limit(mut self, limit: u16) -> Self {
59        self.fields = self.fields.and_then(|mut fields| {
60            validate_pin_limit(limit)?;
61
62            fields.limit = Some(limit);
63
64            Ok(fields)
65        });
66
67        self
68    }
69}
70
71#[cfg(not(target_os = "wasi"))]
72impl IntoFuture for GetPins<'_> {
73    type Output = Result<Response<PinsListing>, Error>;
74
75    type IntoFuture = ResponseFuture<PinsListing>;
76
77    fn into_future(self) -> Self::IntoFuture {
78        let http = self.http;
79
80        match self.try_into_request() {
81            Ok(request) => http.request(request),
82            Err(source) => ResponseFuture::error(source),
83        }
84    }
85}
86
87impl TryIntoRequest for GetPins<'_> {
88    fn try_into_request(self) -> Result<Request, Error> {
89        let fields = self.fields.map_err(Error::validation)?;
90
91        Ok(Request::from_route(&Route::GetPins {
92            channel_id: self.channel_id.get(),
93            limit: fields.limit,
94            before: fields.before.map(|t| t.iso_8601().to_string()),
95        }))
96    }
97}