Ok list_weights_by_user works properly now.

This commit is contained in:
Michael Chung committed 2023-12-03 22:55:56 -05:00
1 parent 8072d13596
commit 9c8ee8dbbb
4 files changed
+54 -16

No files matched your search

@@ -77,11 +77,7 @@ pub async fn list_weights_by_creator_username(
let raw_weights: Vec<RawWeightJoinUser> = get_raw_weights_by_creator_username(&mut connection, creator_username, can_see_deleted).await?;
let weights_records: Vec<WeightsJoinUserRecord> = map_to_weights(raw_weights).await;
let filtered_weights: Vec<WeightsJoinUserRecord> = weights_records.into_iter().filter(|weight| {
weight.creator_username == creator_username && weight.creator_set_visibility == Visibility::Public && can_see_deleted
}).collect();
Ok(filtered_weights)
Ok(weights_records)
}
async fn get_raw_weights_by_creator_username(
@@ -292,6 +292,10 @@ mod tests {
seed_weights_with_category_and_type(WeightsType::LoRA,WeightsCategory::ImageGeneration,5,&creator_username).await?;
let weights_by_username = list_weights_by_creator_username(&pool, &creator_username, can_see_deleted).await?;
for weight in weights_by_username.iter() {
// print weight
println!("weight: {:?}", weight.creator_username);
}
assert_eq!(weights_by_username.len(), 5);
Ok(())
@@ -9,7 +9,7 @@ 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 mysql_queries::queries::model_weights::list_weights_by_user::{list_weights_by_creator_username, WeightsJoinUserRecord};
use tokens::tokens::model_weights::ModelWeightToken;
use crate::http_server::common_responses::user_details_lite::UserDetailsLight;
@@ -19,11 +19,23 @@ use crate::server_state::ServerState;
pub struct Weight {
weight_token: ModelWeightToken,
title: String,
creator: UserDetailsLight,
creator_set_visibility: Visibility,
maybe_thumbnail_token: Option<String>,
description_markdown: String,
description_rendered_html: String,
file_size_bytes: i32,
file_checksum_sha2: 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>,
created_at: DateTime<Utc>,
updated_at: DateTime<Utc>,
}
@@ -96,17 +108,16 @@ pub async fn list_weights_by_user_handler(
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| {
let weights:Vec<Weight> = weights.into_iter().map(|weight| {
Weight {
weight_token: weight.token,
title: weight.title,
@@ -116,16 +127,41 @@ pub async fn list_weights_by_user_handler(
&weight.creator_display_name,
&weight.creator_email_gravatar_hash,
),
maybe_thumbnail_token: weight.maybe_thumbnail_token,
description_markdown: weight.description_markdown,
description_rendered_html: weight.description_rendered_html,
file_size_bytes: weight.file_size_bytes,
file_checksum_sha2: weight.file_checksum_sha2,
cached_user_ratings_total_count: weight.cached_user_ratings_total_count,
cached_user_ratings_positive_count: weight.cached_user_ratings_positive_count,
cached_user_ratings_negative_count: weight.cached_user_ratings_negative_count,
maybe_cached_user_ratings_ratio: weight.maybe_cached_user_ratings_ratio,
cached_user_ratings_last_updated_at: weight.cached_user_ratings_last_updated_at,
creator_set_visibility: weight.creator_set_visibility,
created_at: weight.created_at,
updated_at: weight.updated_at,
}
}).collect();
let response = ListWeightsByUserSuccessResponse {
let final_weights:Vec<Weight>;
// if it's not the user ... then only show public weights else show private and public
if (creator_user_token != user_session.user_token) {
final_weights = weights.into_iter().filter(|weight| {
weight.creator_set_visibility == Visibility::Public
}).collect();
}
else {
final_weights = weights;
}
let response: ListWeightsByUserSuccessResponse = ListWeightsByUserSuccessResponse {
success: true,
weights,
weights: final_weights,
};
let body = serde_json::to_string(&response)
.map_err(|e| ListWeightsByUserError::ServerError)?;
@@ -1249,10 +1249,12 @@ fn add_weights_routes<T, B>(app: App<T>) -> App<T>
.route(web::post().to(update_weight_handler))
.route(web::delete().to(delete_weight_handler))
)
// .route("/by_user/{username}", web::get().to(get_weights_by_user_handler))
.route("/by_user/{username}", web::get().to(list_weights_by_user_handler))
// .route(
// "/by_user_and_type/{username}/{type}",
// web::get().to(get_weights_by_user_and_type_handler)
// )
)
}
// use crate::http_server::endpoints::weights::list_availible_weights::list_availible_weights_handler;
// use crate::http_server::endpoints::weights::list_weights_by_user::list_weights_by_user_handler;