2024-12-14 19:03:27 -08:00
|
|
|
use anyhow::Result;
|
2025-01-20 22:13:44 -08:00
|
|
|
use diesel::prelude::*;
|
2024-12-14 19:03:27 -08:00
|
|
|
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,
|
2025-01-20 22:05:34 -08:00
|
|
|
Message,
|
2025-04-25 15:42:46 -07:00
|
|
|
Participant,
|
2024-12-21 17:09:37 -08:00
|
|
|
db::conversation::Record as ConversationRecord,
|
2025-01-20 22:05:34 -08:00
|
|
|
db::participant::{
|
|
|
|
|
ConversationParticipant,
|
|
|
|
|
Record as ParticipantRecord,
|
|
|
|
|
InsertableRecord as InsertableParticipantRecord
|
|
|
|
|
},
|
|
|
|
|
db::message::Record as MessageRecord,
|
2024-12-21 17:09:37 -08:00
|
|
|
},
|
|
|
|
|
schema,
|
|
|
|
|
};
|
2024-12-08 21:12:17 -08:00
|
|
|
|
2025-04-25 15:42:46 -07:00
|
|
|
pub struct Repository<'a> {
|
2025-04-25 16:34:00 -07:00
|
|
|
connection: &'a mut SqliteConnection,
|
2024-12-08 21:12:17 -08:00
|
|
|
}
|
|
|
|
|
|
2025-04-25 15:42:46 -07:00
|
|
|
impl<'a> Repository<'a> {
|
2025-04-25 16:34:00 -07:00
|
|
|
pub fn new(connection: &'a mut SqliteConnection) -> Self {
|
|
|
|
|
Self { connection }
|
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)
|
2025-04-25 16:34:00 -07:00
|
|
|
.execute(self.connection)?;
|
2024-12-14 19:03:27 -08:00
|
|
|
|
|
|
|
|
diesel::replace_into(participants)
|
|
|
|
|
.values(&db_participants)
|
2025-04-25 16:34:00 -07:00
|
|
|
.execute(self.connection)?;
|
2024-12-14 19:03:27 -08:00
|
|
|
|
|
|
|
|
// 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))
|
2025-04-25 16:34:00 -07:00
|
|
|
.first::<i32>(self.connection)?;
|
2024-12-14 19:03:27 -08:00
|
|
|
|
|
|
|
|
diesel::replace_into(conversation_participants)
|
|
|
|
|
.values((
|
|
|
|
|
conversation_id.eq(&db_conversation.id),
|
|
|
|
|
participant_id.eq(pid),
|
|
|
|
|
))
|
2025-04-25 16:34:00 -07:00
|
|
|
.execute(self.connection)?;
|
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)
|
2025-04-25 16:34:00 -07:00
|
|
|
.first::<ConversationRecord>(self.connection)
|
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())
|
2025-04-25 16:34:00 -07:00
|
|
|
.load::<ParticipantRecord>(self.connection)?;
|
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
|
2025-04-25 16:34:00 -07:00
|
|
|
.load::<ConversationRecord>(self.connection)?;
|
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())
|
2025-04-25 16:34:00 -07:00
|
|
|
.load::<ParticipantRecord>(self.connection)?;
|
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
|
|
|
}
|
2025-01-20 22:05:34 -08:00
|
|
|
|
|
|
|
|
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)
|
2025-04-25 16:34:00 -07:00
|
|
|
.execute(self.connection)?;
|
2025-01-20 22:05:34 -08:00
|
|
|
|
|
|
|
|
diesel::replace_into(conversation_messages)
|
|
|
|
|
.values((
|
|
|
|
|
conversation_id.eq(conversation_guid),
|
|
|
|
|
message_id.eq(&db_message.id),
|
|
|
|
|
))
|
2025-04-25 16:34:00 -07:00
|
|
|
.execute(self.connection)?;
|
2025-01-20 22:05:34 -08:00
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-27 18:07:58 -07:00
|
|
|
pub fn insert_messages(&mut self, conversation_guid: &str, in_messages: Vec<Message>) -> Result<()> {
|
|
|
|
|
use crate::schema::messages::dsl::*;
|
|
|
|
|
use crate::schema::conversation_messages::dsl::*;
|
|
|
|
|
|
|
|
|
|
// Local insertable struct for the join table
|
|
|
|
|
#[derive(Insertable)]
|
|
|
|
|
#[diesel(table_name = crate::schema::conversation_messages)]
|
|
|
|
|
struct InsertableConversationMessage {
|
|
|
|
|
pub conversation_id: String,
|
|
|
|
|
pub message_id: String,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if in_messages.is_empty() {
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Build the collections of insertable records
|
|
|
|
|
let mut db_messages: Vec<MessageRecord> = Vec::with_capacity(in_messages.len());
|
|
|
|
|
let mut conv_msg_records: Vec<InsertableConversationMessage> = Vec::with_capacity(in_messages.len());
|
|
|
|
|
|
|
|
|
|
for message in in_messages {
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
|
|
conv_msg_records.push(InsertableConversationMessage {
|
|
|
|
|
conversation_id: conversation_guid.to_string(),
|
|
|
|
|
message_id: db_message.id.clone(),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
db_messages.push(db_message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Batch insert or replace messages
|
|
|
|
|
diesel::replace_into(messages)
|
|
|
|
|
.values(&db_messages)
|
|
|
|
|
.execute(self.connection)?;
|
|
|
|
|
|
|
|
|
|
// Batch insert the conversation-message links
|
|
|
|
|
diesel::replace_into(conversation_messages)
|
|
|
|
|
.values(&conv_msg_records)
|
|
|
|
|
.execute(self.connection)?;
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-20 22:05:34 -08:00
|
|
|
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())
|
2025-04-25 16:34:00 -07:00
|
|
|
.load::<MessageRecord>(self.connection)?;
|
2025-01-20 22:05:34 -08:00
|
|
|
|
|
|
|
|
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)
|
2025-04-25 16:34:00 -07:00
|
|
|
.first::<ParticipantRecord>(self.connection)?;
|
2025-01-20 22:05:34 -08:00
|
|
|
message.sender = participant.into();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result.push(message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(result)
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-25 15:42:46 -07:00
|
|
|
// 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()"))
|
2025-04-25 16:34:00 -07:00
|
|
|
.get_result(self.connection)?)
|
2025-04-25 15:42:46 -07:00
|
|
|
}
|
|
|
|
|
|
2025-01-20 22:05:34 -08:00
|
|
|
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))
|
2025-04-25 16:34:00 -07:00
|
|
|
.first::<ParticipantRecord>(self.connection)
|
2025-01-20 22:05:34 -08:00
|
|
|
.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)
|
2025-04-25 16:34:00 -07:00
|
|
|
.execute(self.connection)
|
2025-01-20 22:05:34 -08:00
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
self.last_insert_id().ok()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|