2021-10-10 19:23:48 +05:30
|
|
|
/*
|
|
|
|
* Copyright (C) 2021 Aravinth Manivannan <realaravinth@batsense.net>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2021-10-11 09:30:24 +05:30
|
|
|
use std::borrow::Cow;
|
2021-10-10 19:23:48 +05:30
|
|
|
use std::str::FromStr;
|
|
|
|
|
|
|
|
use actix_identity::Identity;
|
2021-10-12 21:56:16 +05:30
|
|
|
use actix_web::{http, web, HttpResponse, Responder};
|
2021-10-10 19:23:48 +05:30
|
|
|
use futures::future::try_join_all;
|
|
|
|
use serde::{Deserialize, Serialize};
|
2021-10-11 09:56:15 +05:30
|
|
|
use sqlx::types::time::OffsetDateTime;
|
2021-10-10 19:23:48 +05:30
|
|
|
use uuid::Uuid;
|
|
|
|
|
|
|
|
use super::get_uuid;
|
|
|
|
use crate::errors::*;
|
|
|
|
use crate::AppData;
|
|
|
|
|
|
|
|
pub mod routes {
|
2021-10-12 21:56:16 +05:30
|
|
|
|
|
|
|
use crate::middleware::auth::GetLoginRoute;
|
|
|
|
|
2021-10-10 19:23:48 +05:30
|
|
|
pub struct Benches {
|
|
|
|
pub submit: &'static str,
|
2021-10-11 09:56:15 +05:30
|
|
|
pub register: &'static str,
|
2021-10-11 18:28:45 +05:30
|
|
|
pub fetch: &'static str,
|
2021-10-11 09:56:15 +05:30
|
|
|
pub scope: &'static str,
|
2021-10-10 19:23:48 +05:30
|
|
|
}
|
|
|
|
|
2021-10-12 21:56:16 +05:30
|
|
|
impl GetLoginRoute for Benches {
|
|
|
|
fn get_login_route(&self, src: Option<&str>) -> String {
|
|
|
|
if let Some(redirect_to) = src {
|
|
|
|
// uri::Builder::new().path_and_query(
|
|
|
|
format!(
|
|
|
|
"{}?redirect_to={}",
|
|
|
|
self.register,
|
|
|
|
urlencoding::encode(redirect_to)
|
|
|
|
)
|
|
|
|
// let mut url: Uri = self.register.parse().unwrap();
|
|
|
|
// url.qu
|
|
|
|
// url.query_pairs_mut()
|
|
|
|
// .append_pair("redirect_to", redirect_to);
|
|
|
|
} else {
|
|
|
|
self.register.to_string()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-10 19:23:48 +05:30
|
|
|
impl Benches {
|
|
|
|
pub const fn new() -> Benches {
|
2021-10-11 13:41:36 +05:30
|
|
|
let submit = "/api/v1/benches/{campaign_id}/submit";
|
2021-10-11 18:28:45 +05:30
|
|
|
let fetch = "/api/v1/benches/{campaign_id}/fetch";
|
2021-10-11 09:56:15 +05:30
|
|
|
let register = "/api/v1/benches/register";
|
|
|
|
let scope = "/api/v1/benches/";
|
2021-10-11 10:09:51 +05:30
|
|
|
Benches {
|
|
|
|
submit,
|
|
|
|
register,
|
2021-10-11 18:28:45 +05:30
|
|
|
fetch,
|
2021-10-11 10:09:51 +05:30
|
|
|
scope,
|
|
|
|
}
|
2021-10-10 19:23:48 +05:30
|
|
|
}
|
2021-10-11 13:41:36 +05:30
|
|
|
pub fn submit_route(&self, campaign_id: &str) -> String {
|
2021-10-12 21:56:16 +05:30
|
|
|
self.submit.replace("{campaign_id}", campaign_id)
|
2021-10-11 13:41:36 +05:30
|
|
|
}
|
2021-10-11 18:28:45 +05:30
|
|
|
pub fn fetch_routes(&self, campaign_id: &str) -> String {
|
2021-10-12 21:56:16 +05:30
|
|
|
self.fetch.replace("{campaign_id}", campaign_id)
|
2021-10-11 18:28:45 +05:30
|
|
|
}
|
2021-10-10 19:23:48 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn services(cfg: &mut web::ServiceConfig) {
|
|
|
|
cfg.service(submit);
|
2021-10-11 09:56:15 +05:30
|
|
|
cfg.service(register);
|
2021-10-11 18:28:45 +05:30
|
|
|
cfg.service(fetch);
|
2021-10-10 19:23:48 +05:30
|
|
|
}
|
|
|
|
|
2021-10-11 09:56:15 +05:30
|
|
|
pub mod runners {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
pub async fn register_runner(data: &AppData) -> ServiceResult<uuid::Uuid> {
|
|
|
|
let mut uuid;
|
|
|
|
let now = OffsetDateTime::now_utc();
|
|
|
|
|
|
|
|
loop {
|
|
|
|
uuid = get_uuid();
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-12 21:56:16 +05:30
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct Query {
|
|
|
|
pub redirect_to: Option<String>,
|
|
|
|
}
|
|
|
|
|
2021-10-11 19:17:28 +05:30
|
|
|
#[my_codegen::get(path = "crate::V1_API_ROUTES.benches.register")]
|
2021-10-12 21:56:16 +05:30
|
|
|
async fn register(
|
|
|
|
data: AppData,
|
|
|
|
id: Identity,
|
|
|
|
path: web::Query<Query>,
|
|
|
|
) -> ServiceResult<HttpResponse> {
|
2021-10-11 09:56:15 +05:30
|
|
|
let uuid = runners::register_runner(&data).await?;
|
|
|
|
id.remember(uuid.to_string());
|
2021-10-12 21:56:16 +05:30
|
|
|
let path = path.into_inner();
|
|
|
|
if let Some(redirect_to) = path.redirect_to {
|
|
|
|
Ok(HttpResponse::Found()
|
|
|
|
.insert_header((http::header::LOCATION, redirect_to))
|
|
|
|
.finish())
|
|
|
|
} else {
|
|
|
|
Ok(HttpResponse::Ok().into())
|
|
|
|
}
|
2021-10-11 09:56:15 +05:30
|
|
|
}
|
|
|
|
|
2021-10-11 19:17:28 +05:30
|
|
|
#[derive(Serialize, Deserialize, Clone)]
|
|
|
|
pub struct Bench {
|
|
|
|
pub duration: f32,
|
|
|
|
pub difficulty: i32,
|
2021-10-10 19:23:48 +05:30
|
|
|
}
|
|
|
|
|
2021-10-11 19:17:28 +05:30
|
|
|
#[derive(Serialize, Deserialize, Clone)]
|
|
|
|
pub struct Submission {
|
|
|
|
pub device_user_provided: String,
|
|
|
|
pub device_software_recognised: String,
|
|
|
|
pub threads: i32,
|
|
|
|
pub benches: Vec<Bench>,
|
2021-10-10 19:23:48 +05:30
|
|
|
}
|
|
|
|
|
2021-10-11 19:17:28 +05:30
|
|
|
#[derive(Serialize, Deserialize, Clone)]
|
|
|
|
pub struct SubmissionProof {
|
|
|
|
pub token: String,
|
|
|
|
pub proof: String,
|
2021-10-11 09:30:24 +05:30
|
|
|
}
|
|
|
|
|
2021-10-12 21:56:16 +05:30
|
|
|
fn get_check_login() -> crate::CheckLogin<routes::Benches> {
|
|
|
|
crate::CheckLogin::new(crate::V1_API_ROUTES.benches)
|
2021-10-11 10:09:51 +05:30
|
|
|
}
|
|
|
|
|
2021-10-10 19:23:48 +05:30
|
|
|
#[my_codegen::post(
|
|
|
|
path = "crate::V1_API_ROUTES.benches.submit",
|
2021-10-11 10:09:51 +05:30
|
|
|
wrap = "get_check_login()"
|
2021-10-10 19:23:48 +05:30
|
|
|
)]
|
|
|
|
async fn submit(
|
|
|
|
data: AppData,
|
|
|
|
id: Identity,
|
|
|
|
payload: web::Json<Submission>,
|
2021-10-11 13:41:36 +05:30
|
|
|
path: web::Path<String>,
|
2021-10-10 19:23:48 +05:30
|
|
|
) -> ServiceResult<impl Responder> {
|
|
|
|
let username = id.identity().unwrap();
|
2021-10-11 13:41:36 +05:30
|
|
|
let path = path.into_inner();
|
|
|
|
let campaign_id = Uuid::parse_str(&path).map_err(|_| ServiceError::NotAnId)?;
|
2021-10-10 19:23:48 +05:30
|
|
|
|
|
|
|
let user_id = Uuid::from_str(&username).unwrap();
|
|
|
|
let payload = payload.into_inner();
|
|
|
|
|
2021-10-11 09:30:24 +05:30
|
|
|
sqlx::query!(
|
|
|
|
"INSERT INTO survey_responses (
|
|
|
|
user_id,
|
2021-10-11 13:41:36 +05:30
|
|
|
campaign_id,
|
2021-10-11 09:30:24 +05:30
|
|
|
device_user_provided,
|
|
|
|
device_software_recognised,
|
|
|
|
threads
|
2021-10-11 13:41:36 +05:30
|
|
|
) VALUES ($1, $2, $3, $4, $5);",
|
2021-10-11 09:30:24 +05:30
|
|
|
&user_id,
|
2021-10-11 13:41:36 +05:30
|
|
|
&campaign_id,
|
2021-10-11 09:30:24 +05:30
|
|
|
&payload.device_user_provided,
|
|
|
|
&payload.device_software_recognised,
|
|
|
|
&payload.threads
|
|
|
|
)
|
|
|
|
.execute(&data.db)
|
|
|
|
.await?;
|
2021-10-10 19:23:48 +05:30
|
|
|
|
|
|
|
struct ID {
|
|
|
|
id: i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
let resp_id = sqlx::query_as!(
|
|
|
|
ID,
|
2021-10-11 09:30:24 +05:30
|
|
|
"SELECT ID
|
|
|
|
FROM survey_responses
|
|
|
|
WHERE
|
|
|
|
user_id = $1
|
|
|
|
AND
|
|
|
|
device_software_recognised = $2;",
|
2021-10-10 19:23:48 +05:30
|
|
|
&user_id,
|
|
|
|
&payload.device_software_recognised
|
|
|
|
)
|
|
|
|
.fetch_one(&data.db)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
let mut futs = Vec::with_capacity(payload.benches.len());
|
|
|
|
|
|
|
|
for bench in payload.benches.iter() {
|
|
|
|
let fut = sqlx::query!(
|
|
|
|
"INSERT INTO survey_benches
|
2021-10-11 09:30:24 +05:30
|
|
|
(resp_id, difficulty, duration)
|
|
|
|
VALUES ($1, $2, $3);",
|
2021-10-10 19:23:48 +05:30
|
|
|
&resp_id.id,
|
|
|
|
&bench.difficulty,
|
|
|
|
bench.duration
|
|
|
|
)
|
|
|
|
.execute(&data.db);
|
|
|
|
|
|
|
|
futs.push(fut);
|
|
|
|
}
|
|
|
|
|
2021-10-11 09:30:24 +05:30
|
|
|
let mut submitions_id;
|
|
|
|
try_join_all(futs).await?;
|
2021-10-10 19:23:48 +05:30
|
|
|
|
2021-10-11 09:30:24 +05:30
|
|
|
loop {
|
|
|
|
submitions_id = get_uuid();
|
2021-10-10 19:23:48 +05:30
|
|
|
|
2021-10-11 09:30:24 +05:30
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-10 19:23:48 +05:30
|
|
|
|
2021-10-11 09:30:24 +05:30
|
|
|
let resp = SubmissionProof {
|
|
|
|
token: username,
|
|
|
|
proof: submitions_id.to_string(),
|
|
|
|
};
|
2021-10-10 19:23:48 +05:30
|
|
|
|
2021-10-11 09:30:24 +05:30
|
|
|
Ok(HttpResponse::Ok().json(resp))
|
2021-10-10 19:23:48 +05:30
|
|
|
}
|
2021-10-11 18:28:45 +05:30
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct BenchConfig {
|
|
|
|
pub difficulties: Vec<i32>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[my_codegen::post(
|
|
|
|
path = "crate::V1_API_ROUTES.benches.fetch",
|
|
|
|
wrap = "get_check_login()"
|
|
|
|
)]
|
|
|
|
async fn fetch(data: AppData, path: web::Path<String>) -> ServiceResult<impl Responder> {
|
|
|
|
let path = path.into_inner();
|
|
|
|
let campaign_id = Uuid::parse_str(&path).map_err(|_| ServiceError::NotAnId)?;
|
|
|
|
|
|
|
|
let config = sqlx::query_as!(
|
|
|
|
BenchConfig,
|
|
|
|
"SELECT difficulties FROM survey_campaigns WHERE id = $1;",
|
|
|
|
&campaign_id,
|
|
|
|
)
|
|
|
|
.fetch_one(&data.db)
|
|
|
|
.await?;
|
|
|
|
Ok(HttpResponse::Ok().json(config))
|
|
|
|
}
|