Skip to content

Commit

Permalink
fix: merge filter and metadata ao we can have ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
lostb1t committed Jul 23, 2024
1 parent b49c823 commit f4c33f0
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 146 deletions.
11 changes: 9 additions & 2 deletions src/plex_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,16 @@ impl PlexClient {
path = format!("{}&includeGuids=1", path);
// dbg!(&path);

let resp = self.get(path).await.unwrap();
let res = self.get(path).await.unwrap();
if !res.status().is_success() {
return Err(anyhow::anyhow!(format!(
"unexpected status code: status = {}",
res.status()
)));
}

let container: MediaContainerWrapper<MediaContainer> =
from_reqwest_response(resp).await.unwrap();
from_reqwest_response(res).await.unwrap();
Ok(container)
}

Expand Down
7 changes: 4 additions & 3 deletions src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ pub async fn transform_hubs_response(
container.content_type = content_type;

TransformBuilder::new(plex_client, context.clone())
.with_filter(HubRestrictionFilter)
.with_transform(HubRestrictionTransform)
.with_transform(HubStyleTransform { is_home: true })
.with_transform(HubWatchedTransform)
.with_transform(HubInterleaveTransform)
Expand Down Expand Up @@ -663,6 +663,7 @@ pub async fn transform_req_android(
}


// rhis handles refresh of individual rows or paging and paging if it
#[handler]
pub async fn get_collections_children(
req: &mut Request,
Expand Down Expand Up @@ -700,12 +701,12 @@ pub async fn get_collections_children(

// filtering of watched happens in the transform
TransformBuilder::new(plex_client, context.clone())
.with_filter(HubRestrictionFilter)
.with_transform(LibraryInterleaveTransform {
collection_ids: collection_ids.clone(),
offset,
limit,
})
.with_transform(HubRestrictionTransform)
.with_transform(CollectionStyleTransform {
collection_ids: collection_ids.clone(),
hub: context.content_directory_id.is_some() // its a guessing game
Expand Down Expand Up @@ -771,7 +772,7 @@ pub async fn default_transform(
container.content_type = content_type;

TransformBuilder::new(plex_client, context.clone())
.with_filter(HubRestrictionFilter)
.with_transform(HubRestrictionTransform)
.with_transform(MediaStyleTransform { style: style })
.with_transform(UserStateTransform)
.with_transform(HubWatchedTransform)
Expand Down
28 changes: 20 additions & 8 deletions src/transform/library_interleave.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,20 @@ impl Transform for LibraryInterleaveTransform {
let mut total_size = 0;

for id in self.collection_ids.clone() {
let collection = plex_client
.clone()
.get_cached(
plex_client.get_collection(id as i32),
format!("collection:{}", id.to_string()),
)
.await
.unwrap();

//match c {
// Ok(v) =>,
// Err(err) =>
//}

let mut c = plex_client
.clone()
.get_cached(
Expand All @@ -90,15 +104,13 @@ impl Transform for LibraryInterleaveTransform {
)
.await
.unwrap();

// should have proper errors but lets assume not found so no access
//match c {
// Ok(v) =>,
// Err(err) =>
//}

let collection = plex_client
.clone()
.get_cached(
plex_client.get_collection(id as i32),
format!("collection:{}", id.to_string()),
)
.await
.unwrap();

if collection.media_container.exclude_watched() {
c.media_container.children_mut().retain(|x| !x.is_watched());
Expand Down
98 changes: 36 additions & 62 deletions src/transform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub mod media_style;
pub mod hub_section_directory;
pub mod hub_style;
pub mod library_interleave;
pub mod restriction_filter;
pub mod restrictions;

pub use collection_style::CollectionStyleTransform;
pub use hub_interleave::HubInterleaveTransform;
Expand All @@ -20,7 +20,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 restriction_filter::HubRestrictionFilter;
pub use restrictions::HubRestrictionTransform;

use crate::{
models::*,
Expand All @@ -40,8 +40,10 @@ pub trait Transform: Send + Sync + 'static {
async fn transform_metadata(
&self,
item: &mut MetaData,
//item: MetaData,
plex_client: PlexClient,
options: PlexContext,
//) -> Option<MediaContainer> {
) {
}
async fn transform_mediacontainer(
Expand All @@ -52,13 +54,9 @@ pub trait Transform: Send + Sync + 'static {
) -> MediaContainer {
item
}
}

#[async_trait]
pub trait Filter: Send + Sync + 'static {
async fn filter_metadata(
&self,
item: &mut MetaData,
item: MetaData,
plex_client: PlexClient,
options: PlexContext,
) -> bool {
Expand All @@ -74,6 +72,7 @@ pub trait Filter: Send + Sync + 'static {
}
}


// #[derive(TypedBuilder)]
// #[derive(Default)]
#[derive(Clone)]
Expand All @@ -82,15 +81,13 @@ pub struct TransformBuilder {
pub options: PlexContext,
pub mix: bool,
pub transforms: Vec<Arc<dyn Transform>>,
pub filters: Vec<Arc<dyn Filter>>,
}

impl TransformBuilder {
#[inline]
pub fn new(plex_client: PlexClient, options: PlexContext) -> Self {
Self {
transforms: Vec::new(),
filters: Vec::new(),
mix: false,
plex_client,
options,
Expand All @@ -104,56 +101,13 @@ impl TransformBuilder {
self
}

#[inline]
pub fn with_filter<T: Filter>(mut self, filter: T) -> Self {
self.filters.push(Arc::new(filter));
self
}

// pub fn merge(mut self, container_left, container_right) -> Self {
// self.mix = true;
// self
// }

#[async_recursion]
pub async fn apply_to_metadata(
&self,
metadata: &mut Vec<MetaData>,
) -> Vec<MetaData> {
let mut filtered_childs: Vec<MetaData> = vec![];
'outer: for item in metadata {
for filter in self.filters.clone() {
// dbg!("filtering");
if filter
.filter_metadata(
item,
self.plex_client.clone(),
self.options.clone(),
)
.await
{
continue 'outer;
}
}

if !item.children().is_empty() {
let childs = self.apply_to_metadata(item.children_mut()).await;
item.set_children(childs);
}

filtered_childs.push(item.to_owned());
}

return filtered_childs;
}

pub async fn apply_to(
pub async fn apply_to_old(
self,
container: &mut MediaContainerWrapper<MediaContainer>,
) {
let children = container.media_container.children_mut();
let new_children = self.apply_to_metadata(children).await;
container.media_container.set_children(new_children);
//let new_children = self.apply_to_metadata(children).await;
//container.media_container.set_children(new_children);

for t in self.transforms.clone() {
let futures =
Expand Down Expand Up @@ -194,19 +148,42 @@ impl TransformBuilder {
}
}

pub async fn apply_to_test(
pub async fn apply_to(
self,
container: &mut MediaContainerWrapper<MediaContainer>,
) {
//let mut childs = container.media_container.children_mut();
let mut idx = 0 as usize;
let mut filter_childs = vec![];
for t in self.transforms.clone() {

//dbg!(&filter_childs);
for child in container.media_container.children_mut() {
t.transform_metadata(
//if filter_childs.contains(child.key.clone().unwrap()) {
// continue;
//}
//dbg!(&child.rating_key);
//dbg!(&child.key);
if !t.filter_metadata(
child.clone(),
self.plex_client.clone(),
self.options.clone(),
)
.await
{
//childs.remove(idx);
filter_childs.push(child.key.clone().unwrap());
continue
}
t.transform_metadata(
child,
self.plex_client.clone(),
self.options.clone(),
).await;
//if
//idx = idx + 1;
}
container.media_container.children_mut().retain(|x| !filter_childs.contains(&x.key.clone().unwrap()));
//item.children_mut().retain(|x| !x.is_watched());
//future::join_all(futures).await;

// dont use join as it needs ti be executed in order
Expand All @@ -221,10 +198,7 @@ impl TransformBuilder {
// dbg!(container.media_container.size);
}

// filter behind transform as transform can load in additional data
let children = container.media_container.children_mut();
let new_children = self.apply_to_metadata(children).await;
container.media_container.set_children(new_children);
//container.media_container.set_children(childs);

if container.media_container.size.is_some() {
container.media_container.size = Some(
Expand Down
71 changes: 0 additions & 71 deletions src/transform/restriction_filter.rs

This file was deleted.

0 comments on commit f4c33f0

Please sign in to comment.