fauna-api 1.0.692

The Fauna SDK for Rust: accounts, Drive, groups, calendar, chats, payments, Parrot and live rooms.
Documentation
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;

/// Someone you have people only chats with.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Friend {
    pub id: String,
    pub person: Profile,
    /// Under Hidden: they started at no trust while you hide strangers, and you
    /// haven't replied or shown them.
    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 {
    /// Keep chats from people you have no trust in under Hidden until you reply
    /// or show them. On by default.
    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>,
}

/// How much you trust someone or a group (`credentials:{id}`, `group:{id}`), in
/// [0, 1).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TrustEdge {
    pub target: String,
    pub weight: f64,
}

/// The `{ friend, chat }` `start_chat` resolves to.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StartedChat {
    pub friend: Friend,
    pub chat: String,
}

#[derive(Deserialize)]
struct Created {
    id: String,
}

/// Your friends, each with your chats together and what you haven't read. An
/// API key reads them too.
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?)
}

/// Start a chat with someone by their Fauna email. The first chat makes you
/// friends.
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?)
}

/// Another chat with a friend. Resolves to its id.
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)
}

/// Delete a chat with a friend, for both of you.
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(())
}

/// Bring a hidden friend out of Hidden.
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?)
}

/// End a friendship. Its chats and their files go with it, for both of you.
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?)
}

/// Your own trust edges, strongest first. Nobody else can read them.
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?)
}