Populate nekomata pfps
All checks were successful
Build & Test / build-run (push) Successful in 44s

Now that there's an init function I should probably fail early on invalid knownsoftware json
This commit is contained in:
CenTdemeern1 2025-02-13 05:15:06 +01:00
parent 8c7a845884
commit 5c06cee68a
6 changed files with 66 additions and 14 deletions

1
Cargo.lock generated
View file

@ -1527,6 +1527,7 @@ dependencies = [
"base64",
"bytes",
"encoding_rs",
"futures-channel",
"futures-core",
"futures-util",
"h2 0.4.7",

View file

@ -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"] }

View file

@ -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
]

View 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())
}

View file

@ -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())

View file

@ -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");