/* 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) 2023 metamuffin */ pub mod error; use std::path::PathBuf; use super::ui::account::{login_logic, LoginForm}; use crate::{ database::Database, library::{Library, Node}, routes::{api::error::ApiResult, ui::account::session::Session}, }; use anyhow::Context; use jellycommon::api::ApiNode; use rocket::{get, http::CookieJar, post, response::Redirect, serde::json::Json, State}; use serde_json::{json, Value}; #[get("/api")] pub fn r_api_root() -> Redirect { Redirect::moved("https://codeberg.org/metamuffin/jellything/src/branch/master/api.md") } #[get("/api/version")] pub fn r_api_version() -> &'static str { "1" } #[post("/api/account/login", data = "")] pub fn r_api_account_login( database: &State, jar: &CookieJar, data: Json, ) -> ApiResult { login_logic(jar, database, &data.username, &data.password)?; Ok(json!({ "ok": true })) } #[get("/api/library/")] pub fn r_api_library_node( _sess: Session, path: PathBuf, library: &State, ) -> ApiResult> { let node = library .nested_path(&path) .context("retrieving library node")?; Ok(Json(match node.as_ref() { Node::Directory(d) => ApiNode::Directory { identifier: d.identifier.clone(), info: d.info.clone(), children: d .children .iter() .map(|c| c.identifier().to_string()) .collect::>(), }, Node::Item(i) => ApiNode::Item { identifier: i.identifier.clone(), info: i.info.clone(), }, })) }