Fix new ad
This commit is contained in:
parent
a9abb885f4
commit
427110836e
|
@ -18,6 +18,7 @@ pub struct NewAdQuery {
|
|||
pub pubkey: String,
|
||||
pub quantity: String,
|
||||
pub title: String,
|
||||
pub group: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
|
@ -61,8 +62,3 @@ pub enum AdminQuery {
|
|||
#[serde(rename = "rm_ad")]
|
||||
RmAd(AdminRmAdQuery),
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct SelectAdQuery {
|
||||
pub ad: String,
|
||||
}
|
||||
|
|
177
src/server.rs
177
src/server.rs
|
@ -60,6 +60,14 @@ pub async fn start_server(
|
|||
)
|
||||
}
|
||||
});
|
||||
app.at(&format!("{}g/:group", config.root_url)).post({
|
||||
let config = config.clone();
|
||||
let templates = templates.clone();
|
||||
let dbs = dbs.clone();
|
||||
move |req: tide::Request<()>| {
|
||||
handle_post_index(req, config.clone(), templates.clone(), dbs.clone())
|
||||
}
|
||||
});
|
||||
app.at(&format!("{}g/:group/:ad", config.root_url)).get({
|
||||
let config = config.clone();
|
||||
let templates = templates.clone();
|
||||
|
@ -181,13 +189,16 @@ async fn serve_index<'a>(
|
|||
dbs.ad_by_group
|
||||
.scan_prefix(ROOT_GROUP_ID)
|
||||
.filter_map(|x| {
|
||||
let (k, ad) = x.ok()?;
|
||||
let ad_id =
|
||||
hex::encode(AdId::try_from(&k.as_ref()[16..32]).ok()?);
|
||||
let (k, _) = x.ok()?;
|
||||
let ad_id = AdId::try_from(&k.as_ref()[16..32]).ok()?;
|
||||
let ad_id_hex = hex::encode(&ad_id);
|
||||
Some(AdWithId {
|
||||
ad: bincode::deserialize::<Ad>(&ad).ok()?,
|
||||
selected: selected_ad.map_or(false, |i| i == ad_id),
|
||||
id: ad_id,
|
||||
ad: bincode::deserialize::<Ad>(
|
||||
&dbs.ad.get(&ad_id).unwrap()?,
|
||||
)
|
||||
.ok()?,
|
||||
selected: selected_ad.map_or(false, |i| i == ad_id_hex),
|
||||
id: ad_id_hex,
|
||||
})
|
||||
})
|
||||
.collect::<Vec<AdWithId>>(),
|
||||
|
@ -237,15 +248,18 @@ async fn serve_group<'a>(
|
|||
dbs.ad_by_group
|
||||
.scan_prefix(group_id.clone())
|
||||
.filter_map(|x| {
|
||||
let (k, ad) = x.ok()?;
|
||||
let ad_id = hex::encode(
|
||||
AdId::try_from(&k.as_ref()[16..32]).ok()?,
|
||||
);
|
||||
let (k, _) = x.ok()?;
|
||||
let ad_id =
|
||||
AdId::try_from(&k.as_ref()[16..32]).ok()?;
|
||||
let ad_id_hex = hex::encode(&ad_id);
|
||||
Some(AdWithId {
|
||||
ad: bincode::deserialize::<Ad>(&ad).ok()?,
|
||||
ad: bincode::deserialize::<Ad>(
|
||||
&dbs.ad.get(&ad_id).unwrap()?,
|
||||
)
|
||||
.ok()?,
|
||||
selected: selected_ad
|
||||
.map_or(false, |i| i == ad_id),
|
||||
id: ad_id,
|
||||
.map_or(false, |i| i == ad_id_hex),
|
||||
id: ad_id_hex,
|
||||
})
|
||||
})
|
||||
.collect::<Vec<AdWithId>>(),
|
||||
|
@ -330,15 +344,18 @@ async fn serve_admin_group<'a>(
|
|||
dbs.ad_by_group
|
||||
.scan_prefix(group_id.clone())
|
||||
.filter_map(|x| {
|
||||
let (k, ad) = x.ok()?;
|
||||
let ad_id = hex::encode(
|
||||
AdId::try_from(&k.as_ref()[16..32]).ok()?,
|
||||
);
|
||||
let (k, _) = x.ok()?;
|
||||
let ad_id =
|
||||
AdId::try_from(&k.as_ref()[16..32]).ok()?;
|
||||
let ad_id_hex = hex::encode(&ad_id);
|
||||
Some(AdWithId {
|
||||
ad: bincode::deserialize::<Ad>(&ad).ok()?,
|
||||
ad: bincode::deserialize::<Ad>(
|
||||
&dbs.ad.get(&ad_id).unwrap()?,
|
||||
)
|
||||
.ok()?,
|
||||
selected: selected_ad
|
||||
.map_or(false, |i| i == ad_id),
|
||||
id: ad_id,
|
||||
.map_or(false, |i| i == ad_id_hex),
|
||||
id: ad_id_hex,
|
||||
})
|
||||
})
|
||||
.collect::<Vec<AdWithId>>(),
|
||||
|
@ -416,13 +433,16 @@ async fn serve_admin<'a>(
|
|||
dbs.ad_by_group
|
||||
.scan_prefix(ROOT_GROUP_ID)
|
||||
.filter_map(|x| {
|
||||
let (k, ad) = x.ok()?;
|
||||
let ad_id =
|
||||
hex::encode(AdId::try_from(&k.as_ref()[16..32]).ok()?);
|
||||
let (k, _) = x.ok()?;
|
||||
let ad_id = AdId::try_from(&k.as_ref()[16..32]).ok()?;
|
||||
let ad_id_hex = hex::encode(&ad_id);
|
||||
Some(AdWithId {
|
||||
ad: bincode::deserialize::<Ad>(&ad).ok()?,
|
||||
selected: selected_ad.map_or(false, |i| i == ad_id),
|
||||
id: ad_id,
|
||||
ad: bincode::deserialize::<Ad>(
|
||||
&dbs.ad.get(&ad_id).unwrap()?,
|
||||
)
|
||||
.ok()?,
|
||||
selected: selected_ad.map_or(false, |i| i == ad_id_hex),
|
||||
id: ad_id_hex,
|
||||
})
|
||||
})
|
||||
.collect::<Vec<AdWithId>>(),
|
||||
|
@ -489,52 +509,65 @@ async fn handle_new_ad(
|
|||
dbs: Dbs,
|
||||
query: NewAdQuery,
|
||||
) -> tide::Result<tide::Response> {
|
||||
dbs.ad
|
||||
.insert(
|
||||
AdId::random(),
|
||||
bincode::serialize(&Ad {
|
||||
pubkey: if query.pubkey.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(match format_pubkey(&query.pubkey) {
|
||||
Ok(pubkey) => pubkey,
|
||||
Err(e) => {
|
||||
return serve_index(
|
||||
req,
|
||||
config,
|
||||
templates,
|
||||
dbs,
|
||||
&[ErrorTemplate {
|
||||
text: match e {
|
||||
PubkeyDecodeError::BadChecksum => {
|
||||
"La somme de contrôle de la clé publique est \
|
||||
incorrecte."
|
||||
}
|
||||
PubkeyDecodeError::BadFormat => {
|
||||
"Le format de la clé publique est incorrect."
|
||||
}
|
||||
},
|
||||
}],
|
||||
Some(query),
|
||||
)
|
||||
.await
|
||||
}
|
||||
})
|
||||
},
|
||||
author: query.author,
|
||||
password: Argon2::default()
|
||||
.hash_password(query.psw.as_bytes(), &SaltString::generate(&mut OsRng))
|
||||
.unwrap()
|
||||
.to_string(),
|
||||
price: query.price,
|
||||
quantity: query.quantity,
|
||||
time: 0,
|
||||
title: query.title,
|
||||
})
|
||||
.unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
dbs.ad.flush_async().await.unwrap();
|
||||
if let Some(Ok(group_id)) = if query.group.is_empty() {
|
||||
Some(Ok(ROOT_GROUP_ID))
|
||||
} else {
|
||||
dbs.group_by_name
|
||||
.get(&query.group)
|
||||
.unwrap()
|
||||
.map(|o| GroupId::try_from(o.as_ref()))
|
||||
} {
|
||||
let ad_id = AdId::random();
|
||||
dbs.ad
|
||||
.insert(
|
||||
ad_id.clone(),
|
||||
bincode::serialize(&Ad {
|
||||
pubkey: if query.pubkey.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(match format_pubkey(&query.pubkey) {
|
||||
Ok(pubkey) => pubkey,
|
||||
Err(e) => {
|
||||
return serve_index(
|
||||
req,
|
||||
config,
|
||||
templates,
|
||||
dbs,
|
||||
&[ErrorTemplate {
|
||||
text: match e {
|
||||
PubkeyDecodeError::BadChecksum => {
|
||||
"La somme de contrôle de la clé publique est \
|
||||
incorrecte."
|
||||
}
|
||||
PubkeyDecodeError::BadFormat => {
|
||||
"Le format de la clé publique est incorrect."
|
||||
}
|
||||
},
|
||||
}],
|
||||
Some(query),
|
||||
)
|
||||
.await
|
||||
}
|
||||
})
|
||||
},
|
||||
author: query.author,
|
||||
password: Argon2::default()
|
||||
.hash_password(query.psw.as_bytes(), &SaltString::generate(&mut OsRng))
|
||||
.unwrap()
|
||||
.to_string(),
|
||||
price: query.price,
|
||||
quantity: query.quantity,
|
||||
time: 0,
|
||||
title: query.title,
|
||||
})
|
||||
.unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
dbs.ad_by_group
|
||||
.insert([group_id, ad_id.0].concat(), &[])
|
||||
.unwrap();
|
||||
dbs.ad.flush_async().await.unwrap();
|
||||
}
|
||||
Ok(tide::Redirect::new(&config.root_url).into())
|
||||
}
|
||||
|
||||
|
|
|
@ -14,7 +14,7 @@ pub struct Group {
|
|||
}
|
||||
|
||||
#[derive(Clone, Debug, Hash, Eq, PartialEq, Serialize)]
|
||||
pub struct AdId([u8; 16]);
|
||||
pub struct AdId(pub [u8; 16]);
|
||||
|
||||
impl AdId {
|
||||
pub fn random() -> Self {
|
||||
|
|
|
@ -76,6 +76,7 @@
|
|||
|
||||
<form method="post">
|
||||
<input type="hidden" name="a" value="new_ad" autocomplete="off"/>
|
||||
<input type="hidden" name="group" value="{{group.name}}" autocomplete="off"/>
|
||||
<fieldset>
|
||||
<legend>Nouvelle annonce</legend>
|
||||
<label for="f_new_title">Titre :</label>
|
||||
|
|
|
@ -76,6 +76,7 @@
|
|||
|
||||
<form method="post">
|
||||
<input type="hidden" name="a" value="new_ad" autocomplete="off"/>
|
||||
<input type="hidden" name="group" value="" autocomplete="off"/>
|
||||
<fieldset>
|
||||
<legend>Nouvelle annonce</legend>
|
||||
<label for="f_new_title">Titre :</label>
|
||||
|
|
Loading…
Reference in New Issue