kordophone-db: participants, but still need to "upsert" these
Might move to SeaORM instead of trying to do this with microrm
This commit is contained in:
@@ -1,7 +1,14 @@
|
||||
use std::error::Error;
|
||||
use microrm::prelude::*;
|
||||
use microrm::Stored;
|
||||
use crate::models::conversation::{self, Conversation, ConversationID};
|
||||
use std::error::Error;
|
||||
|
||||
use crate::models::participant::ParticipantID;
|
||||
use crate::models::{
|
||||
participant::Participant,
|
||||
conversation::{
|
||||
self, Conversation, ConversationID, PendingConversation
|
||||
}
|
||||
};
|
||||
|
||||
pub struct ChatDatabase {
|
||||
db: DB,
|
||||
@@ -10,6 +17,7 @@ pub struct ChatDatabase {
|
||||
#[derive(Database)]
|
||||
struct DB {
|
||||
conversations: microrm::IDMap<Conversation>,
|
||||
participants: microrm::IDMap<Participant>,
|
||||
}
|
||||
|
||||
impl ChatDatabase {
|
||||
@@ -20,20 +28,27 @@ impl ChatDatabase {
|
||||
})
|
||||
}
|
||||
|
||||
pub fn insert_conversation(&self, conversation: Conversation) -> Result<ConversationID, microrm::Error> {
|
||||
pub fn insert_conversation(&self, conversation: PendingConversation) -> Result<ConversationID, microrm::Error> {
|
||||
// First see if conversation guid already exists, update it if so
|
||||
let guid = &conversation.guid;
|
||||
let mut existing = self.db.conversations
|
||||
.with(Conversation::Guid, guid)
|
||||
.get()?;
|
||||
let guid = conversation.guid();
|
||||
let mut existing = self.stored_conversation_by_guid(guid)?;
|
||||
|
||||
if let Some(existing) = existing.first_mut() {
|
||||
existing.display_name = conversation.display_name;
|
||||
if let Some(existing) = existing.as_mut() {
|
||||
conversation.update(existing);
|
||||
existing.sync();
|
||||
return Ok(existing.id());
|
||||
} else {
|
||||
// Otherwise, insert.
|
||||
return self.db.conversations.insert(conversation);
|
||||
let inserted = self.db.conversations.insert_and_return(conversation.get_conversation())?;
|
||||
|
||||
// Insert participants
|
||||
let participants = conversation.get_participants();
|
||||
let inserted_participants = participants.iter()
|
||||
.map(|p| self.db.participants.insert(p.clone()).unwrap())
|
||||
.collect::<Vec<_>>();
|
||||
inserted.connect_participants(inserted_participants);
|
||||
|
||||
return Ok(inserted.id());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,6 +81,26 @@ impl ChatDatabase {
|
||||
)
|
||||
}
|
||||
|
||||
fn upsert_participants(&self, participants: Vec<Participant>) -> Vec<ParticipantID> {
|
||||
// Filter existing participants and add to result
|
||||
let existing_participants = participants.iter()
|
||||
.filter_map(|p| self.db.participants
|
||||
.with(Participant::DisplayName, &p.display_name)
|
||||
.get()
|
||||
.ok()
|
||||
.and_then(|v| v
|
||||
.into_iter()
|
||||
.last()
|
||||
.map(|p| p.id())
|
||||
)
|
||||
)
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
participants.iter()
|
||||
.map(|p| self.db.participants.insert(p.clone()).unwrap())
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn stored_conversation_by_guid(&self, guid: &str) -> Result<Option<Stored<Conversation>>, microrm::Error> {
|
||||
self.db.conversations
|
||||
.with(Conversation::Guid, guid)
|
||||
|
||||
Reference in New Issue
Block a user