diff --git a/deltachat-ffi/deltachat.h b/deltachat-ffi/deltachat.h index 7d74c9f83..0168c13e2 100644 --- a/deltachat-ffi/deltachat.h +++ b/deltachat-ffi/deltachat.h @@ -495,6 +495,8 @@ char* dc_get_blobdir (const dc_context_t* context); * Post-Messages are automatically downloaded if they are smaller than the download_limit. * 0=no limit (default). * Changes affect future messages only. + * - `hide_pre_message_metadata_text` = Pre-Messages get information added to their text field. + * UI can set this to true when it already displays this information in a prettier manner. * - `protect_autocrypt` = Enable Header Protection for Autocrypt header. * This is an experimental option not compatible to other MUAs * and older Delta Chat versions. diff --git a/src/config.rs b/src/config.rs index 7c4dce49e..6609400bb 100644 --- a/src/config.rs +++ b/src/config.rs @@ -446,6 +446,12 @@ pub enum Config { /// Return an error from `receive_imf_inner()`. For tests. SimulateReceiveImfError, + + /// Disable the addition of the metadata to the text field of Pre-Messages. + /// + /// This is used by tests and can be used in the UI, + /// when the UI wants to displays that information in a prettier manner. + HidePreMessageMetadataText, } impl Config { diff --git a/src/context.rs b/src/context.rs index 26c05f636..3c1e14869 100644 --- a/src/context.rs +++ b/src/context.rs @@ -1093,6 +1093,13 @@ impl Context { .await? .unwrap_or_default(), ); + res.insert( + "hide_pre_message_metadata_text", + self.sql + .get_raw_config("hide_pre_message_metadata_text") + .await? + .unwrap_or_default(), + ); let elapsed = time_elapsed(&self.creation_time); res.insert("uptime", duration_to_str(elapsed)); diff --git a/src/imex.rs b/src/imex.rs index 261e76c27..eff10ef7a 100644 --- a/src/imex.rs +++ b/src/imex.rs @@ -783,6 +783,13 @@ async fn export_database( let res = conn .query_row("SELECT sqlcipher_export('backup')", [], |_row| Ok(())) .context("failed to export to attached backup database"); + // This option is device/client specific, so we reset it on backup. + // The test for this is tests::pre_messages::additional_text::test_disable_option_is_exluded_from_backup + conn.execute( + "UPDATE backup.config SET value='0' WHERE keyname=?;", + [Config::HidePreMessageMetadataText.to_string()], + ) + .ok(); conn.execute( "UPDATE backup.config SET value='0' WHERE keyname='verified_one_on_one_chats';", [], diff --git a/src/message.rs b/src/message.rs index d36e43e76..8646598d6 100644 --- a/src/message.rs +++ b/src/message.rs @@ -604,27 +604,32 @@ impl Message { param: &Params, ) -> Result { if download_state != DownloadState::Done { - let file_size = param - .get(Param::PostMessageFileBytes) - .and_then(|s| s.parse().ok()) - .map(|file_size: usize| format_size(file_size, BINARY)) - .unwrap_or("?".to_owned()); - let viewtype = param - .get_i64(Param::PostMessageViewtype) - .and_then(Viewtype::from_i64) - .unwrap_or(Viewtype::Unknown); - let file_name = param - .get(Param::Filename) - .map(sanitize_filename) - .unwrap_or("?".to_owned()); + let hide_pre_message_metadata = context + .get_config_bool(Config::HidePreMessageMetadataText) + .await?; + if !hide_pre_message_metadata { + let file_size = param + .get(Param::PostMessageFileBytes) + .and_then(|s| s.parse().ok()) + .map(|file_size: usize| format_size(file_size, BINARY)) + .unwrap_or("?".to_owned()); + let viewtype = param + .get_i64(Param::PostMessageViewtype) + .and_then(Viewtype::from_i64) + .unwrap_or(Viewtype::Unknown); + let file_name = param + .get(Param::Filename) + .map(sanitize_filename) + .unwrap_or("?".to_owned()); - return match viewtype { - Viewtype::File => Ok(format!(" [{file_name} - {file_size}]")), - _ => { - let translated_viewtype = viewtype.to_locale_string(context).await; - Ok(format!(" [{translated_viewtype} - {file_size}]")) - } - }; + return match viewtype { + Viewtype::File => Ok(format!(" [{file_name} - {file_size}]")), + _ => { + let translated_viewtype = viewtype.to_locale_string(context).await; + Ok(format!(" [{translated_viewtype} - {file_size}]")) + } + }; + } } Ok(String::new()) } diff --git a/src/tests/pre_messages/additional_text.rs b/src/tests/pre_messages/additional_text.rs index b894d34e9..a44efe724 100644 --- a/src/tests/pre_messages/additional_text.rs +++ b/src/tests/pre_messages/additional_text.rs @@ -1,7 +1,10 @@ use anyhow::Result; use pretty_assertions::assert_eq; -use crate::message::Viewtype; +use crate::EventType; +use crate::config::Config; +use crate::imex::{ImexMode, has_backup, imex}; +use crate::message::{Message, Viewtype}; use crate::test_utils::TestContextManager; use crate::tests::pre_messages::util::{ send_large_file_message, send_large_image_message, send_large_webxdc_message, @@ -38,3 +41,75 @@ async fn test_additional_text_on_different_viewtypes() -> Result<()> { Ok(()) } + +/// Test that disabling the addition of the download info works +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn test_disable_additional_text() -> Result<()> { + let mut tcm = TestContextManager::new(); + let alice = &tcm.alice().await; + let bob = &tcm.bob().await; + let alice_group_id = alice.create_group_with_members("test group", &[bob]).await; + + let (pre_message, _post_message, _alice_msg_id) = + send_large_file_message(alice, alice_group_id, Viewtype::File, &vec![0u8; 1_000_000]) + .await?; + let msg = bob.recv_msg(&pre_message).await; + assert_eq!(msg.get_text(), "test [test.bin - 976.56 KiB]".to_owned()); + + bob.set_config_bool(Config::HidePreMessageMetadataText, true) + .await?; + + let msg = Message::load_from_db(bob, msg.id).await?; + assert_eq!(msg.get_text(), "test".to_owned()); + Ok(()) +} + +/// Test that disabling the addition of the download info works +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn test_disable_option_is_exluded_from_backup() -> Result<()> { + let backup_dir = tempfile::tempdir()?; + let mut tcm = TestContextManager::new(); + let alice = &tcm.alice().await; + + tcm.section("set config option"); + alice + .set_config_bool(Config::HidePreMessageMetadataText, true) + .await?; + assert_eq!( + alice + .get_config_bool(Config::HidePreMessageMetadataText) + .await?, + true + ); + + tcm.section("export backup"); + imex(alice, ImexMode::ExportBackup, backup_dir.path(), None).await?; + let _event = alice + .evtracker + .get_matching(|evt| matches!(evt, EventType::ImexProgress(1000))) + .await; + + tcm.section("import backup"); + let alice2 = &tcm.unconfigured().await; + let backup = has_backup(alice2, backup_dir.path()).await?; + imex(alice2, ImexMode::ImportBackup, backup.as_ref(), None).await?; + let _event = alice2 + .evtracker + .get_matching(|evt| matches!(evt, EventType::ImexProgress(1000))) + .await; + assert_eq!( + alice.get_primary_self_addr().await?, + alice2.get_primary_self_addr().await?, + "address should be the same" + ); + + tcm.section("test if config is reset as expected"); + assert_eq!( + alice2 + .get_config_bool(Config::HidePreMessageMetadataText) + .await?, + false + ); + + Ok(()) +}