add hide_pre_message_metadata_text config option to hide the additional text that would get added to pre-messages

This commit is contained in:
Simon Laux
2025-12-03 17:46:04 +01:00
committed by link2xt
parent bfc58e9204
commit 612f3f32aa
6 changed files with 123 additions and 21 deletions

View File

@@ -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.

View File

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

View File

@@ -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));

View File

@@ -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';",
[],

View File

@@ -604,27 +604,32 @@ impl Message {
param: &Params,
) -> Result<String> {
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())
}

View File

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