diff --git a/Cargo.lock b/Cargo.lock index 945c1a8..fb670a2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2013,6 +2013,7 @@ dependencies = [ "rocket_codegen", "rocket_http", "serde", + "serde_json", "state", "tempfile", "time", diff --git a/Cargo.toml b/Cargo.toml index 7bdbf97..a51e2b7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ version = "0.1.0" edition = "2021" [dependencies] -rocket = { version = "0.5.1", features = ["secrets"] } +rocket = { version = "0.5.1", features = ["secrets", "json"] } rocket_dyn_templates = { version = "0.2.0", features = ["tera"] } rocket_db_pools = { version = "0.2.0", features = ["sqlx_postgres"] } sqlx = { version = "0.7", default-features = false, features = [ diff --git a/README.md b/README.md index 2f297c4..72ca1d2 100644 --- a/README.md +++ b/README.md @@ -24,5 +24,4 @@ a profile is - p2: saving register form as it gets filled / re-display it with partial values - account - p2: optional email for forgotten password i guess - - ability to delete the whole account (incl. all registered tags) - deleting a tag keeping the account allows to re-use the tag later on. no one else can use it still diff --git a/src/main.rs b/src/main.rs index 3c4f515..64e0976 100644 --- a/src/main.rs +++ b/src/main.rs @@ -38,6 +38,7 @@ fn rocket() -> _ { account::confirm_delete, account::ask_terminate_account, account::confirm_terminate_account, + account::export_data, ], ) .mount( diff --git a/src/routes/account.rs b/src/routes/account.rs index c51dc97..011e08c 100644 --- a/src/routes/account.rs +++ b/src/routes/account.rs @@ -1,4 +1,8 @@ -use rocket::{http::CookieJar, response::Redirect}; +use rocket::{ + http::CookieJar, + response::Redirect, + serde::{json::Json, Serialize}, +}; use rocket_dyn_templates::{context, Template}; use sqlx::Acquire; @@ -6,13 +10,13 @@ use crate::{ auth::session, db::{ doll, - schema::{DollTagsDb, User}, + schema::{DollProfile, DollTagsDb, User}, user, }, pages::CommonTemplateState, }; -use super::error_handlers::PageResult; +use super::error_handlers::{PageResult, RawResult}; #[get("/")] pub async fn index(mut db: DollTagsDb, user: User, meta: CommonTemplateState) -> PageResult { @@ -36,6 +40,26 @@ pub fn show_settings(user: User, meta: CommonTemplateState) -> Template { Template::render("account/settings", context! {user,meta}) } +#[derive(Debug, Serialize)] +#[serde(crate = "rocket::serde")] +pub struct DataDump { + account: User, + tags: Vec, + reserved_tags: Vec, +} + +#[get("/data_dump")] +pub async fn export_data(mut db: DollTagsDb, user: User) -> RawResult> { + let tags = doll::list(&mut *db, &user.id).await?; + let reserved_tags = doll::list_archived(&mut *db, &user.id).await?; + + Ok(Json(DataDump { + account: user, + tags, + reserved_tags, + })) +} + #[get("/delete/")] pub async fn ask_delete( mut db: DollTagsDb, diff --git a/templates/account/settings.html.tera b/templates/account/settings.html.tera index 46e4b2f..acda537 100644 --- a/templates/account/settings.html.tera +++ b/templates/account/settings.html.tera @@ -50,7 +50,7 @@

You can export all your account's data using this button.

It will be provided in a JSON file whose structure is documented.

- Export all my data + Export all my data