Do not send ephemeral timer updates to unpromoted chats

This commit is contained in:
link2xt
2022-12-15 19:40:29 +00:00
parent c3a0bb2b77
commit 90c478e58d
3 changed files with 63 additions and 10 deletions

View File

@@ -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<bool> {
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<bool> {
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<bool> {
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 {

View File

@@ -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<()> {