From 3c3710420b5156f212e98517908a5d27fef95366 Mon Sep 17 00:00:00 2001 From: iequidoo Date: Wed, 18 Jan 2023 13:47:32 -0300 Subject: [PATCH] Don't unpin chat on sending / receiving not fresh messages (#3967) This bug was introduced by 3cf78749df36126d35d8c6a88e2f2b598e7edb77 - there are three visibility states: Archived, Pinned and Normal - in the database, this "visibility" is named historically "archived" ... the original code has an AND archived=1 therefore. --- src/chat.rs | 52 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git a/src/chat.rs b/src/chat.rs index d80364b9f..5a9bfde64 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -544,10 +544,14 @@ impl ChatId { msg_state: MessageState, ) -> Result<()> { if msg_state != MessageState::InFresh { - context.sql.execute( - "UPDATE chats SET archived=0 WHERE id=? AND NOT(muted_until=-1 OR muted_until>?)", - paramsv![self, time()], - ).await?; + context + .sql + .execute( + "UPDATE chats SET archived=0 WHERE id=? AND archived=1 \ + AND NOT(muted_until=-1 OR muted_until>?)", + paramsv![self, time()], + ) + .await?; return Ok(()); } let chat = Chat::load_from_db(context, self).await?; @@ -4690,6 +4694,46 @@ mod tests { assert_eq!(chatlist, vec![chat_id3, chat_id2, chat_id1]); } + #[tokio::test(flavor = "multi_thread", worker_threads = 2)] + async fn test_pinned_after_new_msgs() -> Result<()> { + let alice = TestContext::new_alice().await; + let bob = TestContext::new_bob().await; + let alice_chat_id = alice.create_chat(&bob).await.id; + let bob_chat_id = bob.create_chat(&alice).await.id; + + assert!(alice_chat_id + .set_visibility(&alice, ChatVisibility::Pinned) + .await + .is_ok()); + assert_eq!( + Chat::load_from_db(&alice, alice_chat_id) + .await? + .get_visibility(), + ChatVisibility::Pinned, + ); + + send_text_msg(&alice, alice_chat_id, "hi!".into()).await?; + assert_eq!( + Chat::load_from_db(&alice, alice_chat_id) + .await? + .get_visibility(), + ChatVisibility::Pinned, + ); + + let mut msg = Message::new(Viewtype::Text); + msg.set_text(Some("hi!".into())); + let sent_msg = bob.send_msg(bob_chat_id, &mut msg).await; + let msg = alice.recv_msg(&sent_msg).await; + assert_eq!(msg.chat_id, alice_chat_id); + assert_eq!( + Chat::load_from_db(&alice, alice_chat_id) + .await? + .get_visibility(), + ChatVisibility::Pinned, + ); + Ok(()) + } + #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn test_set_chat_name() { let t = TestContext::new().await;