diff --git a/src/chat.rs b/src/chat.rs index 93f8cb8b6..9ee0a5f85 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -46,8 +46,8 @@ use crate::stock_str; use crate::sync::{self, Sync::*, SyncData}; use crate::tools::{ buf_compress, create_id, create_outgoing_rfc724_mid, create_smeared_timestamp, - create_smeared_timestamps, get_abs_path, gm2local_offset, smeared_time, time, IsNoneOrEmpty, - SystemTime, + create_smeared_timestamps, get_abs_path, gm2local_offset, smeared_time, time, + truncate_msg_text, IsNoneOrEmpty, SystemTime, }; use crate::webxdc::StatusUpdateSerial; @@ -2074,6 +2074,8 @@ impl Chat { msg.from_id = ContactId::SELF; msg.rfc724_mid = new_rfc724_mid; msg.timestamp_sort = timestamp; + let (msg_text, was_truncated) = truncate_msg_text(context, msg.text.clone()).await?; + let mime_modified = new_mime_headers.is_some() | was_truncated; // add message to the database if let Some(update_msg_id) = update_msg_id { @@ -2095,14 +2097,14 @@ impl Chat { msg.timestamp_sort, msg.viewtype, msg.state, - msg.text, - message::normalize_text(&msg.text), + msg_text, + message::normalize_text(&msg_text), &msg.subject, msg.param.to_string(), msg.hidden, msg.in_reply_to.as_deref().unwrap_or_default(), new_references, - new_mime_headers.is_some(), + mime_modified, new_mime_headers.unwrap_or_default(), location_id as i32, ephemeral_timer, @@ -2146,14 +2148,14 @@ impl Chat { msg.timestamp_sort, msg.viewtype, msg.state, - msg.text, - message::normalize_text(&msg.text), + msg_text, + message::normalize_text(&msg_text), &msg.subject, msg.param.to_string(), msg.hidden, msg.in_reply_to.as_deref().unwrap_or_default(), new_references, - new_mime_headers.is_some(), + mime_modified, new_mime_headers.unwrap_or_default(), location_id as i32, ephemeral_timer, diff --git a/src/mimeparser.rs b/src/mimeparser.rs index 027f3bdf0..a9cd67326 100644 --- a/src/mimeparser.rs +++ b/src/mimeparser.rs @@ -17,7 +17,7 @@ use crate::aheader::{Aheader, EncryptPreference}; use crate::blob::BlobObject; use crate::chat::{add_info_msg, ChatId}; use crate::config::Config; -use crate::constants::{self, Chattype, DC_DESIRED_TEXT_LINES, DC_DESIRED_TEXT_LINE_LEN}; +use crate::constants::{self, Chattype}; use crate::contact::{Contact, ContactId, Origin}; use crate::context::Context; use crate::decrypt::{ @@ -34,7 +34,7 @@ use crate::peerstate::Peerstate; use crate::simplify::{simplify, SimplifiedText}; use crate::sync::SyncItems; use crate::tools::{ - create_smeared_timestamp, get_filemeta, parse_receive_headers, smeared_time, truncate_by_lines, + create_smeared_timestamp, get_filemeta, parse_receive_headers, smeared_time, truncate_msg_text, validate_id, }; use crate::{chatlist_events, location, stock_str, tools}; @@ -1179,22 +1179,11 @@ impl MimeMessage { (simplified_txt, top_quote) }; - let is_bot = context.get_config_bool(Config::Bot).await?; - - let simplified_txt = if is_bot { - simplified_txt - } else { - // Truncate text if it has too many lines - let (simplified_txt, was_truncated) = truncate_by_lines( - simplified_txt, - DC_DESIRED_TEXT_LINES, - DC_DESIRED_TEXT_LINE_LEN, - ); - if was_truncated { - self.is_mime_modified = was_truncated; - } - simplified_txt - }; + let (simplified_txt, was_truncated) = + truncate_msg_text(context, simplified_txt).await?; + if was_truncated { + self.is_mime_modified = was_truncated; + } if !simplified_txt.is_empty() || simplified_quote.is_some() { let mut part = Part { @@ -3609,6 +3598,17 @@ On 2020-10-25, Bob wrote: assert!(mimemsg.parts[0].msg.len() <= DC_DESIRED_TEXT_LEN + DC_ELLIPSIS.len()); } + { + let chat = t.get_self_chat().await; + t.send_text(chat.id, &long_txt).await; + let msg = t.get_last_msg_in(chat.id).await; + assert!(msg.has_html()); + assert!( + msg.text.matches("just repeated").count() <= DC_DESIRED_TEXT_LEN / REPEAT_TXT.len() + ); + assert!(msg.text.len() <= DC_DESIRED_TEXT_LEN + DC_ELLIPSIS.len()); + } + t.set_config(Config::Bot, Some("1")).await?; { diff --git a/src/tools.rs b/src/tools.rs index d1ec13d6d..cdfce4eff 100644 --- a/src/tools.rs +++ b/src/tools.rs @@ -36,7 +36,8 @@ use tokio::{fs, io}; use url::Url; use crate::chat::{add_device_msg, add_device_msg_with_importance}; -use crate::constants::{DC_ELLIPSIS, DC_OUTDATED_WARNING_DAYS}; +use crate::config::Config; +use crate::constants::{self, DC_ELLIPSIS, DC_OUTDATED_WARNING_DAYS}; use crate::context::Context; use crate::events::EventType; use crate::message::{Message, Viewtype}; @@ -123,6 +124,22 @@ pub(crate) fn truncate_by_lines( } } +/// Shortens a message text if necessary according to the configuration. Adds "[...]" to the end of +/// the shortened text. +/// +/// 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? { + return Ok((text, false)); + } + // Truncate text if it has too many lines + Ok(truncate_by_lines( + text, + constants::DC_DESIRED_TEXT_LINES, + constants::DC_DESIRED_TEXT_LINE_LEN, + )) +} + /* ****************************************************************************** * date/time tools ******************************************************************************/