Add API endpoint to check if known handle.

This commit is contained in:
Syfaro 2020-05-14 01:34:44 -05:00
parent 801d63f9d9
commit ec9baf0c8a
3 changed files with 33 additions and 1 deletions

View File

@ -10,7 +10,8 @@ pub fn search(
search_image(db.clone(), tree.clone()) search_image(db.clone(), tree.clone())
.or(search_hashes(db.clone(), tree.clone())) .or(search_hashes(db.clone(), tree.clone()))
.or(stream_search_image(db.clone(), tree)) .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<Extract = impl Reply, Error = Rejection> + Clone { pub fn search_file(db: Pool) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
@ -66,6 +67,14 @@ pub fn stream_search_image(
.and_then(handlers::stream_image) .and_then(handlers::stream_image)
} }
pub fn check_handle(db: Pool) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::path("handle")
.and(warp::get())
.and(warp::query::<HandleOpts>())
.and(with_pool(db))
.and_then(handlers::check_handle)
}
fn with_api_key() -> impl Filter<Extract = (String,), Error = Rejection> + Clone { fn with_api_key() -> impl Filter<Extract = (String,), Error = Rejection> + Clone {
warp::header::<String>("x-api-key") warp::header::<String>("x-api-key")
} }

View File

@ -290,6 +290,24 @@ pub async fn search_file(
Ok(warp::reply::json(&matches)) Ok(warp::reply::json(&matches))
} }
pub async fn check_handle(opts: HandleOpts, db: Pool) -> Result<impl Reply, Rejection> {
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] #[tracing::instrument]
pub async fn handle_rejection(err: Rejection) -> Result<impl Reply, std::convert::Infallible> { pub async fn handle_rejection(err: Rejection) -> Result<impl Reply, std::convert::Infallible> {
warn!("had rejection"); warn!("had rejection");

View File

@ -107,3 +107,8 @@ pub struct HashSearchOpts {
pub hashes: String, pub hashes: String,
pub distance: Option<i64>, pub distance: Option<i64>,
} }
#[derive(Debug, Deserialize)]
pub struct HandleOpts {
pub twitter: Option<String>,
}