Skip to content

Commit

Permalink
Quick fixes from regressions introduced by #271
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronleopold committed Feb 20, 2024
1 parent 9a2ee30 commit 75c8a4c
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 28 deletions.
2 changes: 2 additions & 0 deletions apps/server/src/routers/api/v1/library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ pub(crate) fn library_not_hidden_from_user_filter(user: &User) -> WhereParam {
library::hidden_from_users::none(vec![user::id::equals(user.id.clone())])
}

// FIXME: hidden libraries introduced a bug here, need to fix!

pub(crate) fn apply_library_filters_for_user(
filters: LibraryFilter,
user: &User,
Expand Down
27 changes: 20 additions & 7 deletions apps/server/src/routers/api/v1/media.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use axum::{
};
use axum_extra::extract::Query;
use prisma_client_rust::{
and, chrono::Utc, operator::or, or, raw, Direction, PrismaValue,
and, chrono::Utc, operator, operator::or, or, raw, Direction, PrismaValue,
};
use serde::{Deserialize, Serialize};
use serde_qs::axum::QsQuery;
Expand Down Expand Up @@ -177,6 +177,8 @@ pub(crate) fn apply_media_library_not_hidden_for_user_filter(
])])]
}

// FIXME: hidden libraries introduced a bug here, need to fix!

pub(crate) fn apply_media_filters_for_user(
filters: MediaFilter,
user: &User,
Expand All @@ -188,12 +190,23 @@ pub(crate) fn apply_media_filters_for_user(
.map(|ar| apply_media_age_restriction(ar.age, ar.restrict_on_unset));

let read_status_filters = filters.base_filter.read_status.clone();
apply_media_filters(filters)
.into_iter()
.chain(age_restrictions.map(|ar| vec![ar]).unwrap_or_default())
.chain(apply_media_read_status_filter(user_id, read_status_filters))
.chain(apply_media_library_not_hidden_for_user_filter(user))
.collect::<Vec<WhereParam>>()
let base_filters = operator::and(
apply_media_filters(filters)
.into_iter()
.chain(age_restrictions.map(|ar| vec![ar]).unwrap_or_default())
.chain(apply_media_read_status_filter(user_id, read_status_filters))
.collect::<Vec<WhereParam>>(),
);

// TODO: This is not ideal, I am adding an _additional_ relation filter for
// the library exclusion, when I need to merge any requested filters with this one,
// instead. This was a regression from the exclusion feature I need to tackle
vec![and![
base_filters,
media::series::is(vec![series::library::is(vec![
library_not_hidden_from_user_filter(user),
])])
]]
}

pub(crate) fn apply_media_pagination<'a>(
Expand Down
55 changes: 38 additions & 17 deletions apps/server/src/routers/api/v1/series.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use axum::{
Json, Router,
};
use axum_extra::extract::Query;
use prisma_client_rust::{or, Direction};
use prisma_client_rust::{and, operator, or, Direction};
use serde::{Deserialize, Serialize};
use serde_qs::axum::QsQuery;
use stump_core::{
Expand Down Expand Up @@ -152,13 +152,6 @@ pub(crate) fn apply_series_library_not_hidden_for_user_filter(
])]
}

fn apply_series_filters_for_user(filters: SeriesFilter, user: &User) -> Vec<WhereParam> {
apply_series_filters(filters)
.into_iter()
.chain(apply_series_library_not_hidden_for_user_filter(user))
.collect()
}

