From ec9baf0c8a8e2c71ce485a4d0257015035508ffa Mon Sep 17 00:00:00 2001 From: Syfaro Date: Thu, 14 May 2020 01:34:44 -0500 Subject: [PATCH] Add API endpoint to check if known handle. --- src/filters.rs | 11 ++++++++++- src/handlers.rs | 18 ++++++++++++++++++ src/types.rs | 5 +++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/filters.rs b/src/filters.rs index 1cd4e6e..9cec3f0 100644 --- a/src/filters.rs +++ b/src/filters.rs @@ -10,7 +10,8 @@ pub fn search( search_image(db.clone(), tree.clone()) .or(search_hashes(db.clone(), tree.clone())) .or(stream_search_image(db.clone(), tree)) - .or(search_file(db)) + .or(search_file(db.clone())) + .or(check_handle(db)) } pub fn search_file(db: Pool) -> impl Filter + Clone { @@ -66,6 +67,14 @@ pub fn stream_search_image( .and_then(handlers::stream_image) } +pub fn check_handle(db: Pool) -> impl Filter + Clone { + warp::path("handle") + .and(warp::get()) + .and(warp::query::()) + .and(with_pool(db)) + .and_then(handlers::check_handle) +} + fn with_api_key() -> impl Filter + Clone { warp::header::("x-api-key") } diff --git a/src/handlers.rs b/src/handlers.rs index fdeccaa..c6f2c39 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -290,6 +290,24 @@ pub async fn search_file( Ok(warp::reply::json(&matches)) } +pub async fn check_handle(opts: HandleOpts, db: Pool) -> Result { + let db = db.get().await.map_err(map_bb8_err)?; + + let exists = if let Some(handle) = opts.twitter { + !db.query( + "SELECT 1 FROM twitter_user WHERE lower(data->>'screen_name') = lower($1)", + &[&handle], + ) + .await + .map_err(map_postgres_err)? + .is_empty() + } else { + false + }; + + Ok(warp::reply::json(&exists)) +} + #[tracing::instrument] pub async fn handle_rejection(err: Rejection) -> Result { warn!("had rejection"); diff --git a/src/types.rs b/src/types.rs index 5d266a0..6106a6c 100644 --- a/src/types.rs +++ b/src/types.rs @@ -107,3 +107,8 @@ pub struct HashSearchOpts { pub hashes: String, pub distance: Option, } + +#[derive(Debug, Deserialize)] +pub struct HandleOpts { + pub twitter: Option, +}