hezarfen_backend
Clone
https://git.tahinli.com/archive/hezarfen_backend.git
0 watching0 forksRust8.3 MiB6d68754Ahmet Kaan Gümüş2 months ago566 commits
| 1 | //! HTTP layer: axum routers, request/response DTOs (serde), and the auth |
| 2 | //! extractors. DTOs are plain strings on the wire; handlers parse them into |
| 3 | //! validated domain newtypes before doing anything. Role-gated endpoints take a |
| 4 | //! `RequireTeacher` / `RequireAdmin` extractor instead of `CurrentUser`. |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | // `pub(crate)` for `AiPrincipal`: the AI bridge injects it as an extension on |
| 56 | // the requests it dispatches into the router (see `ai::server`). |
| 57 | pub |
| 58 | |
| 59 | |
| 60 | |
| 61 | pub use |
| 62 | |
| 63 | |
| 64 | ; |
| 65 | pub use |
| 66 | |
| 67 | |
| 68 | ; |
| 69 | pub use ; |
| 70 | pub use ; |
| 71 | |
| 72 | use ; |
| 73 | |
| 74 | use Json; |
| 75 | use Multipart; |
| 76 | use MultipartError; |
| 77 | use ; |
| 78 | use ; |
| 79 | use ; |
| 80 | use ; |
| 81 | use json; |
| 82 | use ToSchema; |
| 83 | |
| 84 | use crate; |
| 85 | use crateDatabase; |
| 86 | use crate; |
| 87 | use crateFileContentType; |
| 88 | use crateRole; |
| 89 | use crateTimestamp; |
| 90 | use crateUserId; |
| 91 | use crate; |
| 92 | |
| 93 | /// Close the window between "this account is teacher+" and the write that |
| 94 | /// records a teacher-only assignment (a class's homeroom teacher, a course's |
| 95 | /// teacher list). Call it *after* the write, with the account it named. |
| 96 | /// |
| 97 | /// WHY: the demotion sweeps in `set_role` run **once**, over the rows that |
| 98 | /// exist at that moment. A `PATCH /users/{id}/role` interleaving between a |
| 99 | /// handler's role check and its write sweeps nothing — and nothing ever |
| 100 | /// re-sweeps — so the assignment stands forever, naming an account that may no |
| 101 | /// longer be a teacher at all. Re-reading the live role after the write is what |
| 102 | /// makes the pair ordered: either the sweep sees the row, or this read sees the |
| 103 | /// demotion. |
| 104 | /// |
| 105 | /// The undo is the very sweep `set_role` owed, and both halves of it are |
| 106 | /// conditional writes scoped to *this* user (`teacher = $usr`, |
| 107 | /// `$usr IN teachers`), so a legitimate concurrent assignment of somebody else |
| 108 | /// is untouched — no lock, the repo's existing idiom. Both are run, not just |
| 109 | /// the caller's own: a demoted account may hold neither, and the row this |
| 110 | /// request did not write is exactly the one a lost sweep left behind. |
| 111 | /// |
| 112 | /// The Sender half of one SSE turn stream. Shared by the chatbot and RAG |
| 113 | /// nests, which stream a finished answer identically. |
| 114 | pub type EventSender = |
| 115 | Sender; |
| 116 | |
| 117 | /// Queue one SSE event. `Err` means the client hung up — stop streaming. |
| 118 | pub async |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | /// Queue the terminal `error` event, with a stable code the frontend branches |
| 136 | /// on and a human-readable message. |
| 137 | pub async |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | /// Cut a finished answer into a handful of `delta` chunks. |
| 147 | /// |
| 148 | /// Fake streaming: `hab/2` is unary, so the whole text is already in hand and |
| 149 | /// this only lets the UI paint it progressively instead of in one jump. The |
| 150 | /// day the protocol grows chunk frames, this is the single function that goes |
| 151 | /// away — nothing else in the stream knows where a chunk came from. |
| 152 | /// |
| 153 | /// Splits on character boundaries (never bytes: a clipped UTF-8 sequence would |
| 154 | /// render as garbage), and never returns an empty chunk. Short answers stay |
| 155 | /// whole — dribbling "hi" out one letter at a time is worse than not |
| 156 | /// pretending to stream at all. |
| 157 | pub |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | /// A missing user counts as demoted. The ordinary path is one extra read and no |
| 173 | /// write at all. |
| 174 | pub async |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | /// If both ends are present, `ends_at` must not precede `starts_at`. Shared by |
| 189 | /// everything that carries a time range (events, course sessions). The PATCH |
| 190 | /// paths re-check this at write time via |
| 191 | /// [`crate::db::field_update::FieldUpdate::ordered`], which answers with the |
| 192 | /// same error — this is the pre-flight, that is the race closer. |
| 193 | pub |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | /// A schedule instant a request explicitly sets must not lie in the past — |
| 206 | /// nothing can be scheduled to start (or end) before now. Applies only to |
| 207 | /// values the request itself carries: stored values merged into a PATCH are |
| 208 | /// exempt, because a running exam's `starts_at` is legitimately past and a |
| 209 | /// title-only edit must not trip over it. `SCHEDULE_PAST_GRACE_MS` keeps |
| 210 | /// "starts now" from failing by the width of its own round trip. |
| 211 | pub |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | // ---- uploaded blobs --------------------------------------------------------- |
| 227 | // Note files and question images share one story: bytes on disk under the |
| 228 | // configured directory in a file named by a server-generated ULID (user input |
| 229 | // never shapes a path), metadata in a row. Upload writes the blob before the |
| 230 | // row; delete removes the row before the blob — so a stored row always points |
| 231 | // at a real blob and a crash strands at worst an unreachable file. |
| 232 | |
| 233 | /// Where a stored row's blob lives: one file under the configured directory, |
| 234 | /// named by the row's server-generated key. |
| 235 | pub |
| 236 | |
| 237 | |
| 238 | |
| 239 | /// Make sure a school's blob directory exists before writing into it. |
| 240 | /// |
| 241 | /// `FILES_PATH` is the deployment root and `main` creates only that; each |
| 242 | /// school's subdirectory appears the first time that school stores a file, so |
| 243 | /// nothing has to be provisioned when a school is created. |
| 244 | pub async |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | /// Best-effort blob removal after its row is gone. Failure only strands an |
| 254 | /// unreachable file on disk, so it is logged rather than surfaced. |
| 255 | pub async |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | /// How much of a stored blob is read per chunk, and how many chunks its body |
| 265 | /// may have in flight. 4 × 64 KiB is the whole memory ceiling one stream |
| 266 | /// costs, however large the file behind it. Shared by every streaming blob |
| 267 | /// door (podcast audio, the insight report document). |
| 268 | const BLOB_CHUNK_BYTES: usize = 64 * 1024; |
| 269 | const BLOB_CHUNKS_IN_FLIGHT: usize = 4; |
| 270 | |
| 271 | /// A stored file as a byte stream: a reader task hands chunks to the body over |
| 272 | /// a bounded channel, so a client that stops reading parks the pump at the |
| 273 | /// next send instead of pinning the whole file in memory, and a closed body |
| 274 | /// ends the task by itself. |
| 275 | pub |
| 276 | |
| 277 | + Send + 'static |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | /// The declared content type of an inline-displayed image upload, held to the |
| 308 | /// raster allowlist — SVG stays out (it can script) since these bytes are |
| 309 | /// rendered inline to whole classes. Shared by exam question images and pool |
| 310 | /// question photos. |
| 311 | pub |
| 312 | |
| 313 | |
| 314 | |
| 315 | |
| 316 | |
| 317 | |
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | /// A stored blob as an inline-displayable response: the declared (and |
| 323 | /// allowlisted) content type, `nosniff`, and `no-store` — moderated/exam |
| 324 | /// content has no business in shared caches, and a replaced image must not |
| 325 | /// linger. |
| 326 | pub async |
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | |
| 338 | |
| 339 | |
| 340 | |
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | /// Multipart read failures: the route-level body cap maps to 413 like the |
| 352 | /// school-limit check; anything else is a malformed body. |
| 353 | pub |
| 354 | |
| 355 | |
| 356 | |
| 357 | |
| 358 | |
| 359 | |
| 360 | |
| 361 | |
| 362 | |
| 363 | |
| 364 | /// Schema-only mirror of an upload form; the handlers read the multipart |
| 365 | /// stream directly. |
| 366 | |
| 367 | |
| 368 | pub |
| 369 | |
| 370 | |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | /// One uploaded file's raw parts, as the client sent them — the caller |
| 376 | /// validates what it cares about (notes need a filename, images an |
| 377 | /// allowlisted content type). |
| 378 | pub |
| 379 | |
| 380 | |
| 381 | |
| 382 | |
| 383 | |
| 384 | /// Pull the `file` field out of a multipart body, reading it chunkwise so an |
| 385 | /// oversized upload dies at `limit`, not after buffering whole — the check is |
| 386 | /// on real bytes received, so a lying `Content-Length` can't sneak past it. |
| 387 | /// Empty files are refused; stray extra fields are ignored. |
| 388 | pub async |
| 389 | |
| 390 | |
| 391 | |
| 392 | |
| 393 | |
| 394 | |
| 395 | |
| 396 | |
| 397 | |
| 398 | |
| 399 | |
| 400 | |
| 401 | |
| 402 | |
| 403 | |
| 404 | |
| 405 | |
| 406 | |
| 407 | |
| 408 | |
| 409 | |
| 410 | |
| 411 | |
| 412 | |
| 413 | |
| 414 | |
| 415 | |
| 416 | |
| 417 | |
| 418 | |
| 419 | |
| 420 | |
| 421 | |
| 422 | |
| 423 | |
| 424 | |
| 425 | |
| 426 | /// One option of a choice question as a client sends it — the wire form of |
| 427 | /// [`crate::domain::exam_question::ChoiceInput`], shared by the exam-question |
| 428 | /// and bank-template routes. |
| 429 | /// |
| 430 | /// `id` is the option's key within the payload: send back the `id` the server |
| 431 | /// returned to keep that option (and its picture) across an edit, or any fresh |
| 432 | /// string to introduce a new one. The server mints the stored id either way, so |
| 433 | /// nothing a client sends here ever reaches storage. |
| 434 | |
| 435 | pub |
| 436 | |
| 437 | |
| 438 | |
| 439 | |
| 440 | |
| 441 | |
| 442 | |
| 443 | |
| 444 | |
| 445 | |
| 446 | |
| 447 | |
| 448 | |
| 449 | |
| 450 | |
| 451 | |
| 452 | |
| 453 | |
| 454 | |
| 455 | |
| 456 | |
| 457 | |
| 458 | |
| 459 | |
| 460 | |
| 461 | |
| 462 | |
| 463 | |
| 464 | |
| 465 | |
| 466 | |
| 467 | |
| 468 | |
| 469 | /// Distinguishes an *absent* PATCH field from an explicit `null`: absent never |
| 470 | /// reaches the deserializer and stays `None` via `#[serde(default)]` (keep the |
| 471 | /// stored value); `null` or a value lands here as `Some(inner)` (clear or set). |
| 472 | pub |
| 473 | |
| 474 | |
| 475 | |
| 476 | |
| 477 | |
| 478 | |
| 479 | |
| 480 | /// The AI-unavailable `503`. Shared by every route that can refuse because no |
| 481 | /// AI service is reachable: built here rather than as an `AppError` variant — |
| 482 | /// `AppError`'s 503s all mean "the database is reconnecting", which this is |
| 483 | /// not. No `Retry-After` — when a service will dial back in is unknowable. |
| 484 | pub |
| 485 | |
| 486 | |
| 487 | |
| 488 | |
| 489 | |
| 490 | |
| 491 | |
| 492 | |
| 493 | |
| 494 | |
| 495 | |
| 496 | |
| 497 | |
| 498 | |
| 499 | |
| 500 | |
| 501 | |
| 502 | |
| 503 | |
| 504 | |
| 505 | |
| 506 | |
| 507 | |
| 508 | |
| 509 | |
| 510 | |
| 511 | |
| 512 | |
| 513 | |
| 514 | |
| 515 | |
| 516 | |
| 517 | |
| 518 | |
| 519 | |
| 520 | |
| 521 | |
| 522 | |
| 523 | |
| 524 | |
| 525 | |
| 526 | |
| 527 | |
| 528 | |
| 529 | |
| 530 | |
| 531 | |
| 532 | |
| 533 | |
| 534 | |
| 535 | |
| 536 | |
| 537 | |
| 538 | |
| 539 | |
| 540 | |
| 541 | |
| 542 | |
| 543 | |
| 544 | |
| 545 | |
| 546 | |
| 547 | |
| 548 | |
| 549 | |
| 550 | |
| 551 | |
| 552 | |
| 553 | |
| 554 | |
| 555 | |
| 556 | |
| 557 | |
| 558 | |
| 559 | |
| 560 | |
| 561 | |
| 562 | |
| 563 | |
| 564 | |
| 565 | |
| 566 | |
| 567 | |
| 568 | |
| 569 | |
| 570 | |
| 571 | |
| 572 | |
| 573 | |
| 574 | |
| 575 | |
| 576 | |
| 577 | |
| 578 | |
| 579 | |
| 580 | |
| 581 | |
| 582 | |
| 583 | |
| 584 | |
| 585 | |
| 586 | |
| 587 | |
| 588 | |
| 589 | |
| 590 | |
| 591 | |
| 592 | |
| 593 | |
| 594 | |
| 595 | |
| 596 | |
| 597 | |
| 598 | |
| 599 | |
| 600 | |
| 601 | |
| 602 | |
| 603 | |
| 604 | |
| 605 | |
| 606 | |
| 607 | |
| 608 | |
| 609 | |
| 610 | |
| 611 | |
| 612 | |
| 613 | |
| 614 | |
| 615 | |
| 616 | |
| 617 | |
| 618 | |
| 619 | |
| 620 | |
| 621 | |
| 622 | |
| 623 | |
| 624 | |
| 625 | |
| 626 | |
| 627 | |
| 628 | |
| 629 | |
| 630 | |
| 631 | |
| 632 | |
| 633 | |
| 634 | |
| 635 | |
| 636 | |
| 637 | |
| 638 | |
| 639 | |
| 640 | |
| 641 | |
| 642 | |
| 643 | |
| 644 | |
| 645 | |
| 646 | |
| 647 | |
| 648 | |
| 649 | |
| 650 | |
| 651 | |
| 652 | |
| 653 | |
| 654 | |
| 655 | |
| 656 | |
| 657 | |
| 658 | |
| 659 |