hezarfen_backend
Clone
https://git.tahinli.com/archive/hezarfen_backend.git
0 watching0 forksRust8.3 MiB08aa13eAhmet Kaan Gümüş2 months ago378 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 | // `pub(crate)` for `AiPrincipal`: the AI bridge injects it as an extension on |
| 45 | // the requests it dispatches into the router (see `ai::server`). |
| 46 | pub |
| 47 | |
| 48 | |
| 49 | |
| 50 | pub use |
| 51 | |
| 52 | |
| 53 | ; |
| 54 | pub use |
| 55 | |
| 56 | |
| 57 | ; |
| 58 | pub use ; |
| 59 | pub use ; |
| 60 | |
| 61 | use ; |
| 62 | |
| 63 | use Multipart; |
| 64 | use MultipartError; |
| 65 | use ; |
| 66 | use ; |
| 67 | use ; |
| 68 | use ; |
| 69 | use ToSchema; |
| 70 | |
| 71 | use crate; |
| 72 | use crateDatabase; |
| 73 | use crate; |
| 74 | use crateFileContentType; |
| 75 | use crateRole; |
| 76 | use crateTimestamp; |
| 77 | use crateUserId; |
| 78 | use crate; |
| 79 | |
| 80 | /// Close the window between "this account is teacher+" and the write that |
| 81 | /// records a teacher-only assignment (a class's homeroom teacher, a course's |
| 82 | /// teacher list). Call it *after* the write, with the account it named. |
| 83 | /// |
| 84 | /// WHY: the demotion sweeps in `set_role` run **once**, over the rows that |
| 85 | /// exist at that moment. A `PATCH /users/{id}/role` interleaving between a |
| 86 | /// handler's role check and its write sweeps nothing — and nothing ever |
| 87 | /// re-sweeps — so the assignment stands forever, naming an account that may no |
| 88 | /// longer be a teacher at all. Re-reading the live role after the write is what |
| 89 | /// makes the pair ordered: either the sweep sees the row, or this read sees the |
| 90 | /// demotion. |
| 91 | /// |
| 92 | /// The undo is the very sweep `set_role` owed, and both halves of it are |
| 93 | /// conditional writes scoped to *this* user (`teacher = $usr`, |
| 94 | /// `$usr IN teachers`), so a legitimate concurrent assignment of somebody else |
| 95 | /// is untouched — no lock, the repo's existing idiom. Both are run, not just |
| 96 | /// the caller's own: a demoted account may hold neither, and the row this |
| 97 | /// request did not write is exactly the one a lost sweep left behind. |
| 98 | /// |
| 99 | /// A missing user counts as demoted. The ordinary path is one extra read and no |
| 100 | /// write at all. |
| 101 | pub async |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | /// If both ends are present, `ends_at` must not precede `starts_at`. Shared by |
| 116 | /// everything that carries a time range (events, course sessions). The PATCH |
| 117 | /// paths re-check this at write time via |
| 118 | /// [`crate::db::field_update::FieldUpdate::ordered`], which answers with the |
| 119 | /// same error — this is the pre-flight, that is the race closer. |
| 120 | pub |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | /// A schedule instant a request explicitly sets must not lie in the past — |
| 133 | /// nothing can be scheduled to start (or end) before now. Applies only to |
| 134 | /// values the request itself carries: stored values merged into a PATCH are |
| 135 | /// exempt, because a running exam's `starts_at` is legitimately past and a |
| 136 | /// title-only edit must not trip over it. `SCHEDULE_PAST_GRACE_MS` keeps |
| 137 | /// "starts now" from failing by the width of its own round trip. |
| 138 | pub |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | // ---- uploaded blobs --------------------------------------------------------- |
| 154 | // Note files and question images share one story: bytes on disk under the |
| 155 | // configured directory in a file named by a server-generated ULID (user input |
| 156 | // never shapes a path), metadata in a row. Upload writes the blob before the |
| 157 | // row; delete removes the row before the blob — so a stored row always points |
| 158 | // at a real blob and a crash strands at worst an unreachable file. |
| 159 | |
| 160 | /// Where a stored row's blob lives: one file under the configured directory, |
| 161 | /// named by the row's server-generated key. |
| 162 | pub |
| 163 | |
| 164 | |
| 165 | |
| 166 | /// Make sure a school's blob directory exists before writing into it. |
| 167 | /// |
| 168 | /// `FILES_PATH` is the deployment root and `main` creates only that; each |
| 169 | /// school's subdirectory appears the first time that school stores a file, so |
| 170 | /// nothing has to be provisioned when a school is created. |
| 171 | pub async |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | /// Best-effort blob removal after its row is gone. Failure only strands an |
| 181 | /// unreachable file on disk, so it is logged rather than surfaced. |
| 182 | pub async |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | /// The declared content type of an inline-displayed image upload, held to the |
| 192 | /// raster allowlist — SVG stays out (it can script) since these bytes are |
| 193 | /// rendered inline to whole classes. Shared by exam question images and pool |
| 194 | /// question photos. |
| 195 | pub |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | /// A stored blob as an inline-displayable response: the declared (and |
| 207 | /// allowlisted) content type, `nosniff`, and `no-store` — moderated/exam |
| 208 | /// content has no business in shared caches, and a replaced image must not |
| 209 | /// linger. |
| 210 | pub async |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | /// Multipart read failures: the route-level body cap maps to 413 like the |
| 236 | /// school-limit check; anything else is a malformed body. |
| 237 | pub |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | /// Schema-only mirror of an upload form; the handlers read the multipart |
| 249 | /// stream directly. |
| 250 | |
| 251 | |
| 252 | pub |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | /// One uploaded file's raw parts, as the client sent them — the caller |
| 260 | /// validates what it cares about (notes need a filename, images an |
| 261 | /// allowlisted content type). |
| 262 | pub |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | /// Pull the `file` field out of a multipart body, reading it chunkwise so an |
| 269 | /// oversized upload dies at `limit`, not after buffering whole — the check is |
| 270 | /// on real bytes received, so a lying `Content-Length` can't sneak past it. |
| 271 | /// Empty files are refused; stray extra fields are ignored. |
| 272 | pub async |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 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 | |
| 308 | |
| 309 | |
| 310 | /// One option of a choice question as a client sends it — the wire form of |
| 311 | /// [`crate::domain::exam_question::ChoiceInput`], shared by the exam-question |
| 312 | /// and bank-template routes. |
| 313 | /// |
| 314 | /// `id` is the option's key within the payload: send back the `id` the server |
| 315 | /// returned to keep that option (and its picture) across an edit, or any fresh |
| 316 | /// string to introduce a new one. The server mints the stored id either way, so |
| 317 | /// nothing a client sends here ever reaches storage. |
| 318 | |
| 319 | pub |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 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 | |
| 352 | |
| 353 | /// Distinguishes an *absent* PATCH field from an explicit `null`: absent never |
| 354 | /// reaches the deserializer and stays `None` via `#[serde(default)]` (keep the |
| 355 | /// stored value); `null` or a value lands here as `Some(inner)` (clear or set). |
| 356 | pub |
| 357 | |
| 358 | |
| 359 | |
| 360 | |
| 361 | |
| 362 | |
| 363 | |
| 364 | |
| 365 | |
| 366 | |
| 367 | |
| 368 | |
| 369 | |
| 370 | |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | |
| 376 | |
| 377 | |
| 378 | |
| 379 | |
| 380 | |
| 381 | |
| 382 | |
| 383 | |
| 384 | |
| 385 | |
| 386 | |
| 387 | |
| 388 | |
| 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 | |
| 427 | |
| 428 | |
| 429 | |
| 430 | |
| 431 | |
| 432 | |
| 433 | |
| 434 | |
| 435 | |
| 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 | |
| 470 | |
| 471 | |
| 472 | |
| 473 | |
| 474 | |
| 475 | |
| 476 | |
| 477 | |
| 478 | |
| 479 | |
| 480 | |
| 481 | |
| 482 | |
| 483 | |
| 484 | |
| 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 |