From 71199aa9ce6d14bc8b53da2c69234ea60b103404 Mon Sep 17 00:00:00 2001 From: realaravinth Date: Mon, 11 Oct 2021 09:30:24 +0530 Subject: [PATCH] register on first visit --- Makefile | 2 +- migrations/20211001071311_survey_users.sql | 3 +- src/api/v1/auth.rs | 57 +++++++-------- src/api/v1/bench.rs | 84 ++++++++++++++++------ src/middleware/auth.rs | 2 +- 5 files changed, 91 insertions(+), 57 deletions(-) diff --git a/Makefile b/Makefile index 09f568d..1c10b78 100644 --- a/Makefile +++ b/Makefile @@ -30,7 +30,7 @@ frontend: ## Build frontend assets @yarn install @-rm -rf ./static/cache/bundle/ @-mkdir ./static/cache/bundle/css/ - @yarn run dart-sass -s compressed templates/main.scss ./static/cache/bundle/css/main.css + #@yarn run dart-sass -s compressed templates/main.scss ./static/cache/bundle/css/main.css lint: ## Lint codebase cargo fmt -v --all -- --emit files diff --git a/migrations/20211001071311_survey_users.sql b/migrations/20211001071311_survey_users.sql index 7be1d78..20dfbd4 100644 --- a/migrations/20211001071311_survey_users.sql +++ b/migrations/20211001071311_survey_users.sql @@ -1,5 +1,4 @@ CREATE TABLE IF NOT EXISTS survey_users ( ID UUID PRIMARY KEY NOT NULL UNIQUE, - created_at TIMESTAMPTZ NOT NULL, - device VARCHAR(100) NOT NULL + created_at TIMESTAMPTZ NOT NULL ) diff --git a/src/api/v1/auth.rs b/src/api/v1/auth.rs index d11af04..f127482 100644 --- a/src/api/v1/auth.rs +++ b/src/api/v1/auth.rs @@ -14,13 +14,12 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ +use std::borrow::Cow; use actix_identity::Identity; -use actix_web::http::header; use actix_web::{web, HttpResponse, Responder}; -use serde::{Deserialize, Serialize}; +use sqlx::types::time::OffsetDateTime; -use super::get_random; use super::get_uuid; use crate::errors::*; use crate::AppData; @@ -43,39 +42,33 @@ pub mod runners { use super::*; - pub async fn register_runner() -> ServiceResult { + pub async fn register_runner(data: &AppData) -> ServiceResult { let mut uuid; + let now = OffsetDateTime::now_utc(); loop { uuid = get_uuid(); - // let res= sqlx::query!( - // "INSERT INTO - // kaizen_feedbacks (helpful , description, uuid, campaign_id, time, page_url) - // VALUES ($1, $2, $3, $4, $5, - // (SELECT ID from kaizen_campaign_pages WHERE page_url = $6))", - // &payload.helpful, - // &payload.description, - // &uuid, - // &campaign_id, - // &now, - // &payload.page_url, - // ) - // .execute(&data.db) - // .await; - // - // if res.is_ok() { - // break; - // } else if let Err(sqlx::Error::Database(err)) = res { - // if err.code() == Some(Cow::from("23505")) - // && err.message().contains("kaizen_campaign_uuid_key") - // { - // continue; - // } else { - // return Err(sqlx::Error::Database(err).into()); - // } - // } - // } + let res = sqlx::query!( + " + INSERT INTO survey_users (created_at, id) VALUES($1, $2)", + &now, + &uuid + ) + .execute(&data.db) + .await; + + if res.is_ok() { + break; + } else if let Err(sqlx::Error::Database(err)) = res { + if err.code() == Some(Cow::from("23505")) + && err.message().contains("survey_users_id_key") + { + continue; + } else { + return Err(sqlx::Error::Database(err).into()); + } + } } Ok(uuid) } @@ -86,7 +79,7 @@ pub fn services(cfg: &mut web::ServiceConfig) { } #[my_codegen::post(path = "crate::V1_API_ROUTES.auth.register")] async fn register(data: AppData, id: Identity) -> ServiceResult { - let uuid = runners::register_runner().await?; + let uuid = runners::register_runner(&data).await?; id.remember(uuid.to_string()); Ok(HttpResponse::Ok()) } diff --git a/src/api/v1/bench.rs b/src/api/v1/bench.rs index 4c4acc0..712a4c2 100644 --- a/src/api/v1/bench.rs +++ b/src/api/v1/bench.rs @@ -14,6 +14,7 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ +use std::borrow::Cow; use std::str::FromStr; use actix_identity::Identity; @@ -22,7 +23,6 @@ use futures::future::try_join_all; use serde::{Deserialize, Serialize}; use uuid::Uuid; -use super::get_random; use super::get_uuid; use crate::errors::*; use crate::AppData; @@ -58,6 +58,12 @@ struct Submission { benches: Vec, } +#[derive(Serialize, Deserialize)] +struct SubmissionProof { + token: String, + proof: String, +} + #[my_codegen::post( path = "crate::V1_API_ROUTES.benches.submit", wrap = "crate::CheckLogin" @@ -72,9 +78,20 @@ async fn submit( let user_id = Uuid::from_str(&username).unwrap(); let payload = payload.into_inner(); - sqlx::query!("INSERT INTO survey_responses (user_id, device_user_provided, device_software_recognised, - threads) VALUES ($1, $2, $3, $4)", &user_id, &payload.device_user_provided, - &payload.device_software_recognised, &payload.threads).execute(&data.db).await?; + sqlx::query!( + "INSERT INTO survey_responses ( + user_id, + device_user_provided, + device_software_recognised, + threads + ) VALUES ($1, $2, $3, $4);", + &user_id, + &payload.device_user_provided, + &payload.device_software_recognised, + &payload.threads + ) + .execute(&data.db) + .await?; struct ID { id: i32, @@ -82,8 +99,12 @@ async fn submit( let resp_id = sqlx::query_as!( ID, - "SELECT ID FROM survey_responses - WHERE user_id = $1 AND device_software_recognised = $2", + "SELECT ID + FROM survey_responses + WHERE + user_id = $1 + AND + device_software_recognised = $2;", &user_id, &payload.device_software_recognised ) @@ -95,7 +116,8 @@ async fn submit( for bench in payload.benches.iter() { let fut = sqlx::query!( "INSERT INTO survey_benches - (resp_id, difficulty, duration) VALUES ($1, $2, $3)", + (resp_id, difficulty, duration) + VALUES ($1, $2, $3);", &resp_id.id, &bench.difficulty, bench.duration @@ -105,20 +127,40 @@ async fn submit( futs.push(fut); } - let submitions_id = get_uuid(); - - let fut = sqlx::query!( - "INSERT INTO survey_response_tokens (resp_id, user_id, id) - VALUES ($1, $2, $3)", - &resp_id.id, - &user_id, - &submitions_id - ) - .execute(&data.db); - - futs.push(fut); - + let mut submitions_id; try_join_all(futs).await?; - Ok(HttpResponse::Ok()) + loop { + submitions_id = get_uuid(); + + let res = sqlx::query!( + "INSERT INTO survey_response_tokens + (resp_id, user_id, id) + VALUES ($1, $2, $3);", + &resp_id.id, + &user_id, + &submitions_id + ) + .execute(&data.db) + .await; + + if res.is_ok() { + break; + } else if let Err(sqlx::Error::Database(err)) = res { + if err.code() == Some(Cow::from("23505")) + && err.message().contains("survey_response_tokens_id_key") + { + continue; + } else { + return Err(sqlx::Error::Database(err).into()); + } + } + } + + let resp = SubmissionProof { + token: username, + proof: submitions_id.to_string(), + }; + + Ok(HttpResponse::Ok().json(resp)) } diff --git a/src/middleware/auth.rs b/src/middleware/auth.rs index ef18902..3377923 100644 --- a/src/middleware/auth.rs +++ b/src/middleware/auth.rs @@ -24,7 +24,7 @@ use actix_web::{http, Error, FromRequest, HttpResponse}; use futures::future::{ok, Either, Ready}; -pub const AUTH: &str = "/login"; //crate::PAGES.auth.login; +pub const AUTH: &str = crate::V1_API_ROUTES.auth.register; pub struct CheckLogin;