Add about section #6
6 changed files with 66 additions and 14 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -1527,6 +1527,7 @@ dependencies = [
|
|||
"base64",
|
||||
"bytes",
|
||||
"encoding_rs",
|
||||
"futures-channel",
|
||||
"futures-core",
|
||||
"futures-util",
|
||||
"h2 0.4.7",
|
||||
|
|
|
@ -6,7 +6,7 @@ edition = "2021"
|
|||
[dependencies]
|
||||
bytes = "1.9.0"
|
||||
favicon-scraper = "0.3.1"
|
||||
reqwest = { version = "0.12.12", features = ["stream"] }
|
||||
reqwest = { version = "0.12.12", features = ["stream", "blocking"] }
|
||||
rocket = { version = "0.5.1", features = ["json"] }
|
||||
semver = "1.0.24"
|
||||
serde = { version = "1.0.217", features = ["derive"] }
|
||||
|
|
|
@ -1,11 +1,17 @@
|
|||
use rocket::Route;
|
||||
|
||||
pub mod instance_info;
|
||||
pub mod nekomata_avatars;
|
||||
pub mod proxy;
|
||||
|
||||
pub fn init() {
|
||||
nekomata_avatars::init();
|
||||
}
|
||||
|
||||
pub fn get_routes() -> Vec<Route> {
|
||||
routes![
|
||||
instance_info::instance_info,
|
||||
nekomata_avatars::nekomata_avatars,
|
||||
// Proxy is temporarily disabled as it's not needed
|
||||
// proxy::proxy
|
||||
]
|
||||
|
|
49
src/api/nekomata_avatars.rs
Normal file
49
src/api/nekomata_avatars.rs
Normal file
|
@ -0,0 +1,49 @@
|
|||
use reqwest::blocking::Client;
|
||||
use rocket::serde::json::Json;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::json;
|
||||
use std::sync::OnceLock;
|
||||
|
||||
#[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();
|
||||
println!("{body}");
|
||||
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("/nekomata_avatars")]
|
||||
pub async fn nekomata_avatars() -> Json<&'static Avatars> {
|
||||
Json(AVATARS.get().unwrap())
|
||||
}
|
|
@ -44,6 +44,8 @@ fn route_for_unknown_instance_software(instance: &str, route: PathBuf) -> (Conte
|
|||
|
||||
#[launch]
|
||||
fn rocket() -> _ {
|
||||
api::init();
|
||||
|
||||
rocket::build()
|
||||
.mount("/static", FileServer::from("static").rank(0))
|
||||
.mount("/api", api::get_routes())
|
||||
|
|
|
@ -1,17 +1,11 @@
|
|||
import { findImageOrFail } from "./dom.mjs";
|
||||
|
||||
async function populateUser(selector: string, origin: string, userId: string) {
|
||||
const avatarImage = findImageOrFail(document.body, selector);
|
||||
const userData = await fetch(`https://${origin}/api/users/show`, {
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
userId,
|
||||
})
|
||||
}).then(r => r.json());
|
||||
if (userData.avatarUrl) {
|
||||
avatarImage.src = userData.avatarUrl;
|
||||
}
|
||||
async function populateUsers(charlotteSelector: string, kioSelector: string) {
|
||||
const charlotteImage = findImageOrFail(document.body, charlotteSelector);
|
||||
const kioImage = findImageOrFail(document.body, kioSelector);
|
||||
const { charlotte, kio } = await fetch("/api/nekomata_avatars").then(r => r.json());
|
||||
if (charlotte) charlotteImage.src = charlotte;
|
||||
if (kio) kioImage.src = kio;
|
||||
}
|
||||
|
||||
populateUser("#charlotteAvatar", "eepy.moe", "9xt2s326nxev039h");
|
||||
populateUser("#kioAvatar", "kitsunes.club", "9810gvfne3");
|
||||
populateUsers("#charlotteAvatar", "#kioAvatar");
|
||||
|
|
Loading…
Add table
Reference in a new issue