hezarfen_backend
Clone
https://git.tahinli.com/archive/hezarfen_backend.git
0 watching0 forksRust8.3 MiB69d4295Ahmet Kaan Gümüş2 months ago536 commits
| 1 | //! `GET /limits` — every compile-time validation bound, served as data. |
| 2 | //! |
| 3 | //! The frontend has to enforce the same rules the newtypes do (a `maxlength` |
| 4 | //! on an input, a client-side check before a doomed request). Hard-coding them |
| 5 | //! there means two copies that silently drift apart the day one moves. This |
| 6 | //! endpoint is the single source: every field below reads a `crate::constant` |
| 7 | //! item directly, so a changed constant changes the response in the same |
| 8 | //! commit. |
| 9 | //! |
| 10 | //! School-adjustable policy (exam kinds, attendance statuses, grade bands, the |
| 11 | //! live `max_file_bytes`) lives in `GET /settings` — this route carries only |
| 12 | //! the fixed bounds those settings must themselves stay inside. |
| 13 | |
| 14 | use Json; |
| 15 | use State; |
| 16 | use Serialize; |
| 17 | use ToSchema; |
| 18 | use OpenApiRouter; |
| 19 | use routes; |
| 20 | |
| 21 | use crate*; |
| 22 | use crateBADGES; |
| 23 | use crate; |
| 24 | use crateROLES; |
| 25 | use crateAppState; |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | /// Account fields: credentials, profile, and the closed role set. |
| 32 | |
| 33 | |
| 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 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | $")] |
| 82 | palette_color_pattern: &'static str, |
| 83 | /// Length of a `palette_color`, `#` included. |
| 84 | palette_color_len: usize, |
| 85 | /// How long a login session stays valid, days. |
| 86 | session_duration_days: i64, |
| 87 | } |
| 88 | |
| 89 | /// The auto-earned badge catalog. A group like every other key here, so a |
| 90 | /// client can walk this response uniformly; the catalog itself is the one |
| 91 | /// field inside. |
| 92 | #[derive(Serialize, ToSchema)] |
| 93 | struct BadgeLimits { |
| 94 | /// Every badge the system can auto-award, in catalog order. Hardcoded, so |
| 95 | /// like everything else here it moves only with a deploy. |
| 96 | catalog: Vec<BadgeLimit>, |
| 97 | /// The exam mark that counts as a high one, out of `mark.max_mark`, behind |
| 98 | /// the `high_mark` ladder. Published because the id alone does not say it: |
| 99 | /// deliberately not the school's grade bands, which are renameable labels. |
| 100 | #[schema(example = 90)] |
| 101 | high_mark_min: i64, |
| 102 | } |
| 103 | |
| 104 | /// One badge in the auto-earned catalog. The id is the whole contract: a |
| 105 | /// profile hands back ids and nothing else, and the label and icon behind one |
| 106 | /// live in the client, exactly as they do for a role or a course kind. |
| 107 | #[derive(Serialize, ToSchema)] |
| 108 | struct BadgeLimit { |
| 109 | /// Stable id, never reused for another meaning. A retired badge simply |
| 110 | /// stops appearing here. |
| 111 | #[schema(example = "homework_submitted_10")] |
| 112 | id: &'static str, |
| 113 | /// The lifetime counter it reads — badges sharing one form a ladder. An |
| 114 | /// API-side name, not the column it is stored in, so storage can be |
| 115 | /// renamed without moving this contract. |
| 116 | #[schema(example = "homework_submitted")] |
| 117 | stat: &'static str, |
| 118 | /// Counter value that earns it. Awards are permanent: a counter that later |
| 119 | /// falls back below this does not take the badge away. |
| 120 | #[schema(example = 10)] |
| 121 | threshold: i64, |
| 122 | } |
| 123 | |
| 124 | /// Note bodies and their attachments. |
| 125 | #[derive(Serialize, ToSchema)] |
| 126 | struct NoteLimits { |
| 127 | max_title_len: usize, |
| 128 | max_content_len: usize, |
| 129 | /// How many files one note may carry. |
| 130 | max_files: usize, |
| 131 | /// How many files one course note may carry. |
| 132 | max_course_note_files: usize, |
| 133 | } |
| 134 | |
| 135 | /// Uploaded-file metadata, shared by every upload route. |
| 136 | #[derive(Serialize, ToSchema)] |
| 137 | struct FileLimits { |
| 138 | max_name_len: usize, |
| 139 | max_content_type_len: usize, |
| 140 | /// Inclusive range a manager may set `max_file_bytes` to in `/settings`; |
| 141 | /// the live value is on `GET /settings`, this is the hard ceiling above it. |
| 142 | min_max_file_bytes: i64, |
| 143 | max_max_file_bytes: i64, |
| 144 | default_max_file_bytes: i64, |
| 145 | /// Content types an inline image (question illustrations, option pictures, |
| 146 | /// pool photos, answer drawings) may declare. Raster only — no SVG. |
| 147 | #[schema(example = json!(["image/png", "image/jpeg", "image/webp", "image/gif"]))] |
| 148 | image_content_types: Vec<&'static str>, |
| 149 | } |
| 150 | |
| 151 | /// One-to-one messages. |
| 152 | #[derive(Serialize, ToSchema)] |
| 153 | struct MessageLimits { |
| 154 | max_subject_len: usize, |
| 155 | max_body_len: usize, |
| 156 | /// The sender's optional free-text badge. |
| 157 | max_label_len: usize, |
| 158 | } |
| 159 | |
| 160 | /// Events and their registration lists. |
| 161 | #[derive(Serialize, ToSchema)] |
| 162 | struct EventLimits { |
| 163 | max_title_len: usize, |
| 164 | max_description_len: usize, |
| 165 | } |
| 166 | |
| 167 | /// Courses, their curriculum subjects, lesson sessions, and the classes that |
| 168 | /// bulk-enroll into them. |
| 169 | #[derive(Serialize, ToSchema)] |
| 170 | struct CourseLimits { |
| 171 | max_title_len: usize, |
| 172 | max_description_len: usize, |
| 173 | /// The only accepted `kind` values. Only `course` is taught through class |
| 174 | /// instances (exams, sessions, homework); `study` (supervised study) and |
| 175 | /// `club` are joined school-wide. |
| 176 | #[schema(example = json!(["course", "study", "club"]))] |
| 177 | kinds: Vec<&'static str>, |
| 178 | max_subject_name_len: usize, |
| 179 | max_subject_description_len: usize, |
| 180 | max_session_topic_len: usize, |
| 181 | max_term_name_len: usize, |
| 182 | max_class_name_len: usize, |
| 183 | /// A class's optional free-text grade label ("9", "10-A"). |
| 184 | max_class_grade_len: usize, |
| 185 | /// Students one class may hold. Removing one frees a place. |
| 186 | max_class_members: i64, |
| 187 | /// Instances one class may carry. Detaching one frees a place. |
| 188 | max_class_courses: i64, |
| 189 | /// Inclusive bounds for an instance's weekly lesson hours (`ders_saati`). |
| 190 | min_ders_saati: i64, |
| 191 | max_ders_saati: i64, |
| 192 | /// An academic year's name (`POST /academic-years`). |
| 193 | max_academic_year_name_len: usize, |
| 194 | } |
| 195 | |
| 196 | /// Exams, their questions, answers, and marks. |
| 197 | #[derive(Serialize, ToSchema)] |
| 198 | struct ExamLimits { |
| 199 | max_title_len: usize, |
| 200 | max_description_len: usize, |
| 201 | /// The only accepted `mode` values; an exam with no mode is offline-graded. |
| 202 | #[schema(example = json!(["sync", "async", "open"]))] |
| 203 | modes: Vec<&'static str>, |
| 204 | /// Inclusive bounds for `duration_ms` (1 minute – 24 hours). |
| 205 | min_duration_ms: i64, |
| 206 | max_duration_ms: i64, |
| 207 | /// Upper bound for `max_attempts`; `unlimited_attempts` (0) means no limit. |
| 208 | max_attempts: i64, |
| 209 | unlimited_attempts: i64, |
| 210 | /// The only accepted question `kind` values. |
| 211 | #[schema(example = json!(["choice", "text"]))] |
| 212 | question_kinds: Vec<&'static str>, |
| 213 | max_question_text_len: usize, |
| 214 | min_question_points: i64, |
| 215 | max_question_points: i64, |
| 216 | min_question_choices: usize, |
| 217 | max_question_choices: usize, |
| 218 | max_choice_text_len: usize, |
| 219 | max_answer_text_len: usize, |
| 220 | min_mark: i64, |
| 221 | max_mark: i64, |
| 222 | /// Cadence of the exam-room WebSocket's `state` frames, seconds. |
| 223 | ws_tick_secs: u64, |
| 224 | /// Ceiling on a `question_id` sent over the exam-room WebSocket. |
| 225 | ws_max_question_id_len: usize, |
| 226 | } |
| 227 | |
| 228 | /// Homework assignments and submissions. |
| 229 | #[derive(Serialize, ToSchema)] |
| 230 | struct HomeworkLimits { |
| 231 | max_title_len: usize, |
| 232 | max_description_len: usize, |
| 233 | /// A submission's optional free-text note. |
| 234 | max_text_len: usize, |
| 235 | max_files_per_submission: usize, |
| 236 | /// How many students an explicit `assigned` subset may name. |
| 237 | max_assigned: usize, |
| 238 | /// The only accepted teacher-set `status` values. |
| 239 | #[schema(example = json!(["done", "incomplete", "missing"]))] |
| 240 | statuses: Vec<&'static str>, |
| 241 | } |
| 242 | |
| 243 | /// The school question pool and its solutions. |
| 244 | #[derive(Serialize, ToSchema)] |
| 245 | struct QuestionPoolLimits { |
| 246 | max_title_len: usize, |
| 247 | max_body_len: usize, |
| 248 | max_solution_body_len: usize, |
| 249 | } |
| 250 | |
| 251 | /// Teacher availability slots and the bookings on them. |
| 252 | #[derive(Serialize, ToSchema)] |
| 253 | struct AppointmentLimits { |
| 254 | /// A published slot's optional note. |
| 255 | max_note_len: usize, |
| 256 | /// The requester's required reason. |
| 257 | max_reason_len: usize, |
| 258 | /// How many slots one weekly-repeating publish may expand into. |
| 259 | max_slot_occurrences: usize, |
| 260 | } |
| 261 | |
| 262 | /// The food program: menus and their dishes, dietary profiles, bookings and |
| 263 | /// the meal ledger. The school's own lists (`meal_slots`, `dietary_tags`) and |
| 264 | /// its booking cutoff are on `GET /settings`; what stands here is the fixed |
| 265 | /// range those must stay inside. |
| 266 | #[derive(Serialize, ToSchema)] |
| 267 | struct MealLimits { |
| 268 | max_dish_name_len: usize, |
| 269 | max_dish_description_len: usize, |
| 270 | /// How many dishes one menu may list. |
| 271 | max_dishes_per_menu: usize, |
| 272 | /// Dietary tags one dish may carry. |
| 273 | max_dish_tags: usize, |
| 274 | /// Upper bound for a menu's optional `capacity`; absent = uncapped. |
| 275 | max_menu_capacity: i64, |
| 276 | /// How many times one student's seat on one menu may be taken — the first |
| 277 | /// booking plus the re-bookings after a cancel. Past it, `POST |
| 278 | /// /meals/menus/{id}/bookings` is a `409`: every cycle writes two |
| 279 | /// permanent ledger lines, so this bounds the statement, not the mind |
| 280 | /// changing. Only the canteen can free such a seat afterwards. |
| 281 | max_booking_attempts: i64, |
| 282 | /// Dietary tags one student's profile may carry, and its kitchen note. |
| 283 | max_dietary_tags: usize, |
| 284 | max_dietary_note_len: usize, |
| 285 | /// Money is **minor units** as an integer everywhere — never a |
| 286 | /// decimal, never a float. These cap one dish and one ledger line. |
| 287 | max_dish_price_minor: i64, |
| 288 | max_ledger_amount_minor: i64, |
| 289 | /// How the money moved, and why. |
| 290 | max_ledger_method_len: usize, |
| 291 | max_ledger_note_len: usize, |
| 292 | /// Ceiling on the `/settings` knob that closes booking (and cancelling) |
| 293 | /// ahead of a meal, minutes; the live value is on `GET /settings`. |
| 294 | max_cancel_cutoff_minutes: i64, |
| 295 | /// Ceiling on a meal slot's `serving_minute` — minutes past midnight |
| 296 | /// **UTC** on the menu's date, which is what the cutoff counts back from. |
| 297 | /// `1439` = 23:59 UTC. There is no school timezone: staff enter UTC. |
| 298 | max_serving_minute: i64, |
| 299 | /// The only accepted booking `status` values. |
| 300 | #[schema(example = json!(["booked", "cancelled"]))] |
| 301 | booking_statuses: Vec<&'static str>, |
| 302 | /// The only accepted meal-attendance `status` values — deliberately not |
| 303 | /// the school's lesson attendance statuses. |
| 304 | #[schema(example = json!(["served", "missed"]))] |
| 305 | attendance_statuses: Vec<&'static str>, |
| 306 | /// The only accepted ledger `kind` values. |
| 307 | #[schema(example = json!(["charge", "credit", "reversal"]))] |
| 308 | ledger_kinds: Vec<&'static str>, |
| 309 | } |
| 310 | |
| 311 | /// School payments: fee plans, their assignment, and the payment ledger. Money |
| 312 | /// is **minor units** as an integer, and one ledger line is capped by |
| 313 | /// the same `max_ledger_amount_minor` the meal ledger publishes. |
| 314 | #[derive(Serialize, ToSchema)] |
| 315 | struct PaymentLimits { |
| 316 | max_plan_name_len: usize, |
| 317 | /// How many installments one fee plan may schedule. |
| 318 | max_plan_installments: usize, |
| 319 | /// How many students one bulk assignment may name. |
| 320 | max_assign_students: usize, |
| 321 | /// How many charge lines one bulk assignment may append — the students it |
| 322 | /// names times the plan's installments. A request past this is a `400` |
| 323 | /// telling the caller to split the batch; nothing is written. |
| 324 | max_assign_writes: usize, |
| 325 | /// How many lines may be applied to one ledger line: the payments under a |
| 326 | /// charge, the refunds under a payment, and the reversals among them. Past |
| 327 | /// it, a further payment or refund against that line is a `409`. |
| 328 | max_applied_lines: usize, |
| 329 | /// Ceiling on the optional `request_key` that makes a credit or a refund |
| 330 | /// retry-safe; charset `[A-Za-z0-9-]` (no `_`), at least one character. |
| 331 | max_request_key_len: usize, |
| 332 | /// The only accepted payment-ledger `kind` values. |
| 333 | #[schema(example = json!(["charge", "credit", "reversal", "refund"]))] |
| 334 | ledger_kinds: Vec<&'static str>, |
| 335 | } |
| 336 | |
| 337 | /// The AI chatbot. Each range bounds the matching `/settings` knob; the live |
| 338 | /// values are on `GET /settings`. |
| 339 | #[derive(Serialize, ToSchema)] |
| 340 | struct ChatbotLimits { |
| 341 | /// Hard ceiling on a message, above which no school setting can reach. |
| 342 | max_message_len: usize, |
| 343 | /// A thread's display name. Threads are named by hand or left untitled — |
| 344 | /// nothing auto-titles one. |
| 345 | max_thread_title_len: usize, |
| 346 | min_max_message_len: i64, |
| 347 | max_max_message_len: i64, |
| 348 | default_max_message_len: i64, |
| 349 | min_history_turns: i64, |
| 350 | max_history_turns: i64, |
| 351 | default_history_turns: i64, |
| 352 | min_max_threads: i64, |
| 353 | max_max_threads: i64, |
| 354 | default_max_threads: i64, |
| 355 | } |
| 356 | |
| 357 | /// The RAG nest: a tier of its own — every message spends a retrieval over the |
| 358 | /// whole course-note corpus before it generates anything — plus the school |
| 359 | /// knobs it reuses from the chatbot. One `rag.chat` message is a chat message |
| 360 | /// in every way the newtypes check, so message length, thread titles, history |
| 361 | /// depth and the thread cap come from the same `/settings` fields; their fixed |
| 362 | /// ranges are published here a second time so a RAG client need not read the |
| 363 | /// chatbot group to bound its input. |
| 364 | #[derive(Serialize, ToSchema)] |
| 365 | struct RagLimits { |
| 366 | /// Hard ceiling on the `(class, course)` scope pairs one ask may carry. The |
| 367 | /// corpus is routed by the pair, so this bounds what one question can ask |
| 368 | /// a retrieval to sweep. |
| 369 | max_scope_pairs: usize, |
| 370 | /// Hard ceiling on the practice questions one `POST /rag/questions` call |
| 371 | /// may ask for. The caller waits on the generation, so this bounds one |
| 372 | /// model round trip rather than a corpus sweep. |
| 373 | max_questions: u32, |
| 374 | /// Hard ceiling on the citations one answer may store in its message row. |
| 375 | max_citations: usize, |
| 376 | /// Pages one citation may span. A citation names a page range, and an |
| 377 | /// answer that could name every page of every document would be a copy of |
| 378 | /// the corpus, not a citation. |
| 379 | max_citation_pages: usize, |
| 380 | /// The chatbot's message bound, reused. |
| 381 | max_message_len: usize, |
| 382 | /// The chatbot's thread-title bound, reused. |
| 383 | max_thread_title_len: usize, |
| 384 | /// The school-owned message-length setting's fixed range, reused. |
| 385 | min_max_message_len: i64, |
| 386 | max_max_message_len: i64, |
| 387 | default_max_message_len: i64, |
| 388 | /// The school-owned history-depth setting's fixed range, reused. |
| 389 | min_history_turns: i64, |
| 390 | max_history_turns: i64, |
| 391 | default_history_turns: i64, |
| 392 | /// The school-owned thread-cap setting's fixed range, reused. |
| 393 | min_max_threads: i64, |
| 394 | max_max_threads: i64, |
| 395 | default_max_threads: i64, |
| 396 | } |
| 397 | |
| 398 | /// What a finished pomodoro stint must be to *count* — to move the lifetime |
| 399 | /// counters the badges and the study streak read. Not a validation bound: |
| 400 | /// `POST /pomodoro/finish` never refuses a stint over these, it records it and |
| 401 | /// answers `counted: false`. Published so a client can say what a stint is |
| 402 | /// worth (and what is left of today's quota) instead of guessing why a badge |
| 403 | /// did not arrive. |
| 404 | #[derive(Serialize, ToSchema)] |
| 405 | struct PomodoroLimits { |
| 406 | /// Shortest stint that counts, milliseconds — a stint of exactly this long |
| 407 | /// counts. Well under a conventional 25-minute pomodoro: breaking off early |
| 408 | /// is still studying. Its job is only to price a scripted round-trip out. |
| 409 | #[schema(example = 300000)] |
| 410 | min_counted_ms: i64, |
| 411 | /// How many stints count per UTC day. Every further one that day is |
| 412 | /// recorded and listed as usual, and counts nothing. |
| 413 | #[schema(example = 16)] |
| 414 | max_counted_per_day: i64, |
| 415 | /// How long a stint's optional student-given label may be — the student's |
| 416 | /// own name for what the stint is for, given at `POST /pomodoro/start`. |
| 417 | max_label_len: usize, |
| 418 | } |
| 419 | |
| 420 | /// A collaborative whiteboard: its title, its roster, and the two growth caps |
| 421 | /// that decide when a canvas must be cleared and when a board is finished. |
| 422 | #[derive(Serialize, ToSchema)] |
| 423 | struct BoardLimits { |
| 424 | max_title_len: usize, |
| 425 | /// People the creator may name onto one board. Every participant draws, so |
| 426 | /// this also bounds a room's writer count. |
| 427 | max_participants: usize, |
| 428 | /// Ceiling on one stroke's serialized payload, bytes. |
| 429 | max_stroke_payload_len: usize, |
| 430 | /// The *live* canvas cap: strokes since the last clear. Hitting it is |
| 431 | /// recoverable — the creator clears, the canvas empties, the history is |
| 432 | /// kept, and drawing resumes. |
| 433 | max_epoch_strokes: i64, |
| 434 | /// The *lifetime* storage cap, which never resets because a clear keeps its |
| 435 | /// history. Hitting it makes the board permanently read-only: it stays |
| 436 | /// fully readable and replayable, and a new board must be opened. |
| 437 | max_board_strokes: i64, |
| 438 | /// Boards one creating user may hold. Deleting a board frees a seat. |
| 439 | max_boards_per_creator: i64, |
| 440 | /// The accepted stroke kinds. `clear` is a marker that ends an epoch, not a |
| 441 | /// deletion. |
| 442 | #[schema(example = json!(["stroke", "clear"]))] |
| 443 | stroke_kinds: Vec<&'static str>, |
| 444 | /// Cadence of the board-room WebSocket's keepalive ticks, seconds. |
| 445 | ws_tick_secs: u64, |
| 446 | /// Ceiling on a `board_id` sent over the board-room WebSocket. |
| 447 | ws_max_board_id_len: usize, |
| 448 | } |
| 449 | |
| 450 | /// Bounds on the school-editable lists in `PATCH /settings`. |
| 451 | #[derive(Serialize, ToSchema)] |
| 452 | struct SettingsLimits { |
| 453 | /// Entry count for the exam-kind and attendance-status lists. |
| 454 | max_list_len: usize, |
| 455 | /// Per-entry characters in those lists. |
| 456 | max_item_len: usize, |
| 457 | min_exam_kind_weight: i64, |
| 458 | max_exam_kind_weight: i64, |
| 459 | max_grade_bands: usize, |
| 460 | max_grade_label_len: usize, |
| 461 | /// Attendance statuses no school may remove. |
| 462 | #[schema(example = json!(["present", "absent", "late", "excused"]))] |
| 463 | required_attendance_statuses: Vec<&'static str>, |
| 464 | } |
| 465 | |
| 466 | /// The request budgets a caller is held to before it starts getting `429`s |
| 467 | /// with a `Retry-After`. Unlike everything else here these are **deployment** |
| 468 | /// configuration, not compile-time constants — they come from environment |
| 469 | /// variables, so the numbers are this server's live values rather than fixed |
| 470 | /// ones. `0` means the tier is switched off. |
| 471 | #[derive(Serialize, ToSchema)] |
| 472 | struct RateLimits { |
| 473 | /// Fixed window every tier is counted over, seconds. |
| 474 | window_secs: u64, |
| 475 | /// Per-IP budget for `/auth/login` and `/auth/register`. |
| 476 | #[schema(example = 10)] |
| 477 | auth_per_minute: u32, |
| 478 | /// Per-IP budget across the whole API, this endpoint included. |
| 479 | #[schema(example = 300)] |
| 480 | api_per_minute: u32, |
| 481 | /// Per-*user* budget for sending chatbot messages (keyed by account, not |
| 482 | /// address, so it survives a changing IP). |
| 483 | #[schema(example = 20)] |
| 484 | chatbot_per_minute: u32, |
| 485 | /// Per-*user* budget for sending RAG messages, keyed the same way. |
| 486 | #[schema(example = 6)] |
| 487 | rag_per_minute: u32, |
| 488 | } |
| 489 | |
| 490 | /// Rules that hold across every endpoint. |
| 491 | #[derive(Serialize, ToSchema)] |
| 492 | struct RequestLimits { |
| 493 | /// Upper bound on a list endpoint's `?limit=`. Omitting it returns |
| 494 | /// everything remaining. |
| 495 | max_page_limit: i64, |
| 496 | /// How far in the past a submitted schedule instant may lie before it is |
| 497 | /// refused as backdated — the grace that absorbs clock skew. |
| 498 | schedule_past_grace_ms: i64, |
| 499 | /// Ceiling on how long one request may take before it is aborted. |
| 500 | request_timeout_secs: u64, |
| 501 | /// Longest `x-request-id` header the server keeps. A longer one — or one |
| 502 | /// carrying anything outside `[A-Za-z0-9._-]` — is replaced by a |
| 503 | /// server-minted UUID, so the caller loses correlation on its own id. |
| 504 | max_request_id_len: usize, |
| 505 | } |
| 506 | |
| 507 | /// Every fixed limit the API enforces, grouped by the resource it applies to. |
| 508 | #[derive(Serialize, ToSchema)] |
| 509 | struct LimitsResponse { |
| 510 | user: UserLimits, |
| 511 | badges: BadgeLimits, |
| 512 | note: NoteLimits, |
| 513 | file: FileLimits, |
| 514 | message: MessageLimits, |
| 515 | event: EventLimits, |
| 516 | course: CourseLimits, |
| 517 | exam: ExamLimits, |
| 518 | homework: HomeworkLimits, |
| 519 | question_pool: QuestionPoolLimits, |
| 520 | appointment: AppointmentLimits, |
| 521 | meal: MealLimits, |
| 522 | payment: PaymentLimits, |
| 523 | chatbot: ChatbotLimits, |
| 524 | rag: RagLimits, |
| 525 | pomodoro: PomodoroLimits, |
| 526 | board: BoardLimits, |
| 527 | settings: SettingsLimits, |
| 528 | request: RequestLimits, |
| 529 | rate: RateLimits, |
| 530 | } |
| 531 | |
| 532 | impl LimitsResponse { |
| 533 | fn new(st: &AppState) -> Self { |
| 534 | Self { |
| 535 | user: UserLimits { |
| 536 | min_slug_len: MIN_SLUG_LEN, |
| 537 | max_slug_len: MAX_SLUG_LEN, |
| 538 | reserved_slugs: crate::tenant::RESERVED_SLUGS.to_vec(), |
| 539 | max_school_name_len: MAX_SCHOOL_NAME_LEN, |
| 540 | min_username_len: MIN_USERNAME_LEN, |
| 541 | max_username_len: MAX_USERNAME_LEN, |
| 542 | username_separators: USERNAME_SEPARATORS.iter().map(char::to_string).collect(), |
| 543 | reserved_usernames: RESERVED_USERNAMES.to_vec(), |
| 544 | min_password_len: MIN_PASSWORD_LEN, |
| 545 | max_password_len: MAX_PASSWORD_LEN, |
| 546 | max_name_len: MAX_NAME_LEN, |
| 547 | max_display_name_len: MAX_DISPLAY_NAME_LEN, |
| 548 | max_bio_len: MAX_BIO_LEN, |
| 549 | max_profile_courses: MAX_PROFILE_COURSES, |
| 550 | max_profile_classes: MAX_PROFILE_CLASSES, |
| 551 | max_email_len: MAX_EMAIL_LEN, |
| 552 | min_phone_digits: MIN_PHONE_DIGITS, |
| 553 | max_phone_digits: MAX_PHONE_DIGITS, |
| 554 | roles: ROLES.iter().map(|role| role.as_str()).collect(), |
| 555 | themes: THEMES.iter().map(|theme| theme.as_str()).collect(), |
| 556 | languages: LANGUAGES.iter().map(|language| language.as_str()).collect(), |
| 557 | palette_color_pattern: PALETTE_COLOR_PATTERN, |
| 558 | palette_color_len: PALETTE_COLOR_LEN, |
| 559 | session_duration_days: SESSION_DURATION_DAYS, |
| 560 | }, |
| 561 | badges: BadgeLimits { |
| 562 | catalog: BADGES |
| 563 | .iter() |
| 564 | .map(|(id, stat, threshold)| BadgeLimit { |
| 565 | id, |
| 566 | stat: stat.as_str(), |
| 567 | threshold: *threshold, |
| 568 | }) |
| 569 | .collect(), |
| 570 | high_mark_min: HIGH_MARK_MIN, |
| 571 | }, |
| 572 | note: NoteLimits { |
| 573 | max_title_len: MAX_NOTE_TITLE_LEN, |
| 574 | max_content_len: MAX_NOTE_CONTENT_LEN, |
| 575 | max_files: MAX_NOTE_FILES, |
| 576 | max_course_note_files: MAX_COURSE_NOTE_FILES, |
| 577 | }, |
| 578 | file: FileLimits { |
| 579 | max_name_len: MAX_FILE_NAME_LEN, |
| 580 | max_content_type_len: MAX_FILE_CONTENT_TYPE_LEN, |
| 581 | min_max_file_bytes: MIN_MAX_FILE_BYTES, |
| 582 | max_max_file_bytes: MAX_MAX_FILE_BYTES, |
| 583 | default_max_file_bytes: DEFAULT_MAX_FILE_BYTES, |
| 584 | image_content_types: QUESTION_IMAGE_CONTENT_TYPES.to_vec(), |
| 585 | }, |
| 586 | message: MessageLimits { |
| 587 | max_subject_len: MAX_MESSAGE_SUBJECT_LEN, |
| 588 | max_body_len: MAX_MESSAGE_BODY_LEN, |
| 589 | max_label_len: MAX_MESSAGE_LABEL_LEN, |
| 590 | }, |
| 591 | event: EventLimits { |
| 592 | max_title_len: MAX_EVENT_TITLE_LEN, |
| 593 | max_description_len: MAX_EVENT_DESCRIPTION_LEN, |
| 594 | }, |
| 595 | course: CourseLimits { |
| 596 | max_title_len: MAX_COURSE_TITLE_LEN, |
| 597 | max_description_len: MAX_COURSE_DESCRIPTION_LEN, |
| 598 | kinds: COURSE_KINDS.to_vec(), |
| 599 | max_subject_name_len: MAX_SUBJECT_NAME_LEN, |
| 600 | max_subject_description_len: MAX_SUBJECT_DESCRIPTION_LEN, |
| 601 | max_session_topic_len: MAX_SESSION_TOPIC_LEN, |
| 602 | max_term_name_len: MAX_TERM_NAME_LEN, |
| 603 | max_class_name_len: MAX_CLASS_NAME_LEN, |
| 604 | max_class_grade_len: MAX_CLASS_GRADE_LEN, |
| 605 | max_class_members: MAX_CLASS_MEMBERS, |
| 606 | max_class_courses: MAX_CLASS_COURSES, |
| 607 | min_ders_saati: MIN_DERS_SAATI, |
| 608 | max_ders_saati: MAX_DERS_SAATI, |
| 609 | max_academic_year_name_len: MAX_ACADEMIC_YEAR_NAME_LEN, |
| 610 | }, |
| 611 | exam: ExamLimits { |
| 612 | max_title_len: MAX_EXAM_TITLE_LEN, |
| 613 | max_description_len: MAX_EXAM_DESCRIPTION_LEN, |
| 614 | modes: EXAM_MODES.to_vec(), |
| 615 | min_duration_ms: MIN_EXAM_DURATION_MS, |
| 616 | max_duration_ms: MAX_EXAM_DURATION_MS, |
| 617 | max_attempts: MAX_EXAM_ATTEMPTS, |
| 618 | unlimited_attempts: UNLIMITED_EXAM_ATTEMPTS, |
| 619 | question_kinds: QUESTION_KINDS.to_vec(), |
| 620 | max_question_text_len: MAX_QUESTION_TEXT_LEN, |
| 621 | min_question_points: MIN_QUESTION_POINTS, |
| 622 | max_question_points: MAX_QUESTION_POINTS, |
| 623 | min_question_choices: MIN_QUESTION_CHOICES, |
| 624 | max_question_choices: MAX_QUESTION_CHOICES, |
| 625 | max_choice_text_len: MAX_CHOICE_TEXT_LEN, |
| 626 | max_answer_text_len: MAX_ANSWER_TEXT_LEN, |
| 627 | min_mark: MIN_MARK, |
| 628 | max_mark: MAX_MARK, |
| 629 | ws_tick_secs: EXAM_WS_TICK_SECS, |
| 630 | ws_max_question_id_len: MAX_QUESTION_ID_LEN, |
| 631 | }, |
| 632 | homework: HomeworkLimits { |
| 633 | max_title_len: MAX_HOMEWORK_TITLE_LEN, |
| 634 | max_description_len: MAX_HOMEWORK_DESCRIPTION_LEN, |
| 635 | max_text_len: MAX_HOMEWORK_TEXT_LEN, |
| 636 | max_files_per_submission: MAX_HOMEWORK_FILES_PER_SUBMISSION, |
| 637 | max_assigned: MAX_HOMEWORK_ASSIGNED, |
| 638 | statuses: HOMEWORK_STATUSES.to_vec(), |
| 639 | }, |
| 640 | question_pool: QuestionPoolLimits { |
| 641 | max_title_len: MAX_POOL_QUESTION_TITLE_LEN, |
| 642 | max_body_len: MAX_POOL_QUESTION_BODY_LEN, |
| 643 | max_solution_body_len: MAX_SOLUTION_BODY_LEN, |
| 644 | }, |
| 645 | appointment: AppointmentLimits { |
| 646 | max_note_len: MAX_APPOINTMENT_NOTE_LEN, |
| 647 | max_reason_len: MAX_APPOINTMENT_REASON_LEN, |
| 648 | max_slot_occurrences: MAX_SLOT_OCCURRENCES, |
| 649 | }, |
| 650 | meal: MealLimits { |
| 651 | max_dish_name_len: MAX_DISH_NAME_LEN, |
| 652 | max_dish_description_len: MAX_DISH_DESCRIPTION_LEN, |
| 653 | max_dishes_per_menu: MAX_DISHES_PER_MENU, |
| 654 | max_dish_tags: MAX_DISH_TAGS, |
| 655 | max_menu_capacity: MAX_MENU_CAPACITY, |
| 656 | max_booking_attempts: MAX_MEAL_BOOKING_ATTEMPTS, |
| 657 | max_dietary_tags: MAX_DIETARY_TAGS, |
| 658 | max_dietary_note_len: MAX_DIETARY_NOTE_LEN, |
| 659 | max_dish_price_minor: MAX_DISH_PRICE_MINOR, |
| 660 | max_ledger_amount_minor: MAX_LEDGER_AMOUNT_MINOR, |
| 661 | max_ledger_method_len: MAX_LEDGER_METHOD_LEN, |
| 662 | max_ledger_note_len: MAX_LEDGER_NOTE_LEN, |
| 663 | max_cancel_cutoff_minutes: MAX_MEAL_CANCEL_CUTOFF_MINUTES, |
| 664 | max_serving_minute: MAX_MEAL_SERVING_MINUTE, |
| 665 | booking_statuses: MEAL_BOOKING_STATUSES.to_vec(), |
| 666 | attendance_statuses: MEAL_ATTENDANCE_STATUSES.to_vec(), |
| 667 | ledger_kinds: MEAL_LEDGER_KINDS.to_vec(), |
| 668 | }, |
| 669 | payment: PaymentLimits { |
| 670 | max_plan_name_len: MAX_FEE_PLAN_NAME_LEN, |
| 671 | max_plan_installments: MAX_FEE_PLAN_INSTALLMENTS, |
| 672 | max_assign_students: MAX_FEE_PLAN_ASSIGN_STUDENTS, |
| 673 | max_assign_writes: MAX_FEE_PLAN_ASSIGN_WRITES, |
| 674 | max_applied_lines: MAX_LEDGER_APPLIED_LINES, |
| 675 | max_request_key_len: MAX_PAYMENT_REQUEST_KEY_LEN, |
| 676 | ledger_kinds: PAYMENT_LEDGER_KINDS.to_vec(), |
| 677 | }, |
| 678 | chatbot: ChatbotLimits { |
| 679 | max_message_len: MAX_CHATBOT_MESSAGE_LEN, |
| 680 | max_thread_title_len: MAX_CHATBOT_THREAD_TITLE_LEN, |
| 681 | min_max_message_len: MIN_MAX_CHATBOT_MESSAGE_LEN, |
| 682 | max_max_message_len: MAX_MAX_CHATBOT_MESSAGE_LEN, |
| 683 | default_max_message_len: DEFAULT_MAX_CHATBOT_MESSAGE_LEN, |
| 684 | min_history_turns: MIN_CHATBOT_HISTORY_TURNS, |
| 685 | max_history_turns: MAX_CHATBOT_HISTORY_TURNS, |
| 686 | default_history_turns: DEFAULT_CHATBOT_HISTORY_TURNS, |
| 687 | min_max_threads: MIN_MAX_CHATBOT_THREADS, |
| 688 | max_max_threads: MAX_MAX_CHATBOT_THREADS, |
| 689 | default_max_threads: DEFAULT_MAX_CHATBOT_THREADS, |
| 690 | }, |
| 691 | rag: RagLimits { |
| 692 | max_scope_pairs: MAX_RAG_SCOPE_PAIRS, |
| 693 | max_questions: MAX_RAG_QUESTIONS, |
| 694 | max_citations: MAX_RAG_CITATIONS, |
| 695 | max_citation_pages: MAX_RAG_CITATION_PAGES, |
| 696 | max_message_len: MAX_CHATBOT_MESSAGE_LEN, |
| 697 | max_thread_title_len: MAX_CHATBOT_THREAD_TITLE_LEN, |
| 698 | min_max_message_len: MIN_MAX_CHATBOT_MESSAGE_LEN, |
| 699 | max_max_message_len: MAX_MAX_CHATBOT_MESSAGE_LEN, |
| 700 | default_max_message_len: DEFAULT_MAX_CHATBOT_MESSAGE_LEN, |
| 701 | min_history_turns: MIN_CHATBOT_HISTORY_TURNS, |
| 702 | max_history_turns: MAX_CHATBOT_HISTORY_TURNS, |
| 703 | default_history_turns: DEFAULT_CHATBOT_HISTORY_TURNS, |
| 704 | min_max_threads: MIN_MAX_CHATBOT_THREADS, |
| 705 | max_max_threads: MAX_MAX_CHATBOT_THREADS, |
| 706 | default_max_threads: DEFAULT_MAX_CHATBOT_THREADS, |
| 707 | }, |
| 708 | pomodoro: PomodoroLimits { |
| 709 | min_counted_ms: MIN_COUNTED_POMODORO_MS, |
| 710 | max_counted_per_day: MAX_COUNTED_POMODORO_PER_DAY, |
| 711 | max_label_len: MAX_POMODORO_LABEL_LEN, |
| 712 | }, |
| 713 | board: BoardLimits { |
| 714 | max_title_len: MAX_BOARD_TITLE_LEN, |
| 715 | max_participants: MAX_BOARD_PARTICIPANTS, |
| 716 | max_stroke_payload_len: MAX_STROKE_PAYLOAD_LEN, |
| 717 | max_epoch_strokes: MAX_EPOCH_STROKES, |
| 718 | max_board_strokes: MAX_BOARD_STROKES, |
| 719 | max_boards_per_creator: MAX_BOARDS_PER_CREATOR, |
| 720 | stroke_kinds: BOARD_STROKE_KINDS.to_vec(), |
| 721 | ws_tick_secs: BOARD_WS_TICK_SECS, |
| 722 | ws_max_board_id_len: MAX_BOARD_ID_LEN, |
| 723 | }, |
| 724 | settings: SettingsLimits { |
| 725 | max_list_len: MAX_SETTINGS_LIST_LEN, |
| 726 | max_item_len: MAX_SETTINGS_ITEM_LEN, |
| 727 | min_exam_kind_weight: MIN_EXAM_KIND_WEIGHT, |
| 728 | max_exam_kind_weight: MAX_EXAM_KIND_WEIGHT, |
| 729 | max_grade_bands: MAX_GRADE_BANDS, |
| 730 | max_grade_label_len: MAX_GRADE_LABEL_LEN, |
| 731 | required_attendance_statuses: DEFAULT_ATTENDANCE_STATUSES.to_vec(), |
| 732 | }, |
| 733 | rate: RateLimits { |
| 734 | window_secs: 60, |
| 735 | auth_per_minute: st.rate_limit.auth_per_minute, |
| 736 | api_per_minute: st.rate_limit.api_per_minute, |
| 737 | chatbot_per_minute: st.chatbot_limit.max_per_window(), |
| 738 | rag_per_minute: st.rag_limit.max_per_window(), |
| 739 | }, |
| 740 | request: RequestLimits { |
| 741 | max_page_limit: MAX_PAGE_LIMIT, |
| 742 | schedule_past_grace_ms: SCHEDULE_PAST_GRACE_MS, |
| 743 | request_timeout_secs: REQUEST_TIMEOUT_SECS, |
| 744 | max_request_id_len: MAX_REQUEST_ID_LEN, |
| 745 | }, |
| 746 | } |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | /// Every fixed validation bound the API enforces. Unauthenticated: the |
| 751 | /// registration and login forms need the username and password bounds before a |
| 752 | /// session exists, and none of these values are secrets — they are the same |
| 753 | /// rules a 400 would spell out. |
| 754 | /// |
| 755 | /// The values are compile-time constants, so the response only ever changes |
| 756 | /// with a deploy — cache it for the session rather than re-fetching per form. |
| 757 | /// School-adjustable policy (exam kinds, attendance statuses, grade bands, the |
| 758 | /// live `max_file_bytes` and chatbot knobs) is on `GET /settings` instead; what |
| 759 | /// appears here for those is the fixed range a manager may set them within. |
| 760 | #[utoipa::path( |
| 761 | get, |
| 762 | path = "/limits", |
| 763 | tag = "meta", |
| 764 | responses((status = 200, description = "Every fixed validation bound", body = LimitsResponse)), |
| 765 | )] |
| 766 | async fn get_limits(State(st): State<AppState>) -> Json<LimitsResponse> { |
| 767 | Json(LimitsResponse::new(&st)) |
| 768 | } |