From 256972dad2d3766446972d0f886e5d4aabf64917 Mon Sep 17 00:00:00 2001 From: Steven Adams Date: Mon, 9 Sep 2024 17:02:22 +1000 Subject: [PATCH] initial draft --- src/config.rs | 2 ++ src/routes.rs | 3 ++ src/transform/hub_reorder.rs | 54 ++++++++++++++++++++++++++++++++++++ src/transform/mod.rs | 2 ++ 4 files changed, 61 insertions(+) create mode 100644 src/transform/hub_reorder.rs diff --git a/src/config.rs b/src/config.rs index bb3b837..380d386 100644 --- a/src/config.rs +++ b/src/config.rs @@ -106,6 +106,8 @@ pub struct Config { deserialize_with = "figment::util::bool_from_str_or_int" )] pub ntf_watchlist_force: bool, + #[serde(default, deserialize_with = "deserialize_comma_seperated_string")] + pub custom_sorting: Option>, } fn default_cache_ttl() -> u64 { diff --git a/src/routes.rs b/src/routes.rs index 54652e5..af68fa0 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -709,6 +709,9 @@ pub async fn get_collections_children( offset, limit, }) + .with_transform(HubReorderTransform { + collection_ids: collection_ids.clone() + }) .with_transform(HubRestrictionTransform) .with_transform(CollectionStyleTransform { collection_ids: collection_ids.clone(), diff --git a/src/transform/hub_reorder.rs b/src/transform/hub_reorder.rs new file mode 100644 index 0000000..1ec3782 --- /dev/null +++ b/src/transform/hub_reorder.rs @@ -0,0 +1,54 @@ +use crate::{ + config::Config, + models::*, + plex_client::{PlexClient}, +}; +use super::Transform; +use async_trait::async_trait; + +#[derive(Default, Debug, Clone)] +pub struct HubReorderTransform { + pub collection_ids: Vec, +} + +#[async_trait] +impl Transform for HubReorderTransform { + async fn transform_mediacontainer( + &self, + mut item: MediaContainer, + plex_client: PlexClient, + options: PlexContext, + ) -> MediaContainer { + let config: Config = Config::figment().extract().unwrap(); + if !config.custom_sorting.unwrap() { // Assuming this flag is in config.rs + return item; + } + + let mut children: Vec = item.metadata.clone(); + + // Create a hash map for the custom order and any hubs not specified + let mut ordered_children = vec![]; + let mut unordered_children = vec![]; + + for child in children.drain(..) { + if let Some(hub_index) = config.custom_sorting.unwrap().position(|&id| id == child.rating_key) { + ordered_children.push((hub_index, child)); + } else { + unordered_children.push(child); + } + } + + // Sort the ordered children by their position in the hub_order + ordered_children.sort_by_key(|(hub_index, _)| *hub_index); + + // Flatten the sorted children and append the unordered ones at the end + let sorted_children: Vec = ordered_children + .into_iter() + .map(|(_, child)| child) + .chain(unordered_children.into_iter()) + .collect(); + + item.metadata = sorted_children; + item + } +} \ No newline at end of file diff --git a/src/transform/mod.rs b/src/transform/mod.rs index 9182fb3..5ac2819 100644 --- a/src/transform/mod.rs +++ b/src/transform/mod.rs @@ -9,6 +9,7 @@ pub mod hub_section_directory; pub mod hub_style; pub mod library_interleave; pub mod restrictions; +pub mod hub_reorder; pub use collection_style::CollectionStyleTransform; pub use hub_interleave::HubInterleaveTransform; @@ -20,6 +21,7 @@ pub use media_style::MediaStyleTransform; pub use hub_section_directory::HubSectionDirectoryTransform; pub use hub_style::{ClientHeroStyle, HubStyleTransform}; pub use library_interleave::LibraryInterleaveTransform; +pub use hub_reorder::HubReorderTransform; pub use restrictions::HubRestrictionTransform; use crate::{