From 5a2dce85e8951140e6a0f837e4f406c50a03a421 Mon Sep 17 00:00:00 2001 From: Cameron Cordes Date: Wed, 22 Mar 2023 13:55:10 -0400 Subject: [PATCH] Fix searching by tags and fixed All FilterMode Manually parsing the tag_ids for the file filtering isn't amazing, but this works in a more friendly format. Also the All filter mode was set up in the wrong direction instead of checking that the file had ALL the tag ids provided, it checked that all the tag-ids were on a file, which is too restrictive and wouldn't show many files. Perhaps an ONLY option could exist for being even more specific. --- src/data/mod.rs | 2 +- src/files.rs | 16 ++++++++++++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/data/mod.rs b/src/data/mod.rs index d520f7a..bc93e7e 100644 --- a/src/data/mod.rs +++ b/src/data/mod.rs @@ -103,7 +103,7 @@ pub struct PhotosResponse { #[derive(Deserialize)] pub struct FilesRequest { pub path: String, - pub tag_ids: Option>, + pub tag_ids: Option, // comma separated numbers pub tag_filter_mode: Option, } diff --git a/src/files.rs b/src/files.rs index c18c608..caa6ed3 100644 --- a/src/files.rs +++ b/src/files.rs @@ -13,7 +13,7 @@ use actix_web::{ }; use log::{debug, error}; -use crate::data::{Claims, FilesRequest, FilterMode, PhotosResponse, ThumbnailRequest}; +use crate::data::{Claims, FilesRequest, FilterMode, PhotosResponse}; use crate::AppState; use crate::tags::TagDao; @@ -46,14 +46,22 @@ pub async fn list_photos( relative.to_path_buf() }) .map(|f| f.to_str().unwrap().to_string()) - .filter(|path| { + .filter(|file_path| { if let (Some(tag_ids), Ok(mut tag_dao)) = (&req.tag_ids, tag_dao.lock()) { + let tag_ids = tag_ids + .split(',') + .filter_map(|t| t.parse().ok()) + .collect::>(); + let filter_mode = &req.tag_filter_mode.unwrap_or(FilterMode::Any); - let file_tags = tag_dao.get_tags_for_path(path).unwrap_or_default(); + let file_tags = tag_dao.get_tags_for_path(file_path).unwrap_or_default(); + return match filter_mode { FilterMode::Any => file_tags.iter().any(|t| tag_ids.contains(&t.id)), - FilterMode::All => file_tags.iter().all(|t| tag_ids.contains(&t.id)), + FilterMode::All => tag_ids + .iter() + .all(|id| file_tags.iter().any(|tag| &tag.id == id)), }; }