1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
/*
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 <metamuffin.org>
*/
use super::ui::{
account::{login_logic, session::AdminSession, LoginForm},
error::MyResult,
};
use crate::database::Database;
use anyhow::{anyhow, Context};
use jellycommon::Node;
use rocket::{
get,
http::MediaType,
outcome::Outcome,
post,
request::{self, FromRequest},
response::Redirect,
serde::json::Json,
Request, State,
};
use serde_json::{json, Value};
use std::ops::Deref;
#[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 {
"2"
}
#[post("/api/create_session", data = "<data>")]
pub fn r_api_account_login(database: &State<Database>, data: Json<LoginForm>) -> MyResult<Value> {
let token = login_logic(database, &data.username, &data.password)?;
Ok(json!(token))
}
#[get("/api/node_raw/<id>")]
pub fn r_api_node_raw(
admin: AdminSession,
database: &State<Database>,
id: &str,
) -> MyResult<Json<Node>> {
drop(admin);
let node = database
.node
.get(&id.to_string())
.context("retrieving library node")?
.ok_or(anyhow!("node does not exist"))?;
Ok(Json(node))
}
pub struct AcceptJson(bool);
impl Deref for AcceptJson {
type Target = bool;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<'r> FromRequest<'r> for AcceptJson {
type Error = ();
fn from_request<'life0, 'async_trait>(
request: &'r Request<'life0>,
) -> ::core::pin::Pin<
Box<
dyn ::core::future::Future<Output = request::Outcome<Self, Self::Error>>
+ ::core::marker::Send
+ 'async_trait,
>,
>
where
'r: 'async_trait,
'life0: 'async_trait,
Self: 'async_trait,
{
Box::pin(async move {
Outcome::Success(AcceptJson(
request
.accept()
.map(|a| a.preferred().exact_eq(&MediaType::JSON))
.unwrap_or(false),
))
})
}
}
|