use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use serde_json::json;
use crate::authentification::{Credentials, CredentialsOrApiKey, Profile};
use crate::error::Result;
use crate::http::{client, encode, ok};
use crate::parrot::chat::ChatSummary;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Friend {
pub id: String,
pub person: Profile,
pub hidden: bool,
#[serde(default)]
pub chats: Vec<ChatSummary>,
pub created: DateTime<Utc>,
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Settings {
pub hide_strangers: bool,
}
#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PartialSettings {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub hide_strangers: Option<bool>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TrustEdge {
pub target: String,
pub weight: f64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StartedChat {
pub friend: Friend,
pub chat: String,
}
#[derive(Deserialize)]
struct Created {
id: String,
}
pub async fn list(authorization: &CredentialsOrApiKey) -> Result<Vec<Friend>> {
let response = client()
.get(format!("{}/friends", crate::get_url()))
.header(
"Authorization",
match authorization {
CredentialsOrApiKey::Credentials(credentials) => {
format!("Token {}", credentials.token)
}
CredentialsOrApiKey::ApiKey(key) => format!("ApiKey {}", key.id),
},
)
.send()
.await?;
Ok(ok(response).await?.json().await?)
}
pub async fn start_chat(
credentials: &Credentials,
email: &str,
name: Option<&str>,
) -> Result<StartedChat> {
let response = client()
.post(format!("{}/friends/chats", crate::get_url()))
.header("Authorization", format!("Token {}", credentials.token))
.json(&json!({ "email": email, "name": name }))
.send()
.await?;
Ok(ok(response).await?.json().await?)
}
pub async fn create_chat(
credentials: &Credentials,
friend: &str,
name: Option<&str>,
) -> Result<String> {
let response = client()
.post(format!(
"{}/friends/{}/chats",
crate::get_url(),
encode(friend)
))
.header("Authorization", format!("Token {}", credentials.token))
.json(&json!({ "name": name }))
.send()
.await?;
let created: Created = ok(response).await?.json().await?;
Ok(created.id)
}
pub async fn delete_chat(credentials: &Credentials, friend: &str, chat: &str) -> Result<()> {
let response = client()
.delete(format!(
"{}/friends/{}/chats/{}",
crate::get_url(),
encode(friend),
encode(chat)
))
.header("Authorization", format!("Token {}", credentials.token))
.send()
.await?;
ok(response).await?;
Ok(())
}
pub async fn show(credentials: &Credentials, friend: &str) -> Result<Friend> {
let response = client()
.post(format!(
"{}/friends/{}/show",
crate::get_url(),
encode(friend)
))
.header("Authorization", format!("Token {}", credentials.token))
.send()
.await?;
Ok(ok(response).await?.json().await?)
}
pub async fn end(credentials: &Credentials, friend: &str) -> Result<()> {
let response = client()
.delete(format!("{}/friends/{}", crate::get_url(), encode(friend)))
.header("Authorization", format!("Token {}", credentials.token))
.send()
.await?;
ok(response).await?;
Ok(())
}
pub async fn settings(credentials: &Credentials) -> Result<Settings> {
let response = client()
.get(format!("{}/friends/settings", crate::get_url()))
.header("Authorization", format!("Token {}", credentials.token))
.send()
.await?;
Ok(ok(response).await?.json().await?)
}
pub async fn update_settings(
credentials: &Credentials,
update: &PartialSettings,
) -> Result<Settings> {
let response = client()
.put(format!("{}/friends/settings", crate::get_url()))
.header("Authorization", format!("Token {}", credentials.token))
.json(update)
.send()
.await?;
Ok(ok(response).await?.json().await?)
}
pub async fn trust(authorization: &CredentialsOrApiKey) -> Result<Vec<TrustEdge>> {
let response = client()
.get(format!("{}/friends/trust", crate::get_url()))
.header(
"Authorization",
match authorization {
CredentialsOrApiKey::Credentials(credentials) => {
format!("Token {}", credentials.token)
}
CredentialsOrApiKey::ApiKey(key) => format!("ApiKey {}", key.id),
},
)
.send()
.await?;
Ok(ok(response).await?.json().await?)
}