Cargo fmt the question-pool and keepalive code
Browse files at this commitbdde8273b0cbc5957bc2981e350c055a72587599Parent: bebca49- style
- cargo fmt the question-pool and keepalive code
5 changed files +54 -31
- src/domain/pool_question.rs +27 -5
- src/domain/solution.rs +13 -10
- src/main.rs +2 -3
- src/web/questions.rs +11 -12
- tests/persistence.rs +1 -1
src/domain/pool_question.rsmodified+27 -5
| @@ -162,7 +162,10 @@ impl PoolQuestion { | |||
| 162 | created.ok_or_else(|| AppError::Internal("failed to create pool question".into())) | 162 | created.ok_or_else(|| AppError::Internal("failed to create pool question".into())) |
| 163 | } | 163 | } |
| 164 | 164 | ||
| 165 | - pub async fn read(id: &PoolQuestionId, db: &Database) -> Result<Option<PoolQuestion>, AppError> { | 165 | + pub async fn read( |
| 166 | + id: &PoolQuestionId, | ||
| 167 | + db: &Database, | ||
| 168 | + ) -> Result<Option<PoolQuestion>, AppError> { | ||
| 166 | Ok(db.select(id.record()).await?) | 169 | Ok(db.select(id.record()).await?) |
| 167 | } | 170 | } |
| 168 | 171 | ||
| @@ -355,7 +358,13 @@ mod tests { | |||
| 355 | 358 | ||
| 356 | // The asker sees their own pending question plus the pool; a stranger | 359 | // The asker sees their own pending question plus the pool; a stranger |
| 357 | // sees only the pool; the teacher view (list_all) sees everything. | 360 | // sees only the pool; the teacher view (list_all) sees everything. |
| 358 | - assert_eq!(PoolQuestion::list_visible_to(&asker, &db).await.unwrap().len(), 2); | 361 | + assert_eq!( |
| 362 | + PoolQuestion::list_visible_to(&asker, &db) | ||
| 363 | + .await | ||
| 364 | + .unwrap() | ||
| 365 | + .len(), | ||
| 366 | + 2 | ||
| 367 | + ); | ||
| 359 | let stranger_view = PoolQuestion::list_visible_to(&other, &db).await.unwrap(); | 368 | let stranger_view = PoolQuestion::list_visible_to(&other, &db).await.unwrap(); |
| 360 | assert_eq!(stranger_view.len(), 1); | 369 | assert_eq!(stranger_view.len(), 1); |
| 361 | assert_eq!(stranger_view[0].get_id(), published.get_id()); | 370 | assert_eq!(stranger_view[0].get_id(), published.get_id()); |
| @@ -444,14 +453,27 @@ mod tests { | |||
| 444 | 453 | ||
| 445 | // The swept solutions ride back with the question so the caller can | 454 | // The swept solutions ride back with the question so the caller can |
| 446 | // take their image blobs off disk. | 455 | // take their image blobs off disk. |
| 447 | - let (removed, swept) = PoolQuestion::delete(q.get_id(), &db).await.unwrap().unwrap(); | 456 | + let (removed, swept) = PoolQuestion::delete(q.get_id(), &db) |
| 457 | + .await | ||
| 458 | + .unwrap() | ||
| 459 | + .unwrap(); | ||
| 448 | assert_eq!(removed.get_id(), q.get_id()); | 460 | assert_eq!(removed.get_id(), q.get_id()); |
| 449 | assert_eq!(swept.len(), 1); | 461 | assert_eq!(swept.len(), 1); |
| 450 | assert_eq!(swept[0].get_id(), offered.get_id()); | 462 | assert_eq!(swept[0].get_id(), offered.get_id()); |
| 451 | assert!(PoolQuestion::read(q.get_id(), &db).await.unwrap().is_none()); | 463 | assert!(PoolQuestion::read(q.get_id(), &db).await.unwrap().is_none()); |
| 452 | - assert!(Solution::list_for(q.get_id(), &db).await.unwrap().is_empty()); | 464 | + assert!( |
| 465 | + Solution::list_for(q.get_id(), &db) | ||
| 466 | + .await | ||
| 467 | + .unwrap() | ||
| 468 | + .is_empty() | ||
| 469 | + ); | ||
| 453 | 470 | ||
| 454 | // Deleting the already-deleted is None, not an error. | 471 | // Deleting the already-deleted is None, not an error. |
| 455 | - assert!(PoolQuestion::delete(q.get_id(), &db).await.unwrap().is_none()); | 472 | + assert!( |
| 473 | + PoolQuestion::delete(q.get_id(), &db) | ||
| 474 | + .await | ||
| 475 | + .unwrap() | ||
| 476 | + .is_none() | ||
| 477 | + ); | ||
| 456 | } | 478 | } |
| 457 | } | 479 | } |
src/domain/solution.rsmodified+13 -10
| @@ -223,10 +223,7 @@ impl Solution { | |||
| 223 | 223 | ||
| 224 | /// Detach the solution's image. Returns the *before* row — its | 224 | /// Detach the solution's image. Returns the *before* row — its |
| 225 | /// `image_file` is the blob the caller must remove. | 225 | /// `image_file` is the blob the caller must remove. |
| 226 | - pub async fn clear_image( | 226 | + pub async fn clear_image(id: &SolutionId, db: &Database) -> Result<Option<Solution>, AppError> { |
| 227 | - id: &SolutionId, | ||
| 228 | - db: &Database, | ||
| 229 | - ) -> Result<Option<Solution>, AppError> { | ||
| 230 | let mut result = db | 227 | let mut result = db |
| 231 | .query( | 228 | .query( |
| 232 | "UPDATE $s SET image_file = NONE, image_content_type = NONE, image_size = NONE \ | 229 | "UPDATE $s SET image_file = NONE, image_content_type = NONE, image_size = NONE \ |
| @@ -276,7 +273,12 @@ mod tests { | |||
| 276 | assert_eq!(listed.len(), 2); | 273 | assert_eq!(listed.len(), 2); |
| 277 | assert_eq!(listed[0].get_id(), first.get_id()); | 274 | assert_eq!(listed[0].get_id(), first.get_id()); |
| 278 | assert_eq!(listed[1].get_id(), second.get_id()); | 275 | assert_eq!(listed[1].get_id(), second.get_id()); |
| 279 | - assert!(Solution::list_for(&question_b, &db).await.unwrap().is_empty()); | 276 | + assert!( |
| 277 | + Solution::list_for(&question_b, &db) | ||
| 278 | + .await | ||
| 279 | + .unwrap() | ||
| 280 | + .is_empty() | ||
| 281 | + ); | ||
| 280 | 282 | ||
| 281 | // Readable under its own question, invisible under another. | 283 | // Readable under its own question, invisible under another. |
| 282 | assert!( | 284 | assert!( |
| @@ -418,10 +420,9 @@ mod tests { | |||
| 418 | } | 420 | } |
| 419 | } | 421 | } |
| 420 | 422 | ||
| 421 | - let counts = | 423 | + let counts = Solution::counts_for(&[two.clone(), one.clone(), none.clone()], &db) |
| 422 | - Solution::counts_for(&[two.clone(), one.clone(), none.clone()], &db) | 424 | + .await |
| 423 | - .await | 425 | + .unwrap(); |
| 424 | - .unwrap(); | ||
| 425 | assert_eq!(counts.get(two.key()), Some(&2)); | 426 | assert_eq!(counts.get(two.key()), Some(&2)); |
| 426 | assert_eq!(counts.get(one.key()), Some(&1)); | 427 | assert_eq!(counts.get(one.key()), Some(&1)); |
| 427 | // No solutions = no entry; the caller reads the miss as zero. | 428 | // No solutions = no entry; the caller reads the miss as zero. |
| @@ -435,7 +436,9 @@ mod tests { | |||
| 435 | assert!(SolutionBody::try_new(" ").is_err()); | 436 | assert!(SolutionBody::try_new(" ").is_err()); |
| 436 | assert!(SolutionBody::try_new(&"x".repeat(10_001)).is_err()); | 437 | assert!(SolutionBody::try_new(&"x".repeat(10_001)).is_err()); |
| 437 | assert_eq!( | 438 | assert_eq!( |
| 438 | - SolutionBody::try_new("Kısmi integrasyon.").unwrap().as_str(), | 439 | + SolutionBody::try_new("Kısmi integrasyon.") |
| 440 | + .unwrap() | ||
| 441 | + .as_str(), | ||
| 439 | "Kısmi integrasyon." | 442 | "Kısmi integrasyon." |
| 440 | ); | 443 | ); |
| 441 | } | 444 | } |
src/main.rsmodified+2 -3
| @@ -61,9 +61,8 @@ fn keepalive(db: Database, health: DbHealth) { | |||
| 61 | hezarfen_backend::constant::DB_KEEPALIVE_INTERVAL_SECS, | 61 | hezarfen_backend::constant::DB_KEEPALIVE_INTERVAL_SECS, |
| 62 | )); | 62 | )); |
| 63 | interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay); | 63 | interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay); |
| 64 | - let ping_timeout = std::time::Duration::from_secs( | 64 | + let ping_timeout = |
| 65 | - hezarfen_backend::constant::DB_PING_TIMEOUT_SECS, | 65 | + std::time::Duration::from_secs(hezarfen_backend::constant::DB_PING_TIMEOUT_SECS); |
| 66 | - ); | ||
| 67 | loop { | 66 | loop { |
| 68 | interval.tick().await; | 67 | interval.tick().await; |
| 69 | // ponytail: an abandoned ping stays queued in the SDK and replays | 68 | // ponytail: an abandoned ping stays queued in the SDK and replays |
src/web/questions.rsmodified+11 -12
| @@ -167,12 +167,12 @@ impl PoolQuestionResponse { | |||
| 167 | approved_by: question | 167 | approved_by: question |
| 168 | .get_approved_by() | 168 | .get_approved_by() |
| 169 | .map(|approver| PersonRef::resolve(people, approver)), | 169 | .map(|approver| PersonRef::resolve(people, approver)), |
| 170 | - image: question.get_image_content_type().map(|content_type| { | 170 | + image: question |
| 171 | - PoolImageMeta { | 171 | + .get_image_content_type() |
| 172 | + .map(|content_type| PoolImageMeta { | ||
| 172 | content_type: content_type.as_str().to_string(), | 173 | content_type: content_type.as_str().to_string(), |
| 173 | size: question.get_image_size().unwrap_or(0), | 174 | size: question.get_image_size().unwrap_or(0), |
| 174 | - } | 175 | + }), |
| 175 | - }), | ||
| 176 | solution_count, | 176 | solution_count, |
| 177 | } | 177 | } |
| 178 | } | 178 | } |
| @@ -202,12 +202,12 @@ impl SolutionResponse { | |||
| 202 | author: PersonRef::resolve(people, solution.get_author()), | 202 | author: PersonRef::resolve(people, solution.get_author()), |
| 203 | body: solution.get_body().as_str().to_string(), | 203 | body: solution.get_body().as_str().to_string(), |
| 204 | offered_at: solution.get_offered_at().as_millis(), | 204 | offered_at: solution.get_offered_at().as_millis(), |
| 205 | - image: solution.get_image_content_type().map(|content_type| { | 205 | + image: solution |
| 206 | - PoolImageMeta { | 206 | + .get_image_content_type() |
| 207 | + .map(|content_type| PoolImageMeta { | ||
| 207 | content_type: content_type.as_str().to_string(), | 208 | content_type: content_type.as_str().to_string(), |
| 208 | size: solution.get_image_size().unwrap_or(0), | 209 | size: solution.get_image_size().unwrap_or(0), |
| 209 | - } | 210 | + }), |
| 210 | - }), | ||
| 211 | } | 211 | } |
| 212 | } | 212 | } |
| 213 | } | 213 | } |
| @@ -220,8 +220,7 @@ async fn question_responses( | |||
| 220 | st: &AppState, | 220 | st: &AppState, |
| 221 | ) -> Result<Vec<PoolQuestionResponse>, AppError> { | 221 | ) -> Result<Vec<PoolQuestionResponse>, AppError> { |
| 222 | let ids = questions.iter().flat_map(|question| { | 222 | let ids = questions.iter().flat_map(|question| { |
| 223 | - std::iter::once(question.get_asker().clone()) | 223 | + std::iter::once(question.get_asker().clone()).chain(question.get_approved_by().cloned()) |
| 224 | - .chain(question.get_approved_by().cloned()) | ||
| 225 | }); | 224 | }); |
| 226 | let people = person_map(ids, &st.db).await?; | 225 | let people = person_map(ids, &st.db).await?; |
| 227 | let question_ids: Vec<PoolQuestionId> = questions | 226 | let question_ids: Vec<PoolQuestionId> = questions |
| @@ -376,8 +375,8 @@ async fn approve_question( | |||
| 376 | RequireTeacher(user): RequireTeacher, | 375 | RequireTeacher(user): RequireTeacher, |
| 377 | Path(id): Path<String>, | 376 | Path(id): Path<String>, |
| 378 | ) -> Result<Json<PoolQuestionResponse>, AppError> { | 377 | ) -> Result<Json<PoolQuestionResponse>, AppError> { |
| 379 | - let approved = PoolQuestion::approve(&PoolQuestionId::from_key(&id), user.get_id(), &st.db) | 378 | + let approved = |
| 380 | - .await?; | 379 | + PoolQuestion::approve(&PoolQuestionId::from_key(&id), user.get_id(), &st.db).await?; |
| 381 | let question = match approved { | 380 | let question = match approved { |
| 382 | Some(question) => question, | 381 | Some(question) => question, |
| 383 | // Nothing was pending under that id: either it's already approved | 382 | // Nothing was pending under that id: either it's already approved |
tests/persistence.rsmodified+1 -1
| @@ -6,8 +6,8 @@ | |||
| 6 | 6 | ||
| 7 | mod common; | 7 | mod common; |
| 8 | 8 | ||
| 9 | -use axum::http::StatusCode; | ||
| 10 | use axum::Router; | 9 | use axum::Router; |
| 10 | +use axum::http::StatusCode; | ||
| 11 | use common::{create_course, create_exam, enroll, me_id, send, set_role}; | 11 | use common::{create_course, create_exam, enroll, me_id, send, set_role}; |
| 12 | use hezarfen_backend::database::Database; | 12 | use hezarfen_backend::database::Database; |
| 13 | use hezarfen_backend::rate_limit::RateLimitConfig; | 13 | use hezarfen_backend::rate_limit::RateLimitConfig; |