mirror of
https://github.com/storytold/artcraft.git
synced 2026-11-19 08:05:20 +00:00
wip on moderation of templates
This commit is contained in:
1 parent
9495798583
commit
13e2e32ca5
10 files changed
+567
-195
No files matched your search
@@ -27,6 +27,8 @@ use sqlx::mysql::MySqlDatabaseError;
|
||||
use std::sync::Arc;
|
||||
use crate::database_helpers::boolean_converters::nullable_i8_to_optional_bool;
|
||||
|
||||
// TODO: This is duplicated in query_w2l_template
|
||||
|
||||
/// For the URL PathInfo
|
||||
#[derive(Deserialize)]
|
||||
pub struct GetW2lTemplatePathInfo {
|
||||
|
||||
@@ -5,4 +5,5 @@ pub mod get_w2l_inference_job_status;
|
||||
pub mod get_w2l_result;
|
||||
pub mod get_w2l_template;
|
||||
pub mod get_w2l_upload_template_job_status;
|
||||
pub mod list_w2l_templates;
|
||||
pub mod list_w2l_templates;
|
||||
pub mod set_w2l_template_mod_approval;
|
||||
@@ -0,0 +1,169 @@
|
||||
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_template::select_w2l_template_by_token;
|
||||
use crate::database_helpers::enums::{DownloadUrlType, CreatorSetVisibility, W2lTemplateType};
|
||||
use crate::http_server::web_utils::ip_address::get_request_ip;
|
||||
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::error::DatabaseError;
|
||||
use sqlx::error::Error::Database;
|
||||
use sqlx::mysql::MySqlDatabaseError;
|
||||
use std::sync::Arc;
|
||||
|
||||
/// For the URL PathInfo
|
||||
#[derive(Deserialize)]
|
||||
pub struct SetW2lTemplateModApprovalPathInfo {
|
||||
w2l_template_token: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct SetW2lTemplateModApprovalRequest {
|
||||
is_approved: bool,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct SetW2lTemplateModApprovalSuccessResponse {
|
||||
pub success: bool,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct ErrorResponse {
|
||||
pub success: bool,
|
||||
pub error_reason: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Display)]
|
||||
pub enum SetW2lTemplateModApprovalError {
|
||||
BadInput(String),
|
||||
NotAuthorized,
|
||||
NotFound,
|
||||
ServerError,
|
||||
}
|
||||
|
||||
impl ResponseError for SetW2lTemplateModApprovalError {
|
||||
fn status_code(&self) -> StatusCode {
|
||||
match *self {
|
||||
SetW2lTemplateModApprovalError::BadInput(_) => StatusCode::BAD_REQUEST,
|
||||
SetW2lTemplateModApprovalError::NotAuthorized => StatusCode::UNAUTHORIZED,
|
||||
SetW2lTemplateModApprovalError::NotFound => StatusCode::NOT_FOUND,
|
||||
SetW2lTemplateModApprovalError::ServerError => StatusCode::INTERNAL_SERVER_ERROR,
|
||||
}
|
||||
}
|
||||
|
||||
fn error_response(&self) -> HttpResponse {
|
||||
let error_reason = match self {
|
||||
SetW2lTemplateModApprovalError::BadInput(reason) => reason.to_string(),
|
||||
SetW2lTemplateModApprovalError::NotAuthorized => "unauthorized".to_string(),
|
||||
SetW2lTemplateModApprovalError::NotFound => "not found".to_string(),
|
||||
SetW2lTemplateModApprovalError::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)
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn set_w2l_template_mod_approval_handler(
|
||||
http_request: HttpRequest,
|
||||
path: Path<SetW2lTemplateModApprovalPathInfo>,
|
||||
request: web::Json<SetW2lTemplateModApprovalRequest>,
|
||||
server_state: web::Data<Arc<ServerState>>) -> Result<HttpResponse, SetW2lTemplateModApprovalError>
|
||||
{
|
||||
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);
|
||||
SetW2lTemplateModApprovalError::ServerError
|
||||
})?;
|
||||
|
||||
let user_session = match maybe_user_session {
|
||||
Some(session) => session,
|
||||
None => {
|
||||
warn!("not logged in");
|
||||
return Err(SetW2lTemplateModApprovalError::NotAuthorized);
|
||||
}
|
||||
};
|
||||
|
||||
if !user_session.can_approve_w2l_templates {
|
||||
warn!("user is not allowed to approve templates: {}", user_session.user_token);
|
||||
return Err(SetW2lTemplateModApprovalError::NotAuthorized);
|
||||
}
|
||||
|
||||
let template_query_result = select_w2l_template_by_token(
|
||||
&path.w2l_template_token,
|
||||
true, // Only mods can perform this action
|
||||
&server_state.mysql_pool,
|
||||
).await;
|
||||
|
||||
let w2l_template = match template_query_result {
|
||||
Err(e) => {
|
||||
warn!("query error: {:?}", e);
|
||||
return Err(SetW2lTemplateModApprovalError::ServerError);
|
||||
}
|
||||
Ok(None) => return Err(SetW2lTemplateModApprovalError::NotFound),
|
||||
Ok(Some(template)) => template,
|
||||
};
|
||||
|
||||
let ip_address = get_request_ip(&http_request);
|
||||
|
||||
let query_result = sqlx::query!(
|
||||
r#"
|
||||
UPDATE w2l_templates
|
||||
SET
|
||||
is_mod_approved = ?,
|
||||
maybe_mod_user_token = ?
|
||||
WHERE
|
||||
token = ?
|
||||
LIMIT 1
|
||||
"#,
|
||||
|
||||
&request.is_approved,
|
||||
&user_session.user_token,
|
||||
&path.w2l_template_token,
|
||||
)
|
||||
.execute(&server_state.mysql_pool)
|
||||
.await;
|
||||
|
||||
match query_result {
|
||||
Ok(_) => {},
|
||||
Err(err) => {
|
||||
warn!("Update w2l mod approval status DB error: {:?}", err);
|
||||
return Err(SetW2lTemplateModApprovalError::ServerError);
|
||||
}
|
||||
};
|
||||
let response = SetW2lTemplateModApprovalSuccessResponse {
|
||||
success: true,
|
||||
};
|
||||
|
||||
let body = serde_json::to_string(&response)
|
||||
.map_err(|e| SetW2lTemplateModApprovalError::ServerError)?;
|
||||
|
||||
Ok(HttpResponse::Ok()
|
||||
.content_type("application/json")
|
||||
.body(body))
|
||||
}
|
||||
Reference in new issue
Block a user