add API to opt out of truncating long messages

This commit is contained in:
link2xt
2026-01-21 08:58:44 +00:00
parent f5c374ec62
commit 533f4fbb4a
4 changed files with 46 additions and 3 deletions

View File

@@ -23,8 +23,9 @@ use deltachat::ephemeral::Timer;
use deltachat::imex;
use deltachat::location;
use deltachat::message::{
self, delete_msgs_ex, get_existing_msg_ids, get_msg_read_receipt_count, get_msg_read_receipts,
markseen_msgs, Message, MessageState, MsgId, Viewtype,
self, delete_msgs_ex, dont_truncate_long_messages, get_existing_msg_ids,
get_msg_read_receipt_count, get_msg_read_receipts, markseen_msgs, Message, MessageState, MsgId,
Viewtype,
};
use deltachat::peer_channels::{
leave_webxdc_realtime, send_webxdc_realtime_advertisement, send_webxdc_realtime_data,
@@ -1434,6 +1435,15 @@ impl CommandApi {
MsgId::new(message_id).get_html(&ctx).await
}
/// Opt out of truncating long messages when loading.
///
/// Should be used by the UIs that can handle long text messages.
async fn dont_truncate_long_messages(&self, account_id: u32) -> Result<()> {
let ctx = self.get_context(account_id).await?;
dont_truncate_long_messages(&ctx);
Ok(())
}
/// get multiple messages in one call,
/// if loading one message fails the error is stored in the result object in it's place.
///

View File

@@ -227,7 +227,18 @@ impl WeakContext {
pub struct InnerContext {
/// Blob directory path
pub(crate) blobdir: PathBuf,
pub(crate) sql: Sql,
/// True if long text messages should be truncated
/// and full message HTML added.
///
/// This should be set by the UIs that cannot handle
/// long messages but can display HTML messages.
///
/// Ignored for bots, bots never get truncated messages.
pub(crate) truncate_long_messages: AtomicBool,
pub(crate) smeared_timestamp: SmearedTimestamp,
/// The global "ongoing" process state.
///
@@ -491,6 +502,7 @@ impl Context {
blobdir,
running_state: RwLock::new(Default::default()),
sql: Sql::new(dbfile),
truncate_long_messages: AtomicBool::new(true),
smeared_timestamp: SmearedTimestamp::new(),
oauth2_mutex: Mutex::new(()),
wrong_pw_warning_mutex: Mutex::new(()),

View File

@@ -4,6 +4,7 @@ use std::collections::BTreeSet;
use std::collections::HashSet;
use std::path::{Path, PathBuf};
use std::str;
use std::sync::atomic::Ordering;
use anyhow::{Context as _, Result, ensure, format_err};
use deltachat_contact_tools::{VcardContact, parse_vcard};
@@ -2398,5 +2399,22 @@ impl Viewtype {
}
}
/// Opt out of truncating long messages.
///
/// After calling this function, long messages
/// will not be truncated during loading.
///
/// UIs should call this function if they
/// can handle long messages by cutting them
/// and displaying "Show full message" option.
///
/// Has no effect for bots which never
/// truncate messages when loading.
pub fn dont_truncate_long_messages(context: &Context) {
context
.truncate_long_messages
.store(false, Ordering::Relaxed);
}
#[cfg(test)]
mod message_tests;

View File

@@ -9,6 +9,7 @@ use std::mem;
use std::ops::{AddAssign, Deref};
use std::path::{Path, PathBuf};
use std::str::from_utf8;
use std::sync::atomic::Ordering;
// If a time value doesn't need to be sent to another host, saved to the db or otherwise used across
// program restarts, a monotonically nondecreasing clock (`Instant`) should be used. But as
// `Instant` may use `libc::clock_gettime(CLOCK_MONOTONIC)`, e.g. on Android, and does not advance
@@ -139,7 +140,9 @@ pub(crate) fn truncate_by_lines(
///
/// Returns the resulting text and a bool telling whether a truncation was done.
pub(crate) async fn truncate_msg_text(context: &Context, text: String) -> Result<(String, bool)> {
if context.get_config_bool(Config::Bot).await? {
if !context.truncate_long_messages.load(Ordering::Relaxed)
|| context.get_config_bool(Config::Bot).await?
{
return Ok((text, false));
}
// Truncate text if it has too many lines