FeDirect/src/api/about.rs

54 linhas
1,3 KiB
Rust

use reqwest::blocking::Client;
use rocket::serde::json::Json;
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::sync::OnceLock;
/// Gets the FeDirect version
#[get("/about/version")]
pub async fn version() -> &'static str {
env!("CARGO_PKG_VERSION")
}
#[derive(Debug, Serialize)]
pub struct Avatars {
charlotte: Option<String>,
kio: Option<String>,
}
#[derive(Deserialize)]
struct MisskeyUser {
#[serde(rename = "avatarUrl")]
avatar_url: String,
}
fn get_avatar(client: &Client, origin: &str, user_id: &str) -> Option<String> {
let body = json!({
"userId": user_id
})
.to_string();
let user: MisskeyUser = client
.post(format!("https://{origin}/api/users/show"))
.header("content-type", "application/json")
.body(body)
.send()
.ok()?
.json()
.ok()?;
Some(user.avatar_url)
}
pub static AVATARS: OnceLock<Avatars> = OnceLock::new();
pub fn init() {
let client = Client::new();
let charlotte = get_avatar(&client, "eepy.moe", "9xt2s326nxev039h");
let kio = get_avatar(&client, "kitsunes.club", "9810gvfne3");
AVATARS.set(Avatars { charlotte, kio }).unwrap();
}
/// Gets (relatively) up-to-date Nekomata avatars
#[get("/about/nekomata_avatars")]
pub async fn nekomata_avatars() -> Json<&'static Avatars> {
Json(AVATARS.get().unwrap())
}