diff --git a/src/common_queries/list_tts_inference_results.rs b/src/common_queries/list_tts_inference_results.rs index 7203058e95..27f78788da 100644 --- a/src/common_queries/list_tts_inference_results.rs +++ b/src/common_queries/list_tts_inference_results.rs @@ -257,3 +257,111 @@ WHERE Ok(maybe_results) } + +#[derive(sqlx::FromRow)] +pub struct RawInternalTtsRecord { + pub tts_result_id: i64, + pub tts_result_token: String, + + pub tts_model_token: Option, + pub raw_inference_text: String, + + pub maybe_creator_user_token : Option, + pub maybe_creator_username: Option, + pub maybe_creator_display_name: Option, + + pub file_size_bytes : i64, + pub duration_millis : i64, + pub created_at: DateTime, + pub updated_at: DateTime, +} + +async fn list_tts_inference_results_fixed ( + mysql_pool: &MySqlPool, + scope_creator_username: Option<&str>, + sort_ascending: bool, + block_mod_disabled : bool, + limit: u16, + offset: Option, +) -> AnyhowResult> { + info!("listing tts inference results"); + + // TODO/NB: Unfortunately SQLx can't statically typecheck this query + let mut query = r#" +SELECT + tts_results.id as tts_result_id, + tts_results.token as tts_result_token, + + tts_results.model_token as tts_model_token, + tts_results.raw_inference_text as raw_inference_text, + + users.token as maybe_creator_user_token, + users.username as maybe_creator_username, + users.display_name as maybe_creator_display_name, + + 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 + "#.to_string(); + + let mut first_predicate_added = false; + + if let Some(offset) = offset { + if !first_predicate_added { + query.push_str(r#" WHERE tts_results.id < ? "#); + first_predicate_added = true; + } else { + query.push_str(r#" AND tts_results.id < ? "#); + } + } + + if let Some(username) = scope_creator_username { + if !first_predicate_added { + query.push_str(r#" WHERE users.username = ? "#); + first_predicate_added = true; + } else { + query.push_str(r#" AND users.username = ? "#); + } + } + + if block_mod_disabled { + if !first_predicate_added { + query.push_str(r#" + WHERE tts_results.user_deleted_at IS NULL + AND tts_results.mod_deleted_at IS NULL + "#); + first_predicate_added = true; + } else { + query.push_str(r#" + AND tts_results.user_deleted_at IS NULL + AND tts_results.mod_deleted_at IS NULL + "#); + } + } + + query.push_str(" LIMIT ? "); + + let mut query = sqlx::query_as::<_, RawInternalTtsRecord>(&query); + + if let Some(offset) = offset { + query = query.bind(offset); + } + + if let Some(username) = scope_creator_username { + query = query.bind(username); + } + + query = query.bind(limit); + + let results = query.fetch_all(mysql_pool) + .await?; + + Ok(results) +} diff --git a/src/http_server/endpoints/users/list_user_tts_inference_results.rs b/src/http_server/endpoints/users/list_user_tts_inference_results.rs index 0eceb177d1..f2f0371133 100644 --- a/src/http_server/endpoints/users/list_user_tts_inference_results.rs +++ b/src/http_server/endpoints/users/list_user_tts_inference_results.rs @@ -33,12 +33,16 @@ use crate::http_server::web_utils::response_error_helpers::to_simple_json_error; #[derive(Deserialize)] pub struct GetProfilePathInfo { username: String, + sort_ascending: Option, + limit: Option, + cursor: Option, } #[derive(Serialize)] pub struct ListTtsInferenceResultsForUserSuccessResponse { pub success: bool, pub results: Vec, + pub next_cursor: Option, } #[derive(Debug, Display)] @@ -87,6 +91,7 @@ pub async fn list_user_tts_inference_results_handler( let response = ListTtsInferenceResultsForUserSuccessResponse { success: true, results, + next_cursor: Some("".to_string()), }; let body = serde_json::to_string(&response)