robespierre_http/
account.rs

1use robespierre_models::{
2    auth::{Account, Session, SessionInfo},
3    id::SessionId,
4};
5
6use super::impl_prelude::*;
7
8impl Http {
9    // account
10    pub async fn fetch_account(&self) -> Result<Account> {
11        Ok(self
12            .client_user_session_auth_type()
13            .get(ep!(self, "/auth/account"))
14            .send()
15            .await?
16            .error_for_status()?
17            .json()
18            .await?)
19    }
20
21    pub async fn create_account(
22        email: &str,
23        password: &str,
24        invite: Option<&str>,
25        captcha: Option<&str>,
26    ) -> Result {
27        #[derive(serde::Serialize)]
28        struct CreateAccountRequest<'a> {
29            email: &'a str,
30            password: &'a str,
31            #[serde(skip_serializing_if = "Option::is_none")]
32            invite: Option<&'a str>,
33            #[serde(skip_serializing_if = "Option::is_none")]
34            captcha: Option<&'a str>,
35        }
36
37        reqwest::Client::new()
38            .post(ep!(
39                api_root = "https://api.revolt.chat",
40                "/auth/account/create"
41            ))
42            .json(&CreateAccountRequest {
43                email,
44                password,
45                invite,
46                captcha,
47            })
48            .send()
49            .await?
50            .error_for_status()?;
51
52        Ok(())
53    }
54
55    pub async fn resend_verification(email: &str, captcha: Option<&str>) -> Result {
56        #[derive(serde::Serialize)]
57        struct ResendVerificationRequest<'a> {
58            email: &'a str,
59            #[serde(skip_serializing_if = "Option::is_none")]
60            captcha: Option<&'a str>,
61        }
62
63        reqwest::Client::new()
64            .post(ep!(
65                api_root = "https://api.revolt.chat",
66                "/auth/account/reverify"
67            ))
68            .json(&ResendVerificationRequest { email, captcha })
69            .send()
70            .await?
71            .error_for_status()?;
72
73        Ok(())
74    }
75
76    pub async fn verify_email(code: &str) -> Result {
77        reqwest::Client::new()
78            .post(ep!(
79                api_root = "https://api.revolt.chat",
80                "/auth/account/verify/{}" code
81            ))
82            .send()
83            .await?
84            .error_for_status()?;
85
86        Ok(())
87    }
88
89    pub async fn send_password_reset(email: &str, captcha: Option<&str>) -> Result {
90        #[derive(serde::Serialize)]
91        struct SendPasswordResetRequest<'a> {
92            email: &'a str,
93            #[serde(skip_serializing_if = "Option::is_none")]
94            captcha: Option<&'a str>,
95        }
96
97        reqwest::Client::new()
98            .post(ep!(
99                api_root = "https://api.revolt.chat",
100                "/auth/account/reset_password"
101            ))
102            .json(&SendPasswordResetRequest { email, captcha })
103            .send()
104            .await?
105            .error_for_status()?;
106
107        Ok(())
108    }
109
110    pub async fn password_reset(password: &str, token: &str) -> Result {
111        #[derive(serde::Serialize)]
112        struct PasswordResetRequest<'a> {
113            password: &'a str,
114            token: &'a str,
115        }
116
117        reqwest::Client::new()
118            .patch(ep!(
119                api_root = "https://api.revolt.chat",
120                "/auth/account/reset_password"
121            ))
122            .json(&PasswordResetRequest { password, token })
123            .send()
124            .await?
125            .error_for_status()?;
126
127        Ok(())
128    }
129
130    pub async fn change_password(&self, password: &str, current_password: &str) -> Result {
131        #[derive(serde::Serialize)]
132        struct ChangePasswordRequest<'a> {
133            password: &'a str,
134            current_password: &'a str,
135        }
136
137        self.client_user_session_auth_type()
138            .post(ep!(self, "/auth/account/change/password"))
139            .json(&ChangePasswordRequest {
140                password,
141                current_password,
142            })
143            .send()
144            .await?
145            .error_for_status()?;
146
147        Ok(())
148    }
149
150    pub async fn change_email(&self, email: &str, current_password: &str) -> Result {
151        #[derive(serde::Serialize)]
152        struct ChangeEmailRequest<'a> {
153            email: &'a str,
154            current_password: &'a str,
155        }
156
157        self.client_user_session_auth_type()
158            .post(ep!(self, "/auth/account/change/email"))
159            .json(&ChangeEmailRequest {
160                email,
161                current_password,
162            })
163            .send()
164            .await?
165            .error_for_status()?;
166
167        Ok(())
168    }
169
170    pub async fn login(
171        email: &str,
172        password: Option<&str>,
173        challange: Option<&str>,
174        friendly_name: Option<&str>,
175        captcha: Option<&str>,
176    ) -> Result<Session> {
177        #[derive(serde::Serialize)]
178        struct LoginRequest<'a> {
179            email: &'a str,
180            #[serde(skip_serializing_if = "Option::is_none")]
181            password: Option<&'a str>,
182            #[serde(skip_serializing_if = "Option::is_none")]
183            challange: Option<&'a str>,
184            #[serde(skip_serializing_if = "Option::is_none")]
185            friendly_name: Option<&'a str>,
186            #[serde(skip_serializing_if = "Option::is_none")]
187            captcha: Option<&'a str>,
188        }
189
190        Ok(reqwest::Client::new()
191            .post(ep!(
192                api_root = "https://api.revolt.chat",
193                "/auth/session/login"
194            ))
195            .json(&LoginRequest {
196                email,
197                password,
198                challange,
199                friendly_name,
200                captcha,
201            })
202            .send()
203            .await?
204            .error_for_status()?
205            .json()
206            .await?)
207    }
208
209    pub async fn logout(self) -> Result {
210        self.client_user_session_auth_type()
211            .delete(ep!(self, "/auth/session/logout"))
212            .send()
213            .await?
214            .error_for_status()?;
215
216        Ok(())
217    }
218
219    pub async fn edit_session(&self, session: SessionId, friendly_name: &str) -> Result {
220        #[derive(serde::Serialize)]
221        struct EditSessionRequest<'a> {
222            friendly_name: &'a str,
223        }
224
225        self.client_user_session_auth_type()
226            .patch(ep!(self, "/auth/session/{}" session))
227            .json(&EditSessionRequest { friendly_name })
228            .send()
229            .await?
230            .error_for_status()?;
231
232        Ok(())
233    }
234
235    pub async fn delete_session(&self, session: SessionId) -> Result {
236        self.client_user_session_auth_type()
237            .delete(ep!(self, "/auth/session/{}" session))
238            .send()
239            .await?
240            .error_for_status()?;
241
242        Ok(())
243    }
244
245    pub async fn fetch_sessions(&self) -> Result<Vec<SessionInfo>> {
246        Ok(self
247            .client_user_session_auth_type()
248            .get(ep!(self, "/auth/session/all"))
249            .send()
250            .await?
251            .error_for_status()?
252            .json()
253            .await?)
254    }
255
256    pub async fn delete_all_sessions(&self, revoke_self: bool) -> Result {
257        self.client_user_session_auth_type()
258            .delete(ep!(self, "/auth/session/all"))
259            .query(&[("revoke_self", revoke_self)])
260            .send()
261            .await?
262            .error_for_status()?;
263
264        Ok(())
265    }
266}