hezarfen_backend
Clone
https://git.tahinli.com/archive/hezarfen_backend.git
0 watching0 forksRust8.3 MiB08aa13eAhmet Kaan Gümüş2 months ago378 commits
| 1 | //! The `chatbot_thread` table: one user's AI-chat threads, listed |
| 2 | //! newest-activity-first, deleted together with every turn in them. The |
| 3 | //! entity and its validated newtypes live in |
| 4 | //! [`crate::domain::chatbot_thread`]; this module is where their rows are |
| 5 | //! read and written. |
| 6 | |
| 7 | use ; |
| 8 | |
| 9 | use crate |
| 10 | |
| 11 | ; |
| 12 | use crate; |
| 13 | use cratecap; |
| 14 | use cratePagedList; |
| 15 | use crate; |
| 16 | use crateTimestamp; |
| 17 | use crateUserId; |
| 18 | use crateAppError; |
| 19 | |
| 20 | /// Move the thread's `updated_at` **and** run `statement` — a write to a turn |
| 21 | /// hanging off that thread — in one transaction, handing back the row it |
| 22 | /// returned. [`AppError::NotFound`] means the thread is gone and `statement` |
| 23 | /// wrote nothing. |
| 24 | /// |
| 25 | /// The bump is what ties a turn's fate to its thread, and reading the thread |
| 26 | /// first does not: [`delete`] sweeps `chatbot_message` and drops |
| 27 | /// the thread in one transaction, so a create landing after that sweep but |
| 28 | /// before its commit reads a thread that is still there (uncommitted) while the |
| 29 | /// sweep ran on a snapshot predating the new row — both commit, and the turn |
| 30 | /// outlives the thread it was deleted with, readable and deletable by nothing. |
| 31 | /// Reads do not conflict; only a write that *moves* a value on a key the delete |
| 32 | /// also writes does. That is the shape |
| 33 | /// [`bump_menu_and_write`](crate::domain::menu::bump_menu_and_write) and |
| 34 | /// [`ExamAnswer::save`](crate::domain::exam_answer::ExamAnswer::save) already |
| 35 | /// use, and the sweep's ordering inside the delete stops mattering once it is |
| 36 | /// in place. |
| 37 | /// |
| 38 | /// `updated_at` is the value moved, and it doubles as the stamp the thread list |
| 39 | /// sorts on — this *is* the activity touch the send path used to issue |
| 40 | /// afterwards as a separate, best-effort query whose failure was only logged. |
| 41 | /// It is written strictly upwards rather than to `$now`: an `UPDATE` that |
| 42 | /// leaves the document unchanged is elided and never reaches the store's write |
| 43 | /// set, a turn's two rows are routinely written inside one millisecond, and a |
| 44 | /// write that does not happen collides with nothing. The stamp can therefore |
| 45 | /// sit a few milliseconds ahead of the clock, which an ordering key does not |
| 46 | /// care about. |
| 47 | /// |
| 48 | /// Admissible for [`transaction_with_retry`] as long as `statement` is: the |
| 49 | /// `UPDATE`, the `IF`/`THROW` and the `RETURN` can never answer "already |
| 50 | /// exists", and neither can a `CREATE` on a freshly minted ULID on a table with |
| 51 | /// no `UNIQUE` index — no rival can have aimed at it, and a lost round wrote |
| 52 | /// nothing, so re-sending it is the recovery. |
| 53 | pub async |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | /// Start a thread unless `user` is already at the school's |
| 92 | /// `max_chatbot_threads`. The slot is taken on the user row in the same |
| 93 | /// transaction as the thread ([`cap::claim_live_and_create`]) — an atomic |
| 94 | /// single-record write, so two requests racing the same user's last slot |
| 95 | /// cannot both win and the counter can never count a row that did not |
| 96 | /// commit. |
| 97 | /// |
| 98 | /// The cap is the one *live* on the settings singleton, sub-queried inside |
| 99 | /// that same conditional write rather than bound as a number: this cap does |
| 100 | /// not live on the parent row (the seat is on the user, the limit is the |
| 101 | /// school's), and a snapshot of it admits every request already in flight |
| 102 | /// when a `PATCH /settings` lowers it. The subquery costs one extra record |
| 103 | /// read per claim on a path that already reads the row it writes. |
| 104 | /// `DEFAULT_MAX_CHATBOT_THREADS` is the fallback the settings row's own |
| 105 | /// absent column means — no row, or a school that never set the knob. |
| 106 | pub async |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | /// A user's threads, most recently active first — the sort the |
| 147 | /// `chatbot_thread_user_updated` index exists for. |
| 148 | pub async |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | /// How many threads `user` keeps — the `max_chatbot_threads` cap check. |
| 164 | pub async |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | /// Read a thread only if `user` owns it — a foreign id reads as absent, so |
| 179 | /// the web layer answers 404 rather than leaking that it exists. |
| 180 | pub async |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | // Stamping new activity is not a call of its own: it is the bump inside |
| 190 | // [`touch_and_write`], which every turn already rides, so a stamp can no |
| 191 | // longer be lost (it used to be a best-effort query after the two creates, |
| 192 | // whose failure was a `warn!`) and no turn can be written without it. |
| 193 | |
| 194 | /// Rename the thread (`None` clears the name back to untitled), stamping |
| 195 | /// the edit as activity. Field-scoped for the same reason as |
| 196 | /// [`touch_and_write`]: a turn may be landing concurrently. |
| 197 | pub async |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | /// Delete the thread and every turn in it — one transaction, so a crash |
| 217 | /// can't orphan messages under a vanished thread. The owner's slot comes |
| 218 | /// back in that same transaction, or the cap would ratchet shut. |
| 219 | /// |
| 220 | /// A turn, a rename and this delete all write the thread row (see |
| 221 | /// [`touch_and_write`]), so the store aborting one of them is ordinary |
| 222 | /// here — that collision is exactly what keeps a turn from outliving the |
| 223 | /// thread — and a lost round is re-sent rather than reported as the `500` |
| 224 | /// it used to be. Re-sending is sound: every statement is a `DELETE` or a |
| 225 | /// field-scoped `UPDATE`, none of which can ever answer "already exists" |
| 226 | /// (see [`transaction_with_retry`]). On the re-sent round the thread is |
| 227 | /// already gone, so the `RETURN` is empty and the caller gets the `404` |
| 228 | /// that is the truth. |
| 229 | pub async |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 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 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | |
| 316 | |
| 317 | |
| 318 | |
| 319 | |
| 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 | |
| 354 | |
| 355 | |
| 356 | |
| 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 |