Implements mark as read
This commit is contained in:
@@ -7,4 +7,8 @@ pub mod settings;
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
pub mod target {
|
||||
pub static REPOSITORY: &str = "repository";
|
||||
}
|
||||
|
||||
pub use repository::Repository;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use crate::models::participant::Participant;
|
||||
use crate::models::{message::Message, participant::Participant};
|
||||
use chrono::{DateTime, NaiveDateTime};
|
||||
use uuid::Uuid;
|
||||
|
||||
@@ -27,6 +27,36 @@ impl Conversation {
|
||||
display_name: self.display_name.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn merge(&self, other: &Conversation, last_message: Option<&Message>) -> Conversation {
|
||||
let mut new_conversation = self.clone();
|
||||
new_conversation.unread_count = other.unread_count;
|
||||
new_conversation.participants = other.participants.clone();
|
||||
new_conversation.display_name = other.display_name.clone();
|
||||
|
||||
if let Some(last_message) = last_message {
|
||||
if last_message.date > self.date {
|
||||
new_conversation.date = last_message.date;
|
||||
}
|
||||
|
||||
if !last_message.text.is_empty() && !last_message.text.trim().is_empty() {
|
||||
new_conversation.last_message_preview = Some(last_message.text.clone());
|
||||
}
|
||||
}
|
||||
|
||||
new_conversation
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for Conversation {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.guid == other.guid &&
|
||||
self.unread_count == other.unread_count &&
|
||||
self.display_name == other.display_name &&
|
||||
self.last_message_preview == other.last_message_preview &&
|
||||
self.date == other.date &&
|
||||
self.participants == other.participants
|
||||
}
|
||||
}
|
||||
|
||||
impl From<kordophone::model::Conversation> for Conversation {
|
||||
|
||||
@@ -14,6 +14,7 @@ use crate::{
|
||||
Conversation, Message, Participant,
|
||||
},
|
||||
schema,
|
||||
target,
|
||||
};
|
||||
|
||||
pub struct Repository<'a> {
|
||||
@@ -323,25 +324,35 @@ impl<'a> Repository<'a> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn merge_conversation_metadata(&mut self, in_conversation: Conversation) -> Result<bool> {
|
||||
let mut updated = false;
|
||||
let conversation = self.get_conversation_by_guid(&in_conversation.guid)?;
|
||||
if let Some(conversation) = conversation {
|
||||
let merged_conversation = conversation.merge(&in_conversation, None);
|
||||
|
||||
if merged_conversation != conversation {
|
||||
self.insert_conversation(merged_conversation)?;
|
||||
updated = true;
|
||||
}
|
||||
}
|
||||
|
||||
log::debug!(target: target::REPOSITORY, "Merged conversation metadata: {} updated: {}", in_conversation.guid, updated);
|
||||
Ok(updated)
|
||||
}
|
||||
|
||||
fn update_conversation_metadata(&mut self, conversation_guid: &str) -> Result<()> {
|
||||
let conversation = self.get_conversation_by_guid(conversation_guid)?;
|
||||
if let Some(mut conversation) = conversation {
|
||||
if let Some(conversation) = conversation {
|
||||
if let Some(last_message) = self.get_last_message_for_conversation(conversation_guid)? {
|
||||
log::debug!(
|
||||
target: target::REPOSITORY,
|
||||
"Updating conversation metadata: {} message: {:?}",
|
||||
conversation_guid,
|
||||
last_message
|
||||
);
|
||||
|
||||
if last_message.date > conversation.date {
|
||||
conversation.date = last_message.date;
|
||||
}
|
||||
|
||||
if !last_message.text.is_empty() && !last_message.text.trim().is_empty() {
|
||||
conversation.last_message_preview = Some(last_message.text.clone());
|
||||
}
|
||||
|
||||
self.insert_conversation(conversation)?;
|
||||
let merged_conversation = conversation.merge(&conversation, Some(&last_message));
|
||||
self.insert_conversation(merged_conversation)?;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user