From f4753862f1c57031118ab61e33c44a09b31f5659 Mon Sep 17 00:00:00 2001 From: iequidoo Date: Thu, 2 Nov 2023 23:33:26 -0300 Subject: [PATCH] feat: Sync chat state immediately (#4817) Sync messages are only sent on explicit user actions and only one per action, so it's safe to send them right away not worrying about the rate limit on the server. --- src/chat.rs | 3 ++- src/contact.rs | 1 + src/sync.rs | 8 +++++--- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/chat.rs b/src/chat.rs index 1001e2102..35d03b836 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -252,7 +252,7 @@ impl ChatId { let chat_id = match ChatIdBlocked::lookup_by_contact(context, contact_id).await? { Some(chat) => { if create_blocked == Blocked::Not && chat.blocked != Blocked::Not { - chat.id.unblock_ex(context, Nosync).await?; + chat.id.set_blocked(context, Blocked::Not).await?; } chat.id } @@ -1903,6 +1903,7 @@ impl Chat { context .add_sync_item(SyncData::AlterChat { id, action }) .await?; + context.send_sync_msg().await?; } Ok(()) } diff --git a/src/contact.rs b/src/contact.rs index 750b7a762..f375123a3 100644 --- a/src/contact.rs +++ b/src/contact.rs @@ -1466,6 +1466,7 @@ WHERE type=? AND id IN ( action, }) .await?; + context.send_sync_msg().await?; } } diff --git a/src/sync.rs b/src/sync.rs index 9e7de104b..a078e42d8 100644 --- a/src/sync.rs +++ b/src/sync.rs @@ -78,6 +78,9 @@ pub(crate) struct SyncItems { impl Context { /// Adds an item to the list of items that should be synchronized to other devices. + /// + /// NB: Private and `pub(crate)` functions shouldn't call this unless `Sync::Sync` is explicitly + /// passed to them. This way it's always clear whether the code performs synchronisation. pub(crate) async fn add_sync_item(&self, data: SyncData) -> Result<()> { self.add_sync_item_with_timestamp(data, time()).await } @@ -584,9 +587,8 @@ mod tests { alices[1].recv_msg(&sent_msg).await; async fn sync(alices: &[TestContext]) -> Result<()> { - alices.get(0).unwrap().send_sync_msg().await?.unwrap(); - let sent_msg = alices.get(0).unwrap().pop_sent_msg().await; - alices.get(1).unwrap().recv_msg(&sent_msg).await; + let sync_msg = alices.get(0).unwrap().pop_sent_msg().await; + alices.get(1).unwrap().recv_msg(&sync_msg).await; Ok(()) }