From 7be6138d1ca41108cb924119af803e09413ca608 Mon Sep 17 00:00:00 2001 From: lostb1t Date: Tue, 23 Jul 2024 17:02:17 +0200 Subject: [PATCH] fix: merge filter and metadata ao we can have ordering --- src/transform/restrictions.rs | 71 +++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 src/transform/restrictions.rs diff --git a/src/transform/restrictions.rs b/src/transform/restrictions.rs new file mode 100644 index 0000000..d267f6a --- /dev/null +++ b/src/transform/restrictions.rs @@ -0,0 +1,71 @@ +use async_trait::async_trait; +use crate::{ + config::Config, + models::*, + plex_client::{PlexClient}, +}; + +use super::Transform; +#[derive(Default)] +pub struct HubRestrictionTransform; + +#[async_trait] +impl Transform for HubRestrictionTransform { + async fn filter_metadata( + &self, + item: MetaData, + plex_client: PlexClient, + options: PlexContext, + ) -> bool { + let config: Config = Config::figment().extract().unwrap(); + + if !config.hub_restrictions { + return true; + } + + if item.is_hub() && !item.is_collection_hub() { + return true; + } + + if !item.is_hub() { + return true; + } + + if item.size.unwrap() == 0 { + return false; + } + + let section_id: i64 = item.library_section_id.unwrap_or_else(|| { + item.hub_identifier.clone().unwrap().split('.').collect::>()[2].parse().unwrap() + }); + + //let start = Instant::now(); + let mut custom_collections = plex_client + .clone() + .get_cached( + plex_client.get_section_collections(section_id), + format!("sectioncollections:{}", section_id).to_string(), + ) + .await + .unwrap(); + + //println!("Elapsed time: {:.2?}", start.elapsed()); + let custom_collections_ids: Vec = custom_collections + .media_container + .children() + .iter() + .map(|c| c.rating_key.clone().unwrap()) + .collect(); + + custom_collections_ids.contains( + &item + .hub_identifier + .clone() + .unwrap() + .split('.') + .last() + .unwrap() + .to_owned(), + ) + } +} \ No newline at end of file