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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
/*
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 <metamuffin.org>
*/
use super::ui::error::MyResult;
use crate::{helper::A, locale::AcceptLanguage};
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,
http::MediaType,
outcome::Outcome,
post,
request::{self, FromRequest},
response::Redirect,
serde::json::Json,
Either, Request, State,
};
use serde_json::{json, Value};
use std::{collections::HashMap, ops::Deref};
#[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<Json<&'static HashMap<&'static str, &'static str>>, 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 = "<data>")]
pub fn r_api_account_login(
database: &State<Database>,
data: Json<CreateSessionParams>,
) -> MyResult<Value> {
let token = login_logic(
database,
&data.username,
&data.password,
data.expire,
data.drop_permissions.clone(),
)?;
Ok(json!(token))
}
#[get("/api/asset_token_raw/<token>")]
pub fn r_api_asset_token_raw(_admin: A<AdminSession>, token: &str) -> MyResult<Json<AssetInner>> {
Ok(Json(AssetInner::deser(token)?))
}
#[get("/nodes_modified?<since>")]
pub fn r_nodes_modified_since(
_session: A<Session>,
database: &State<Database>,
since: u64,
) -> MyResult<Json<Vec<NodeID>>> {
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))
}
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),
))
})
}
}
|