mirror of
https://github.com/storytold/artcraft.git
synced 2026-11-16 06:42:14 +00:00
delete w2l results endpoint (not tested)
This commit is contained in:
1 parent
dfb552abfc
commit
727c57ddaa
8 files changed
+561
-208
No files matched your search
@@ -58,6 +58,7 @@ CREATE TABLE tts_results (
|
||||
-- ========== MODERATION DETAILS ==========
|
||||
|
||||
-- In this case, a moderator hides it from public.
|
||||
-- It won't appear in lists on profiles or otherwise.
|
||||
is_mod_hidden_from_public BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
|
||||
-- If a moderator has comments.
|
||||
@@ -134,6 +135,7 @@ CREATE TABLE w2l_results (
|
||||
-- ========== MODERATION DETAILS ==========
|
||||
|
||||
-- In this case, a moderator hides it from public.
|
||||
-- It won't appear in lists on profiles or otherwise.
|
||||
is_mod_hidden_from_public BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
|
||||
-- If a moderator has comments.
|
||||
|
||||
@@ -6,5 +6,6 @@ pub mod list_w2l_inference_results;
|
||||
pub mod list_w2l_templates;
|
||||
pub mod query_tts_model;
|
||||
pub mod query_user_profile;
|
||||
pub mod query_w2l_result;
|
||||
pub mod query_w2l_template;
|
||||
pub mod sessions;
|
||||
@@ -0,0 +1,259 @@
|
||||
use anyhow::anyhow;
|
||||
use chrono::{DateTime, Utc};
|
||||
use crate::AnyhowResult;
|
||||
use crate::database_helpers::boolean_converters::i8_to_bool;
|
||||
use crate::database_helpers::boolean_converters::nullable_i8_to_optional_bool;
|
||||
use derive_more::{Display, Error};
|
||||
use log::{info, warn, log};
|
||||
use regex::Regex;
|
||||
use sqlx::MySqlPool;
|
||||
use sqlx::error::DatabaseError;
|
||||
use sqlx::error::Error::Database;
|
||||
use sqlx::mysql::MySqlDatabaseError;
|
||||
use std::sync::Arc;
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct W2lResultRecordForResponse {
|
||||
pub w2l_result_token: String,
|
||||
pub maybe_w2l_template_token: Option<String>,
|
||||
pub maybe_tts_inference_result_token: Option<String>,
|
||||
|
||||
pub public_bucket_video_path: String,
|
||||
|
||||
pub template_type: Option<String>,
|
||||
pub template_title: Option<String>,
|
||||
|
||||
pub maybe_creator_user_token: Option<String>,
|
||||
pub maybe_creator_username: Option<String>,
|
||||
pub maybe_creator_display_name: Option<String>,
|
||||
pub maybe_creator_gravatar_hash: Option<String>,
|
||||
|
||||
pub maybe_template_creator_user_token: Option<String>,
|
||||
pub maybe_template_creator_username: Option<String>,
|
||||
pub maybe_template_creator_display_name: Option<String>,
|
||||
pub maybe_template_creator_gravatar_hash: Option<String>,
|
||||
|
||||
pub file_size_bytes: u32,
|
||||
pub frame_width: u32,
|
||||
pub frame_height: u32,
|
||||
pub duration_millis: u32,
|
||||
|
||||
//pub is_mod_hidden_from_public: bool, // converted
|
||||
//pub template_is_mod_approved: bool, // converted
|
||||
//pub maybe_mod_user_token: Option<String>,
|
||||
|
||||
pub created_at: DateTime<Utc>,
|
||||
pub updated_at: DateTime<Utc>,
|
||||
|
||||
pub user_deleted_at: Option<DateTime<Utc>>,
|
||||
pub mod_deleted_at: Option<DateTime<Utc>>,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct W2lResultRecordRaw {
|
||||
pub w2l_result_token: String, // from field `w2l_results.token`
|
||||
|
||||
pub maybe_w2l_template_token: Option<String>,
|
||||
pub maybe_tts_inference_result_token: Option<String>,
|
||||
|
||||
pub public_bucket_video_path: String,
|
||||
|
||||
pub template_type: Option<String>,
|
||||
pub template_title: Option<String>, // from field `w2l_templates.title`
|
||||
|
||||
pub maybe_creator_user_token: Option<String>,
|
||||
pub maybe_creator_username: Option<String>,
|
||||
pub maybe_creator_display_name: Option<String>,
|
||||
pub maybe_creator_gravatar_hash: Option<String>,
|
||||
|
||||
pub maybe_template_creator_user_token: Option<String>,
|
||||
pub maybe_template_creator_username: Option<String>,
|
||||
pub maybe_template_creator_display_name: Option<String>,
|
||||
pub maybe_template_creator_gravatar_hash: Option<String>,
|
||||
|
||||
pub file_size_bytes: i32,
|
||||
pub frame_width: i32,
|
||||
pub frame_height: i32,
|
||||
pub duration_millis: i32,
|
||||
|
||||
//pub is_mod_hidden_from_public: i8, // needs convert
|
||||
//pub template_is_mod_approved: i8, // needs convert
|
||||
//pub maybe_mod_user_token: Option<String>,
|
||||
|
||||
pub created_at: DateTime<Utc>,
|
||||
pub updated_at: DateTime<Utc>,
|
||||
|
||||
pub user_deleted_at: Option<DateTime<Utc>>,
|
||||
pub mod_deleted_at: Option<DateTime<Utc>>,
|
||||
}
|
||||
|
||||
pub async fn select_w2l_result_by_token(
|
||||
w2l_result_token: &str,
|
||||
can_see_deleted: bool,
|
||||
mysql_pool: &MySqlPool
|
||||
) -> AnyhowResult<Option<W2lResultRecordForResponse>> {
|
||||
|
||||
let maybe_record = if can_see_deleted {
|
||||
select_including_deleted(w2l_result_token, mysql_pool).await
|
||||
} else {
|
||||
select_without_deleted(w2l_result_token, mysql_pool).await
|
||||
};
|
||||
|
||||
let ir : W2lResultRecordRaw = match maybe_record {
|
||||
Ok(inference_result) => inference_result,
|
||||
Err(ref err) => {
|
||||
match err {
|
||||
RowNotFound => {
|
||||
warn!("w2l result not found: {:?}", &err);
|
||||
return Ok(None);
|
||||
},
|
||||
_ => {
|
||||
warn!("w2l result query error: {:?}", &err);
|
||||
return Err(anyhow!("database error"));
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
let ir_for_response = W2lResultRecordForResponse {
|
||||
w2l_result_token: ir.w2l_result_token.clone(),
|
||||
maybe_w2l_template_token: ir.maybe_w2l_template_token.clone(),
|
||||
maybe_tts_inference_result_token: ir.maybe_tts_inference_result_token.clone(),
|
||||
|
||||
public_bucket_video_path: ir.public_bucket_video_path.clone(),
|
||||
|
||||
template_type: ir.template_type.clone(),
|
||||
template_title: ir.template_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_template_creator_user_token: ir.maybe_template_creator_user_token.clone(),
|
||||
maybe_template_creator_username: ir.maybe_template_creator_username.clone(),
|
||||
maybe_template_creator_display_name: ir.maybe_template_creator_display_name.clone(),
|
||||
maybe_template_creator_gravatar_hash: ir.maybe_template_creator_gravatar_hash.clone(),
|
||||
|
||||
//is_mod_hidden_from_public: if ir.is_mod_hidden_from_public == 0 { false } else { true },
|
||||
//template_is_mod_approved: if ir.template_is_mod_approved == 0 { false } else { true },
|
||||
|
||||
file_size_bytes: if ir.file_size_bytes > 0 { ir.file_size_bytes as u32 } else { 0 },
|
||||
frame_width: if ir.frame_width > 0 { ir.frame_width as u32 } else { 0 },
|
||||
frame_height: if ir.frame_height > 0 { ir.frame_height 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(),
|
||||
user_deleted_at: ir.user_deleted_at.clone(),
|
||||
mod_deleted_at: ir.mod_deleted_at.clone(),
|
||||
};
|
||||
|
||||
Ok(Some(ir_for_response))
|
||||
}
|
||||
|
||||
async fn select_including_deleted(
|
||||
w2l_result_token: &str,
|
||||
mysql_pool: &MySqlPool
|
||||
) -> Result<W2lResultRecordRaw, sqlx::Error> {
|
||||
sqlx::query_as!(
|
||||
W2lResultRecordRaw,
|
||||
r#"
|
||||
SELECT
|
||||
w2l_results.token as w2l_result_token,
|
||||
w2l_results.maybe_tts_inference_result_token,
|
||||
|
||||
w2l_results.public_bucket_video_path,
|
||||
|
||||
w2l_templates.token as maybe_w2l_template_token,
|
||||
w2l_templates.template_type,
|
||||
w2l_templates.title as template_title,
|
||||
|
||||
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,
|
||||
|
||||
template_users.token as maybe_template_creator_user_token,
|
||||
template_users.username as maybe_template_creator_username,
|
||||
template_users.display_name as maybe_template_creator_display_name,
|
||||
template_users.email_gravatar_hash as maybe_template_creator_gravatar_hash,
|
||||
|
||||
w2l_results.file_size_bytes,
|
||||
w2l_results.frame_width,
|
||||
w2l_results.frame_height,
|
||||
w2l_results.duration_millis,
|
||||
w2l_results.created_at,
|
||||
w2l_results.updated_at,
|
||||
w2l_results.user_deleted_at,
|
||||
w2l_results.mod_deleted_at
|
||||
|
||||
FROM w2l_results
|
||||
LEFT OUTER JOIN w2l_templates
|
||||
ON w2l_results.maybe_w2l_template_token = w2l_templates.token
|
||||
LEFT OUTER JOIN users
|
||||
ON w2l_results.maybe_creator_user_token = users.token
|
||||
LEFT OUTER JOIN users as template_users
|
||||
ON w2l_templates.creator_user_token = template_users.token
|
||||
WHERE
|
||||
w2l_results.token = ?
|
||||
"#,
|
||||
w2l_result_token
|
||||
)
|
||||
.fetch_one(mysql_pool)
|
||||
.await // TODO: This will return error if it doesn't exist
|
||||
}
|
||||
|
||||
async fn select_without_deleted(
|
||||
w2l_result_token: &str,
|
||||
mysql_pool: &MySqlPool
|
||||
) -> Result<W2lResultRecordRaw, sqlx::Error> {
|
||||
sqlx::query_as!(
|
||||
W2lResultRecordRaw,
|
||||
r#"
|
||||
SELECT
|
||||
w2l_results.token as w2l_result_token,
|
||||
w2l_results.maybe_tts_inference_result_token,
|
||||
|
||||
w2l_results.public_bucket_video_path,
|
||||
|
||||
w2l_templates.token as maybe_w2l_template_token,
|
||||
w2l_templates.template_type,
|
||||
w2l_templates.title as template_title,
|
||||
|
||||
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,
|
||||
|
||||
template_users.token as maybe_template_creator_user_token,
|
||||
template_users.username as maybe_template_creator_username,
|
||||
template_users.display_name as maybe_template_creator_display_name,
|
||||
template_users.email_gravatar_hash as maybe_template_creator_gravatar_hash,
|
||||
|
||||
w2l_results.file_size_bytes,
|
||||
w2l_results.frame_width,
|
||||
w2l_results.frame_height,
|
||||
w2l_results.duration_millis,
|
||||
w2l_results.created_at,
|
||||
w2l_results.updated_at,
|
||||
w2l_results.user_deleted_at,
|
||||
w2l_results.mod_deleted_at
|
||||
|
||||
FROM w2l_results
|
||||
LEFT OUTER JOIN w2l_templates
|
||||
ON w2l_results.maybe_w2l_template_token = w2l_templates.token
|
||||
LEFT OUTER JOIN users
|
||||
ON w2l_results.maybe_creator_user_token = users.token
|
||||
LEFT OUTER JOIN users as template_users
|
||||
ON w2l_templates.creator_user_token = template_users.token
|
||||
WHERE
|
||||
w2l_results.token = ?
|
||||
AND w2l_results.user_deleted_at IS NULL
|
||||
AND w2l_results.mod_deleted_at IS NULL
|
||||
"#,
|
||||
w2l_result_token
|
||||
)
|
||||
.fetch_one(mysql_pool)
|
||||
.await // TODO: This will return error if it doesn't exist
|
||||
}
|
||||
@@ -0,0 +1,252 @@
|
||||
use actix_http::Error;
|
||||
use actix_http::http::header;
|
||||
use actix_web::cookie::Cookie;
|
||||
use actix_web::dev::HttpResponseBuilder;
|
||||
use actix_web::error::ResponseError;
|
||||
use actix_web::http::StatusCode;
|
||||
use actix_web::web::Path;
|
||||
use actix_web::{Responder, web, HttpResponse, error, HttpRequest};
|
||||
use crate::common_queries::query_w2l_result::select_w2l_result_by_token;
|
||||
use crate::http_server::web_utils::ip_address::get_request_ip;
|
||||
use crate::http_server::web_utils::response_error_helpers::to_simple_json_error;
|
||||
use crate::http_server::web_utils::response_success_helpers::simple_json_success;
|
||||
use crate::server_state::ServerState;
|
||||
use crate::util::random_crockford_token::random_crockford_token;
|
||||
use crate::validations::model_uploads::validate_model_title;
|
||||
use crate::validations::passwords::validate_passwords;
|
||||
use crate::validations::username::validate_username;
|
||||
use derive_more::{Display, Error};
|
||||
use log::{info, warn, log};
|
||||
use regex::Regex;
|
||||
use sqlx::MySqlPool;
|
||||
use sqlx::error::DatabaseError;
|
||||
use sqlx::error::Error::Database;
|
||||
use sqlx::mysql::MySqlDatabaseError;
|
||||
use std::sync::Arc;
|
||||
|
||||
/// For the URL PathInfo
|
||||
#[derive(Deserialize)]
|
||||
pub struct DeleteW2lInferenceResultPathInfo {
|
||||
token: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct DeleteW2lInferenceResultRequest {
|
||||
set_delete: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Display)]
|
||||
pub enum DeleteW2lInferenceResultError {
|
||||
BadInput(String),
|
||||
NotAuthorized,
|
||||
NotFound,
|
||||
ServerError,
|
||||
}
|
||||
|
||||
impl ResponseError for DeleteW2lInferenceResultError {
|
||||
fn status_code(&self) -> StatusCode {
|
||||
match *self {
|
||||
DeleteW2lInferenceResultError::BadInput(_) => StatusCode::BAD_REQUEST,
|
||||
DeleteW2lInferenceResultError::NotAuthorized => StatusCode::UNAUTHORIZED,
|
||||
DeleteW2lInferenceResultError::NotFound => StatusCode::NOT_FOUND,
|
||||
DeleteW2lInferenceResultError::ServerError => StatusCode::INTERNAL_SERVER_ERROR,
|
||||
}
|
||||
}
|
||||
|
||||
fn error_response(&self) -> HttpResponse {
|
||||
let error_reason = match self {
|
||||
DeleteW2lInferenceResultError::BadInput(reason) => reason.to_string(),
|
||||
DeleteW2lInferenceResultError::NotAuthorized => "unauthorized".to_string(),
|
||||
DeleteW2lInferenceResultError::NotFound => "not found".to_string(),
|
||||
DeleteW2lInferenceResultError::ServerError => "server error".to_string(),
|
||||
};
|
||||
|
||||
to_simple_json_error(&error_reason, self.status_code())
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn delete_w2l_inference_result_handler(
|
||||
http_request: HttpRequest,
|
||||
path: Path<DeleteW2lInferenceResultPathInfo>,
|
||||
request: web::Json<DeleteW2lInferenceResultRequest>,
|
||||
server_state: web::Data<Arc<ServerState>>
|
||||
) -> Result<HttpResponse, DeleteW2lInferenceResultError> {
|
||||
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);
|
||||
DeleteW2lInferenceResultError::ServerError
|
||||
})?;
|
||||
|
||||
let user_session = match maybe_user_session {
|
||||
Some(session) => session,
|
||||
None => {
|
||||
warn!("not logged in");
|
||||
return Err(DeleteW2lInferenceResultError::NotAuthorized);
|
||||
}
|
||||
};
|
||||
|
||||
// NB: First permission check.
|
||||
// Only mods should see deleted models (both user_* and mod_* deleted).
|
||||
let is_mod_that_can_see_deleted = user_session.can_delete_other_users_w2l_results;
|
||||
|
||||
let inference_result_query_result = select_w2l_result_by_token(
|
||||
&path.token,
|
||||
is_mod_that_can_see_deleted,
|
||||
&server_state.mysql_pool,
|
||||
).await;
|
||||
|
||||
let w2l_inference_result = match inference_result_query_result {
|
||||
Err(e) => {
|
||||
warn!("query error: {:?}", e);
|
||||
return Err(DeleteW2lInferenceResultError::ServerError);
|
||||
}
|
||||
Ok(None) => return Err(DeleteW2lInferenceResultError::NotFound),
|
||||
Ok(Some(inference_result)) => inference_result,
|
||||
};
|
||||
|
||||
// NB: Second set of permission checks
|
||||
let is_author = w2l_inference_result.maybe_creator_user_token
|
||||
.as_deref()
|
||||
.map(|creator_token| creator_token == &user_session.user_token)
|
||||
.unwrap_or(false);
|
||||
|
||||
let is_mod = user_session.can_delete_other_users_w2l_results;
|
||||
|
||||
if !is_author && !is_mod {
|
||||
warn!("user is not allowed to delete inference results: {}", user_session.user_token);
|
||||
return Err(DeleteW2lInferenceResultError::NotAuthorized);
|
||||
}
|
||||
|
||||
// NB: I can't imagine we need to store this.
|
||||
// let ip_address = get_request_ip(&http_request);
|
||||
|
||||
let query_result = if request.set_delete {
|
||||
if is_author {
|
||||
user_delete_inference_result(
|
||||
&path.token,
|
||||
&server_state.mysql_pool
|
||||
).await
|
||||
} else {
|
||||
mod_delete_inference_result(
|
||||
&path.token,
|
||||
&user_session.user_token,
|
||||
&server_state.mysql_pool
|
||||
).await
|
||||
}
|
||||
} else {
|
||||
if is_author {
|
||||
// NB: Technically only mods can see their own inference_results here
|
||||
user_undelete_inference_result(
|
||||
&path.token,
|
||||
&server_state.mysql_pool
|
||||
).await
|
||||
} else {
|
||||
mod_undelete_inference_result(
|
||||
&path.token,
|
||||
&user_session.user_token,
|
||||
&server_state.mysql_pool
|
||||
).await
|
||||
}
|
||||
};
|
||||
|
||||
match query_result {
|
||||
Ok(_) => {},
|
||||
Err(err) => {
|
||||
warn!("Update w2l mod approval status DB error: {:?}", err);
|
||||
return Err(DeleteW2lInferenceResultError::ServerError);
|
||||
}
|
||||
};
|
||||
|
||||
Ok(simple_json_success())
|
||||
}
|
||||
|
||||
async fn user_delete_inference_result(
|
||||
inference_result_token: &str,
|
||||
mysql_pool: &MySqlPool
|
||||
) -> Result<(), sqlx::Error> {
|
||||
let _r = sqlx::query!(
|
||||
r#"
|
||||
UPDATE w2l_results
|
||||
SET
|
||||
user_deleted_at = CURRENT_TIMESTAMP
|
||||
WHERE
|
||||
token = ?
|
||||
LIMIT 1
|
||||
"#,
|
||||
inference_result_token,
|
||||
)
|
||||
.execute(mysql_pool)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn mod_delete_inference_result(
|
||||
inference_result_token: &str,
|
||||
mod_user_token: &str,
|
||||
mysql_pool: &MySqlPool
|
||||
) -> Result<(), sqlx::Error> {
|
||||
let _r = sqlx::query!(
|
||||
r#"
|
||||
UPDATE w2l_results
|
||||
SET
|
||||
mod_deleted_at = CURRENT_TIMESTAMP,
|
||||
maybe_mod_user_token = ?
|
||||
WHERE
|
||||
token = ?
|
||||
LIMIT 1
|
||||
"#,
|
||||
mod_user_token,
|
||||
inference_result_token,
|
||||
)
|
||||
.execute(mysql_pool)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn user_undelete_inference_result(
|
||||
inference_result_token: &str,
|
||||
mysql_pool: &MySqlPool
|
||||
) -> Result<(), sqlx::Error> {
|
||||
|
||||
let _r = sqlx::query!(
|
||||
r#"
|
||||
UPDATE w2l_results
|
||||
SET
|
||||
user_deleted_at = NULL
|
||||
WHERE
|
||||
token = ?
|
||||
LIMIT 1
|
||||
"#,
|
||||
inference_result_token,
|
||||
)
|
||||
.execute(mysql_pool)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn mod_undelete_inference_result(
|
||||
inference_result_token: &str,
|
||||
mod_user_token: &str,
|
||||
mysql_pool: &MySqlPool
|
||||
) -> Result<(), sqlx::Error> {
|
||||
|
||||
let _r = sqlx::query!(
|
||||
r#"
|
||||
UPDATE w2l_results
|
||||
SET
|
||||
mod_deleted_at = NULL,
|
||||
maybe_mod_user_token = ?
|
||||
WHERE
|
||||
token = ?
|
||||
LIMIT 1
|
||||
"#,
|
||||
mod_user_token,
|
||||
inference_result_token,
|
||||
)
|
||||
.execute(mysql_pool)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
@@ -22,6 +22,8 @@ use sqlx::error::Error::Database;
|
||||
use sqlx::mysql::MySqlDatabaseError;
|
||||
use std::sync::Arc;
|
||||
use sqlx::MySqlPool;
|
||||
use crate::http_server::web_utils::response_success_helpers::simple_json_success;
|
||||
use crate::http_server::web_utils::response_error_helpers::to_simple_json_error;
|
||||
|
||||
/// For the URL PathInfo
|
||||
#[derive(Deserialize)]
|
||||
@@ -34,17 +36,6 @@ pub struct DeleteW2lTemplateRequest {
|
||||
set_delete: bool,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct DeleteW2lTemplateSuccessResponse {
|
||||
pub success: bool,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct ErrorResponse {
|
||||
pub success: bool,
|
||||
pub error_reason: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Display)]
|
||||
pub enum DeleteW2lTemplateError {
|
||||
BadInput(String),
|
||||
@@ -71,19 +62,7 @@ impl ResponseError for DeleteW2lTemplateError {
|
||||
DeleteW2lTemplateError::ServerError => "server error".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())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -185,16 +164,7 @@ pub async fn delete_w2l_template_handler(
|
||||
}
|
||||
};
|
||||
|
||||
let response = DeleteW2lTemplateSuccessResponse {
|
||||
success: true,
|
||||
};
|
||||
|
||||
let body = serde_json::to_string(&response)
|
||||
.map_err(|e| DeleteW2lTemplateError::ServerError)?;
|
||||
|
||||
Ok(HttpResponse::Ok()
|
||||
.content_type("application/json")
|
||||
.body(body))
|
||||
Ok(simple_json_success())
|
||||
}
|
||||
|
||||
async fn user_delete_template(
|
||||
|
||||
@@ -8,6 +8,8 @@ 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_w2l_result::W2lResultRecordForResponse;
|
||||
use crate::common_queries::query_w2l_result::select_w2l_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;
|
||||
@@ -25,6 +27,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,40 +35,6 @@ pub struct GetW2lResultPathInfo {
|
||||
token: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct W2lResultRecordForResponse {
|
||||
pub w2l_result_token: String,
|
||||
pub maybe_w2l_template_token: Option<String>,
|
||||
pub maybe_tts_inference_result_token: Option<String>,
|
||||
|
||||
pub public_bucket_video_path: String,
|
||||
|
||||
pub template_type: Option<String>,
|
||||
pub template_title: Option<String>,
|
||||
|
||||
pub maybe_creator_user_token: Option<String>,
|
||||
pub maybe_creator_username: Option<String>,
|
||||
pub maybe_creator_display_name: Option<String>,
|
||||
pub maybe_creator_gravatar_hash: Option<String>,
|
||||
|
||||
pub maybe_template_creator_user_token: Option<String>,
|
||||
pub maybe_template_creator_username: Option<String>,
|
||||
pub maybe_template_creator_display_name: Option<String>,
|
||||
pub maybe_template_creator_gravatar_hash: Option<String>,
|
||||
|
||||
pub file_size_bytes: u32,
|
||||
pub frame_width: u32,
|
||||
pub frame_height: u32,
|
||||
pub duration_millis: u32,
|
||||
|
||||
//pub is_mod_hidden_from_public: bool, // converted
|
||||
//pub template_is_mod_approved: bool, // converted
|
||||
//pub maybe_mod_user_token: Option<String>,
|
||||
|
||||
pub created_at: DateTime<Utc>,
|
||||
pub updated_at: DateTime<Utc>,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct GetW2lResultSuccessResponse {
|
||||
pub success: bool,
|
||||
@@ -81,174 +50,67 @@ pub struct ErrorResponse {
|
||||
#[derive(Debug, Display)]
|
||||
pub enum GetW2lResultError {
|
||||
ServerError,
|
||||
}
|
||||
|
||||
pub struct RawW2lResultRecord {
|
||||
pub w2l_result_token: String, // from field `w2l_results.token`
|
||||
|
||||
pub maybe_w2l_template_token: Option<String>,
|
||||
pub maybe_tts_inference_result_token: Option<String>,
|
||||
|
||||
pub public_bucket_video_path: String,
|
||||
|
||||
pub template_type: Option<String>,
|
||||
pub template_title: Option<String>, // from field `w2l_templates.title`
|
||||
|
||||
pub maybe_creator_user_token: Option<String>,
|
||||
pub maybe_creator_username: Option<String>,
|
||||
pub maybe_creator_display_name: Option<String>,
|
||||
pub maybe_creator_gravatar_hash: Option<String>,
|
||||
|
||||
pub maybe_template_creator_user_token: Option<String>,
|
||||
pub maybe_template_creator_username: Option<String>,
|
||||
pub maybe_template_creator_display_name: Option<String>,
|
||||
pub maybe_template_creator_gravatar_hash: Option<String>,
|
||||
|
||||
pub file_size_bytes: i32,
|
||||
pub frame_width: i32,
|
||||
pub frame_height: i32,
|
||||
pub duration_millis: i32,
|
||||
|
||||
//pub is_mod_hidden_from_public: i8, // needs convert
|
||||
//pub template_is_mod_approved: i8, // needs convert
|
||||
//pub maybe_mod_user_token: Option<String>,
|
||||
|
||||
pub created_at: DateTime<Utc>,
|
||||
pub updated_at: DateTime<Utc>,
|
||||
NotFound,
|
||||
}
|
||||
|
||||
impl ResponseError for GetW2lResultError {
|
||||
fn status_code(&self) -> StatusCode {
|
||||
match *self {
|
||||
GetW2lResultError::ServerError=> StatusCode::INTERNAL_SERVER_ERROR,
|
||||
GetW2lResultError::ServerError => StatusCode::INTERNAL_SERVER_ERROR,
|
||||
GetW2lResultError::NotFound => StatusCode::NOT_FOUND,
|
||||
}
|
||||
}
|
||||
|
||||
fn error_response(&self) -> HttpResponse {
|
||||
let error_reason = match self {
|
||||
GetW2lResultError::ServerError => "server error".to_string(),
|
||||
GetW2lResultError::NotFound => "not founnd".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())
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn get_w2l_inference_result_handler(
|
||||
http_request: HttpRequest,
|
||||
path: Path<GetW2lResultPathInfo>,
|
||||
server_state: web::Data<Arc<ServerState>>) -> Result<HttpResponse, GetW2lResultError>
|
||||
{
|
||||
// NB: Lookup failure is Err(RowNotFound).
|
||||
// NB: Since this is publicly exposed, we don't query sensitive data.
|
||||
let maybe_result = sqlx::query_as!(
|
||||
RawW2lResultRecord,
|
||||
r#"
|
||||
SELECT
|
||||
w2l_results.token as w2l_result_token,
|
||||
w2l_results.maybe_tts_inference_result_token,
|
||||
server_state: web::Data<Arc<ServerState>>
|
||||
) -> Result<HttpResponse, GetW2lResultError> {
|
||||
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);
|
||||
GetW2lResultError::ServerError
|
||||
})?;
|
||||
|
||||
w2l_results.public_bucket_video_path,
|
||||
let mut show_deleted_results = false;
|
||||
|
||||
w2l_templates.token as maybe_w2l_template_token,
|
||||
w2l_templates.template_type,
|
||||
w2l_templates.title as template_title,
|
||||
if let Some(user_session) = maybe_user_session {
|
||||
// NB: Moderators can see deleted templates.
|
||||
// Original creators cannot see them (unless they're moderators!)
|
||||
show_deleted_results = user_session.can_delete_other_users_w2l_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_w2l_result_by_token(
|
||||
&path.token,
|
||||
show_deleted_results,
|
||||
&server_state.mysql_pool
|
||||
).await;
|
||||
|
||||
template_users.token as maybe_template_creator_user_token,
|
||||
template_users.username as maybe_template_creator_username,
|
||||
template_users.display_name as maybe_template_creator_display_name,
|
||||
template_users.email_gravatar_hash as maybe_template_creator_gravatar_hash,
|
||||
|
||||
w2l_results.file_size_bytes,
|
||||
w2l_results.frame_width,
|
||||
w2l_results.frame_height,
|
||||
w2l_results.duration_millis,
|
||||
w2l_results.created_at,
|
||||
w2l_results.updated_at
|
||||
|
||||
FROM w2l_results
|
||||
LEFT OUTER JOIN w2l_templates
|
||||
ON w2l_results.maybe_w2l_template_token = w2l_templates.token
|
||||
LEFT OUTER JOIN users
|
||||
ON w2l_results.maybe_creator_user_token = users.token
|
||||
LEFT OUTER JOIN users as template_users
|
||||
ON w2l_templates.creator_user_token = template_users.token
|
||||
WHERE
|
||||
w2l_results.token = ?
|
||||
AND w2l_results.user_deleted_at IS NULL
|
||||
AND w2l_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 : RawW2lResultRecord = match maybe_result {
|
||||
Ok(result) => result,
|
||||
Err(err) => {
|
||||
match err {
|
||||
RowNotFound => {
|
||||
return Err(GetW2lResultError::ServerError);
|
||||
},
|
||||
_ => {
|
||||
warn!("w2l inference result query error: {:?}", err);
|
||||
return Err(GetW2lResultError::ServerError);
|
||||
}
|
||||
}
|
||||
let inference_result = match inference_result_query_result {
|
||||
Err(e) => {
|
||||
warn!("query error: {:?}", e);
|
||||
return Err(GetW2lResultError::ServerError);
|
||||
}
|
||||
};
|
||||
|
||||
let result_for_response = W2lResultRecordForResponse {
|
||||
w2l_result_token: ir.w2l_result_token.clone(),
|
||||
maybe_w2l_template_token: ir.maybe_w2l_template_token.clone(),
|
||||
maybe_tts_inference_result_token: ir.maybe_tts_inference_result_token.clone(),
|
||||
|
||||
public_bucket_video_path: ir.public_bucket_video_path.clone(),
|
||||
|
||||
template_type: ir.template_type.clone(),
|
||||
template_title: ir.template_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_template_creator_user_token: ir.maybe_template_creator_user_token.clone(),
|
||||
maybe_template_creator_username: ir.maybe_template_creator_username.clone(),
|
||||
maybe_template_creator_display_name: ir.maybe_template_creator_display_name.clone(),
|
||||
maybe_template_creator_gravatar_hash: ir.maybe_template_creator_gravatar_hash.clone(),
|
||||
|
||||
//is_mod_hidden_from_public: if ir.is_mod_hidden_from_public == 0 { false } else { true },
|
||||
//template_is_mod_approved: if ir.template_is_mod_approved == 0 { false } else { true },
|
||||
|
||||
file_size_bytes: if ir.file_size_bytes > 0 { ir.file_size_bytes as u32 } else { 0 },
|
||||
frame_width: if ir.frame_width > 0 { ir.frame_width as u32 } else { 0 },
|
||||
frame_height: if ir.frame_height > 0 { ir.frame_height 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(GetW2lResultError::NotFound),
|
||||
Ok(Some(inference_result)) => inference_result,
|
||||
};
|
||||
|
||||
let response = GetW2lResultSuccessResponse {
|
||||
success: true,
|
||||
result: result_for_response,
|
||||
result: inference_result,
|
||||
};
|
||||
|
||||
let body = serde_json::to_string(&response)
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
pub mod delete_w2l_result;
|
||||
pub mod delete_w2l_template;
|
||||
pub mod edit_w2l_template;
|
||||
pub mod enqueue_infer_w2l;
|
||||
|
||||
@@ -72,6 +72,7 @@ use crate::http_server::endpoints::w2l::set_w2l_template_mod_approval::set_w2l_t
|
||||
use crate::http_server::endpoints::w2l::delete_w2l_template::delete_w2l_template_handler;
|
||||
use crate::http_server::endpoints::w2l::edit_w2l_template::edit_w2l_template_handler;
|
||||
use crate::http_server::endpoints::tts::edit_tts_model::edit_tts_model_handler;
|
||||
use crate::http_server::endpoints::w2l::delete_w2l_result::delete_w2l_inference_result_handler;
|
||||
|
||||
// TODO TODO TODO TODO
|
||||
// TODO TODO TODO TODO
|
||||
@@ -358,6 +359,11 @@ pub async fn serve(server_state: ServerState) -> AnyhowResult<()>
|
||||
.route(web::get().to(get_w2l_inference_result_handler))
|
||||
.route(web::head().to(|| HttpResponse::Ok()))
|
||||
)
|
||||
.service(
|
||||
web::resource("/result/{token}/delete")
|
||||
.route(web::get().to(delete_w2l_inference_result_handler))
|
||||
.route(web::head().to(|| HttpResponse::Ok()))
|
||||
)
|
||||
.service(
|
||||
web::resource("/job/{token}")
|
||||
.route(web::get().to(get_w2l_inference_job_status_handler))
|
||||
|
||||
Reference in new issue
Block a user