daemon: maintain outgoing message reference so model is consistent
This commit is contained in:
@@ -2,7 +2,7 @@ use diesel::prelude::*;
|
||||
use chrono::NaiveDateTime;
|
||||
use crate::models::{Message, Participant};
|
||||
|
||||
#[derive(Queryable, Selectable, Insertable, AsChangeset, Clone, Identifiable)]
|
||||
#[derive(Queryable, Selectable, Insertable, AsChangeset, Clone, Identifiable, Debug)]
|
||||
#[diesel(table_name = crate::schema::messages)]
|
||||
#[diesel(check_for_backend(diesel::sqlite::Sqlite))]
|
||||
pub struct Record {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use chrono::{DateTime, NaiveDateTime};
|
||||
use uuid::Uuid;
|
||||
use crate::models::participant::Participant;
|
||||
use kordophone::model::outgoing_message::OutgoingMessage;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Message {
|
||||
@@ -40,6 +41,17 @@ impl From<kordophone::model::Message> for Message {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&OutgoingMessage> for Message {
|
||||
fn from(value: &OutgoingMessage) -> Self {
|
||||
Self {
|
||||
id: value.guid.to_string(),
|
||||
sender: Participant::Me,
|
||||
text: value.text.clone(),
|
||||
date: value.date,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct MessageBuilder {
|
||||
id: Option<String>,
|
||||
sender: Option<Participant>,
|
||||
|
||||
@@ -246,6 +246,7 @@ impl<'a> Repository<'a> {
|
||||
fn update_conversation_metadata(&mut self, conversation_guid: &str, last_message: &MessageRecord) -> Result<()> {
|
||||
let conversation = self.get_conversation_by_guid(conversation_guid)?;
|
||||
if let Some(mut conversation) = conversation {
|
||||
log::debug!("Updating conversation metadata: {} message: {:?}", conversation_guid, last_message);
|
||||
conversation.date = last_message.date;
|
||||
conversation.last_message_preview = Some(last_message.text.clone());
|
||||
self.insert_conversation(conversation)?;
|
||||
|
||||
@@ -54,7 +54,7 @@ async fn test_add_conversation() {
|
||||
repository.insert_conversation(modified_conversation.clone()).unwrap();
|
||||
|
||||
// Make sure we still only have one conversation.
|
||||
let all_conversations = repository.all_conversations().unwrap();
|
||||
let all_conversations = repository.all_conversations(i32::MAX, 0).unwrap();
|
||||
assert_eq!(all_conversations.len(), 1);
|
||||
|
||||
// And make sure the display name was updated
|
||||
@@ -125,7 +125,7 @@ async fn test_all_conversations_with_participants() {
|
||||
repository.insert_conversation(conversation2).unwrap();
|
||||
|
||||
// Get all conversations and verify the results
|
||||
let all_conversations = repository.all_conversations().unwrap();
|
||||
let all_conversations = repository.all_conversations(i32::MAX, 0).unwrap();
|
||||
assert_eq!(all_conversations.len(), 2);
|
||||
|
||||
// Find and verify each conversation's participants
|
||||
|
||||
Reference in New Issue
Block a user