// TODO: this is wrong
pub(crate) fn apply_series_age_restriction(
min_age: i32,
Expand Down Expand Up @@ -192,7 +185,39 @@ pub(crate) fn apply_series_age_restriction(
or![direct_restriction, media_restriction]
}

// TODO: use age restrictions!
// FIXME: hidden libraries introduced a bug here, need to fix!

// fn apply_series_filters_for_user(filters: SeriesFilter, user: &User) -> Vec<WhereParam> {
// apply_series_filters(filters)
// .into_iter()
// .chain(apply_series_library_not_hidden_for_user_filter(user))
// .collect()
// }

pub(crate) fn apply_series_filters_for_user(
filters: SeriesFilter,
user: &User,
) -> Vec<WhereParam> {
let age_restrictions = user
.age_restriction
.as_ref()
.map(|ar| apply_series_age_restriction(ar.age, ar.restrict_on_unset));

let base_filters = operator::and(
apply_series_filters(filters)
.into_iter()
.chain(age_restrictions.map(|ar| vec![ar]).unwrap_or_default())
.collect::<Vec<WhereParam>>(),
);

// TODO: This is not ideal, I am adding an _additional_ relation filter for
// the library exclusion, when I need to merge any requested filters with this one,
// instead. This was a regression from the exclusion feature I need to tackle
vec![and![
base_filters,
series::library::is(vec![library_not_hidden_from_user_filter(user)])
]]
}

#[utoipa::path(
get,
Expand Down Expand Up @@ -226,21 +251,17 @@ async fn get_series(
let db = &ctx.db;
let user = get_session_user(&session)?;
let user_id = user.id.clone();
let age_restrictions = user
.age_restriction
.as_ref()
.map(|ar| apply_series_age_restriction(ar.age, ar.restrict_on_unset));

let is_unpaged = pagination.is_unpaged();
let order_by: OrderByParam = ordering.try_into()?;

let load_media = relation_query.load_media.unwrap_or(false);
let count_media = relation_query.count_media.unwrap_or(false);

let where_conditions = apply_series_filters_for_user(filters, &user)
.into_iter()
.chain(age_restrictions.map(|ar| vec![ar]).unwrap_or_default())
.collect::<Vec<WhereParam>>();
let where_conditions = apply_series_filters_for_user(filters, &user);
// .into_iter()
// .chain(age_restrictions.map(|ar| vec![ar]).unwrap_or_default())
// .collect::<Vec<WhereParam>>();

// series, total series count
let (series, series_count) = db
Expand Down
7 changes: 6 additions & 1 deletion core/src/filesystem/scanner/library_scan_job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,11 @@ impl JobExt for LibraryScanJob {
format!("Scanning series at {}", path_buf.display()).as_str(),
));

let max_depth = self
.options
.as_ref()
.and_then(|o| o.is_collection_based().then(|| 1));

let ignore_rules = generate_rule_set(&[
path_buf.clone(),
PathBuf::from(self.path.clone()),
Expand All @@ -401,7 +406,7 @@ impl JobExt for LibraryScanJob {
WalkerCtx {
db: ctx.db.clone(),
ignore_rules,
max_depth: None,
max_depth,
},
)
.await;
Expand Down
3 changes: 2 additions & 1 deletion core/src/filesystem/scanner/series_scan_job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ impl JobExt for SeriesScanJob {
"Associated library options not found".to_string(),
))?;
let ignore_rules = generate_rule_set(&[PathBuf::from(self.path.clone())]);
let max_depth = library_options.is_collection_based().then_some(1);

self.options = Some(library_options);

Expand All @@ -118,7 +119,7 @@ impl JobExt for SeriesScanJob {
WalkerCtx {
db: ctx.db.clone(),
ignore_rules,
max_depth: None,
max_depth,
},
)
.await?;
Expand Down
12 changes: 10 additions & 2 deletions core/src/filesystem/scanner/walk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,15 @@ pub async fn walk_library(path: &str, ctx: WalkerCtx) -> CoreResult<WalkedLibrar
}

let walk_start = std::time::Instant::now();
tracing::debug!("Walking library at {}", path);

let is_collection_based = max_depth.is_some_and(|d| d == 1);
tracing::debug!(
?path,
max_depth,
is_collection_based,
?ignore_rules,
"Walking library",
);

let (valid_entries, ignored_entries) = walkdir
// Set min_depth to 0 so we include the library path itself,
// which allows us to add it as a series when there are media items in it
Expand All @@ -88,6 +94,8 @@ pub async fn walk_library(path: &str, ctx: WalkerCtx) -> CoreResult<WalkedLibrar
&& (check_deep && entry_path.dir_has_media_deep()
|| (!check_deep && entry_path.dir_has_media()));

tracing::trace!(?is_valid, ?entry_path_str);

if is_valid {
Either::Left(entry)
} else {
Expand Down

0 comments on commit 75c8a4c

Please sign in to comment.