From 4fab757f2daca174180927640c0e39c99f1f2937 Mon Sep 17 00:00:00 2001 From: dal Date: Wed, 5 Mar 2025 10:31:35 -0700 Subject: [PATCH] message ids --- .../handlers/src/chats/get_chat_handler.rs | 29 +++---- .../handlers/src/chats/post_chat_handler.rs | 87 ++++++++++--------- api/libs/handlers/src/chats/types.rs | 84 +++++++++++++++++- 3 files changed, 141 insertions(+), 59 deletions(-) diff --git a/api/libs/handlers/src/chats/get_chat_handler.rs b/api/libs/handlers/src/chats/get_chat_handler.rs index 737e12ad7..935466cb0 100644 --- a/api/libs/handlers/src/chats/get_chat_handler.rs +++ b/api/libs/handlers/src/chats/get_chat_handler.rs @@ -119,7 +119,7 @@ pub async fn get_chat_handler(chat_id: &Uuid, user_id: &Uuid) -> Result = messages .into_iter() .map(|msg| { let sender_avatar = msg @@ -148,15 +148,13 @@ pub async fn get_chat_handler(chat_id: &Uuid, user_id: &Uuid) -> Result Result, + pub message_ids: Vec, + pub messages: HashMap, pub created_at: String, pub updated_at: String, pub created_by: String, @@ -17,3 +20,82 @@ pub struct ChatWithMessages { pub created_by_avatar: Option, } +impl ChatWithMessages { + pub fn new( + title: String, + created_by_id: String, + created_by_name: String, + created_by_avatar: Option, + ) -> Self { + let now = chrono::Utc::now().to_rfc3339(); + Self { + id: uuid::Uuid::new_v4(), + title, + is_favorited: false, + message_ids: Vec::new(), + messages: HashMap::new(), + created_at: now.clone(), + updated_at: now, + created_by: created_by_id.clone(), + created_by_id, + created_by_name, + created_by_avatar, + } + } + + pub fn new_with_messages( + id: uuid::Uuid, + title: String, + messages: Vec, + is_favorited: bool, + created_by_id: String, + created_by_name: String, + created_by_avatar: Option, + ) -> Self { + let now = chrono::Utc::now().to_rfc3339(); + + // Convert messages into a HashMap and collect their IDs + let message_ids: Vec = messages.iter() + .map(|msg| msg.id.to_string()) + .collect(); + + let messages_map: HashMap = messages.into_iter() + .map(|msg| (msg.id.to_string(), msg)) + .collect(); + + Self { + id, + title, + is_favorited, + message_ids, + messages: messages_map, + created_at: now.clone(), + updated_at: now, + created_by: created_by_id.clone(), + created_by_id, + created_by_name, + created_by_avatar, + } + } + + pub fn add_message(&mut self, message: ChatMessage) { + let message_id = message.id.to_string(); + if !self.message_ids.contains(&message_id) { + self.message_ids.push(message_id.clone()); + } + self.messages.insert(message_id, message); + self.updated_at = chrono::Utc::now().to_rfc3339(); + } + + pub fn update_message(&mut self, message: ChatMessage) { + let message_id = message.id.to_string(); + if !self.message_ids.contains(&message_id) { + self.message_ids.push(message_id.clone()); + } + self.messages.insert(message_id, message); + self.updated_at = chrono::Utc::now().to_rfc3339(); + } +} + + +