implemented all the new flows and removed the big chonk get method

This commit is contained in:
Artemis 2025-04-02 11:57:39 +02:00
parent 5f620c0442
commit 639df81aec
7 changed files with 79 additions and 44 deletions

View file

@ -281,8 +281,14 @@ p.note {
font-size: .9em;
}
div.submit {
margin: 2em 0;
display: flex;
justify-content: space-around;
align-items: center;
}
button.submit {
margin: 2em auto;
font-size: 1.2em;
display: block;
}
@ -451,6 +457,6 @@ pre.recovery-key {
@media screen and (min-width: 800px) {
.dual-fields>*:last-child {
flex: 2;
flex: 1;
}
}

View file

@ -34,37 +34,36 @@ pub async fn list_all(db: &mut DbHook, from: &Uuid) -> sqlx::Result<Vec<DollProf
.fetch_all(&mut **db)
.await
}
// TODO: its a fkin mess and i dont wanna deal with multiple state variables right now. probably need a struct instead of bools in the rust side
/// Tries to get the requested tag based on the provided ID or microchip ID.
/// Will only include public non-archived tags.
pub async fn get_public(
db: &mut DbHook,
ident: i32,
microchip_id: &str,
) -> sqlx::Result<Option<DollProfile>> {
sqlx::query_as!(
DollProfile,
r#"select * from doll_profiles where (id = $1 or microchip_id = $2) and archived_at is null and is_public is true"#,
ident,
microchip_id
).fetch_optional(&mut **db)
.await
}
pub async fn get(
db: &mut DbHook,
ident: i32,
microchip_id: &str,
include_archived: bool,
) -> sqlx::Result<Option<DollProfile>> {
if include_archived {
sqlx::query_as!(
DollProfile,
r#"
select * from doll_profiles where (id = $1 or microchip_id = $2)
"#,
ident,
microchip_id
)
.fetch_optional(&mut **db)
.await
} else {
sqlx::query_as!(
DollProfile,
r#"
select * from doll_profiles where (id = $1 or microchip_id = $2) and archived_at is null and is_public is true
"#,
ident,
microchip_id
)
.fetch_optional(&mut **db)
.await
}
sqlx::query_as!(
DollProfile,
r#"select * from doll_profiles where (id = $1 or microchip_id = $2)"#,
ident,
microchip_id
)
.fetch_optional(&mut **db)
.await
}
/// Checks if some of the provided IDs are already used, returning the list of IDs that are used

View file

