Skip to content

Commit 1d1f074

Browse files
committed
feat: stop truncating message texts
After this change message texts should be now truncated in the UIs.
1 parent f5e1e26 commit 1d1f074

File tree

4 files changed

+5
-168
lines changed

4 files changed

+5
-168
lines changed

src/chat.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ use crate::sync::{self, Sync::*, SyncData};
4949
use crate::tools::{
5050
IsNoneOrEmpty, SystemTime, buf_compress, create_broadcast_secret, create_id,
5151
create_outgoing_rfc724_mid, create_smeared_timestamp, create_smeared_timestamps, get_abs_path,
52-
gm2local_offset, normalize_text, smeared_time, time, truncate_msg_text,
52+
gm2local_offset, normalize_text, smeared_time, time,
5353
};
5454
use crate::webxdc::StatusUpdateSerial;
5555

@@ -1884,7 +1884,8 @@ impl Chat {
18841884
EphemeralTimer::Enabled { duration } => time().saturating_add(duration.into()),
18851885
};
18861886

1887-
let (msg_text, was_truncated) = truncate_msg_text(context, msg.text.clone()).await?;
1887+
let msg_text = msg.text.clone();
1888+
18881889
let new_mime_headers = if msg.has_html() {
18891890
if msg.param.exists(Param::Forwarded) {
18901891
msg.get_id().get_html(context).await?
@@ -1901,13 +1902,6 @@ impl Chat {
19011902
html_part.write_part(cursor).ok();
19021903
String::from_utf8_lossy(&buffer).to_string()
19031904
});
1904-
let new_mime_headers = new_mime_headers.or_else(|| match was_truncated {
1905-
// We need to add some headers so that they are stripped before formatting HTML by
1906-
// `MsgId::get_html()`, not a part of the actual text. Let's add "Content-Type", it's
1907-
// anyway a useful metadata about the stored text.
1908-
true => Some("Content-Type: text/plain; charset=utf-8\r\n\r\n".to_string() + &msg.text),
1909-
false => None,
1910-
});
19111905
let new_mime_headers = match new_mime_headers {
19121906
Some(h) => Some(tokio::task::block_in_place(move || {
19131907
buf_compress(h.as_bytes())

src/mimeparser.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ use crate::message::{self, Message, MsgId, Viewtype, get_vcard_summary, set_msg_
3232
use crate::param::{Param, Params};
3333
use crate::simplify::{SimplifiedText, simplify};
3434
use crate::sync::SyncItems;
35-
use crate::tools::{
36-
get_filemeta, parse_receive_headers, smeared_time, time, truncate_msg_text, validate_id,
37-
};
35+
use crate::tools::{get_filemeta, parse_receive_headers, smeared_time, time, validate_id};
3836
use crate::{chatlist_events, location, tools};
3937

4038
/// Public key extracted from `Autocrypt-Gossip`
@@ -1445,12 +1443,6 @@ impl MimeMessage {
14451443
(simplified_txt, top_quote)
14461444
};
14471445

1448-
let (simplified_txt, was_truncated) =
1449-
truncate_msg_text(context, simplified_txt).await?;
1450-
if was_truncated {
1451-
self.is_mime_modified = was_truncated;
1452-
}
1453-
14541446
if !simplified_txt.is_empty() || simplified_quote.is_some() {
14551447
let mut part = Part {
14561448
dehtml_failed,

src/tools.rs

Lines changed: 1 addition & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ use url::Url;
3737
use uuid::Uuid;
3838

3939
use crate::chat::{add_device_msg, add_device_msg_with_importance};
40-
use crate::config::Config;
41-
use crate::constants::{self, DC_ELLIPSIS, DC_OUTDATED_WARNING_DAYS};
40+
use crate::constants::{DC_ELLIPSIS, DC_OUTDATED_WARNING_DAYS};
4241
use crate::context::Context;
4342
use crate::events::EventType;
4443
use crate::log::warn;
@@ -73,81 +72,6 @@ pub(crate) fn truncate(buf: &str, approx_chars: usize) -> Cow<'_, str> {
7372
}
7473
}
7574

76-
/// Shortens a string to a specified line count and adds "[...]" to the
77-
/// end of the shortened string.
78-
///
79-
/// returns tuple with the String and a boolean whether is was truncated
80-
pub(crate) fn truncate_by_lines(
81-
buf: String,
82-
max_lines: usize,
83-
max_line_len: usize,
84-
) -> (String, bool) {
85-
let mut lines = 0;
86-
let mut line_chars = 0;
87-
let mut break_point: Option<usize> = None;
88-
89-
for (index, char) in buf.char_indices() {
90-
if char == '\n' {
91-
line_chars = 0;
92-
lines += 1;
93-
} else {
94-
line_chars += 1;
95-
if line_chars > max_line_len {
96-
line_chars = 1;
97-
lines += 1;
98-
}
99-
}
100-
if lines == max_lines {
101-
break_point = Some(index);
102-
break;
103-
}
104-
}
105-
106-
if let Some(end_pos) = break_point {
107-
// Text has too many lines and needs to be truncated.
108-
let text = {
109-
if let Some(buffer) = buf.get(..end_pos) {
110-
if let Some(index) = buffer.rfind([' ', '\n']) {
111-
buf.get(..=index)
112-
} else {
113-
buf.get(..end_pos)
114-
}
115-
} else {
116-
None
117-
}
118-
};
119-
120-
if let Some(truncated_text) = text {
121-
(format!("{truncated_text}{DC_ELLIPSIS}"), true)
122-
} else {
123-
// In case of indexing/slicing error, we return an error
124-
// message as a preview and add HTML version. This should
125-
// never happen.
126-
let error_text = "[Truncation of the message failed, this is a bug in the Delta Chat core. Please report it.\nYou can still open the full text to view the original message.]";
127-
(error_text.to_string(), true)
128-
}
129-
} else {
130-
// text is unchanged
131-
(buf, false)
132-
}
133-
}
134-
135-
/// Shortens a message text if necessary according to the configuration. Adds "[...]" to the end of
136-
/// the shortened text.
137-
///
138-
/// Returns the resulting text and a bool telling whether a truncation was done.
139-
pub(crate) async fn truncate_msg_text(context: &Context, text: String) -> Result<(String, bool)> {
140-
if context.get_config_bool(Config::Bot).await? {
141-
return Ok((text, false));
142-
}
143-
// Truncate text if it has too many lines
144-
Ok(truncate_by_lines(
145-
text,
146-
constants::DC_DESIRED_TEXT_LINES,
147-
constants::DC_DESIRED_TEXT_LINE_LEN,
148-
))
149-
}
150-
15175
/* ******************************************************************************
15276
* date/time tools
15377
******************************************************************************/

src/tools/tools_tests.rs

Lines changed: 0 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -120,79 +120,6 @@ fn test_truncate_edge() {
120120
);
121121
}
122122

123-
mod truncate_by_lines {
124-
use super::*;
125-
126-
#[test]
127-
fn test_just_text() {
128-
let s = "this is a little test string".to_string();
129-
assert_eq!(
130-
truncate_by_lines(s, 4, 6),
131-
("this is a little test [...]".to_string(), true)
132-
);
133-
}
134-
135-
#[test]
136-
fn test_with_linebreaks() {
137-
let s = "this\n is\n a little test string".to_string();
138-
assert_eq!(
139-
truncate_by_lines(s, 4, 6),
140-
("this\n is\n a little [...]".to_string(), true)
141-
);
142-
}
143-
144-
#[test]
145-
fn test_only_linebreaks() {
146-
let s = "\n\n\n\n\n\n\n".to_string();
147-
assert_eq!(
148-
truncate_by_lines(s, 4, 5),
149-
("\n\n\n[...]".to_string(), true)
150-
);
151-
}
152-
153-
#[test]
154-
fn limit_hits_end() {
155-
let s = "hello\n world !".to_string();
156-
assert_eq!(
157-
truncate_by_lines(s, 2, 8),
158-
("hello\n world !".to_string(), false)
159-
);
160-
}
161-
162-
#[test]
163-
fn test_edge() {
164-
assert_eq!(
165-
truncate_by_lines("".to_string(), 2, 4),
166-
("".to_string(), false)
167-
);
168-
169-
assert_eq!(
170-
truncate_by_lines("\n hello \n world".to_string(), 2, 4),
171-
("\n [...]".to_string(), true)
172-
);
173-
assert_eq!(
174-
truncate_by_lines("𐠈0Aᝮa𫝀®!ꫛa¡0A𐢧00𐹠®A 丽ⷐએ".to_string(), 1, 2),
175-
("𐠈0[...]".to_string(), true)
176-
);
177-
assert_eq!(
178-
truncate_by_lines("𐠈0Aᝮa𫝀®!ꫛa¡0A𐢧00𐹠®A 丽ⷐએ".to_string(), 1, 0),
179-
("[...]".to_string(), true)
180-
);
181-
182-
// 9 characters, so no truncation
183-
assert_eq!(
184-
truncate_by_lines("𑒀ὐ¢🜀\u{1e01b}A a🟠".to_string(), 1, 12),
185-
("𑒀ὐ¢🜀\u{1e01b}A a🟠".to_string(), false),
186-
);
187-
188-
// 12 characters, truncation
189-
assert_eq!(
190-
truncate_by_lines("𑒀ὐ¢🜀\u{1e01b}A a🟠bcd".to_string(), 1, 7),
191-
("𑒀ὐ¢🜀\u{1e01b}A [...]".to_string(), true),
192-
);
193-
}
194-
}
195-
196123
#[test]
197124
fn test_create_id() {
198125
let buf = create_id();

0 commit comments

Comments
 (0)