aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2023-08-07 11:10:10 +0200
committermetamuffin <metamuffin@disroot.org>2023-08-07 11:59:59 +0200
commitdc7ed1ccaa5e727b3ab0569fd7fe56a2d0084bd5 (patch)
treeeb724ec5c0de92abef03549365eba01e07c06d90
parent55eba6fdc6742d2850d003059220284bace059fc (diff)
downloadjellything-dc7ed1ccaa5e727b3ab0569fd7fe56a2d0084bd5.tar
jellything-dc7ed1ccaa5e727b3ab0569fd7fe56a2d0084bd5.tar.bz2
jellything-dc7ed1ccaa5e727b3ab0569fd7fe56a2d0084bd5.tar.zst
node sort ui
-rw-r--r--Cargo.lock1
-rw-r--r--common/Cargo.toml4
-rw-r--r--common/src/impl.rs8
-rw-r--r--common/src/lib.rs21
-rw-r--r--server/Cargo.toml2
-rw-r--r--server/src/routes/mod.rs4
-rw-r--r--server/src/routes/ui/mod.rs1
-rw-r--r--server/src/routes/ui/node.rs25
-rw-r--r--server/src/routes/ui/sort.rs95
9 files changed, 137 insertions, 24 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 9653265..7526190 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1601,6 +1601,7 @@ name = "jellycommon"
version = "0.1.0"
dependencies = [
"bincode 2.0.0-rc.3",
+ "rocket",
"serde",
]
diff --git a/common/Cargo.toml b/common/Cargo.toml
index ce864eb..6bbc513 100644
--- a/common/Cargo.toml
+++ b/common/Cargo.toml
@@ -6,3 +6,7 @@ edition = "2021"
[dependencies]
serde = { version = "1.0.182", features = ["derive"] }
bincode = { version = "2.0.0-rc.3", features = ["derive"] }
+rocket = { version = "0.5.0-rc.3", optional = true }
+
+[features]
+rocket = ["dep:rocket"]
diff --git a/common/src/impl.rs b/common/src/impl.rs
index d7759af..55364ab 100644
--- a/common/src/impl.rs
+++ b/common/src/impl.rs
@@ -3,7 +3,7 @@
which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
Copyright (C) 2023 metamuffin <metamuffin.org>
*/
-use crate::{NodeKind, SourceTrack, SourceTrackKind};
+use crate::{SourceTrack, SourceTrackKind};
impl SourceTrackKind {
pub fn letter(&self) -> char {
@@ -34,9 +34,3 @@ impl std::fmt::Display for SourceTrack {
))
}
}
-
-impl Default for NodeKind {
- fn default() -> Self {
- Self::Collection
- }
-}
diff --git a/common/src/lib.rs b/common/src/lib.rs
index a96df9e..9d5a31e 100644
--- a/common/src/lib.rs
+++ b/common/src/lib.rs
@@ -7,6 +7,7 @@ pub mod config;
pub mod r#impl;
use bincode::{Decode, Encode};
+use rocket::{FromFormField, UriDisplayQuery};
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
@@ -57,17 +58,19 @@ pub enum AssetLocation {
Assets(PathBuf),
}
-#[derive(Debug, Clone, Deserialize, Serialize)]
+#[rustfmt::skip]
+#[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq, Eq, Default)]
+#[cfg_attr(feature = "rocket", derive(FromFormField, UriDisplayQuery))]
#[serde(rename_all = "snake_case")]
pub enum NodeKind {
- Movie,
- Video,
- Collection,
- Channel,
- Show,
- Series,
- Season,
- Episode,
+ #[cfg_attr(feature = "rocket", field(value = "movie"))] #[default] Movie,
+ #[cfg_attr(feature = "rocket", field(value = "video"))] Video,
+ #[cfg_attr(feature = "rocket", field(value = "collection"))] Collection,
+ #[cfg_attr(feature = "rocket", field(value = "channel"))] Channel,
+ #[cfg_attr(feature = "rocket", field(value = "show"))] Show,
+ #[cfg_attr(feature = "rocket", field(value = "series"))] Series,
+ #[cfg_attr(feature = "rocket", field(value = "season"))] Season,
+ #[cfg_attr(feature = "rocket", field(value = "episode"))] Episode,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
diff --git a/server/Cargo.toml b/server/Cargo.toml
index 7d6b18f..8938768 100644
--- a/server/Cargo.toml
+++ b/server/Cargo.toml
@@ -4,7 +4,7 @@ version = "0.1.0"
edition = "2021"
[dependencies]
-jellycommon = { path = "../common" }
+jellycommon = { path = "../common", features = ["rocket"] }
jellybase = { path = "../base" }
jellyremuxer = { path = "../remuxer" }
jellyclient = { path = "../client" }
diff --git a/server/src/routes/mod.rs b/server/src/routes/mod.rs
index e898c8f..fe9ef39 100644
--- a/server/src/routes/mod.rs
+++ b/server/src/routes/mod.rs
@@ -30,7 +30,7 @@ use ui::{
browser::r_all_items,
error::{r_api_catch, r_catch},
home::{r_home, r_home_unpriv},
- node::r_library_node,
+ node::r_library_node_filter,
player::r_player,
style::{r_assets_font, r_assets_js, r_assets_style},
};
@@ -88,7 +88,7 @@ pub fn build_rocket(
r_favicon,
r_item_assets,
r_all_items,
- r_library_node,
+ r_library_node_filter,
r_assets_style,
r_assets_font,
r_assets_js,
diff --git a/server/src/routes/ui/mod.rs b/server/src/routes/ui/mod.rs
index d561627..07098c8 100644
--- a/server/src/routes/ui/mod.rs
+++ b/server/src/routes/ui/mod.rs
@@ -31,6 +31,7 @@ pub mod layout;
pub mod node;
pub mod player;
pub mod style;
+pub mod sort;
pub struct HtmlTemplate<'a>(pub markup::DynRender<'a>);
diff --git a/server/src/routes/ui/node.rs b/server/src/routes/ui/node.rs
index 301f89c..ba16c73 100644
--- a/server/src/routes/ui/node.rs
+++ b/server/src/routes/ui/node.rs
@@ -3,7 +3,12 @@
which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
Copyright (C) 2023 metamuffin <metamuffin.org>
*/
-use super::{assets::rocket_uri_macro_r_item_assets, error::MyError, player::player_uri};
+use super::{
+ assets::rocket_uri_macro_r_item_assets,
+ error::MyError,
+ player::player_uri,
+ sort::{filter_and_sort_nodes, NodeFilterSort, NodeFilterSortForm},
+};
use crate::{
database::Database,
routes::{
@@ -20,12 +25,19 @@ use anyhow::{anyhow, Context};
use jellycommon::{MediaInfo, NodeKind, NodePublic, Rating, SourceTrackKind};
use rocket::{get, serde::json::Json, Either, State};
+/// This function is a stub and only useful for use in the uri! macro.
#[get("/n/<id>")]
-pub async fn r_library_node(
+pub fn r_library_node(id: String) -> () {
+ drop(id)
+}
+
+#[get("/n/<id>?<filter..>")]
+pub async fn r_library_node_filter(
_sess: Session,
id: String,
db: &State<Database>,
aj: AcceptJson,
+ filter: NodeFilterSort,
) -> Result<Either<DynLayoutPage<'_>, Json<NodePublic>>, MyError> {
let node = db
.node
@@ -38,7 +50,7 @@ pub async fn r_library_node(
return Ok(Either::Right(Json(node)));
}
- let children = node
+ let mut children = node
.children
.iter()
.map(|c| {
@@ -54,11 +66,13 @@ pub async fn r_library_node(
.into_iter()
.collect();
+ filter_and_sort_nodes(&filter, &mut children);
+
Ok(Either::Left(LayoutPage {
title: node.title.to_string(),
show_back: true, //- !matches!(node.kind, NodeKind::Collection),
content: markup::new! {
- @NodePage { node: &node, id: &id, children: &children }
+ @NodePage { node: &node, id: &id, children: &children, filter: &filter }
},
..Default::default()
}))
@@ -88,7 +102,7 @@ markup::define! {
}
}
}
- NodePage<'a>(id: &'a str, node: &'a NodePublic, children: &'a Vec<(String, NodePublic)>) {
+ NodePage<'a>(id: &'a str, node: &'a NodePublic, children: &'a Vec<(String, NodePublic)>, filter: &'a NodeFilterSort) {
@if !matches!(node.kind, NodeKind::Collection) {
img.backdrop[src=uri!(r_item_assets(id, AssetRole::Backdrop, Some(2048)))];
}
@@ -110,6 +124,7 @@ markup::define! {
@if let NodeKind::Collection = node.kind {
@if let Some(parent) = &node.parent {
a.dirup[href=uri!(r_library_node(parent))] { "Go up" }
+ @NodeFilterSortForm { f: filter }
}
}
@match node.kind {
diff --git a/server/src/routes/ui/sort.rs b/server/src/routes/ui/sort.rs
new file mode 100644
index 0000000..3ab6cef
--- /dev/null
+++ b/server/src/routes/ui/sort.rs
@@ -0,0 +1,95 @@
+use jellycommon::{NodeKind, NodePublic};
+use rocket::{
+ http::uri::fmt::{Query, UriDisplay},
+ FromForm, FromFormField, UriDisplayQuery,
+};
+
+#[derive(FromForm, UriDisplayQuery)]
+pub struct NodeFilterSort {
+ sort_by: Option<SortProperty>,
+ filter_kind: Option<Vec<NodeKind>>,
+ sort_order: Option<SortOrder>,
+}
+
+#[rustfmt::skip]
+#[derive(FromFormField, UriDisplayQuery, Clone, Copy, PartialEq, Eq)]
+enum SortProperty {
+ #[field(value = "release_date")] ReleaseDate,
+ #[field(value = "title")] Title,
+}
+
+#[rustfmt::skip]
+#[derive(FromFormField, UriDisplayQuery, Clone, Copy, PartialEq, Eq)]
+enum SortOrder {
+ #[field(value = "ascending")] Ascending,
+ #[field(value = "descending")] Descending,
+}
+
+pub fn filter_and_sort_nodes(f: &NodeFilterSort, nodes: &mut Vec<(String, NodePublic)>) {
+ nodes.retain(|(_id, node)| {
+ let mut o = true;
+ if let Some(kind) = &f.filter_kind {
+ o &= kind.contains(&node.kind)
+ }
+ o
+ });
+ if let Some(sort_prop) = f.sort_by {
+ match sort_prop {
+ SortProperty::ReleaseDate => nodes.sort_by_key(|(_, _n)| 0), // TODO
+ SortProperty::Title => nodes.sort_by(|(_, a), (_, b)| a.title.cmp(&b.title)),
+ }
+ }
+ match f.sort_order.unwrap_or(SortOrder::Ascending) {
+ SortOrder::Ascending => (),
+ SortOrder::Descending => nodes.reverse(),
+ }
+}
+
+markup::define! {
+ NodeFilterSortForm<'a>(f: &'a NodeFilterSort) {
+ details {
+ summary { "Filter and Sort" }
+ form[method="GET", action=""] {
+ fieldset {
+ legend { "Filter" }
+ @use NodeKind::*;
+ @for (value, label) in [(Movie, "Movie"), (Episode, "Episode"), (Video, "Video"), (Channel, "Channel")] {
+ label { input[type="checkbox", name="filter_kind", value=A(value), checked=f.filter_kind.as_ref().map(|k|k.contains(&value)).unwrap_or(true)]; @label } br;
+ }
+ }
+ fieldset {
+ legend { "Sort By" }
+ @use SortProperty::*;
+ @for (value, label) in [(Title, "Title"), (ReleaseDate, "Release Date")] {
+ label { input[type="radio", name="sort_by", value=value, checked=Some(value)==f.sort_by]; @label } br;
+ }
+ }
+ fieldset {
+ legend { "Sort Order" }
+ @use SortOrder::*;
+ @for (value, label) in [(Ascending, "Ascending"), (Descending, "Descending")] {
+ label { input[type="radio", name="sort_order", value=value, checked=Some(value)==f.sort_order]; @label } br;
+ }
+ }
+ input[type="submit", value="Apply"];
+ }
+ }
+ }
+}
+
+impl markup::Render for SortProperty {
+ fn render(&self, writer: &mut impl std::fmt::Write) -> std::fmt::Result {
+ writer.write_fmt(format_args!("{}", self as &dyn UriDisplay<Query>))
+ }
+}
+impl markup::Render for SortOrder {
+ fn render(&self, writer: &mut impl std::fmt::Write) -> std::fmt::Result {
+ writer.write_fmt(format_args!("{}", self as &dyn UriDisplay<Query>))
+ }
+}
+struct A(NodeKind);
+impl markup::Render for A {
+ fn render(&self, writer: &mut impl std::fmt::Write) -> std::fmt::Result {
+ writer.write_fmt(format_args!("{}", &self.0 as &dyn UriDisplay<Query>))
+ }
+}