Private
Public Access
1
0

kordophone-db: switch to diesel for more features

This commit is contained in:
2024-12-14 19:03:27 -08:00
parent 86601b027a
commit f79cbbbc85
12 changed files with 432 additions and 249 deletions

View File

@@ -1,26 +1,58 @@
use microrm::prelude::*;
use microrm::Stored;
use time::OffsetDateTime;
use diesel::prelude::*;
use chrono::NaiveDateTime;
use uuid::Uuid;
use crate::models::{
date::Date,
participant::Participant,
};
use super::participant::ParticipantID;
#[derive(Entity, Clone)]
pub struct Conversation {
#[unique]
pub guid: String,
#[derive(Queryable, Selectable, Insertable, AsChangeset, Clone, Identifiable)]
#[diesel(table_name = crate::schema::conversations)]
#[diesel(check_for_backend(diesel::sqlite::Sqlite))]
pub struct DbConversation {
pub id: String,
pub unread_count: i64,
pub display_name: Option<String>,
pub last_message_preview: Option<String>,
pub date: OffsetDateTime,
participant_display_names: microrm::RelationMap<Participant>,
pub date: NaiveDateTime,
}
impl From<Conversation> for DbConversation {
fn from(conversation: Conversation) -> Self {
Self {
id: conversation.guid,
unread_count: conversation.unread_count as i64,
display_name: conversation.display_name,
last_message_preview: conversation.last_message_preview,
date: conversation.date,
}
}
}
impl From<Conversation> for (DbConversation, Vec<Participant>) {
fn from(conversation: Conversation) -> Self {
(
DbConversation {
id: conversation.guid,
unread_count: conversation.unread_count as i64,
display_name: conversation.display_name,
last_message_preview: conversation.last_message_preview,
date: conversation.date,
},
conversation.participants
)
}
}
#[derive(Clone, Debug)]
pub struct Conversation {
pub guid: String,
pub unread_count: u16,
pub display_name: Option<String>,
pub last_message_preview: Option<String>,
pub date: NaiveDateTime,
pub participants: Vec<Participant>,
}
impl Conversation {
@@ -31,76 +63,35 @@ impl Conversation {
pub fn into_builder(&self) -> ConversationBuilder {
ConversationBuilder {
guid: Some(self.guid.clone()),
date: Date::new(self.date),
participant_display_names: None,
date: self.date,
participants: None,
unread_count: Some(self.unread_count),
last_message_preview: self.last_message_preview.clone(),
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,
impl From<DbConversation> for Conversation {
fn from(db_conversation: DbConversation) -> Self {
Self {
guid: db_conversation.id,
unread_count: db_conversation.unread_count as u16,
display_name: db_conversation.display_name,
last_message_preview: db_conversation.last_message_preview,
date: db_conversation.date,
participants: vec![],
}
}
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)]
pub struct ConversationBuilder {
guid: Option<String>,
date: Date,
unread_count: Option<i64>,
date: NaiveDateTime,
unread_count: Option<u16>,
last_message_preview: Option<String>,
participant_display_names: Option<Vec<Participant>>,
participants: Option<Vec<Participant>>,
display_name: Option<String>,
}
@@ -114,12 +105,12 @@ impl ConversationBuilder {
self
}
pub fn date(mut self, date: Date) -> Self {
pub fn date(mut self, date: NaiveDateTime) -> Self {
self.date = date;
self
}
pub fn unread_count(mut self, unread_count: i64) -> Self {
pub fn unread_count(mut self, unread_count: u16) -> Self {
self.unread_count = Some(unread_count);
self
}
@@ -129,8 +120,8 @@ impl ConversationBuilder {
self
}
pub fn participant_display_names(mut self, participant_display_names: Vec<Participant>) -> Self {
self.participant_display_names = Some(participant_display_names);
pub fn participants(mut self, participants: Vec<Participant>) -> Self {
self.participants = Some(participants);
self
}
@@ -139,21 +130,14 @@ impl ConversationBuilder {
self
}
fn build_conversation(&self) -> Conversation {
pub fn build(&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.clone(),
display_name: self.display_name.clone(),
date: self.date.dt,
participant_display_names: Default::default(),
}
}
pub fn build(self) -> PendingConversation {
PendingConversation {
conversation: self.build_conversation(),
participants: self.participant_display_names.unwrap_or_default(),
date: self.date,
participants: self.participants.clone().unwrap_or_default(),
}
}
}

View File

@@ -1,4 +1,3 @@
use chrono::{DateTime, Local, Utc};
use time::OffsetDateTime;
pub struct Date {

View File

@@ -1,11 +1,35 @@
use microrm::prelude::*;
use diesel::prelude::*;
use crate::{models::conversation::DbConversation, schema::conversation_participants};
#[derive(Entity, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Insertable)]
#[diesel(table_name = crate::schema::participants)]
pub struct Participant {
#[unique]
pub display_name: String,
}
impl From<DbParticipant> for Participant {
fn from(participant: DbParticipant) -> Self {
Participant { display_name: participant.display_name }
}
}
#[derive(Queryable, Selectable, Insertable, AsChangeset, Clone, PartialEq, Debug, Identifiable)]
#[diesel(table_name = crate::schema::participants)]
pub struct DbParticipant {
pub id: i32,
pub display_name: String
}
#[derive(Identifiable, Selectable, Queryable, Associations, Debug)]
#[diesel(belongs_to(DbConversation, foreign_key = conversation_id))]
#[diesel(belongs_to(DbParticipant, foreign_key = participant_id))]
#[diesel(table_name = conversation_participants)]
#[diesel(primary_key(conversation_id, participant_id))]
pub struct ConversationParticipant {
pub conversation_id: String,
pub participant_id: i32,
}
impl Into<Participant> for String {
fn into(self) -> Participant {
Participant { display_name: self }