robespierre_http/
server_information.rs

1use robespierre_models::{
2    channels::{ChannelInviteCode, ServerChannelType},
3    id::{ChannelId, ServerId, UserId},
4    servers::{PartialServer, Server, ServerField},
5};
6
7use super::impl_prelude::*;
8
9impl Http {
10    /// Fetches a server
11    pub async fn fetch_server(&self, server: ServerId) -> Result<Server> {
12        Ok(self
13            .client
14            .get(ep!(self, "/servers/{}" server))
15            .send()
16            .await?
17            .error_for_status()?
18            .json()
19            .await?)
20    }
21
22    /// Edits a server
23    pub async fn edit_server(
24        &self,
25        server_id: ServerId,
26        server: PartialServer,
27        remove: ServerField,
28    ) -> Result {
29        #[derive(serde::Serialize)]
30        struct ServerPatchRequest {
31            #[serde(flatten)]
32            server: PartialServer,
33            remove: ServerField,
34        }
35
36        self.client
37            .patch(ep!(self, "/servers/{}" server_id))
38            .json(&ServerPatchRequest { server, remove })
39            .send()
40            .await?
41            .error_for_status()?;
42
43        Ok(())
44    }
45
46    /// Deletes a server
47    pub async fn delete_server(&self, server_id: ServerId) -> Result {
48        self.client
49            .delete(ep!(self, "/servers/{}" server_id))
50            .send()
51            .await?
52            .error_for_status()?;
53
54        Ok(())
55    }
56
57    /// Creates a server
58    pub async fn create_server(
59        &self,
60        name: String,
61        description: Option<String>,
62        nsfw: Option<bool>,
63        nonce: String,
64    ) -> Result<Server> {
65        #[derive(serde::Serialize)]
66        struct CreateServerRequest {
67            name: String,
68            #[serde(skip_serializing_if = "Option::is_none")]
69            description: Option<String>,
70            #[serde(skip_serializing_if = "Option::is_none")]
71            nsfw: Option<bool>,
72            nonce: String,
73        }
74        Ok(self
75            .client_user_session_auth_type()
76            .post(ep!(self, "/servers/create"))
77            .json(&CreateServerRequest {
78                name,
79                description,
80                nsfw,
81                nonce,
82            })
83            .send()
84            .await?
85            .error_for_status()?
86            .json()
87            .await?)
88    }
89
90    /// Creates a channel
91    pub async fn create_channel(
92        &self,
93        server: ServerId,
94        kind: ServerChannelType,
95        name: String,
96        description: Option<String>,
97        nonce: String,
98    ) -> Result {
99        #[derive(serde::Serialize)]
100        struct CreateServerChannelRequest {
101            #[serde(rename = "type")]
102            kind: ServerChannelType,
103            name: String,
104            #[serde(skip_serializing_if = "Option::is_none")]
105            description: Option<String>,
106            nonce: String,
107        }
108
109        self.client
110            .post(ep!(self, "/servers/{}/channels" server))
111            .json(&CreateServerChannelRequest {
112                kind,
113                name,
114                description,
115                nonce,
116            })
117            .send()
118            .await?
119            .error_for_status()?;
120
121        Ok(())
122    }
123
124    /// Fetches invites in server
125    pub async fn fetch_invites(&self, server: ServerId) -> Result<Vec<FetchInviteResult>> {
126        Ok(self
127            .client
128            .get(ep!(self, "/servers/{}/invites" server))
129            .send()
130            .await?
131            .error_for_status()?
132            .json()
133            .await?)
134    }
135
136    /// Marks server as read
137    pub async fn mark_server_as_read(&self, server: ServerId) -> Result {
138        self.client_user_session_auth_type()
139            .put(ep!(self, "/servers/{}/ack" server))
140            .send()
141            .await?
142            .error_for_status()?;
143        Ok(())
144    }
145}
146
147#[derive(serde::Deserialize, Debug, Clone)]
148#[serde(tag = "type")]
149pub enum FetchInviteResult {
150    Server {
151        #[serde(rename = "_id")]
152        id: ChannelInviteCode,
153        server: ServerId,
154        creator: UserId,
155        channel: ChannelId,
156    },
157}