From 533f4fbb4ae13dcd384d0202d7c8349a6bb110c3 Mon Sep 17 00:00:00 2001 From: link2xt Date: Wed, 21 Jan 2026 08:58:44 +0000 Subject: [PATCH] add API to opt out of truncating long messages --- deltachat-jsonrpc/src/api.rs | 14 ++++++++++++-- src/context.rs | 12 ++++++++++++ src/message.rs | 18 ++++++++++++++++++ src/tools.rs | 5 ++++- 4 files changed, 46 insertions(+), 3 deletions(-) diff --git a/deltachat-jsonrpc/src/api.rs b/deltachat-jsonrpc/src/api.rs index ffdb16c6e..e52aea152 100644 --- a/deltachat-jsonrpc/src/api.rs +++ b/deltachat-jsonrpc/src/api.rs @@ -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. /// diff --git a/src/context.rs b/src/context.rs index adcb3b2a7..bea13148a 100644 --- a/src/context.rs +++ b/src/context.rs @@ -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(()), diff --git a/src/message.rs b/src/message.rs index 99db9eb35..2e8674427 100644 --- a/src/message.rs +++ b/src/message.rs @@ -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; diff --git a/src/tools.rs b/src/tools.rs index 30ec3ee08..f0cf1ce58 100644 --- a/src/tools.rs +++ b/src/tools.rs @@ -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