Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Add Collection Sorting #162

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
initial draft
  • Loading branch information
stevezau committed Sep 9, 2024
commit 256972dad2d3766446972d0f886e5d4aabf64917
2 changes: 2 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<String>>,
}

fn default_cache_ttl() -> u64 {
Expand Down
3 changes: 3 additions & 0 deletions src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
54 changes: 54 additions & 0 deletions src/transform/hub_reorder.rs
Original file line number Diff line number Diff line change
@@ -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<u32>,
}

#[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<MetaData> = 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<MetaData> = ordered_children
.into_iter()
.map(|(_, child)| child)
.chain(unordered_children.into_iter())
.collect();

item.metadata = sorted_children;
item
}
}
2 changes: 2 additions & 0 deletions src/transform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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::{
Expand Down
Loading