Skip to main content

twilight_http/request/channel/thread/
get_thread_members.rs

1#[cfg(not(target_os = "wasi"))]
2use crate::response::{Response, ResponseFuture, marker::ListBody};
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::ThreadMember,
12    id::{
13        Id,
14        marker::{ChannelMarker, UserMarker},
15    },
16};
17use twilight_validate::channel::{ChannelValidationError, thread_member_limit};
18
19struct GetThreadMembersFields {
20    after: Option<Id<UserMarker>>,
21    limit: Option<u32>,
22    with_member: Option<bool>,
23}
24
25/// Returns the [`ThreadMember`]s of the thread.
26///
27/// [`ThreadMember`]: twilight_model::channel::thread::ThreadMember
28#[must_use = "requests must be configured and executed"]
29pub struct GetThreadMembers<'a> {
30    channel_id: Id<ChannelMarker>,
31    fields: Result<GetThreadMembersFields, ChannelValidationError>,
32    http: &'a Client,
33}
34
35impl<'a> GetThreadMembers<'a> {
36    pub(crate) const fn new(http: &'a Client, channel_id: Id<ChannelMarker>) -> Self {
37        Self {
38            channel_id,
39            fields: Ok(GetThreadMembersFields {
40                after: None,
41                limit: None,
42                with_member: None,
43            }),
44            http,
45        }
46    }
47
48    /// Fetch the thread members after the user ID.
49    pub const fn after(mut self, after: Id<UserMarker>) -> Self {
50        if let Ok(fields) = self.fields.as_mut() {
51            fields.after = Some(after);
52        }
53
54        self
55    }
56
57    /// The maximum number of thread members to return.
58    ///
59    /// Must be between 1 and 100. Defaults to 100.
60    ///
61    /// # Errors
62    ///
63    /// Returns a [`ChannelValidationErrorType::ThreadMemberLimitInvalid`] error type if the
64    /// limit is not between 1 and 100.
65    ///
66    /// [`ChannelValidationErrorType::ThreadMemberLimitInvalid`]: twilight_validate::channel::ChannelValidationErrorType::ThreadMemberLimitInvalid
67    pub fn limit(mut self, limit: u32) -> Self {
68        self.fields = self.fields.and_then(|mut fields| {
69            thread_member_limit(limit)?;
70            fields.limit = Some(limit);
71
72            Ok(fields)
73        });
74
75        self
76    }
77
78    /// Include the associated guild members for each thread member.
79    pub const fn with_member(mut self, with_member: bool) -> Self {
80        if let Ok(fields) = self.fields.as_mut() {
81            fields.with_member = Some(with_member);
82        }
83
84        self
85    }
86}
87
88#[cfg(not(target_os = "wasi"))]
89impl IntoFuture for GetThreadMembers<'_> {
90    type Output = Result<Response<ListBody<ThreadMember>>, Error>;
91
92    type IntoFuture = ResponseFuture<ListBody<ThreadMember>>;
93
94    fn into_future(self) -> Self::IntoFuture {
95        let http = self.http;
96
97        match self.try_into_request() {
98            Ok(request) => http.request(request),
99            Err(source) => ResponseFuture::error(source),
100        }
101    }
102}
103
104impl TryIntoRequest for GetThreadMembers<'_> {
105    fn try_into_request(self) -> Result<Request, Error> {
106        let fields = self.fields.map_err(Error::validation)?;
107
108        Ok(Request::from_route(&Route::GetThreadMembers {
109            after: fields.after.map(Id::get),
110            channel_id: self.channel_id.get(),
111            limit: fields.limit,
112            with_member: fields.with_member,
113        }))
114    }
115}