1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
pub mod models;

use self::models::User;

use async_trait::async_trait;
use reqwest::Method;
use std::collections::HashMap;

#[async_trait]
pub trait UserBuilder {
    async fn new(self, client: &mut crate::Https) -> User;
}

#[async_trait]
impl UserBuilder for &str {
    /// Create a new user by name
    async fn new(self, client: &mut crate::Https) -> User {
        let mut map = HashMap::new();
        map.insert("usernames", vec![self]);

        let data = client
            .post(&format!("{}/usernames/users", crate::USER))
            .await
            .json(&map)
            .header("content-length", serde_json::to_vec(&map).unwrap().len())
            .send()
            .await
            .expect("Failed to get user info")
            .json::<serde_json::Value>()
            .await
            .expect("Failed to get user json");

        if let Some(users) = data.get("data") {
            if let Some(id) = users[0].get("id") {
                let builder = id.as_i64().unwrap() as u64;
                builder.new(client).await
            } else {
                panic!("Failed to find users with given name")
            }
        } else {
            panic!("Request failed")
        }
    }
}

#[async_trait]
impl UserBuilder for u64 {
    /// Create a new user with userid
    async fn new(self, client: &mut crate::Https) -> User {
        let user: User = client
            .client
            .request(Method::GET, &format!("{}/users/{}", crate::BASE, self))
            .send()
            .await
            .expect("Failed to get user info from base")
            .json()
            .await
            .expect("Failed to update struct with base");

        let mut u2 = User {
            id: Some(self),
            username: user.username,
            avatarfinal: user.avatarfinal,
            avataruri: user.avataruri,
            isonline: user.isonline,
            ..client
                .client
                .request(Method::GET, &format!("{}/users/{}", crate::USER, self))
                .send()
                .await
                .expect("Failed to get user info from user")
                .json()
                .await
                .expect("Failed to update struct with user")
        };

        u2.client = Some(client.clone());
        u2
    }
}

impl User {
    /// Get all friends of user, requires cookie
    pub async fn friends(&mut self) -> Vec<User> {
        if let Some(friends) = self.friends.clone() {
            friends
        } else {
            let mut friends: Vec<User> = vec![];
            let mut page: i32 = 1;
            let mut page_string = format!("?page={}", page);

            let mut data = self
                .client
                .as_mut()
                .unwrap()
                .client
                .request(
                    Method::GET,
                    &format!(
                        "{}/users/{}/friends{}",
                        crate::BASE,
                        self.id.unwrap(),
                        page_string
                    ),
                )
                .send()
                .await
                .expect("Failed to get friends list")
                .json::<Vec<User>>()
                .await
                .expect("Failed to get friends json");

            loop {
                friends.extend_from_slice(&data[..]);

                page += 1;
                page_string = format!("?page={}", page);
                data = self
                    .client
                    .as_mut()
                    .unwrap()
                    .client
                    .request(
                        Method::GET,
                        &format!(
                            "{}/users/{}/friends{}",
                            crate::BASE,
                            self.id.unwrap(),
                            page_string
                        ),
                    )
                    .send()
                    .await
                    .expect("Failed to get friends list")
                    .json::<Vec<User>>()
                    .await
                    .expect("Failed to get friends json");

                if data.is_empty() {
                    break;
                }
            }

            self.friends = Some(friends.clone());
            friends
        }
    }

    /// Check if user has asset, may require cookie
    pub async fn has_asset(&mut self, asset_id: u64) -> bool {
        self.client
            .as_mut()
            .unwrap()
            .client
            .request(
                Method::GET,
                &format!(
                    "{}/ownership/hasasset?userId={}&assetId={}",
                    crate::BASE,
                    self.id.unwrap(),
                    asset_id
                ),
            )
            .send()
            .await
            .expect("Failed to get ownership info")
            .json::<bool>()
            .await
            .expect("Failed to get ownership json")
    }
}