wip: wanting to go clean but bleh fromuriparam

This commit is contained in:
Artemis 2025-02-13 12:53:13 +01:00
parent 5722b49c37
commit 9ae95b4e93
4 changed files with 80 additions and 4 deletions

View file

@ -1,6 +1,13 @@
use rand::{distributions::Uniform, prelude::Distribution, thread_rng};
use regex::Regex;
use rocket::form;
use rocket::{
form,
http::uri::{
fmt::{FromUriParam, Part},
Path,
},
request::FromParam,
};
use crate::db::{doll, schema::DollTagsDb};
@ -26,12 +33,43 @@ pub fn id_public_to_db(id: &str) -> Option<i32> {
None
}
}
/// TODO: Check if used anywhere else than template rendering
pub fn id_db_to_public(id: i32) -> String {
let first = id / 1000;
let second = id % 1000;
format!("{:0>3}-{:0>3}", first, second)
}
/// A cleaner way to handle on-the-fly in-URL parameter format validation for IDs
/// TODO: check and remove all other usages
pub struct PublicId(pub i32);
impl<'a> FromParam<'a> for PublicId {
type Error = &'static str;
fn from_param(param: &'a str) -> Result<Self, Self::Error> {
if let Some(v) = id_public_to_db(param) {
Ok(PublicId(v))
} else {
Err("id not formatted properly")
}
}
}
impl<P: Part, 'a> FromUriParam<P, &'a str> for PublicId {
type Target = PublicId;
fn from_uri_param(param: T) -> Self::Target {
todo!()
}
}
impl Into<i32> for PublicId {
fn into(self) -> i32 {
self.0
}
}
pub fn validate_id<'v>(id: &str) -> form::Result<'v, ()> {
if let None = id_public_to_db(id) {
Err(form::Error::validation("id not formatted properly"))?;

View file

@ -84,7 +84,14 @@ fn rocket() -> _ {
account::export_data,
],
)
.mount("/admin", routes![admin::index, admin::handle_in_page_forms])
.mount(
"/admin",
routes![
admin::index,
admin::handle_in_page_forms,
admin::show_confirm_tag_handover
],
)
.mount(
"/",
routes![

View file

@ -1,5 +1,6 @@
use rocket::{
form::{Context, Contextual, Error, Form},
response::Redirect,
serde::Serialize,
};
use rocket_dyn_templates::{context, Template};
@ -8,7 +9,7 @@ use sqlx::Acquire;
use crate::{
auth::session::Admin,
db::{admin, doll, schema::DollTagsDb, user},
ids::id_public_to_db,
ids::{id_public_to_db, PublicId},
pages::CommonTemplateState,
};
@ -126,7 +127,12 @@ pub async fn handle_in_page_forms(
}
if user_valid && target_tag.is_some() {
todo!("woof redirect to tag-handover confirm");
let id = target_tag.unwrap().id;
return Ok(Redirect::to(uri!(
"/admin",
show_confirm_tag_handover(PublicId(id), values.dest_account)
))
.into());
}
}
}
@ -147,3 +153,10 @@ pub async fn handle_in_page_forms(
)
.into())
}
#[get("/tag-handover/<id>/<username>")]
pub async fn show_confirm_tag_handover(id: PublicId, username: &str) -> &'static str {
println!("woof id: {}, username: {}", id.0, username);
"blep"
}

View file

@ -0,0 +1,18 @@
{% extends "base" %}
{% block title %}Confirm tag handover? - {% endblock title %}
{% block main %}
<section>
<h2>Confirm tag handover?</h2>
<p>
You are about to hand over the tag <code>WOOF</code> to the user named "".<br />
It will give full control of the tag to them and will not let you edit it anymore as it is a complete handover.
</p>
<p>Do you wish to proceed?</p>
<div>
<a href="/" class="btn">Yes, hand it over to them</a>
<a href="/" class="btn">No, cancel the handover</a>
</div>
</section>
{% endblock main %}