From 1caf3caf1bcc466e9140293c2512bb1570c924dd Mon Sep 17 00:00:00 2001 From: bjoern Date: Thu, 3 Feb 2022 20:40:24 +0100 Subject: [PATCH] do set_visibility() in a transaction (#3053) this avoids archived chats containing fresh messages: before, it could happen that between the two SQL calls a new fresh message arrives, unarchives the chat that is immediately archived by the second SQL call - resulting in an archive chat containing fresh messages. as fresh messages counter are shown on app icon etc. this is pretty weird for the user as they do not see what is "fresh". the other way round, there is no transaction in receive_imf(), however, receive_imf() only unarchives chats, so that is visible and no big issue for the user. the issue is rare at all, however, annoying if you get that as the badge counter may be stuck at "1" nearly forever (until you open the archived chat in question). --- CHANGELOG.md | 6 ++++++ src/chat.rs | 27 +++++++++++++-------------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index df7e65039..6bdd844e9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## Unreleased + +### Fixes +- avoid archived, fresh chats #3053 + + ## 1.75.0 ### Changes diff --git a/src/chat.rs b/src/chat.rs index c22e63f8b..50fc41035 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -474,22 +474,21 @@ impl ChatId { self ); - if visibility == ChatVisibility::Archived { - context - .sql - .execute( - "UPDATE msgs SET state=? WHERE chat_id=? AND state=?;", - paramsv![MessageState::InNoticed, self, MessageState::InFresh], - ) - .await?; - } - context .sql - .execute( - "UPDATE chats SET archived=? WHERE id=?;", - paramsv![visibility, self], - ) + .transaction(move |transaction| { + if visibility == ChatVisibility::Archived { + transaction.execute( + "UPDATE msgs SET state=? WHERE chat_id=? AND state=?;", + paramsv![MessageState::InNoticed, self, MessageState::InFresh], + )?; + } + transaction.execute( + "UPDATE chats SET archived=? WHERE id=?;", + paramsv![visibility, self], + )?; + Ok(()) + }) .await?; context.emit_event(EventType::MsgsChanged {