From 9ae95b4e932361fbfedd2a2be25d65ca966027fd Mon Sep 17 00:00:00 2001 From: Artemis Date: Thu, 13 Feb 2025 12:53:13 +0100 Subject: [PATCH] wip: wanting to go clean but bleh fromuriparam --- src/ids.rs | 40 ++++++++++++++++++- src/main.rs | 9 ++++- src/routes/admin.rs | 17 +++++++- .../admin/confirm_tag_handover.html.tera | 18 +++++++++ 4 files changed, 80 insertions(+), 4 deletions(-) create mode 100644 templates/admin/confirm_tag_handover.html.tera diff --git a/src/ids.rs b/src/ids.rs index 86cc9cb..4d69285 100644 --- a/src/ids.rs +++ b/src/ids.rs @@ -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 { 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 { + if let Some(v) = id_public_to_db(param) { + Ok(PublicId(v)) + } else { + Err("id not formatted properly") + } + } +} + +impl FromUriParam for PublicId { + type Target = PublicId; + + fn from_uri_param(param: T) -> Self::Target { + todo!() + } +} + +impl Into 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"))?; diff --git a/src/main.rs b/src/main.rs index a194c4b..ca6d70c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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![ diff --git a/src/routes/admin.rs b/src/routes/admin.rs index c10344b..4e6d6e7 100644 --- a/src/routes/admin.rs +++ b/src/routes/admin.rs @@ -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//")] +pub async fn show_confirm_tag_handover(id: PublicId, username: &str) -> &'static str { + println!("woof id: {}, username: {}", id.0, username); + + "blep" +} diff --git a/templates/admin/confirm_tag_handover.html.tera b/templates/admin/confirm_tag_handover.html.tera new file mode 100644 index 0000000..ba4ff52 --- /dev/null +++ b/templates/admin/confirm_tag_handover.html.tera @@ -0,0 +1,18 @@ +{% extends "base" %} +{% block title %}Confirm tag handover? - {% endblock title %} +{% block main %} +
+

Confirm tag handover?

+

+ You are about to hand over the tag WOOF to the user named "".
+ It will give full control of the tag to them and will not let you edit it anymore as it is a complete handover. +

+ +

Do you wish to proceed?

+ + +
+{% endblock main %} \ No newline at end of file