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,9 +1,14 @@
|
||||
use microrm::prelude::*;
|
||||
use chrono::{DateTime, Local, Utc};
|
||||
use microrm::Stored;
|
||||
use time::OffsetDateTime;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::models::date::Date;
|
||||
use crate::models::{
|
||||
date::Date,
|
||||
participant::Participant,
|
||||
};
|
||||
|
||||
use super::participant::ParticipantID;
|
||||
|
||||
#[derive(Entity, Clone)]
|
||||
pub struct Conversation {
|
||||
@@ -14,19 +19,8 @@ pub struct Conversation {
|
||||
pub display_name: Option<String>,
|
||||
pub last_message_preview: Option<String>,
|
||||
pub date: OffsetDateTime,
|
||||
pub participant_display_names: microrm::RelationMap<Participant>,
|
||||
}
|
||||
|
||||
#[derive(Entity, Clone)]
|
||||
pub struct Participant {
|
||||
#[unique]
|
||||
pub display_name: String
|
||||
}
|
||||
|
||||
impl Into<Participant> for String {
|
||||
fn into(self) -> Participant {
|
||||
Participant { display_name: self }
|
||||
}
|
||||
|
||||
participant_display_names: microrm::RelationMap<Participant>,
|
||||
}
|
||||
|
||||
impl Conversation {
|
||||
@@ -44,6 +38,60 @@ impl Conversation {
|
||||
display_name: self.display_name.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_participant_display_names(&self) -> Vec<Participant> {
|
||||
self.participant_display_names
|
||||
.get()
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.map(|p| p.wrapped())
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn update(&self, stored_conversation: &mut Stored<Conversation>) {
|
||||
*stored_conversation.as_mut() = self.clone();
|
||||
}
|
||||
|
||||
pub fn connect_participants(&self, participant_ids: Vec<ParticipantID>) {
|
||||
participant_ids.iter().for_each(|id| {
|
||||
self.participant_display_names.connect_to(*id).unwrap();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct PendingConversation {
|
||||
conversation: Conversation,
|
||||
participants: Vec<Participant>,
|
||||
}
|
||||
|
||||
impl PendingConversation {
|
||||
pub fn guid(&self) -> &String {
|
||||
&self.conversation.guid
|
||||
}
|
||||
|
||||
pub fn into_builder(self) -> ConversationBuilder {
|
||||
ConversationBuilder {
|
||||
guid: Some(self.conversation.guid),
|
||||
date: Date::new(self.conversation.date),
|
||||
participant_display_names: Some(self.participants),
|
||||
unread_count: Some(self.conversation.unread_count),
|
||||
last_message_preview: self.conversation.last_message_preview,
|
||||
display_name: self.conversation.display_name,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update(&self, stored_conversation: &mut microrm::Stored<Conversation>) {
|
||||
self.conversation.update(stored_conversation);
|
||||
}
|
||||
|
||||
pub fn get_participants(&self) -> &Vec<Participant> {
|
||||
&self.participants
|
||||
}
|
||||
|
||||
pub fn get_conversation(&self) -> Conversation {
|
||||
self.conversation.clone()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
@@ -52,7 +100,7 @@ pub struct ConversationBuilder {
|
||||
date: Date,
|
||||
unread_count: Option<i64>,
|
||||
last_message_preview: Option<String>,
|
||||
participant_display_names: Option<Vec<String>>,
|
||||
participant_display_names: Option<Vec<Participant>>,
|
||||
display_name: Option<String>,
|
||||
}
|
||||
|
||||
@@ -81,7 +129,7 @@ impl ConversationBuilder {
|
||||
self
|
||||
}
|
||||
|
||||
pub fn participant_display_names(mut self, participant_display_names: Vec<String>) -> Self {
|
||||
pub fn participant_display_names(mut self, participant_display_names: Vec<Participant>) -> Self {
|
||||
self.participant_display_names = Some(participant_display_names);
|
||||
self
|
||||
}
|
||||
@@ -91,23 +139,21 @@ impl ConversationBuilder {
|
||||
self
|
||||
}
|
||||
|
||||
pub fn build(self) -> Conversation {
|
||||
let result = Conversation {
|
||||
guid: self.guid.unwrap_or(Uuid::new_v4().to_string()),
|
||||
fn build_conversation(&self) -> Conversation {
|
||||
Conversation {
|
||||
guid: self.guid.clone().unwrap_or(Uuid::new_v4().to_string()),
|
||||
unread_count: self.unread_count.unwrap_or(0),
|
||||
last_message_preview: self.last_message_preview,
|
||||
display_name: self.display_name,
|
||||
last_message_preview: self.last_message_preview.clone(),
|
||||
display_name: self.display_name.clone(),
|
||||
date: self.date.dt,
|
||||
participant_display_names: Default::default(),
|
||||
};
|
||||
|
||||
// TODO: this isn't right... this is crashing the test.
|
||||
if let Some(participants) = self.participant_display_names {
|
||||
for participant in participants {
|
||||
result.participant_display_names.insert(participant.into()).unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
result
|
||||
pub fn build(self) -> PendingConversation {
|
||||
PendingConversation {
|
||||
conversation: self.build_conversation(),
|
||||
participants: self.participant_display_names.unwrap_or_default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,2 +1,3 @@
|
||||
pub mod conversation;
|
||||
pub mod date;
|
||||
pub mod date;
|
||||
pub mod participant;
|
||||
19
kordophone-db/src/models/participant.rs
Normal file
19
kordophone-db/src/models/participant.rs
Normal file
@@ -0,0 +1,19 @@
|
||||
use microrm::prelude::*;
|
||||
|
||||
#[derive(Entity, Clone, PartialEq)]
|
||||
pub struct Participant {
|
||||
#[unique]
|
||||
pub display_name: String
|
||||
}
|
||||
|
||||
impl Into<Participant> for String {
|
||||
fn into(self) -> Participant {
|
||||
Participant { display_name: self }
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&str> for Participant {
|
||||
fn from(s: &str) -> Self {
|
||||
Participant { display_name: s.into() }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user