hezarfen_backend
Clone
https://git.tahinli.com/archive/hezarfen_backend.git
0 watching0 forksRust8.3 MiB69d4295Ahmet Kaan Gümüş2 months ago536 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 crateDEFAULT_MAX_CHATBOT_THREADS; |
| 8 | use crate; |
| 9 | use cratePagedList; |
| 10 | use crate; |
| 11 | use crateTimestamp; |
| 12 | use crateUserId; |
| 13 | use crateAppError; |
| 14 | use query_as; |
| 15 | |
| 16 | /// Start a thread unless `user` is already at the school's |
| 17 | /// `max_chatbot_threads`. The claim recipe of one: the slot bump on the |
| 18 | /// user row, the live cap read, and the thread's insert are a single |
| 19 | /// statement — two requests racing the same user's last slot cannot both |
| 20 | /// win, and the counter can never count a row that did not commit. |
| 21 | /// |
| 22 | /// The cap is the one *live* on the settings singleton, sub-queried inside |
| 23 | /// that same conditional write rather than bound as a number: this cap does |
| 24 | /// not live on the parent row (the seat is on the user, the limit is the |
| 25 | /// school's), and a snapshot of it admits every request already in flight |
| 26 | /// when a `PATCH /settings` lowers it. `DEFAULT_MAX_CHATBOT_THREADS` is the |
| 27 | /// fallback the settings row's own NULL means — no row, or a school that |
| 28 | /// never set the knob. |
| 29 | /// |
| 30 | /// The seat is taken on the user's own row, which is also the key a role |
| 31 | /// change writes, so this write already contends with a demotion — no |
| 32 | /// separate holder claim is needed. |
| 33 | pub async |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | /// A user's threads, most recently active first — the sort the |
| 72 | /// `chatbot_thread_user_updated` index exists for. |
| 73 | pub async |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | /// How many threads `user` keeps — the `max_chatbot_threads` cap check. |
| 89 | pub async |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | /// Read a thread only if `user` owns it — a foreign id reads as absent, so |
| 100 | /// the web layer answers 404 rather than leaking that it exists. |
| 101 | pub async |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | // Stamping new activity is not a call of its own: it is the monotonic bump |
| 117 | // inside every write that touches the thread (a turn's insert, the rename, |
| 118 | // the delete below), so a stamp can no longer be lost (it used to be a |
| 119 | // best-effort query after the two creates, whose failure was a `warn!`) and |
| 120 | // no turn can be written without it. |
| 121 | |
| 122 | /// Rename the thread (`None` clears the name back to untitled), stamping |
| 123 | /// the edit as activity. Field-scoped: a turn may be landing concurrently. |
| 124 | /// The stamp is written strictly upwards — `GREATEST($now, updated_at + 1)` |
| 125 | /// — because a thread's writes routinely land inside one millisecond and |
| 126 | /// `updated_at` is the list's ordering key; the stamp may sit a few |
| 127 | /// milliseconds ahead of the clock, which an ordering key does not care |
| 128 | /// about. Zero rows: the thread is gone, the same 404 the delete answers. |
| 129 | pub async |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | /// Delete the thread and every turn in it — one transaction, so a crash |
| 150 | /// can't orphan messages under a vanished thread. The owner's slot comes |
| 151 | /// back in that same transaction, or the cap would ratchet shut. |
| 152 | /// |
| 153 | /// A turn's insert (see [`crate::db::chatbot_message`]) writes *through* |
| 154 | /// the thread's row — its guarded statement moves `updated_at`, the key |
| 155 | /// this delete's final write removes — so under Postgres's row locking the |
| 156 | /// two serialize: either the turn saw the thread and committed first (the |
| 157 | /// delete then refuses nothing, the sweep takes the turn), or the thread |
| 158 | /// was gone and the turn's gate matched nothing. No turn outlives its |
| 159 | /// thread. On a re-sent round the thread is already gone, so the caller |
| 160 | /// gets the `404` that is the truth. |
| 161 | pub async |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 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 | |
| 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 |