2024-12-14 19:03:27 -08:00
|
|
|
use anyhow::Result;
|
|
|
|
|
use diesel::prelude::*;
|
|
|
|
|
use diesel::query_dsl::BelongingToDsl;
|
2024-12-14 12:53:44 -08:00
|
|
|
|
2024-12-21 17:09:37 -08:00
|
|
|
use crate::{
|
|
|
|
|
models::{
|
|
|
|
|
Conversation,
|
|
|
|
|
db::conversation::Record as ConversationRecord,
|
|
|
|
|
db::participant::{Record as ParticipantRecord, ConversationParticipant},
|
|
|
|
|
},
|
|
|
|
|
schema,
|
|
|
|
|
};
|
2024-12-08 21:12:17 -08:00
|
|
|
|
2024-12-14 19:03:27 -08:00
|
|
|
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
|
|
|
|
|
pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!();
|
2024-12-08 21:12:17 -08:00
|
|
|
|
2024-12-14 19:03:27 -08:00
|
|
|
pub struct ChatDatabase {
|
|
|
|
|
db: SqliteConnection,
|
2024-12-08 21:12:17 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ChatDatabase {
|
2024-12-14 19:03:27 -08:00
|
|
|
pub fn new_in_memory() -> Result<Self> {
|
|
|
|
|
let mut db = SqliteConnection::establish(":memory:")?;
|
|
|
|
|
db.run_pending_migrations(MIGRATIONS)
|
|
|
|
|
.map_err(|e| anyhow::anyhow!("Error running migrations: {}", e))?;
|
|
|
|
|
|
2024-12-21 18:01:00 -08:00
|
|
|
Ok(Self { db })
|
2024-12-08 21:12:17 -08:00
|
|
|
}
|
|
|
|
|
|
2024-12-14 19:03:27 -08:00
|
|
|
pub fn insert_conversation(&mut self, conversation: Conversation) -> Result<()> {
|
|
|
|
|
use crate::schema::conversations::dsl::*;
|
|
|
|
|
use crate::schema::participants::dsl::*;
|
|
|
|
|
use crate::schema::conversation_participants::dsl::*;
|
|
|
|
|
|
|
|
|
|
let (db_conversation, db_participants) = conversation.into();
|
|
|
|
|
|
|
|
|
|
diesel::replace_into(conversations)
|
|
|
|
|
.values(&db_conversation)
|
|
|
|
|
.execute(&mut self.db)?;
|
|
|
|
|
|
|
|
|
|
diesel::replace_into(participants)
|
|
|
|
|
.values(&db_participants)
|
|
|
|
|
.execute(&mut self.db)?;
|
|
|
|
|
|
|
|
|
|
// Sqlite backend doesn't support batch insert, so we have to do this manually
|
|
|
|
|
for participant in db_participants {
|
|
|
|
|
let pid = participants
|
|
|
|
|
.select(schema::participants::id)
|
|
|
|
|
.filter(schema::participants::display_name.eq(&participant.display_name))
|
|
|
|
|
.first::<i32>(&mut self.db)?;
|
|
|
|
|
|
|
|
|
|
diesel::replace_into(conversation_participants)
|
|
|
|
|
.values((
|
|
|
|
|
conversation_id.eq(&db_conversation.id),
|
|
|
|
|
participant_id.eq(pid),
|
|
|
|
|
))
|
|
|
|
|
.execute(&mut self.db)?;
|
2024-12-08 21:12:17 -08:00
|
|
|
}
|
|
|
|
|
|
2024-12-14 19:03:27 -08:00
|
|
|
Ok(())
|
2024-12-08 21:12:17 -08:00
|
|
|
}
|
|
|
|
|
|
2024-12-14 19:03:27 -08:00
|
|
|
pub fn get_conversation_by_guid(&mut self, match_guid: &str) -> Result<Option<Conversation>> {
|
|
|
|
|
use crate::schema::conversations::dsl::*;
|
|
|
|
|
use crate::schema::participants::dsl::*;
|
2024-12-08 21:12:17 -08:00
|
|
|
|
2024-12-14 19:03:27 -08:00
|
|
|
let result = conversations
|
|
|
|
|
.find(match_guid)
|
2024-12-21 17:09:37 -08:00
|
|
|
.first::<ConversationRecord>(&mut self.db)
|
2024-12-14 19:03:27 -08:00
|
|
|
.optional()?;
|
|
|
|
|
|
|
|
|
|
if let Some(conversation) = result {
|
2024-12-21 17:09:37 -08:00
|
|
|
let db_participants = ConversationParticipant::belonging_to(&conversation)
|
2024-12-14 19:03:27 -08:00
|
|
|
.inner_join(participants)
|
2024-12-21 17:09:37 -08:00
|
|
|
.select(ParticipantRecord::as_select())
|
|
|
|
|
.load::<ParticipantRecord>(&mut self.db)?;
|
2024-12-14 19:03:27 -08:00
|
|
|
|
2024-12-21 17:09:37 -08:00
|
|
|
let mut model_conversation: Conversation = conversation.into();
|
|
|
|
|
model_conversation.participants = db_participants.into_iter().map(|p| p.into()).collect();
|
2024-12-08 21:12:17 -08:00
|
|
|
|
2024-12-21 17:09:37 -08:00
|
|
|
return Ok(Some(model_conversation));
|
2024-12-14 19:03:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(None)
|
2024-12-14 12:53:44 -08:00
|
|
|
}
|
|
|
|
|
|
2024-12-14 19:03:27 -08:00
|
|
|
pub fn all_conversations(&mut self) -> Result<Vec<Conversation>> {
|
|
|
|
|
use crate::schema::conversations::dsl::*;
|
2024-12-21 16:34:47 -08:00
|
|
|
use crate::schema::participants::dsl::*;
|
2024-12-14 19:03:27 -08:00
|
|
|
|
2024-12-21 16:34:47 -08:00
|
|
|
let db_conversations = conversations
|
2024-12-21 17:09:37 -08:00
|
|
|
.load::<ConversationRecord>(&mut self.db)?;
|
2024-12-21 16:34:47 -08:00
|
|
|
|
|
|
|
|
let mut result = Vec::new();
|
|
|
|
|
for db_conversation in db_conversations {
|
|
|
|
|
let db_participants = ConversationParticipant::belonging_to(&db_conversation)
|
|
|
|
|
.inner_join(participants)
|
2024-12-21 17:09:37 -08:00
|
|
|
.select(ParticipantRecord::as_select())
|
|
|
|
|
.load::<ParticipantRecord>(&mut self.db)?;
|
2024-12-14 19:03:27 -08:00
|
|
|
|
2024-12-21 16:34:47 -08:00
|
|
|
let mut model_conversation: Conversation = db_conversation.into();
|
|
|
|
|
model_conversation.participants = db_participants.into_iter().map(|p| p.into()).collect();
|
|
|
|
|
|
|
|
|
|
result.push(model_conversation);
|
|
|
|
|
}
|
2024-12-14 19:03:27 -08:00
|
|
|
|
|
|
|
|
Ok(result)
|
2024-12-08 21:12:17 -08:00
|
|
|
}
|
|
|
|
|
}
|