roblox_api/api/friends/
v1.rs1use serde::{Deserialize, Serialize};
2
3use crate::{Error, client::Client};
4
5pub const URL: &str = "https://friends.roblox.com/v1";
6
7#[derive(Clone, Debug, Deserialize, PartialEq, Eq)]
8pub struct FollowingStatus {
9 #[serde(rename = "userId")]
10 pub id: u64,
11 #[serde(rename = "isFollowing")]
12 pub is_following: bool,
13 #[serde(rename = "isFollowed")]
14 pub is_followed: bool,
15}
16
17async fn generic_count(client: &mut Client, path: &str) -> Result<u16, Error> {
18 let result = client
19 .requestor
20 .client
21 .get(format!("{URL}/{path}/count"))
22 .headers(client.requestor.default_headers.clone())
23 .send()
24 .await;
25
26 #[derive(Debug, Deserialize)]
27 struct Response {
28 count: u16,
29 }
30
31 let response = client.validate_response(result).await?;
32 Ok(client
33 .requestor
34 .parse_json::<Response>(response)
35 .await?
36 .count)
37}
38
39pub async fn friend_requests_count(client: &mut Client) -> Result<u16, Error> {
40 generic_count(client, "user/friend-requests").await
41}
42
43pub async fn new_friend_requests_count(client: &mut Client) -> Result<u16, Error> {
44 generic_count(client, "my/new-friend-requests").await
45}
46
47pub async fn user_friends_count(client: &mut Client, id: u64) -> Result<u16, Error> {
48 generic_count(client, &format!("users/{id}/friends")).await
49}
50
51pub async fn user_followings_count(client: &mut Client, id: u64) -> Result<u16, Error> {
52 generic_count(client, &format!("users/{id}/followings")).await
53}
54
55pub async fn user_followers_count(client: &mut Client, id: u64) -> Result<u16, Error> {
56 generic_count(client, &format!("users/{id}/followers")).await
57}
58
59pub async fn following_status(
60 client: &mut Client,
61 ids: &[u64],
62) -> Result<Vec<FollowingStatus>, Error> {
63 #[derive(Debug, Serialize)]
64 struct Request<'a> {
65 #[serde(rename = "targetUserIds")]
66 user_ids: &'a [u64],
67 }
68
69 let result = client
70 .requestor
71 .client
72 .post(format!("{URL}/user/following-exists"))
73 .json(&Request { user_ids: ids })
74 .headers(client.requestor.default_headers.clone())
75 .send()
76 .await;
77
78 #[derive(Debug, Deserialize)]
79 struct Response {
80 #[serde(rename = "followings")]
81 statuses: Vec<FollowingStatus>,
82 }
83
84 let response = client.validate_response(result).await?;
85 Ok(client
86 .requestor
87 .parse_json::<Response>(response)
88 .await?
89 .statuses)
90}
91
92