/* This file is part of jellything (https://codeberg.org/metamuffin/jellything) which is licensed under the GNU Affero General Public License (version 3); see /COPYING. Copyright (C) 2025 metamuffin */ use super::ui::error::MyResult; use crate::helper::{accept::AcceptJson, language::AcceptLanguage, A}; use jellycommon::{user::CreateSessionParams, NodeID, Visibility}; use jellyimport::asset_token::AssetInner; use jellylogic::{ login::login_logic, session::{AdminSession, Session}, Database, }; use jellyui::locale::get_translation_table; use rocket::{get, post, response::Redirect, serde::json::Json, Either, State}; use serde_json::{json, Value}; use std::collections::HashMap; #[get("/api")] pub fn r_api_root() -> Redirect { Redirect::moved("https://jellything.metamuffin.org/book/api.html#jellything-http-api") } #[get("/version")] pub fn r_version() -> &'static str { env!("CARGO_PKG_VERSION") } #[get("/translations")] pub fn r_translations( lang: AcceptLanguage, aj: AcceptJson, ) -> Either>, String> { let AcceptLanguage(lang) = lang; let table = get_translation_table(&lang); if *aj { Either::Left(Json(table)) } else { let mut s = String::new(); for (k, v) in table { s += k; s += "="; s += v; s += "\n"; } Either::Right(s) } } #[post("/api/create_session", data = "")] pub fn r_api_account_login( database: &State, data: Json, ) -> MyResult { let token = login_logic( database, &data.username, &data.password, data.expire, data.drop_permissions.clone(), )?; Ok(json!(token)) } #[get("/api/asset_token_raw/")] pub fn r_api_asset_token_raw(_admin: A, token: &str) -> MyResult> { Ok(Json(AssetInner::deser(token)?)) } #[get("/nodes_modified?")] pub fn r_nodes_modified_since( _session: A, database: &State, since: u64, ) -> MyResult>> { let mut nodes = database.get_nodes_modified_since(since)?; nodes.retain(|id| { database.get_node(*id).is_ok_and(|n| { n.as_ref() .is_some_and(|n| n.visibility >= Visibility::Reduced) }) }); Ok(Json(nodes)) }