hezarfen_backend
Clone
https://git.tahinli.com/archive/hezarfen_backend.git
0 watching0 forksRust8.3 MiB6d68754Ahmet Kaan Gümüş2 months ago566 commits
| 1 | //! The collaborative board room: a WebSocket at `GET /boards/{id}/ws`. |
| 2 | //! |
| 3 | //! This is where the drawing actually happens. REST next door |
| 4 | //! ([`super::boards`]) opens boards, reads the canvas and the history, and runs |
| 5 | //! the creator's commands; the room is the live half — one socket per |
| 6 | //! participant, every accepted stroke fanned out to the rest through |
| 7 | //! [`crate::state::BoardHub`]. |
| 8 | //! |
| 9 | //! Two rules shape everything below. |
| 10 | //! |
| 11 | //! **Persist, then publish.** A stroke is fanned out only once |
| 12 | //! [`board_stroke::append`] has returned `Ok`, so the channel can never carry a |
| 13 | //! mark the database refused (a locked board, a full canvas, a closed board). |
| 14 | //! The database is the canvas; the channel is a notification about it. A |
| 15 | //! client still dedupes by stroke `id` — a resync deliberately re-serves marks |
| 16 | //! it already drew — but it never sees the echo of its own strokes: those are |
| 17 | //! acknowledged with `saved` and filtered back out of its fan-out. |
| 18 | //! |
| 19 | //! **A clear deletes nothing.** `clear` bumps the board's epoch, so the live |
| 20 | //! canvas empties while every stroke stays stored. A join therefore replays the |
| 21 | //! *current epoch only* — history never rides this socket, it is an explicit |
| 22 | //! REST read (`GET /boards/{id}/history`). A client that reconnects with a |
| 23 | //! cursor from an older epoch is told `cleared` and given the whole current |
| 24 | //! epoch, never a diff from a cursor that no longer means anything. |
| 25 | //! |
| 26 | //! Wire protocol (JSON text frames): |
| 27 | //! |
| 28 | //! client → server |
| 29 | //! - `{"type":"join", after?, epoch?}` — replay the current epoch. `after` is |
| 30 | //! the last stroke id already drawn and `epoch` the canvas it belongs to; |
| 31 | //! send both to resume, neither for a full replay. A stale `epoch` gets a |
| 32 | //! `cleared` frame and a full replay. |
| 33 | //! - `{"type":"stroke", payload, client_seq?}` — any participant |
| 34 | //! - `{"type":"clear"}` / `{"type":"lock", locked}` — **creator only**; anyone |
| 35 | //! else gets `error{code:"forbidden"}` and *keeps the socket* |
| 36 | //! - `{"type":"ping"}` |
| 37 | //! |
| 38 | //! server → client |
| 39 | //! - `{"type":"state", board, epoch, locked, closed_at, creator, participants, now}` |
| 40 | //! on connect and every [`BOARD_WS_TICK_SECS`] |
| 41 | //! - `{"type":"strokes", epoch, strokes:[{id, author, payload}]}` — one replay |
| 42 | //! chunk ([`BOARD_REPLAY_CHUNK`] at a time), then |
| 43 | //! `{"type":"synced", epoch, cursor}` |
| 44 | //! - `{"type":"stroke", id, author, payload, epoch}` — someone *else* drew |
| 45 | //! - `{"type":"saved", id, client_seq?}` — your stroke landed |
| 46 | //! - `{"type":"pong"}` |
| 47 | //! - `{"type":"error", code, message, client_seq?}` |
| 48 | //! - the five frames the REST routes publish, forwarded verbatim: `cleared`, |
| 49 | //! `locked`, `closed`, `participants`, `deleted` |
| 50 | //! |
| 51 | //! `closed` and `deleted` end the room. A frame is never the authority here: |
| 52 | //! every frame is authorized against the database *before* it is delivered, |
| 53 | //! and every stroke, clear and lock re-reads the board and re-checks the role |
| 54 | //! and the roster before it is written. So a caller who lost the right to be |
| 55 | //! here — taken off the roster, or demoted out of whiteboard access — is |
| 56 | //! refused and dropped whether or not any notification reached them. |
| 57 | |
| 58 | use VecDeque; |
| 59 | use Duration; |
| 60 | |
| 61 | use crate; |
| 62 | use Path; |
| 63 | use ; |
| 64 | use Response; |
| 65 | use Deserialize; |
| 66 | use ; |
| 67 | use RecvError; |
| 68 | |
| 69 | use crate; |
| 70 | use crateDatabase; |
| 71 | use crate; |
| 72 | use crate |
| 73 | |
| 74 | ; |
| 75 | use crateRole; |
| 76 | use crateTimestamp; |
| 77 | use crateUserId; |
| 78 | use crateAppError; |
| 79 | use crate; |
| 80 | use crateAppState; |
| 81 | use crateSchoolId; |
| 82 | use crateCurrentUser; |
| 83 | use crate; |
| 84 | |
| 85 | /// How this room names itself in the logs [`room::public_message`] writes. |
| 86 | const ROOM: &str = "board room"; |
| 87 | |
| 88 | /// What the client asked for, tagged by `type`. |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | /// Enter the caller's board room. Every gate runs *before* the upgrade so a |
| 116 | /// rejection is an HTTP status rather than an instant close: no session (401), |
| 117 | /// and a board that does not exist — or that the caller is not on, or that the |
| 118 | /// caller is a `parent` and so barred from the whiteboard outright — is the |
| 119 | /// same **404**, because a non-participant must not learn a board exists (the |
| 120 | /// rationale at `src/lib.rs`). Refusing the door keeps a parent out; keeping |
| 121 | /// one out who was demoted *after* the upgrade is [`live_board`]'s job, which |
| 122 | /// every action already re-reads through. |
| 123 | pub async |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | /// The room loop: the board's frames out, draw/clear/lock/ping in, until the |
| 148 | /// socket closes or the board ends (closed, deleted, or the caller taken off |
| 149 | /// it). |
| 150 | async |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 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 | type Step = ; |
| 200 | |
| 201 | /// The board as it stands right now, for a caller who must still be on it and |
| 202 | /// who must still hold a role the whiteboard is open to. Every action re-reads |
| 203 | /// through here: the role, the roster, the lock and the epoch are all live |
| 204 | /// facts, and a frame this socket may never have seen is not an authority over |
| 205 | /// any of them. |
| 206 | /// |
| 207 | /// The role is re-read for the same reason the roster is: the upgrade door's |
| 208 | /// bar (`board_ws`, mirroring [`super::boards`]'s `board_for`) is checked once, |
| 209 | /// nothing publishes a frame on a role change, and a demotion to `parent` — the |
| 210 | /// role barred from the whiteboard outright — must not leave an open socket |
| 211 | /// still committing strokes. |
| 212 | async |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | /// Push a `state` frame, and end the room if the board is gone or the caller |
| 227 | /// has been taken off it. |
| 228 | async |
| 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 | /// One frame off the hub, on its way to this socket. Forwarded verbatim — the |
| 262 | /// five REST shapes are the contract — with three of them acted on as well, |
| 263 | /// and this socket's own strokes dropped: it already got a `saved` for each. |
| 264 | async |
| 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 | /// Frames were dropped: tell the client, then re-serve the current epoch in |
| 327 | /// full. A diff is impossible — the gap's contents are exactly what is unknown. |
| 328 | async |
| 329 | |
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | |
| 338 | |
| 339 | |
| 340 | |
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | |
| 352 | /// Serve one epoch, oldest first, [`BOARD_REPLAY_CHUNK`] rows at a time, and |
| 353 | /// close with the cursor the client can resume from. The loop runs until a |
| 354 | /// short chunk comes back — stopping at the first full one is how a replay |
| 355 | /// silently truncates at the chunk boundary. |
| 356 | async |
| 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 | async |
| 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 | /// The board for a creator-only command. `None` means it was refused and the |
| 533 | /// client told — **the socket stays open**: a participant who clicks "clear" |
| 534 | /// has made a mistake, not left the room. |
| 535 | async |
| 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 | /// A machine-readable `code` beside the public words. Each refusal the board |
| 565 | /// path can raise means a different thing to a client: `epoch_full` is |
| 566 | /// recoverable by clearing, `locked` is a pause that will lift, `canvas_blank` |
| 567 | /// is nothing to do, `board_closed` is terminal. |
| 568 | /// |
| 569 | /// The match is on the refusal *constants* [`board_stroke`] raises, by value — |
| 570 | /// never on a substring of them. Guessing from a substring made every reworded |
| 571 | /// refusal a silent re-labelling: "the canvas is already blank" contains the |
| 572 | /// word "closed", so a blank canvas told a live room its board was finished. |
| 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 | |
| 660 | |
| 661 | |
| 662 | |
| 663 | |
| 664 | |
| 665 | |
| 666 | |
| 667 | |
| 668 | |
| 669 | |
| 670 | |
| 671 | |
| 672 | |
| 673 | |
| 674 | |
| 675 | |
| 676 | |
| 677 | |
| 678 | |
| 679 | |
| 680 | |
| 681 | |
| 682 | |
| 683 | |
| 684 | |
| 685 | |
| 686 | |
| 687 | |
| 688 | |
| 689 | |
| 690 | |
| 691 | |
| 692 | |
| 693 | |
| 694 | |
| 695 | |
| 696 | |
| 697 | |
| 698 | |
| 699 | |
| 700 | |
| 701 | |
| 702 | |
| 703 | |
| 704 | |
| 705 | |
| 706 | |
| 707 | |
| 708 | |
| 709 | |
| 710 | |
| 711 | |
| 712 | |
| 713 | |
| 714 | |
| 715 | |
| 716 | |
| 717 | |
| 718 | |
| 719 | |
| 720 | |
| 721 | |
| 722 | |
| 723 | |
| 724 | |
| 725 | |
| 726 | |
| 727 | |
| 728 | |
| 729 | |
| 730 | |
| 731 | |
| 732 | |
| 733 | |
| 734 | |
| 735 |