@ -85,10 +85,10 @@ pub async fn ask_delete(
user: User,
meta: CommonTemplateState,
) -> PageResult {
let db_tag = doll::get(&mut *db, id, "", false).await?;
let db_tag = doll::get(&mut *db, id, "").await?;
if let Some(tag) = db_tag {
if tag.bound_to_id != user.id {
if tag.bound_to_id != user.id || tag.archived_at.is_some() {
Ok(Redirect::to(uri!("/account", index)).into())
} else {
Ok(Template::render(

View file

@ -115,7 +115,6 @@ pub async fn handle_in_page_forms(
id_public_to_db(values.tag_id)
.expect("is form-validated so should always succeed"),
"",
true,
)
.await?;
if target_tag.is_none() {

View file

@ -41,6 +41,7 @@ impl From<DollProfile> for FakeContext {
"microchip_id",
vec![tag.microchip_id.unwrap_or(String::from(""))],
),
("is_public", vec![tag.is_public.to_string()]),
("name", vec![tag.name]),
("pronoun_subject", vec![tag.pronoun_subject]),
("pronoun_object", vec![tag.pronoun_object]),
@ -87,7 +88,7 @@ pub async fn show_edit_tag(
Some(v) => v,
None => return Ok(Redirect::to(uri!("/account", account::common::index)).into()),
};
let tag = match doll::get(&mut *db, normalized_id, "", true).await? {
let tag = match doll::get(&mut *db, normalized_id, "").await? {
Some(v) => {
if v.bound_to_id != user.id {
return Ok(Redirect::to(uri!("/account", account::common::index)).into());
@ -102,7 +103,7 @@ pub async fn show_edit_tag(
"register_tag",
context! {
mode: "edit",
id,
id: normalized_id,
previous: FakeContext::from(tag),
meta,
},
@ -117,6 +118,8 @@ pub struct TagForm<'a> {
#[field(validate=len(..32))]
pub microchip_id: &'a str,
pub is_public: bool,
#[field(validate=len(1..=256))]
pub name: &'a str,
#[field(validate=len(1..=32))]
@ -229,6 +232,7 @@ pub async fn handle_register(
chassis_id: normalize_opt(tag.chassis_id),
chassis_color: normalize_opt(tag.chassis_color),
bound_to_id: &user.id,
is_public: tag.is_public,
},
)
.await;
@ -250,7 +254,11 @@ pub async fn handle_register(
tag.ident
);
Ok(Redirect::to(uri!(public::show_profile(Some(tag.ident), microchip_id))).into())
if tag.is_public {
Ok(Redirect::to(uri!(public::show_profile(Some(tag.ident), microchip_id))).into())
} else {
Ok(Redirect::to(uri!(account::common::index)).into())
}
}
#[post("/edit_tag/<id>", data = "<tag>")]
@ -316,9 +324,14 @@ pub async fn handle_edit_tag(
chassis_id: normalize_opt(tag.chassis_id),
chassis_color: normalize_opt(tag.chassis_color),
bound_to_id: &user.id,
is_public: tag.is_public,
},
)
.await?;
Ok(Redirect::to(uri!(public::show_profile(Some(tag.ident), microchip_id))).into())
if tag.is_public {
Ok(Redirect::to(uri!(public::show_profile(Some(tag.ident), microchip_id))).into())
} else {
Ok(Redirect::to(uri!(account::common::index)).into())
}
}

View file

@ -47,7 +47,7 @@ pub async fn show_profile(
let internal_id = ident.and_then(|v| id_public_to_db(v)).unwrap_or(0);
let microchip_id = microchip_id.unwrap_or("");
let profile = match doll::get(&mut *db, internal_id, microchip_id, false).await? {
let profile = match doll::get_public(&mut *db, internal_id, microchip_id).await? {
Some(p) => p,
None => return Ok(Redirect::to(uri!(index(Some(true), ident, Some(microchip_id)))).into()),
};

View file

@ -1,10 +1,11 @@
{% extends "base" %}
{% import "macros/form" as form %}
{% block title %}{% if mode == "register" %}Register a new tag{% else %}Edit {{id}}{% endif %} - {% endblock title
{% block title %}{% if mode == "register" %}Register a new tag{% else %}Edit {{id|pretty_id}}{% endif %} - {% endblock
title
%}
{% block main %}
<h2>{% if mode == "register" %}Register a new tag{% else %}Edit {{id}}{% endif %}</h2>
<h2>{% if mode == "register" %}Register a new tag{% else %}Edit {{id|pretty_id}}{% endif %}</h2>
<aside>
<h3>A foreword</h3>
@ -205,13 +206,30 @@
</div>
</section>
<button class="submit" type="submit">
{% if mode == "register" %}
Register this tag!
{% else %}
Save your changes
{% endif %}
</button>
<div class="submit">
<div>
{% set tag_vis = previous.values | get(key="is_public", default=[]) %}
{% set is_public = tag_vis | length > 0 and tag_vis | first == "true" %}
<label for="is_public">Who can see the tag?</label>
<select name="is_public" id="is_public">
<option value="true">Everyone, it's public</option>
<option value="false" {% if is_public==false %}selected{% endif %}>No one, keep it private for now
</option>
</select>
{{form::error(ctx=previous, name="is_public")}}
</div>
<div>
<button class="submit" type="submit">
{% if mode == "register" %}
Register this tag!
{% else %}
Save your changes
{% endif %}
</button>
</div>
</div>
</form>
</section>