Fixes bug where updates can cause a sync loop
This commit is contained in:
@@ -11,6 +11,7 @@ use kordophone::api::event_socket::EventSocket;
|
||||
use kordophone::model::event::Event as UpdateEvent;
|
||||
|
||||
use kordophone_db::database::Database;
|
||||
use kordophone_db::database::DatabaseAccess;
|
||||
|
||||
use tokio::sync::mpsc::Sender;
|
||||
use std::sync::Arc;
|
||||
@@ -42,13 +43,32 @@ impl UpdateMonitor {
|
||||
match update {
|
||||
UpdateEvent::ConversationChanged(conversation) => {
|
||||
log::info!(target: target::UPDATES, "Conversation changed: {:?}", conversation);
|
||||
if conversation.unread_count > 0 {
|
||||
log::info!(target: target::UPDATES, "Syncing new messages for conversation id: {}", conversation.guid);
|
||||
self.send_event(|r| Event::SyncConversation(conversation.guid, r)).await
|
||||
.unwrap_or_else(|e| {
|
||||
log::error!("Failed to send daemon event: {}", e);
|
||||
});
|
||||
|
||||
// Weird. We can get in a loop because calling getMessages triggers a conversation changed
|
||||
// event for some reason. Check to see if the change event says the last message id is the same
|
||||
// as the last message id in the database. If so, skip the sync.
|
||||
let last_message = self.database.with_repository(|r| r.get_last_message_for_conversation(&conversation.guid)).await.unwrap_or_default();
|
||||
let should_sync_conversation = match (&last_message, &conversation.last_message) {
|
||||
(Some(message), Some(conversation_message)) => {
|
||||
if message.id == conversation_message.guid {
|
||||
false
|
||||
} else {
|
||||
true
|
||||
}
|
||||
}
|
||||
_ => true
|
||||
};
|
||||
|
||||
if !should_sync_conversation {
|
||||
log::info!(target: target::UPDATES, "Skipping sync for conversation id: {}. We already have this message.", conversation.guid);
|
||||
return;
|
||||
}
|
||||
|
||||
log::info!(target: target::UPDATES, "Syncing new messages for conversation id: {}, last message: {:?}", conversation.guid, last_message);
|
||||
self.send_event(|r| Event::SyncConversation(conversation.guid, r)).await
|
||||
.unwrap_or_else(|e| {
|
||||
log::error!("Failed to send daemon event: {}", e);
|
||||
});
|
||||
}
|
||||
|
||||
UpdateEvent::MessageReceived(conversation, message) => {
|
||||
|
||||
Reference in New Issue
Block a user