Skip to main content

twilight_http/request/channel/thread/
get_public_archived_threads.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::{
11    channel::thread::ThreadsListing,
12    id::{Id, marker::ChannelMarker},
13};
14
15/// Returns archived public threads in the channel.
16///
17/// Requires the [`READ_MESSAGE_HISTORY`] permission.
18///
19/// Threads are ordered by [`archive_timestamp`] in descending order.
20///
21/// When called in a [`GuildText`] channel, returns [`PublicThread`]s.
22///
23/// When called in a [`GuildAnnouncement`] channel, returns [`AnnouncementThread`]s.
24///
25/// [`AnnouncementThread`]: twilight_model::channel::ChannelType::AnnouncementThread
26/// [`archive_timestamp`]: twilight_model::channel::thread::ThreadMetadata::archive_timestamp
27/// [`GuildAnnouncement`]: twilight_model::channel::ChannelType::GuildAnnouncement
28/// [`GuildText`]: twilight_model::channel::ChannelType::GuildText
29/// [`PublicThread`]: twilight_model::channel::ChannelType::PublicThread
30/// [`READ_MESSAGE_HISTORY`]: twilight_model::guild::Permissions::READ_MESSAGE_HISTORY
31#[must_use = "requests must be configured and executed"]
32pub struct GetPublicArchivedThreads<'a> {
33    before: Option<&'a str>,
34    channel_id: Id<ChannelMarker>,
35    http: &'a Client,
36    limit: Option<u64>,
37}
38
39impl<'a> GetPublicArchivedThreads<'a> {
40    pub(crate) const fn new(http: &'a Client, channel_id: Id<ChannelMarker>) -> Self {
41        Self {
42            before: None,
43            channel_id,
44            http,
45            limit: None,
46        }
47    }
48
49    /// Return threads before this ISO 8601 timestamp.
50    pub const fn before(mut self, before: &'a str) -> Self {
51        self.before = Some(before);
52
53        self
54    }
55
56    /// Maximum number of threads to return.
57    pub const fn limit(mut self, limit: u64) -> Self {
58        self.limit = Some(limit);
59
60        self
61    }
62}
63
64#[cfg(not(target_os = "wasi"))]
65impl IntoFuture for GetPublicArchivedThreads<'_> {
66    type Output = Result<Response<ThreadsListing>, Error>;
67
68    type IntoFuture = ResponseFuture<ThreadsListing>;
69
70    fn into_future(self) -> Self::IntoFuture {
71        let http = self.http;
72
73        match self.try_into_request() {
74            Ok(request) => http.request(request),
75            Err(source) => ResponseFuture::error(source),
76        }
77    }
78}
79
80impl TryIntoRequest for GetPublicArchivedThreads<'_> {
81    fn try_into_request(self) -> Result<Request, Error> {
82        Ok(Request::from_route(&Route::GetPublicArchivedThreads {
83            before: self.before,
84            channel_id: self.channel_id.get(),
85            limit: self.limit,
86        }))
87    }
88}