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.rs105 lines, 3.1 KiBHistoryRawDownload
1//! A chatbot thread. It only groups turns and carries the ordering key the
2//! list view sorts on: `updated_at` moves on every new turn, so a user's
3//! threads list newest-activity-first. The turns themselves live in
4//! [`crate::db::chatbot_message`], and deleting a thread cascades them; the
5//! threads' own persistence lives in [`crate::db::chatbot_thread`].
6
7use crate::constant::MAX_CHATBOT_THREAD_TITLE_LEN;
8use crate::domain::monotonic_id::next_uuid;
9use crate::domain::timestamp::Timestamp;
10use crate::domain::user::UserId;
11use crate::error::ValidationError;
12use crate::validate::validate_required;
13
14#[derive(Debug, Clone, PartialEq, Eq, sqlx::Type)]
15#[sqlx(transparent)]
16pub struct ChatbotThreadId(uuid::Uuid);
17
18impl ChatbotThreadId {
19 /// Mints from the process-wide monotonic generator, not a plain random
20 /// UUID: threads sort `updated_at DESC, id DESC` and the id breaks the tie
21 /// between two threads last touched in the same millisecond,
22 /// and a random low half sorts arbitrarily inside one millisecond.
23 pub fn generate() -> Self {
24 Self(next_uuid())
25 }
26
27 /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that
28 /// cannot take the newtype. Static `query!` binds take `self` directly.
29 pub fn uuid(&self) -> uuid::Uuid {
30 self.0
31 }
32
33 /// Parses a wire key. A key that is not a UUID parses as the nil UUID,
34 /// which matches no row.
35 pub fn from_key(key: &str) -> Self {
36 Self(uuid::Uuid::parse_str(key).unwrap_or(uuid::Uuid::nil()))
37 }
38
39 pub fn key(&self) -> String {
40 self.0.to_string()
41 }
42}
43
44/// A user-chosen thread name. Required-and-bounded here; "untitled" is
45/// `Option<ChatbotThreadTitle>` on the row.
46#[derive(Debug, Clone, PartialEq, Eq, sqlx::Type)]
47#[sqlx(transparent)]
48pub struct ChatbotThreadTitle(String);
49
50impl ChatbotThreadTitle {
51 pub fn try_new(value: &str) -> Result<Self, ValidationError> {
52 validate_required("title", value, MAX_CHATBOT_THREAD_TITLE_LEN)?;
53 Ok(Self(value.to_string()))
54 }
55
56 pub fn as_str(&self) -> &str {
57 &self.0
58 }
59}
60
61#[derive(Debug, Clone, sqlx::FromRow)]
62pub struct ChatbotThread {
63 pub(crate) id: ChatbotThreadId,
64 pub(crate) user_id: UserId,
65 pub(crate) title: Option<ChatbotThreadTitle>,
66 pub(crate) created_at: Timestamp,
67 pub(crate) updated_at: Timestamp,
68}
69
70impl ChatbotThread {
71 pub fn get_id(&self) -> &ChatbotThreadId {
72 &self.id
73 }
74
75 pub fn get_user_id(&self) -> &UserId {
76 &self.user_id
77 }
78
79 pub fn get_title(&self) -> Option<&ChatbotThreadTitle> {
80 self.title.as_ref()
81 }
82
83 pub fn get_created_at(&self) -> Timestamp {
84 self.created_at
85 }
86
87 pub fn get_updated_at(&self) -> Timestamp {
88 self.updated_at
89 }
90}
91
92#[cfg(test)]
93mod tests {
94 use super::*;
95
96 #[tokio::test]
97 async fn title_is_required_and_bounded() {
98 assert_eq!(
99 ChatbotThreadTitle::try_new("Fizik").unwrap().as_str(),
100 "Fizik"
101 );
102 assert!(ChatbotThreadTitle::try_new(" ").is_err());
103 assert!(ChatbotThreadTitle::try_new(&"x".repeat(201)).is_err());
104 }
105}