Private
Public Access
1
0

chatdatabase: implements all_conversations with participant zippering

This commit is contained in:
2024-12-21 16:34:47 -08:00
parent f79cbbbc85
commit c4c6e4245d
2 changed files with 53 additions and 6 deletions

View File

@@ -86,14 +86,23 @@ impl ChatDatabase {
pub fn all_conversations(&mut self) -> Result<Vec<Conversation>> { pub fn all_conversations(&mut self) -> Result<Vec<Conversation>> {
use crate::schema::conversations::dsl::*; use crate::schema::conversations::dsl::*;
use crate::schema::participants::dsl::*;
let result = conversations let db_conversations = conversations
.load::<DbConversation>(&mut self.db)? .load::<DbConversation>(&mut self.db)?;
.into_iter()
.map(|c| c.into())
.collect();
// TODO: Need to resolve participants here also somehow... let mut result = Vec::new();
for db_conversation in db_conversations {
let db_participants = ConversationParticipant::belonging_to(&db_conversation)
.inner_join(participants)
.select(DbParticipant::as_select())
.load::<DbParticipant>(&mut self.db)?;
let mut model_conversation: Conversation = db_conversation.into();
model_conversation.participants = db_participants.into_iter().map(|p| p.into()).collect();
result.push(model_conversation);
}
Ok(result) Ok(result)
} }

View File

@@ -83,4 +83,42 @@ mod tests {
assert_eq!(participants, read_participants); assert_eq!(participants, read_participants);
} }
#[test]
fn test_all_conversations_with_participants() {
let mut db = ChatDatabase::new_in_memory().unwrap();
// Create two conversations with different participants
let participants1: Vec<Participant> = vec!["one".into(), "two".into()];
let participants2: Vec<Participant> = vec!["three".into(), "four".into()];
let guid1 = uuid::Uuid::new_v4().to_string();
let conversation1 = ConversationBuilder::new()
.guid(&guid1)
.display_name("Test 1")
.participants(participants1.clone())
.build();
let guid2 = uuid::Uuid::new_v4().to_string();
let conversation2 = ConversationBuilder::new()
.guid(&guid2)
.display_name("Test 2")
.participants(participants2.clone())
.build();
// Insert both conversations
db.insert_conversation(conversation1).unwrap();
db.insert_conversation(conversation2).unwrap();
// Get all conversations and verify the results
let all_conversations = db.all_conversations().unwrap();
assert_eq!(all_conversations.len(), 2);
// Find and verify each conversation's participants
let conv1 = all_conversations.iter().find(|c| c.guid == guid1).unwrap();
let conv2 = all_conversations.iter().find(|c| c.guid == guid2).unwrap();
assert_eq!(conv1.participants, participants1);
assert_eq!(conv2.participants, participants2);
}
} }