Private
Public Access
1
0

temporary solution for infinite sync: just remember the times

This commit is contained in:
2025-05-12 20:46:26 -07:00
parent 819b852c1f
commit 4ad9613827

View File

@@ -16,15 +16,22 @@ use kordophone_db::database::DatabaseAccess;
use tokio::sync::mpsc::Sender; use tokio::sync::mpsc::Sender;
use std::sync::Arc; use std::sync::Arc;
use tokio::sync::Mutex; use tokio::sync::Mutex;
use std::collections::HashMap;
use std::time::{Duration, Instant};
pub struct UpdateMonitor { pub struct UpdateMonitor {
database: Arc<Mutex<Database>>, database: Arc<Mutex<Database>>,
event_sender: Sender<Event>, event_sender: Sender<Event>,
last_sync_times: HashMap<String, Instant>,
} }
impl UpdateMonitor { impl UpdateMonitor {
pub fn new(database: Arc<Mutex<Database>>, event_sender: Sender<Event>) -> Self { pub fn new(database: Arc<Mutex<Database>>, event_sender: Sender<Event>) -> Self {
Self { database, event_sender } Self {
database,
event_sender,
last_sync_times: HashMap::new(),
}
} }
pub async fn send_event<T>( pub async fn send_event<T>(
@@ -44,27 +51,33 @@ impl UpdateMonitor {
UpdateEvent::ConversationChanged(conversation) => { UpdateEvent::ConversationChanged(conversation) => {
log::info!(target: target::UPDATES, "Conversation changed: {:?}", conversation); log::info!(target: target::UPDATES, "Conversation changed: {:?}", conversation);
// Weird. We can get in a loop because calling getMessages triggers a conversation changed // Check if we've synced this conversation recently (within 5 seconds)
// event for some reason. Check to see if the change event says the last message id is the same // This is currently a hack/workaround to prevent an infinite loop of sync events, because for some reason
// as the last message id in the database. If so, skip the sync. // imagent will post a conversation changed notification when we call getMessages.
if let Some(last_sync) = self.last_sync_times.get(&conversation.guid) {
if last_sync.elapsed() < Duration::from_secs(5) {
log::info!(target: target::UPDATES, "Skipping sync for conversation id: {}. Last sync was {} seconds ago.",
conversation.guid, last_sync.elapsed().as_secs_f64());
return;
}
}
// This is the non-hacky path once we can reason about chat items with associatedMessageGUIDs (e.g., reactions).
let last_message = self.database.with_repository(|r| r.get_last_message_for_conversation(&conversation.guid)).await.unwrap_or_default(); 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) { match (&last_message, &conversation.last_message) {
(Some(message), Some(conversation_message)) => { (Some(message), Some(conversation_message)) => {
if message.id == conversation_message.guid { if message.id == conversation_message.guid {
false log::info!(target: target::UPDATES, "Skipping sync for conversation id: {}. We already have this message.", conversation.guid);
} else { return;
true
} }
} }
_ => true _ => {}
}; };
if !should_sync_conversation { // Update the last sync time and proceed with sync
log::info!(target: target::UPDATES, "Skipping sync for conversation id: {}. We already have this message.", conversation.guid); self.last_sync_times.insert(conversation.guid.clone(), Instant::now());
return;
}
log::info!(target: target::UPDATES, "Syncing new messages for conversation id: {}, last message: {:?}", conversation.guid, last_message); log::info!(target: target::UPDATES, "Syncing new messages for conversation id: {}", conversation.guid);
self.send_event(|r| Event::SyncConversation(conversation.guid, r)).await self.send_event(|r| Event::SyncConversation(conversation.guid, r)).await
.unwrap_or_else(|e| { .unwrap_or_else(|e| {
log::error!("Failed to send daemon event: {}", e); log::error!("Failed to send daemon event: {}", e);