robespierre_http/
users_information.rs

1use robespierre_models::{
2    id::UserId,
3    users::{Profile, User, UserEditPatch},
4};
5
6use super::impl_prelude::*;
7
8impl Http {
9    /// Gets an user from the api
10    pub async fn fetch_user(&self, user_id: UserId) -> Result<User> {
11        Ok(self
12            .client
13            .get(ep!(self, "/users/{}" user_id))
14            .send()
15            .await?
16            .error_for_status()?
17            .json()
18            .await?)
19    }
20
21    /// Edits user
22    pub async fn edit_user(&self, patch: UserEditPatch) -> Result {
23        self.client
24            .patch(ep!(self, "/users/@me"))
25            .json(&patch)
26            .send()
27            .await?
28            .error_for_status()?;
29
30        Ok(())
31    }
32
33    /// Edits an username
34    pub async fn edit_username(&self, username: &str, password: &str) -> Result {
35        #[derive(serde::Serialize)]
36        struct EditUsernameRequest<'a> {
37            username: &'a str,
38            password: &'a str,
39        }
40
41        self.client_user_session_auth_type()
42            .patch(ep!(self, "/users/@me/username"))
43            .json(&EditUsernameRequest { username, password })
44            .send()
45            .await?
46            .error_for_status()?;
47
48        Ok(())
49    }
50
51    /// Gets information abot an user profile
52    pub async fn fetch_user_profile(&self, user_id: UserId) -> Result<Profile> {
53        Ok(self
54            .client
55            .get(ep!(self, "/users/{}/profile" user_id))
56            .send()
57            .await?
58            .error_for_status()?
59            .json()
60            .await?)
61    }
62
63    // TODO: fetch default avatar
64
65    pub async fn fetch_mutual_friends(&self, user_id: UserId) -> Result<Vec<UserId>> {
66        Ok(self
67            .client
68            .get(ep!(self, "/users/{}/mutual" user_id))
69            .send()
70            .await?
71            .error_for_status()?
72            .json()
73            .await?)
74    }
75}