Accepts ad with no pubkey

This commit is contained in:
Pascal Engélibert 2020-12-08 22:45:29 +01:00
parent 59c69765d7
commit 6a70bfc032
1 changed files with 31 additions and 28 deletions

View File

@ -121,28 +121,31 @@ pub async fn start_server(
.and(warp::get()) .and(warp::get())
.and(warp::fs::dir(opt.dir.0.join(static_files::STATIC_DIR))); .and(warp::fs::dir(opt.dir.0.join(static_files::STATIC_DIR)));
let handle_new_ad = let handle_new_ad = {
{ let handle_index = handle_index.clone();
let handle_index = handle_index.clone(); let dbs = dbs.clone();
let dbs = dbs.clone(); let cache_ads = cache_ads.clone();
let cache_ads = cache_ads.clone(); move |query: NewAdQuery| {
move |query: NewAdQuery| { let mut hasher = Sha512Trunc256::new();
let mut hasher = Sha512Trunc256::new(); hasher.update(query.psw);
hasher.update(query.psw); dbs.ads
dbs.ads
.insert( .insert(
AdId::random(), AdId::random(),
bincode::serialize(&Ad { bincode::serialize(&Ad {
author: query.author, author: query.author,
password: hasher.finalize()[..].try_into().unwrap(), password: hasher.finalize()[..].try_into().unwrap(),
price: query.price, price: query.price,
pubkey: (!query.pubkey.is_empty()).then_some(match format_pubkey(&query.pubkey) { pubkey: if query.pubkey.is_empty() {
None
} else {
Some(match format_pubkey(&query.pubkey) {
Ok(pubkey) => pubkey, Ok(pubkey) => pubkey,
Err(e) => return handle_index(&[ErrorTemplate{text: match e { Err(e) => return handle_index(&[ErrorTemplate{text: match e {
PubkeyDecodeError::BadChecksum => "La somme de contrôle de la clé publique est incorrecte.", PubkeyDecodeError::BadChecksum => "La somme de contrôle de la clé publique est incorrecte.",
PubkeyDecodeError::BadFormat => "Le format de la clé publique est incorrect.", PubkeyDecodeError::BadFormat => "Le format de la clé publique est incorrect.",
}}]) }}])
}), })
},
quantity: query.quantity, quantity: query.quantity,
time: 0, time: 0,
title: query.title, title: query.title,
@ -150,24 +153,24 @@ pub async fn start_server(
.unwrap(), .unwrap(),
) )
.unwrap(); .unwrap();
dbs.ads.flush().unwrap(); dbs.ads.flush().unwrap();
let mut cache_ads = cache_ads.write().unwrap(); let mut cache_ads = cache_ads.write().unwrap();
*cache_ads = to_json( *cache_ads = to_json(
dbs.ads dbs.ads
.iter() .iter()
.filter_map(|x| { .filter_map(|x| {
let (ad_id, ad) = x.ok()?; let (ad_id, ad) = x.ok()?;
Some(AdWithId { Some(AdWithId {
id: hex::encode(ad_id.as_ref()), id: hex::encode(ad_id.as_ref()),
ad: bincode::deserialize::<Ad>(&ad).ok()?, ad: bincode::deserialize::<Ad>(&ad).ok()?,
})
}) })
.collect::<Vec<AdWithId>>(), })
); .collect::<Vec<AdWithId>>(),
drop(cache_ads); );
redirect_302("/").into_response() drop(cache_ads);
} redirect_302("/").into_response()
}; }
};
let handle_rm_ad = { let handle_rm_ad = {
let handle_index = handle_index.clone(); let handle_index = handle_index.clone();