aboutsummaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2025-04-28 18:27:03 +0200
committermetamuffin <metamuffin@disroot.org>2025-04-28 18:27:03 +0200
commit51761cbdefa39107b9e1f931f1aa8df6aebb2a94 (patch)
tree957ca180786ece777e6e1153ada91da741d845ec /common
parent80d28b764c95891551e28c395783f5ff9d065743 (diff)
downloadjellything-51761cbdefa39107b9e1f931f1aa8df6aebb2a94.tar
jellything-51761cbdefa39107b9e1f931f1aa8df6aebb2a94.tar.bz2
jellything-51761cbdefa39107b9e1f931f1aa8df6aebb2a94.tar.zst
many much more generic refactor
Diffstat (limited to 'common')
-rw-r--r--common/Cargo.toml4
-rw-r--r--common/src/api.rs52
-rw-r--r--common/src/lib.rs1
-rw-r--r--common/src/routes.rs17
4 files changed, 54 insertions, 20 deletions
diff --git a/common/Cargo.toml b/common/Cargo.toml
index 775cd2a..90372bc 100644
--- a/common/Cargo.toml
+++ b/common/Cargo.toml
@@ -6,10 +6,6 @@ edition = "2021"
[dependencies]
serde = { version = "1.0.217", features = ["derive", "rc"] }
bincode = { version = "2.0.0-rc.3", features = ["derive"] }
-rocket = { workspace = true, optional = true }
chrono = { version = "0.4.39", features = ["serde"] }
blake3 = "1.5.5"
hex = "0.4.3"
-
-[features]
-rocket = ["dep:rocket"]
diff --git a/common/src/api.rs b/common/src/api.rs
index 6982e76..a58c445 100644
--- a/common/src/api.rs
+++ b/common/src/api.rs
@@ -3,11 +3,9 @@
which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
Copyright (C) 2025 metamuffin <metamuffin.org>
*/
-use crate::{user::NodeUserData, Node};
-#[cfg(feature = "rocket")]
-use rocket::{FromForm, FromFormField, UriDisplayQuery};
+use crate::{user::NodeUserData, Node, NodeKind};
use serde::{Deserialize, Serialize};
-use std::sync::Arc;
+use std::{collections::BTreeMap, sync::Arc, time::Duration};
type NodesWithUdata = Vec<(Arc<Node>, NodeUserData)>;
@@ -23,6 +21,7 @@ pub struct ApiNodeResponse {
pub struct ApiSearchResponse {
pub count: usize,
pub results: NodesWithUdata,
+ pub duration: Duration,
}
#[derive(Serialize, Deserialize)]
@@ -38,6 +37,21 @@ pub struct ApiHomeResponse {
pub categories: Vec<(String, NodesWithUdata)>,
}
+#[derive(Serialize, Deserialize)]
+pub struct ApiStatsResponse {
+ pub kinds: BTreeMap<NodeKind, StatsBin>,
+ pub total: StatsBin,
+}
+
+#[derive(Default, Serialize, Deserialize)]
+pub struct StatsBin {
+ pub runtime: f64,
+ pub size: u64,
+ pub count: usize,
+ pub max_runtime: (f64, String),
+ pub max_size: (u64, String),
+}
+
#[derive(Debug, Default, Clone)]
#[cfg_attr(feature = "rocket", derive(FromForm, UriDisplayQuery))]
pub struct NodeFilterSort {
@@ -46,24 +60,25 @@ pub struct NodeFilterSort {
pub sort_order: Option<SortOrder>,
}
-#[rustfmt::skip]
-#[derive(Debug, Clone, Copy, PartialEq, Eq)]
-#[cfg_attr(feature = "rocket", derive(FromFormField, UriDisplayQuery))]
-pub enum SortOrder {
- #[field(value = "ascending")] Ascending,
- #[field(value = "descending")] Descending,
-}
-macro_rules! form_enum {
+pub trait UrlEnum: Sized {
+ fn to_str(&self) -> &'static str;
+ fn from_str(s: &str) -> Option<Self>;
+}
+macro_rules! url_enum {
(enum $i:ident { $($vi:ident = $vk:literal),*, }) => {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "rocket", derive(FromFormField, UriDisplayQuery))]
pub enum $i { $(#[cfg_attr(feature = "rocket", field(value = $vk))] $vi),* }
- impl $i { #[allow(unused)] const ALL: &'static [$i] = &[$($i::$vi),*]; }
+ impl $i { pub const ALL: &'static [$i] = &[$($i::$vi),*]; }
+ impl UrlEnum for $i {
+ fn to_str(&self) -> &'static str { match self { $(Self::$vi => $vk),* } }
+ fn from_str(s: &str) -> Option<Self> { match s { $($vk => Some(Self::$vi) ),*, _ => None } }
+ }
};
}
-form_enum!(
+url_enum!(
enum FilterProperty {
FederationLocal = "fed_local",
FederationRemote = "fed_remote",
@@ -82,8 +97,7 @@ form_enum!(
KindEpisode = "kind_episode",
}
);
-
-form_enum!(
+url_enum!(
enum SortProperty {
ReleaseDate = "release_date",
Title = "title",
@@ -100,3 +114,9 @@ form_enum!(
RatingLikesDivViews = "rating_loved",
}
);
+url_enum!(
+ enum SortOrder {
+ Ascending = "ascending",
+ Descending = "descending",
+ }
+);
diff --git a/common/src/lib.rs b/common/src/lib.rs
index eaf5900..c606b86 100644
--- a/common/src/lib.rs
+++ b/common/src/lib.rs
@@ -11,6 +11,7 @@ pub mod r#impl;
pub mod jhls;
pub mod stream;
pub mod user;
+pub mod routes;
pub use chrono;
diff --git a/common/src/routes.rs b/common/src/routes.rs
new file mode 100644
index 0000000..71ca3fa
--- /dev/null
+++ b/common/src/routes.rs
@@ -0,0 +1,17 @@
+/*
+ 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 crate::NodeID;
+
+pub fn u_home() -> String {
+ "/home".to_owned()
+}
+pub fn u_node_id(node: NodeID) -> String {
+ format!("/n/{}", node)
+}
+pub fn u_node_slug(node: &str) -> String {
+ format!("/n/{}", node)
+}