mirror of
https://github.com/storytold/artcraft.git
synced 2026-11-05 17:58:55 +00:00
needs some tests for users and query builds with pagination
This commit is contained in:
1 parent
347fe685cd
commit
fba7d2a1eb
2 files changed
+164
-45
No files matched your search
+28
-45
@@ -8,7 +8,12 @@ use log::{ info, warn };
|
||||
|
||||
use enums::common::visibility::Visibility;
|
||||
use mysql_queries::queries::model_weights::list_weights_query_builder::ListWeightsQueryBuilder;
|
||||
use mysql_queries::queries::model_weights::update_weight::{ update_weights, UpdateWeightArgs };
|
||||
|
||||
use enums::by_table::model_weights::{
|
||||
weights_types::WeightsType,
|
||||
weights_category::WeightsCategory,
|
||||
};
|
||||
use tokens::tokens::users::UserToken;
|
||||
use tokens::tokens::model_weights::ModelWeightToken;
|
||||
|
||||
use crate::http_server::common_responses::user_details_lite::UserDetailsLight;
|
||||
@@ -33,7 +38,6 @@ pub struct ListAvailibleWeightsSuccessResponse {
|
||||
#[derive(Serialize)]
|
||||
pub struct ModelWeightForList {
|
||||
pub weight_token: ModelWeightToken,
|
||||
pub title: String,
|
||||
|
||||
|
||||
pub weights_type: WeightsType,
|
||||
@@ -47,11 +51,8 @@ pub struct ModelWeightForList {
|
||||
pub description_rendered_html: String,
|
||||
|
||||
pub creator_user_token: UserToken,
|
||||
pub creator_ip_address: String,
|
||||
pub creator_set_visibility: Visibility,
|
||||
|
||||
pub maybe_last_update_user_token: Option<UserToken>,
|
||||
|
||||
pub file_size_bytes: i32,
|
||||
pub file_checksum_sha2: String,
|
||||
|
||||
@@ -59,7 +60,6 @@ pub struct ModelWeightForList {
|
||||
pub cached_user_ratings_positive_count: u32,
|
||||
pub cached_user_ratings_negative_count: u32,
|
||||
pub maybe_cached_user_ratings_ratio: Option<f32>,
|
||||
pub cached_user_ratings_last_updated_at: DateTime<Utc>,
|
||||
|
||||
pub version: i32,
|
||||
|
||||
@@ -184,53 +184,36 @@ pub async fn list_availible_weights_handler(
|
||||
success: true,
|
||||
weights: weights_page.weights.into_iter()
|
||||
.map(|weights| ModelWeightForList {
|
||||
weight_token: weights.token,
|
||||
title: weights.title,
|
||||
|
||||
weights_type: WeightsType,
|
||||
weights_category: WeightsCategory,
|
||||
weight_token: weights.token,
|
||||
title: weights.title,
|
||||
weights_type: weights.weights_type,
|
||||
weights_category: weights.weights_category,
|
||||
|
||||
title: String,
|
||||
maybe_thumbnail_token:weights.maybe_thumbnail_token,
|
||||
description_markdown: weights.description_markdown,
|
||||
description_rendered_html: weights.description_rendered_html,
|
||||
|
||||
maybe_thumbnail_token: Option<String>,
|
||||
creator_user_token: weights.creator_user_token,
|
||||
creator_set_visibility: weights.creator_set_visibility,
|
||||
|
||||
description_markdown: String,
|
||||
description_rendered_html: String,
|
||||
file_size_bytes:weights.file_size_bytes,
|
||||
file_checksum_sha2: weights.file_checksum_sha2,
|
||||
|
||||
creator_user_token: UserToken,
|
||||
creator_ip_address: String,
|
||||
creator_set_visibility: Visibility,
|
||||
|
||||
|
||||
maybe_last_update_user_token: Option<UserToken>,
|
||||
cached_user_ratings_total_count: weights.cached_user_ratings_total_count,
|
||||
cached_user_ratings_positive_count: weights.cached_user_ratings_positive_count,
|
||||
cached_user_ratings_negative_count: weights.cached_user_ratings_negative_count,
|
||||
maybe_cached_user_ratings_ratio: weights.maybe_cached_user_ratings_ratio,
|
||||
|
||||
original_download_url: Option<String>,
|
||||
original_filename: Option<String>,
|
||||
version: weights.version,
|
||||
|
||||
file_size_bytes: i32,
|
||||
file_checksum_sha2: String,
|
||||
created_at: weights.created_at,
|
||||
updated_at: weights.updated_at,
|
||||
|
||||
private_bucket_hash: String,
|
||||
|
||||
maybe_private_bucket_prefix: Option<String>,
|
||||
maybe_private_bucket_extension: Option<String>,
|
||||
|
||||
cached_user_ratings_total_count: u32,
|
||||
cached_user_ratings_positive_count: u32,
|
||||
cached_user_ratings_negative_count: u32,
|
||||
maybe_cached_user_ratings_ratio: Option<f32>,
|
||||
cached_user_ratings_last_updated_at: DateTime<Utc>,
|
||||
|
||||
version: i32,
|
||||
|
||||
created_at: DateTime<Utc>,
|
||||
updated_at: DateTime<Utc>,
|
||||
|
||||
user_deleted_at: Option<DateTime<Utc>>,
|
||||
mod_deleted_at: Option<DateTime<Utc>>,
|
||||
|
||||
creator_username: String,
|
||||
creator_display_name: String,
|
||||
creator_email_gravatar_hash: String,
|
||||
creator_username: weights.creator_username,
|
||||
creator_display_name: weights.creator_display_name,
|
||||
creator_email_gravatar_hash: weights.creator_email_gravatar_hash
|
||||
}).collect::<Vec<_>>(),
|
||||
cursor_next,
|
||||
cursor_previous,
|
||||
|
||||
+136
@@ -0,0 +1,136 @@
|
||||
use std::fmt;
|
||||
use std::sync::Arc;
|
||||
|
||||
use actix_web::{HttpRequest, HttpResponse, web};
|
||||
use actix_web::error::ResponseError;
|
||||
use actix_web::http::StatusCode;
|
||||
use actix_web::web::Path;
|
||||
use chrono::{DateTime, Utc};
|
||||
use log::warn;
|
||||
|
||||
use enums::common::visibility::Visibility;
|
||||
use mysql_queries::queries::model_weights::list_weights_by_user::list_weights_by_creator_username;
|
||||
use tokens::tokens::model_weights::ModelWeightToken;
|
||||
|
||||
use crate::http_server::common_responses::user_details_lite::UserDetailsLight;
|
||||
use crate::server_state::ServerState;
|
||||
|
||||
#[derive(Serialize, Clone)]
|
||||
pub struct Weight {
|
||||
weight_token: ModelWeightToken,
|
||||
title: String,
|
||||
|
||||
creator: UserDetailsLight,
|
||||
|
||||
creator_set_visibility: Visibility,
|
||||
|
||||
created_at: DateTime<Utc>,
|
||||
updated_at: DateTime<Utc>,
|
||||
}
|
||||
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct ListWeightsByUserSuccessResponse {
|
||||
pub success: bool,
|
||||
pub weights: Vec<Weight>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct ListWeightsByUserPathInfo {
|
||||
username: String,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum ListWeightsByUserError {
|
||||
NotAuthorized,
|
||||
ServerError,
|
||||
}
|
||||
|
||||
impl fmt::Display for ListWeightsByUserError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{:?}", self)
|
||||
}
|
||||
}
|
||||
|
||||
impl ResponseError for ListWeightsByUserError {
|
||||
fn status_code(&self) -> StatusCode {
|
||||
match *self {
|
||||
ListWeightsByUserError::NotAuthorized => StatusCode::UNAUTHORIZED,
|
||||
ListWeightsByUserError::ServerError => StatusCode::INTERNAL_SERVER_ERROR,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn list_weights_by_user_handler(
|
||||
http_request: HttpRequest,
|
||||
path: Path<ListWeightsByUserPathInfo>,
|
||||
server_state: web::Data<Arc<ServerState>>
|
||||
) -> Result<HttpResponse, ListWeightsByUserError> {
|
||||
|
||||
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);
|
||||
ListWeightsByUserError::ServerError
|
||||
})?;
|
||||
|
||||
let user_session = match maybe_user_session {
|
||||
Some(session) => session,
|
||||
None => {
|
||||
warn!("not logged in");
|
||||
return Err(ListWeightsByUserError::NotAuthorized);
|
||||
}
|
||||
};
|
||||
|
||||
let username = path.username.as_ref();
|
||||
let creator_user_token = user_session.user_token.clone();
|
||||
let is_mod = user_session.can_ban_users;
|
||||
|
||||
let query_results = list_weights_by_creator_username(
|
||||
&server_state.mysql_pool,
|
||||
&username,
|
||||
is_mod,
|
||||
).await.map_err(|e| {
|
||||
warn!("Error querying for weights: {:?}", e);
|
||||
ListWeightsByUserError::ServerError
|
||||
});
|
||||
let weights = match query_results {
|
||||
Ok(weights) => weights,
|
||||
Err(e) => {
|
||||
warn!("Error querying for weights: {:?}", e);
|
||||
|
||||
|
||||
return Err(ListWeightsByUserError::ServerError);
|
||||
}
|
||||
};
|
||||
|
||||
let weights = weights.into_iter().map(|weight| {
|
||||
Weight {
|
||||
weight_token: weight.token,
|
||||
title: weight.title,
|
||||
creator: UserDetailsLight::from_db_fields(
|
||||
&weight.creator_user_token,
|
||||
&weight.creator_username,
|
||||
&weight.creator_display_name,
|
||||
&weight.creator_email_gravatar_hash,
|
||||
),
|
||||
creator_set_visibility: weight.creator_set_visibility,
|
||||
created_at: weight.created_at,
|
||||
updated_at: weight.updated_at,
|
||||
}
|
||||
}).collect();
|
||||
|
||||
let response = ListWeightsByUserSuccessResponse {
|
||||
success: true,
|
||||
weights,
|
||||
};
|
||||
|
||||
let body = serde_json::to_string(&response)
|
||||
.map_err(|e| ListWeightsByUserError::ServerError)?;
|
||||
|
||||
Ok(HttpResponse::Ok()
|
||||
.content_type("application/json")
|
||||
.body(body))
|
||||
}
|
||||
Reference in new issue
Block a user