reorg: split repo / database so settings can use db connection as well
This commit is contained in:
204
kordophone-db/src/repository.rs
Normal file
204
kordophone-db/src/repository.rs
Normal file
@@ -0,0 +1,204 @@
|
||||
use anyhow::Result;
|
||||
use diesel::prelude::*;
|
||||
use diesel::query_dsl::BelongingToDsl;
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::{
|
||||
database::Database,
|
||||
models::{
|
||||
Conversation,
|
||||
Message,
|
||||
Participant,
|
||||
db::conversation::Record as ConversationRecord,
|
||||
db::participant::{
|
||||
ConversationParticipant,
|
||||
Record as ParticipantRecord,
|
||||
InsertableRecord as InsertableParticipantRecord
|
||||
},
|
||||
db::message::Record as MessageRecord,
|
||||
},
|
||||
schema,
|
||||
};
|
||||
|
||||
pub struct Repository<'a> {
|
||||
db: &'a mut Database,
|
||||
}
|
||||
|
||||
impl<'a> Repository<'a> {
|
||||
pub fn new(db: &'a mut Database) -> Self {
|
||||
Self { db }
|
||||
}
|
||||
|
||||
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.connection)?;
|
||||
|
||||
diesel::replace_into(participants)
|
||||
.values(&db_participants)
|
||||
.execute(&mut self.db.connection)?;
|
||||
|
||||
// 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.connection)?;
|
||||
|
||||
diesel::replace_into(conversation_participants)
|
||||
.values((
|
||||
conversation_id.eq(&db_conversation.id),
|
||||
participant_id.eq(pid),
|
||||
))
|
||||
.execute(&mut self.db.connection)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn get_conversation_by_guid(&mut self, match_guid: &str) -> Result<Option<Conversation>> {
|
||||
use crate::schema::conversations::dsl::*;
|
||||
use crate::schema::participants::dsl::*;
|
||||
|
||||
let result = conversations
|
||||
.find(match_guid)
|
||||
.first::<ConversationRecord>(&mut self.db.connection)
|
||||
.optional()?;
|
||||
|
||||
if let Some(conversation) = result {
|
||||
let db_participants = ConversationParticipant::belonging_to(&conversation)
|
||||
.inner_join(participants)
|
||||
.select(ParticipantRecord::as_select())
|
||||
.load::<ParticipantRecord>(&mut self.db.connection)?;
|
||||
|
||||
let mut model_conversation: Conversation = conversation.into();
|
||||
model_conversation.participants = db_participants.into_iter().map(|p| p.into()).collect();
|
||||
|
||||
return Ok(Some(model_conversation));
|
||||
}
|
||||
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
pub fn all_conversations(&mut self) -> Result<Vec<Conversation>> {
|
||||
use crate::schema::conversations::dsl::*;
|
||||
use crate::schema::participants::dsl::*;
|
||||
|
||||
let db_conversations = conversations
|
||||
.load::<ConversationRecord>(&mut self.db.connection)?;
|
||||
|
||||
let mut result = Vec::new();
|
||||
for db_conversation in db_conversations {
|
||||
let db_participants = ConversationParticipant::belonging_to(&db_conversation)
|
||||
.inner_join(participants)
|
||||
.select(ParticipantRecord::as_select())
|
||||
.load::<ParticipantRecord>(&mut self.db.connection)?;
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
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.connection)?;
|
||||
|
||||
diesel::replace_into(conversation_messages)
|
||||
.values((
|
||||
conversation_id.eq(conversation_guid),
|
||||
message_id.eq(&db_message.id),
|
||||
))
|
||||
.execute(&mut self.db.connection)?;
|
||||
|
||||
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.connection)?;
|
||||
|
||||
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.connection)?;
|
||||
message.sender = participant.into();
|
||||
}
|
||||
|
||||
result.push(message);
|
||||
}
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
// 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.connection)?)
|
||||
}
|
||||
|
||||
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.connection)
|
||||
.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.connection)
|
||||
.unwrap();
|
||||
|
||||
self.last_insert_id().ok()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user