implements settings, conversation dbus encoding
This commit is contained in:
@@ -130,6 +130,53 @@ impl<'a> Repository<'a> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
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(())
|
||||
}
|
||||
|
||||
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::*;
|
||||
|
||||
Reference in New Issue
Block a user