forge
Explore
Log in

Don't you dare go Hollow

hezarfen_backend

Clone
★ 0

0 watching0 forksRust8.3 MiB6d68754Ahmet Kaan Gümüş2 months ago566 commits

chatbot_thread.rs59 lines, 2.2 KiBHistoryRawDownload
1//! Chatbot thread workflows: the create, rename and delete paths the
2//! chatbot surface drives, all scoped to the owning user — a foreign id
3//! reads as absent. The queries live in [`crate::db::chatbot_thread`].
4
5use crate::database::Database;
6use crate::db::chatbot_thread;
7use crate::domain::chatbot_thread::{ChatbotThread, ChatbotThreadId, ChatbotThreadTitle};
8use crate::domain::user::UserId;
9use crate::error::AppError;
10
11/// Start a thread unless `user` is already at the school's
12/// `max_chatbot_threads` (409 at the cap — the cap is storage protection,
13/// not a usage quota). The cap check and the row write are one atomic
14/// conditional write, so two requests racing the same user's last slot
15/// cannot both win.
16pub async fn create_capped(
17 db: &Database,
18 user: &UserId,
19 title: Option<ChatbotThreadTitle>,
20) -> Result<ChatbotThread, AppError> {
21 chatbot_thread::create_capped(db, user, title).await
22}
23
24/// The caller's own threads, most recently active first.
25pub async fn list_for_user(
26 db: &Database,
27 user: &UserId,
28 limit: Option<i64>,
29 offset: i64,
30) -> Result<(Vec<ChatbotThread>, i64), AppError> {
31 chatbot_thread::list_for_user(db, user, limit, offset).await
32}
33
34/// Read a thread only if `user` owns it — a foreign id reads as absent, so
35/// the web layer answers 404 rather than leaking that it exists.
36pub async fn read_for(
37 db: &Database,
38 id: &ChatbotThreadId,
39 user: &UserId,
40) -> Result<Option<ChatbotThread>, AppError> {
41 chatbot_thread::read_for(db, id, user).await
42}
43
44/// Rename the thread (`None` clears the name back to untitled), stamping
45/// the edit as activity so the thread moves to the top of the list.
46pub async fn rename(
47 db: &Database,
48 thread: &ChatbotThread,
49 title: Option<ChatbotThreadTitle>,
50) -> Result<ChatbotThread, AppError> {
51 chatbot_thread::rename(db, thread, title).await
52}
53
54/// Delete the thread and every turn in it — one transaction, so a crash
55/// can't orphan messages under a vanished thread — and give the owner's
56/// cap slot back in the same transaction.
57pub async fn delete(db: &Database, thread: ChatbotThread) -> Result<ChatbotThread, AppError> {
58 chatbot_thread::delete(db, thread).await
59}