From 68b8b911d22dfc5d18996920a8a908b1201257ec Mon Sep 17 00:00:00 2001 From: Syfaro Date: Wed, 19 Feb 2020 00:40:43 -0600 Subject: [PATCH] Include which hash caused a match in hashes. --- src/handlers.rs | 9 ++++++++- src/models.rs | 7 ++++++- src/types.rs | 4 ++++ src/utils.rs | 1 + 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/handlers.rs b/src/handlers.rs index bb2a17c..9440013 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -203,7 +203,13 @@ pub async fn search_hashes( rate_limit!(&api_key, &db, image_limit, "image", hashes.len() as i16); - let mut results = image_query_sync(pool, tree, hashes.clone(), 10, None); + let mut results = image_query_sync( + pool, + tree, + hashes.clone(), + opts.distance.unwrap_or(10), + None, + ); let mut matches = Vec::new(); while let Some(r) = results.recv().await { @@ -275,6 +281,7 @@ pub async fn search_file( site_info: Some(SiteInfo::FurAffinity(FurAffinityFile { file_id: row.get("file_id"), })), + searched_hash: None, }) .collect(); diff --git a/src/models.rs b/src/models.rs index 0be59b8..580be85 100644 --- a/src/models.rs +++ b/src/models.rs @@ -130,7 +130,12 @@ pub fn image_query_sync( LIMIT 1 ) tm ON hashes.twitter_id IS NOT NULL WHERE hashes.id = $1", &[&item.id]).await; - let rows = query.map(|rows| extract_rows(rows, hash.as_deref()).into_iter().collect()); + let rows = query.map(|rows| { + extract_rows(rows, hash.as_deref()).into_iter().map(|mut file| { + file.searched_hash = Some(query_hash); + file + }).collect() + }); tx.send(rows).await.unwrap(); } } diff --git a/src/types.rs b/src/types.rs index ad9de53..fb529dc 100644 --- a/src/types.rs +++ b/src/types.rs @@ -42,6 +42,9 @@ pub struct File { pub hash: Option, #[serde(skip_serializing_if = "Option::is_none")] pub distance: Option, + + #[serde(skip_serializing_if = "Option::is_none")] + pub searched_hash: Option, } #[derive(Debug, Serialize)] @@ -101,4 +104,5 @@ pub struct ErrorMessage { #[derive(Debug, Deserialize)] pub struct HashSearchOpts { pub hashes: String, + pub distance: Option, } diff --git a/src/utils.rs b/src/utils.rs index 6e663ad..becb04f 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -109,6 +109,7 @@ pub fn extract_rows<'a>( .flatten(), artists: row.get("artists"), filename: row.get("filename"), + searched_hash: None, } }) }