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.
This commit is contained in:
@@ -103,7 +103,7 @@ pub struct PhotosResponse {
|
|||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
pub struct FilesRequest {
|
pub struct FilesRequest {
|
||||||
pub path: String,
|
pub path: String,
|
||||||
pub tag_ids: Option<Vec<i32>>,
|
pub tag_ids: Option<String>, // comma separated numbers
|
||||||
pub tag_filter_mode: Option<FilterMode>,
|
pub tag_filter_mode: Option<FilterMode>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
16
src/files.rs
16
src/files.rs
@@ -13,7 +13,7 @@ use actix_web::{
|
|||||||
};
|
};
|
||||||
use log::{debug, error};
|
use log::{debug, error};
|
||||||
|
|
||||||
use crate::data::{Claims, FilesRequest, FilterMode, PhotosResponse, ThumbnailRequest};
|
use crate::data::{Claims, FilesRequest, FilterMode, PhotosResponse};
|
||||||
use crate::AppState;
|
use crate::AppState;
|
||||||
|
|
||||||
use crate::tags::TagDao;
|
use crate::tags::TagDao;
|
||||||
@@ -46,14 +46,22 @@ pub async fn list_photos<TagD: TagDao>(
|
|||||||
relative.to_path_buf()
|
relative.to_path_buf()
|
||||||
})
|
})
|
||||||
.map(|f| f.to_str().unwrap().to_string())
|
.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()) {
|
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::<Vec<i32>>();
|
||||||
|
|
||||||
let filter_mode = &req.tag_filter_mode.unwrap_or(FilterMode::Any);
|
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 {
|
return match filter_mode {
|
||||||
FilterMode::Any => file_tags.iter().any(|t| tag_ids.contains(&t.id)),
|
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)),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user