From bdc6e6d1e93d44be5c7aa272192babd49d2ddefb Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Thu, 10 Jun 2021 02:04:36 -0400 Subject: [PATCH] cleanup get_tts_result endpoint to use shared queries --- .../endpoints/tts/get_tts_result.rs | 205 +++--------------- .../endpoints/w2l/get_w2l_result.rs | 2 +- 2 files changed, 34 insertions(+), 173 deletions(-) diff --git a/src/http_server/endpoints/tts/get_tts_result.rs b/src/http_server/endpoints/tts/get_tts_result.rs index 7727778d83..b762ed20cb 100644 --- a/src/http_server/endpoints/tts/get_tts_result.rs +++ b/src/http_server/endpoints/tts/get_tts_result.rs @@ -8,9 +8,9 @@ use actix_web::web::Path; use actix_web::{Responder, web, HttpResponse, error, HttpRequest, HttpMessage}; use chrono::{DateTime, Utc}; use crate::AnyhowResult; +use crate::common_queries::query_tts_result::TtsResultRecordForResponse; +use crate::common_queries::query_tts_result::select_tts_result_by_token; use crate::common_queries::sessions::create_session_for_user; -use crate::http_server::endpoints::users::create_account::CreateAccountError::{BadInput, ServerError, UsernameTaken, EmailTaken}; -use crate::http_server::endpoints::users::login::LoginSuccessResponse; use crate::http_server::web_utils::ip_address::get_request_ip; use crate::http_server::web_utils::session_checker::SessionRecord; use crate::server_state::ServerState; @@ -25,6 +25,7 @@ use sqlx::error::DatabaseError; use sqlx::error::Error::Database; use sqlx::mysql::MySqlDatabaseError; use std::sync::Arc; +use crate::http_server::web_utils::response_error_helpers::to_simple_json_error; /// For the URL PathInfo #[derive(Deserialize)] @@ -32,111 +33,33 @@ pub struct GetTtsResultPathInfo { token: String, } -#[derive(Serialize)] -pub struct TtsResultRecordForResponse { - pub tts_result_token: String, - pub inference_text: String, - - pub tts_model_token: String, - pub tts_model_title: Option, // TODO: Shouldn't be Option. - - pub maybe_creator_user_token: Option, - pub maybe_creator_username: Option, - pub maybe_creator_display_name: Option, - pub maybe_creator_gravatar_hash: Option, - - pub maybe_model_creator_user_token: Option, - pub maybe_model_creator_username: Option, - pub maybe_model_creator_display_name: Option, - pub maybe_model_creator_gravatar_hash: Option, - - pub public_bucket_wav_audio_path: String, - pub public_bucket_spectrogram_path: String, - - pub file_size_bytes: u32, - pub duration_millis: u32, - - //pub is_mod_hidden_from_public: bool, // converted - //pub model_is_mod_approved: bool, // converted - //pub maybe_mod_user_token: Option, - - pub created_at: DateTime, - pub updated_at: DateTime, -} - #[derive(Serialize)] pub struct GetTtsResultSuccessResponse { pub success: bool, pub result: TtsResultRecordForResponse, } -#[derive(Serialize)] -pub struct ErrorResponse { - pub success: bool, - pub error_reason: String, -} - #[derive(Debug, Display)] pub enum GetTtsResultError { ServerError, -} - -pub struct RawTtsResultRecord { - pub tts_result_token: String, // from field `tts_results.token` - pub inference_text: String, - - pub tts_model_token: String, - pub tts_model_title: Option, - - pub maybe_creator_user_token: Option, - pub maybe_creator_username: Option, - pub maybe_creator_display_name: Option, - pub maybe_creator_gravatar_hash: Option, - - pub maybe_model_creator_user_token: Option, - pub maybe_model_creator_username: Option, - pub maybe_model_creator_display_name: Option, - pub maybe_model_creator_gravatar_hash: Option, - - pub public_bucket_wav_audio_path: String, - pub public_bucket_spectrogram_path: String, - - pub file_size_bytes: i32, - pub duration_millis: i32, - - //pub is_mod_hidden_from_public: i8, // needs convert - //pub model_is_mod_approved: i8, // needs convert - //pub maybe_mod_user_token: Option, - - pub created_at: DateTime, - pub updated_at: DateTime, + NotFound, } impl ResponseError for GetTtsResultError { fn status_code(&self) -> StatusCode { match *self { GetTtsResultError::ServerError=> StatusCode::INTERNAL_SERVER_ERROR, + GetTtsResultError::NotFound=> StatusCode::NOT_FOUND, } } fn error_response(&self) -> HttpResponse { let error_reason = match self { GetTtsResultError::ServerError => "server error".to_string(), + GetTtsResultError::NotFound => "not found".to_string(), }; - let response = ErrorResponse { - success: false, - error_reason, - }; - - let body = match serde_json::to_string(&response) { - Ok(json) => json, - Err(_) => "{}".to_string(), - }; - - HttpResponseBuilder::new(self.status_code()) - .set_header(header::CONTENT_TYPE, "application/json") - .body(body) + to_simple_json_error(&error_reason, self.status_code()) } } @@ -145,103 +68,41 @@ pub async fn get_tts_inference_result_handler( path: Path, server_state: web::Data>) -> Result { - // NB: Lookup failure is Err(RowNotFound). - // NB: Since this is publicly exposed, we don't query sensitive data. - let maybe_result = sqlx::query_as!( - RawTtsResultRecord, - r#" -SELECT - tts_results.token as tts_result_token, + let maybe_user_session = server_state + .session_checker + .maybe_get_user_session(&http_request, &server_state.mysql_pool) + .await + .map_err(|e| { + warn!("Session checker error: {:?}", e); + GetTtsResultError::ServerError + })?; - tts_results.inference_text, + let mut show_deleted_results = false; - tts_results.model_token as tts_model_token, - tts_models.title as tts_model_title, + if let Some(user_session) = maybe_user_session { + // NB: Moderators can see deleted results. + // Original creators cannot see them (unless they're moderators!) + show_deleted_results = user_session.can_delete_other_users_tts_results; + } - users.token as maybe_creator_user_token, - users.username as maybe_creator_username, - users.display_name as maybe_creator_display_name, - users.email_gravatar_hash as maybe_creator_gravatar_hash, + let inference_result_query_result = select_tts_result_by_token( + &path.token, + show_deleted_results, + &server_state.mysql_pool + ).await; - model_users.token as maybe_model_creator_user_token, - model_users.username as maybe_model_creator_username, - model_users.display_name as maybe_model_creator_display_name, - model_users.email_gravatar_hash as maybe_model_creator_gravatar_hash, - - tts_results.public_bucket_wav_audio_path, - tts_results.public_bucket_spectrogram_path, - - tts_results.file_size_bytes, - tts_results.duration_millis, - tts_results.created_at, - tts_results.updated_at - -FROM tts_results -LEFT OUTER JOIN tts_models - ON tts_results.model_token = tts_models.token -LEFT OUTER JOIN users - ON tts_results.maybe_creator_user_token = users.token -LEFT OUTER JOIN users as model_users - ON tts_models.creator_user_token = model_users.token -WHERE - tts_results.token = ? - AND tts_results.user_deleted_at IS NULL - AND tts_results.mod_deleted_at IS NULL - "#, - &path.token - ) - .fetch_one(&server_state.mysql_pool) - .await; // TODO: This will return error if it doesn't exist - - let ir : RawTtsResultRecord = match maybe_result { - Ok(result) => result, - Err(err) => { - match err { - RowNotFound => { - return Err(GetTtsResultError::ServerError); - }, - _ => { - warn!("tts inference result query error: {:?}", err); - return Err(GetTtsResultError::ServerError); - } - } + let inference_result = match inference_result_query_result { + Err(e) => { + warn!("query error: {:?}", e); + return Err(GetTtsResultError::ServerError); } - }; - - let result_for_response = TtsResultRecordForResponse { - tts_result_token: ir.tts_result_token.clone(), - - inference_text: ir.inference_text.clone(), - - tts_model_token: ir.tts_model_token.clone(), - tts_model_title: ir.tts_model_title.clone(), - - maybe_creator_user_token: ir.maybe_creator_user_token.clone(), - maybe_creator_username: ir.maybe_creator_username.clone(), - maybe_creator_display_name: ir.maybe_creator_display_name.clone(), - maybe_creator_gravatar_hash: ir.maybe_creator_gravatar_hash.clone(), - - maybe_model_creator_user_token: ir.maybe_model_creator_user_token.clone(), - maybe_model_creator_username: ir.maybe_model_creator_username.clone(), - maybe_model_creator_display_name: ir.maybe_model_creator_display_name.clone(), - maybe_model_creator_gravatar_hash: ir.maybe_model_creator_gravatar_hash.clone(), - - //is_mod_hidden_from_public: if ir.is_mod_hidden_from_public == 0 { false } else { true }, - //model_is_mod_approved: if ir.model_is_mod_approved == 0 { false } else { true }, - - public_bucket_wav_audio_path: ir.public_bucket_wav_audio_path.clone(), - public_bucket_spectrogram_path: ir.public_bucket_spectrogram_path.clone(), - - file_size_bytes: if ir.file_size_bytes > 0 { ir.file_size_bytes as u32 } else { 0 }, - duration_millis: if ir.duration_millis > 0 { ir.duration_millis as u32 } else { 0 }, - - created_at: ir.created_at.clone(), - updated_at: ir.updated_at.clone(), + Ok(None) => return Err(GetTtsResultError::NotFound), + Ok(Some(inference_result)) => inference_result, }; let response = GetTtsResultSuccessResponse { success: true, - result: result_for_response, + result: inference_result, }; let body = serde_json::to_string(&response) diff --git a/src/http_server/endpoints/w2l/get_w2l_result.rs b/src/http_server/endpoints/w2l/get_w2l_result.rs index 8010c59990..94a2b6d96f 100644 --- a/src/http_server/endpoints/w2l/get_w2l_result.rs +++ b/src/http_server/endpoints/w2l/get_w2l_result.rs @@ -88,7 +88,7 @@ pub async fn get_w2l_inference_result_handler( let mut show_deleted_results = false; if let Some(user_session) = maybe_user_session { - // NB: Moderators can see deleted templates. + // NB: Moderators can see deleted results. // Original creators cannot see them (unless they're moderators!) show_deleted_results = user_session.can_delete_other_users_w2l_results; }