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