From 12cf89735c778b3c2832be32d8e75da8d7a31205 Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Sun, 4 Oct 2020 17:28:21 +0200 Subject: [PATCH] handling incoming protection-changes messages, always add info-msg --- src/chat.rs | 33 ++++++++++++++++++--------------- src/dc_receive_imf.rs | 23 +++++++++++++++++++++++ src/mimeparser.rs | 4 ++++ 3 files changed, 45 insertions(+), 15 deletions(-) diff --git a/src/chat.rs b/src/chat.rs index 8a88f6e7a..57696bb76 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -181,7 +181,6 @@ impl ChatId { self, context: &Context, protect: ProtectionStatus, - send_to_others: bool, ) -> Result<(), Error> { ensure!(!self.is_special(), "set protection: invalid chat-id."); @@ -224,7 +223,16 @@ impl ChatId { // make sure, the receivers will get all keys reset_gossiped_timestamp(context, self).await?; - // add info message + Ok(()) + } + + // adds or sends out a protection-info-message + pub(crate) async fn add_protection_msg( + self, + context: &Context, + protect: ProtectionStatus, + promote: bool, + ) -> Result<(), Error> { let msg_text = context .stock_system_msg( match protect { @@ -236,7 +244,8 @@ impl ChatId { DC_CONTACT_ID_SELF as u32, ) .await; - if send_to_others { + + if promote { let mut msg = Message::default(); msg.viewtype = Viewtype::Text; msg.text = Some(msg_text); @@ -261,16 +270,12 @@ impl ChatId { let chat = Chat::load_from_db(context, self).await?; - match self - .inner_set_protection(context, protect, chat.is_promoted()) - .await - { - Ok(()) => Ok(()), - Err(err) => { - error!(context, "{}", err); // make error user-visible - Err(err) - } + if let Err(e) = self.inner_set_protection(context, protect).await { + error!(context, "{}", e); // make error user-visible + return Err(e); } + + self.add_protection_msg(context, protect, chat.is_promoted()).await } /// Archives or unarchives a chat. @@ -2030,9 +2035,7 @@ pub async fn create_group_chat( }); if protect == ProtectionStatus::Protected { - chat_id - .inner_set_protection(context, protect, false) - .await?; + chat_id.set_protection(context, protect).await?; } Ok(chat_id) diff --git a/src/dc_receive_imf.rs b/src/dc_receive_imf.rs index 9abc9de6c..6e1ff2071 100644 --- a/src/dc_receive_imf.rs +++ b/src/dc_receive_imf.rs @@ -714,6 +714,17 @@ async fn add_parts( ephemeral_timer = EphemeralTimer::Disabled; } + // change chat protection + if mime_parser.is_system_message == SystemMessage::ChatProtectionEnabled { + chat_id + .inner_set_protection(context, ProtectionStatus::Protected) + .await?; + } else if mime_parser.is_system_message == SystemMessage::ChatProtectionDisabled { + chat_id + .inner_set_protection(context, ProtectionStatus::Unprotected) + .await?; + } + // correct message_timestamp, it should not be used before, // however, we cannot do this earlier as we need from_id to be set let in_fresh = state == MessageState::InFresh; @@ -1227,6 +1238,10 @@ async fn create_or_lookup_group( .await; chat_id_blocked = create_blocked; recreate_member_list = true; + + if create_protected == ProtectionStatus::Protected { + chat_id.add_protection_msg(context, ProtectionStatus::Protected, false).await?; + } } // again, check chat_id @@ -1278,7 +1293,15 @@ async fn create_or_lookup_group( } } } + } else if mime_parser.is_system_message == SystemMessage::ChatProtectionEnabled { + recreate_member_list = true; + if let Err(e) = check_verified_properties(context, mime_parser, from_id, to_ids).await { + warn!(context, "checking verified properties failed: {}", e); + let s = format!("{}. See 'Info' for more details", e); + mime_parser.repl_msg_by_error(s); + } } + if let Some(avatar_action) = &mime_parser.group_avatar { info!(context, "group-avatar change for {}", chat_id); if let Ok(mut chat) = Chat::load_from_db(context, chat_id).await { diff --git a/src/mimeparser.rs b/src/mimeparser.rs index 9d6b63922..42911a5bd 100644 --- a/src/mimeparser.rs +++ b/src/mimeparser.rs @@ -257,6 +257,10 @@ impl MimeMessage { self.is_system_message = SystemMessage::LocationStreamingEnabled; } else if value == "ephemeral-timer-changed" { self.is_system_message = SystemMessage::EphemeralTimerChanged; + } else if value == "protection-enabled" { + self.is_system_message = SystemMessage::ChatProtectionEnabled; + } else if value == "protection-disabled" { + self.is_system_message = SystemMessage::ChatProtectionDisabled; } } Ok(())