kordophone-db: adds support for the Messages table
This commit is contained in:
@@ -3,11 +3,18 @@ use diesel::{prelude::*, sqlite::Sqlite};
|
||||
use diesel::query_dsl::BelongingToDsl;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use crate::models::Participant;
|
||||
use crate::{
|
||||
models::{
|
||||
Conversation,
|
||||
Message,
|
||||
db::conversation::Record as ConversationRecord,
|
||||
db::participant::{Record as ParticipantRecord, ConversationParticipant},
|
||||
db::participant::{
|
||||
ConversationParticipant,
|
||||
Record as ParticipantRecord,
|
||||
InsertableRecord as InsertableParticipantRecord
|
||||
},
|
||||
db::message::Record as MessageRecord,
|
||||
},
|
||||
schema,
|
||||
};
|
||||
@@ -24,6 +31,14 @@ impl ChatDatabase {
|
||||
Self::new(":memory:")
|
||||
}
|
||||
|
||||
// Helper function to get the last inserted row ID
|
||||
// This is a workaround since the Sqlite backend doesn't support `RETURNING`
|
||||
// Huge caveat with this is that it depends on whatever the last insert was, prevents concurrent inserts.
|
||||
fn last_insert_id(&mut self) -> Result<i32> {
|
||||
Ok(diesel::select(diesel::dsl::sql::<diesel::sql_types::Integer>("last_insert_rowid()"))
|
||||
.get_result(&mut self.db)?)
|
||||
}
|
||||
|
||||
pub fn new(db_path: &str) -> Result<Self> {
|
||||
let mut db = SqliteConnection::establish(db_path)?;
|
||||
db.run_pending_migrations(MIGRATIONS)
|
||||
@@ -111,4 +126,88 @@ impl ChatDatabase {
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn insert_message(&mut self, conversation_guid: &str, message: Message) -> Result<()> {
|
||||
use crate::schema::messages::dsl::*;
|
||||
use crate::schema::conversation_messages::dsl::*;
|
||||
|
||||
// Handle participant if message has a remote sender
|
||||
let sender = message.sender.clone();
|
||||
let mut db_message: MessageRecord = message.into();
|
||||
db_message.sender_participant_id = self.get_or_create_participant(&sender);
|
||||
|
||||
diesel::replace_into(messages)
|
||||
.values(&db_message)
|
||||
.execute(&mut self.db)?;
|
||||
|
||||
diesel::replace_into(conversation_messages)
|
||||
.values((
|
||||
conversation_id.eq(conversation_guid),
|
||||
message_id.eq(&db_message.id),
|
||||
))
|
||||
.execute(&mut self.db)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn get_messages_for_conversation(&mut self, conversation_guid: &str) -> Result<Vec<Message>> {
|
||||
use crate::schema::messages::dsl::*;
|
||||
use crate::schema::conversation_messages::dsl::*;
|
||||
use crate::schema::participants::dsl::*;
|
||||
|
||||
let message_records = conversation_messages
|
||||
.filter(conversation_id.eq(conversation_guid))
|
||||
.inner_join(messages)
|
||||
.select(MessageRecord::as_select())
|
||||
.order_by(schema::messages::date.asc())
|
||||
.load::<MessageRecord>(&mut self.db)?;
|
||||
|
||||
let mut result = Vec::new();
|
||||
for message_record in message_records {
|
||||
let mut message: Message = message_record.clone().into();
|
||||
|
||||
// If there's a sender_participant_id, load the participant info
|
||||
if let Some(pid) = message_record.sender_participant_id {
|
||||
let participant = participants
|
||||
.find(pid)
|
||||
.first::<ParticipantRecord>(&mut self.db)?;
|
||||
message.sender = participant.into();
|
||||
}
|
||||
|
||||
result.push(message);
|
||||
}
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
fn get_or_create_participant(&mut self, participant: &Participant) -> Option<i32> {
|
||||
match participant {
|
||||
Participant::Me => None,
|
||||
Participant::Remote { display_name: p_name, .. } => {
|
||||
use crate::schema::participants::dsl::*;
|
||||
|
||||
let existing_participant = participants
|
||||
.filter(display_name.eq(p_name))
|
||||
.first::<ParticipantRecord>(&mut self.db)
|
||||
.optional()
|
||||
.unwrap();
|
||||
|
||||
if let Some(participant) = existing_participant {
|
||||
return Some(participant.id);
|
||||
}
|
||||
|
||||
let participant_record = InsertableParticipantRecord {
|
||||
display_name: Some(participant.display_name()),
|
||||
is_me: false,
|
||||
};
|
||||
|
||||
diesel::insert_into(participants)
|
||||
.values(&participant_record)
|
||||
.execute(&mut self.db)
|
||||
.unwrap();
|
||||
|
||||
self.last_insert_id().ok()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user