From d5668e085f6c1340806f6f5a54c95c45e2f2ac18 Mon Sep 17 00:00:00 2001 From: ntr Date: Wed, 15 Jan 2020 16:06:43 +1000 Subject: [PATCH] fix email confirmation --- server/src/mail.rs | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/server/src/mail.rs b/server/src/mail.rs index 8f3b351f..0322d31c 100644 --- a/server/src/mail.rs +++ b/server/src/mail.rs @@ -232,6 +232,12 @@ pub fn set(tx: &mut Transaction, account: Uuid, email: &String) -> Result<(Uuid, RETURNING id; "; + let select_query = " + SELECT * + FROM emails + WHERE account = $1; + "; + let update_query = " UPDATE emails SET email = $1, confirm_token = $2, confirmed = false, recover_token = $3 @@ -239,18 +245,11 @@ pub fn set(tx: &mut Transaction, account: Uuid, email: &String) -> Result<(Uuid, RETURNING id; "; - let result = match tx.query(insert_query, &[&id, &account, &email, &confirm_token, &recover_token]) { - Ok(r) => r, - // email update probably - Err(_) => { - match tx.query(update_query, &[&email, &confirm_token, &recover_token, &account]) { - Ok(r) => r, - Err(e) => { - warn!("{:?}", e); - return Err(err_msg("no email set")); - }, - } - } + let existing = tx.query(select_query, &[&id])?; + + let result = match existing.iter().next() { + Some(_) => tx.query(insert_query, &[&id, &account, &email, &confirm_token, &recover_token])?, + None => tx.query(update_query, &[&email, &confirm_token, &recover_token, &account])?, }; match result.iter().next() {