From 90c478e58db372b4e566ac140424c4c28a637748 Mon Sep 17 00:00:00 2001 From: link2xt Date: Thu, 15 Dec 2022 19:40:29 +0000 Subject: [PATCH] Do not send ephemeral timer updates to unpromoted chats --- CHANGELOG.md | 1 + src/chat.rs | 15 ++++++++++++- src/ephemeral.rs | 57 ++++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 63 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fad7f1347..a67f888a6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - If a classical-email-user sends an email to a group and adds new recipients, add the new recipients as group members #3781 - Remove `pytest-async` plugin #3846 +- Only send the message about ephemeral timer change if the chat is promoted #3847 ### API-Changes diff --git a/src/chat.rs b/src/chat.rs index 58928cf39..6bd3db115 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -806,6 +806,19 @@ impl ChatId { .unwrap_or_default()) } + /// Returns true if the chat is not promoted. + pub(crate) async fn is_unpromoted(self, context: &Context) -> Result { + let param = self.get_param(context).await?; + let unpromoted = param.get_bool(Param::Unpromoted).unwrap_or_default(); + Ok(unpromoted) + } + + /// Returns true if the chat is promoted. + pub(crate) async fn is_promoted(self, context: &Context) -> Result { + let promoted = !self.is_unpromoted(context).await?; + Ok(promoted) + } + // Returns true if chat is a saved messages chat. pub async fn is_self_talk(self, context: &Context) -> Result { Ok(self.get_param(context).await?.exists(Param::Selftalk)) @@ -1277,7 +1290,7 @@ impl Chat { } pub fn is_unpromoted(&self) -> bool { - self.param.get_int(Param::Unpromoted).unwrap_or_default() == 1 + self.param.get_bool(Param::Unpromoted).unwrap_or_default() } pub fn is_promoted(&self) -> bool { diff --git a/src/ephemeral.rs b/src/ephemeral.rs index aff6d7e32..1118ef077 100644 --- a/src/ephemeral.rs +++ b/src/ephemeral.rs @@ -205,14 +205,17 @@ impl ChatId { return Ok(()); } self.inner_set_ephemeral_timer(context, timer).await?; - let mut msg = Message::new(Viewtype::Text); - msg.text = Some(stock_ephemeral_timer_changed(context, timer, ContactId::SELF).await); - msg.param.set_cmd(SystemMessage::EphemeralTimerChanged); - if let Err(err) = send_msg(context, self, &mut msg).await { - error!( - context, - "Failed to send a message about ephemeral message timer change: {:?}", err - ); + + if self.is_promoted(context).await? { + let mut msg = Message::new(Viewtype::Text); + msg.text = Some(stock_ephemeral_timer_changed(context, timer, ContactId::SELF).await); + msg.param.set_cmd(SystemMessage::EphemeralTimerChanged); + if let Err(err) = send_msg(context, self, &mut msg).await { + error!( + context, + "Failed to send a message about ephemeral message timer change: {:?}", err + ); + } } Ok(()) } @@ -630,7 +633,7 @@ mod tests { use crate::test_utils::TestContext; use crate::tools::MAX_SECONDS_TO_LEND_FROM_FUTURE; use crate::{ - chat::{self, Chat, ChatItem}, + chat::{self, create_group_chat, send_text_msg, Chat, ChatItem, ProtectionStatus}, tools::IsNoneOrEmpty, }; @@ -795,6 +798,42 @@ mod tests { Ok(()) } + /// Test that enabling ephemeral timer in unpromoted group does not send a message. + #[tokio::test(flavor = "multi_thread", worker_threads = 2)] + async fn test_ephemeral_unpromoted() -> Result<()> { + let alice = TestContext::new_alice().await; + + let chat_id = + create_group_chat(&alice, ProtectionStatus::Unprotected, "Group name").await?; + + // Group is unpromoted, the timer can be changed without sending a message. + assert!(chat_id.is_unpromoted(&alice).await?); + chat_id + .set_ephemeral_timer(&alice, Timer::Enabled { duration: 60 }) + .await?; + let sent = alice.pop_sent_msg_opt(Duration::from_secs(1)).await; + assert!(sent.is_none()); + assert_eq!( + chat_id.get_ephemeral_timer(&alice).await?, + Timer::Enabled { duration: 60 } + ); + + // Promote the group. + send_text_msg(&alice, chat_id, "hi!".to_string()).await?; + assert!(chat_id.is_promoted(&alice).await?); + let sent = alice.pop_sent_msg_opt(Duration::from_secs(1)).await; + assert!(sent.is_some()); + + chat_id + .set_ephemeral_timer(&alice.ctx, Timer::Disabled) + .await?; + let sent = alice.pop_sent_msg_opt(Duration::from_secs(1)).await; + assert!(sent.is_some()); + assert_eq!(chat_id.get_ephemeral_timer(&alice).await?, Timer::Disabled); + + Ok(()) + } + /// Test that timer is enabled even if the message explicitly enabling the timer is lost. #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn test_ephemeral_enable_lost() -> Result<()> {