mirror of
https://github.com/chatmail/core.git
synced 2026-05-19 23:06:32 +03:00
fix: Shorten message text in locally sent messages too (#2281)
This commit is contained in:
18
src/chat.rs
18
src/chat.rs
@@ -46,8 +46,8 @@ use crate::stock_str;
|
|||||||
use crate::sync::{self, Sync::*, SyncData};
|
use crate::sync::{self, Sync::*, SyncData};
|
||||||
use crate::tools::{
|
use crate::tools::{
|
||||||
buf_compress, create_id, create_outgoing_rfc724_mid, create_smeared_timestamp,
|
buf_compress, create_id, create_outgoing_rfc724_mid, create_smeared_timestamp,
|
||||||
create_smeared_timestamps, get_abs_path, gm2local_offset, smeared_time, time, IsNoneOrEmpty,
|
create_smeared_timestamps, get_abs_path, gm2local_offset, smeared_time, time,
|
||||||
SystemTime,
|
truncate_msg_text, IsNoneOrEmpty, SystemTime,
|
||||||
};
|
};
|
||||||
use crate::webxdc::StatusUpdateSerial;
|
use crate::webxdc::StatusUpdateSerial;
|
||||||
|
|
||||||
@@ -2074,6 +2074,8 @@ impl Chat {
|
|||||||
msg.from_id = ContactId::SELF;
|
msg.from_id = ContactId::SELF;
|
||||||
msg.rfc724_mid = new_rfc724_mid;
|
msg.rfc724_mid = new_rfc724_mid;
|
||||||
msg.timestamp_sort = timestamp;
|
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
|
// add message to the database
|
||||||
if let Some(update_msg_id) = update_msg_id {
|
if let Some(update_msg_id) = update_msg_id {
|
||||||
@@ -2095,14 +2097,14 @@ impl Chat {
|
|||||||
msg.timestamp_sort,
|
msg.timestamp_sort,
|
||||||
msg.viewtype,
|
msg.viewtype,
|
||||||
msg.state,
|
msg.state,
|
||||||
msg.text,
|
msg_text,
|
||||||
message::normalize_text(&msg.text),
|
message::normalize_text(&msg_text),
|
||||||
&msg.subject,
|
&msg.subject,
|
||||||
msg.param.to_string(),
|
msg.param.to_string(),
|
||||||
msg.hidden,
|
msg.hidden,
|
||||||
msg.in_reply_to.as_deref().unwrap_or_default(),
|
msg.in_reply_to.as_deref().unwrap_or_default(),
|
||||||
new_references,
|
new_references,
|
||||||
new_mime_headers.is_some(),
|
mime_modified,
|
||||||
new_mime_headers.unwrap_or_default(),
|
new_mime_headers.unwrap_or_default(),
|
||||||
location_id as i32,
|
location_id as i32,
|
||||||
ephemeral_timer,
|
ephemeral_timer,
|
||||||
@@ -2146,14 +2148,14 @@ impl Chat {
|
|||||||
msg.timestamp_sort,
|
msg.timestamp_sort,
|
||||||
msg.viewtype,
|
msg.viewtype,
|
||||||
msg.state,
|
msg.state,
|
||||||
msg.text,
|
msg_text,
|
||||||
message::normalize_text(&msg.text),
|
message::normalize_text(&msg_text),
|
||||||
&msg.subject,
|
&msg.subject,
|
||||||
msg.param.to_string(),
|
msg.param.to_string(),
|
||||||
msg.hidden,
|
msg.hidden,
|
||||||
msg.in_reply_to.as_deref().unwrap_or_default(),
|
msg.in_reply_to.as_deref().unwrap_or_default(),
|
||||||
new_references,
|
new_references,
|
||||||
new_mime_headers.is_some(),
|
mime_modified,
|
||||||
new_mime_headers.unwrap_or_default(),
|
new_mime_headers.unwrap_or_default(),
|
||||||
location_id as i32,
|
location_id as i32,
|
||||||
ephemeral_timer,
|
ephemeral_timer,
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ use crate::aheader::{Aheader, EncryptPreference};
|
|||||||
use crate::blob::BlobObject;
|
use crate::blob::BlobObject;
|
||||||
use crate::chat::{add_info_msg, ChatId};
|
use crate::chat::{add_info_msg, ChatId};
|
||||||
use crate::config::Config;
|
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::contact::{Contact, ContactId, Origin};
|
||||||
use crate::context::Context;
|
use crate::context::Context;
|
||||||
use crate::decrypt::{
|
use crate::decrypt::{
|
||||||
@@ -34,7 +34,7 @@ use crate::peerstate::Peerstate;
|
|||||||
use crate::simplify::{simplify, SimplifiedText};
|
use crate::simplify::{simplify, SimplifiedText};
|
||||||
use crate::sync::SyncItems;
|
use crate::sync::SyncItems;
|
||||||
use crate::tools::{
|
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,
|
validate_id,
|
||||||
};
|
};
|
||||||
use crate::{chatlist_events, location, stock_str, tools};
|
use crate::{chatlist_events, location, stock_str, tools};
|
||||||
@@ -1179,22 +1179,11 @@ impl MimeMessage {
|
|||||||
(simplified_txt, top_quote)
|
(simplified_txt, top_quote)
|
||||||
};
|
};
|
||||||
|
|
||||||
let is_bot = context.get_config_bool(Config::Bot).await?;
|
let (simplified_txt, was_truncated) =
|
||||||
|
truncate_msg_text(context, simplified_txt).await?;
|
||||||
let simplified_txt = if is_bot {
|
if was_truncated {
|
||||||
simplified_txt
|
self.is_mime_modified = was_truncated;
|
||||||
} 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
|
|
||||||
};
|
|
||||||
|
|
||||||
if !simplified_txt.is_empty() || simplified_quote.is_some() {
|
if !simplified_txt.is_empty() || simplified_quote.is_some() {
|
||||||
let mut part = Part {
|
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());
|
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?;
|
t.set_config(Config::Bot, Some("1")).await?;
|
||||||
|
|
||||||
{
|
{
|
||||||
|
|||||||
19
src/tools.rs
19
src/tools.rs
@@ -36,7 +36,8 @@ use tokio::{fs, io};
|
|||||||
use url::Url;
|
use url::Url;
|
||||||
|
|
||||||
use crate::chat::{add_device_msg, add_device_msg_with_importance};
|
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::context::Context;
|
||||||
use crate::events::EventType;
|
use crate::events::EventType;
|
||||||
use crate::message::{Message, Viewtype};
|
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
|
* date/time tools
|
||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
|
|||||||
Reference in New Issue
Block a user