1use chrono::{DateTime, Utc};
2use serde::{Deserialize, Serialize};
3use serde_json::json;
4
5use crate::authentification::{Credentials, CredentialsOrApiKey, Profile};
6use crate::error::Result;
7use crate::http::{client, encode, ok};
8use crate::parrot::chat::ChatSummary;
9
10#[derive(Debug, Clone, Serialize, Deserialize)]
12pub struct Friend {
13 pub id: String,
14 pub person: Profile,
15 pub hidden: bool,
18 #[serde(default)]
19 pub chats: Vec<ChatSummary>,
20 pub created: DateTime<Utc>,
21}
22
23#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
24#[serde(rename_all = "camelCase")]
25pub struct Settings {
26 pub hide_strangers: bool,
29}
30
31#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize)]
32#[serde(rename_all = "camelCase")]
33pub struct PartialSettings {
34 #[serde(default, skip_serializing_if = "Option::is_none")]
35 pub hide_strangers: Option<bool>,
36}
37
38#[derive(Debug, Clone, Serialize, Deserialize)]
41pub struct TrustEdge {
42 pub target: String,
43 pub weight: f64,
44}
45
46#[derive(Debug, Clone, Serialize, Deserialize)]
48pub struct StartedChat {
49 pub friend: Friend,
50 pub chat: String,
51}
52
53#[derive(Deserialize)]
54struct Created {
55 id: String,
56}
57
58pub async fn list(authorization: &CredentialsOrApiKey) -> Result<Vec<Friend>> {
61 let response = client()
62 .get(format!("{}/friends", crate::get_url()))
63 .header(
64 "Authorization",
65 match authorization {
66 CredentialsOrApiKey::Credentials(credentials) => {
67 format!("Token {}", credentials.token)
68 }
69 CredentialsOrApiKey::ApiKey(key) => format!("ApiKey {}", key.id),
70 },
71 )
72 .send()
73 .await?;
74 Ok(ok(response).await?.json().await?)
75}
76
77pub async fn start_chat(
80 credentials: &Credentials,
81 email: &str,
82 name: Option<&str>,
83) -> Result<StartedChat> {
84 let response = client()
85 .post(format!("{}/friends/chats", crate::get_url()))
86 .header("Authorization", format!("Token {}", credentials.token))
87 .json(&json!({ "email": email, "name": name }))
88 .send()
89 .await?;
90 Ok(ok(response).await?.json().await?)
91}
92
93pub async fn create_chat(
95 credentials: &Credentials,
96 friend: &str,
97 name: Option<&str>,
98) -> Result<String> {
99 let response = client()
100 .post(format!(
101 "{}/friends/{}/chats",
102 crate::get_url(),
103 encode(friend)
104 ))
105 .header("Authorization", format!("Token {}", credentials.token))
106 .json(&json!({ "name": name }))
107 .send()
108 .await?;
109 let created: Created = ok(response).await?.json().await?;
110 Ok(created.id)
111}
112
113pub async fn delete_chat(credentials: &Credentials, friend: &str, chat: &str) -> Result<()> {
115 let response = client()
116 .delete(format!(
117 "{}/friends/{}/chats/{}",
118 crate::get_url(),
119 encode(friend),
120 encode(chat)
121 ))
122 .header("Authorization", format!("Token {}", credentials.token))
123 .send()
124 .await?;
125 ok(response).await?;
126 Ok(())
127}
128
129pub async fn show(credentials: &Credentials, friend: &str) -> Result<Friend> {
131 let response = client()
132 .post(format!(
133 "{}/friends/{}/show",
134 crate::get_url(),
135 encode(friend)
136 ))
137 .header("Authorization", format!("Token {}", credentials.token))
138 .send()
139 .await?;
140 Ok(ok(response).await?.json().await?)
141}
142
143pub async fn end(credentials: &Credentials, friend: &str) -> Result<()> {
145 let response = client()
146 .delete(format!("{}/friends/{}", crate::get_url(), encode(friend)))
147 .header("Authorization", format!("Token {}", credentials.token))
148 .send()
149 .await?;
150 ok(response).await?;
151 Ok(())
152}
153
154pub async fn settings(credentials: &Credentials) -> Result<Settings> {
155 let response = client()
156 .get(format!("{}/friends/settings", crate::get_url()))
157 .header("Authorization", format!("Token {}", credentials.token))
158 .send()
159 .await?;
160 Ok(ok(response).await?.json().await?)
161}
162
163pub async fn update_settings(
164 credentials: &Credentials,
165 update: &PartialSettings,
166) -> Result<Settings> {
167 let response = client()
168 .put(format!("{}/friends/settings", crate::get_url()))
169 .header("Authorization", format!("Token {}", credentials.token))
170 .json(update)
171 .send()
172 .await?;
173 Ok(ok(response).await?.json().await?)
174}
175
176pub async fn trust(authorization: &CredentialsOrApiKey) -> Result<Vec<TrustEdge>> {
178 let response = client()
179 .get(format!("{}/friends/trust", crate::get_url()))
180 .header(
181 "Authorization",
182 match authorization {
183 CredentialsOrApiKey::Credentials(credentials) => {
184 format!("Token {}", credentials.token)
185 }
186 CredentialsOrApiKey::ApiKey(key) => format!("ApiKey {}", key.id),
187 },
188 )
189 .send()
190 .await?;
191 Ok(ok(response).await?.json().await?)
192}