feat: truncate long messages when loading instead of saving

This commit is contained in:
link2xt
2026-01-19 21:26:39 +00:00
parent b148be2618
commit f5c374ec62
6 changed files with 47 additions and 57 deletions

View File

@@ -34,10 +34,9 @@ use crate::reaction::get_msg_reactions;
use crate::sql;
use crate::summary::Summary;
use crate::sync::SyncData;
use crate::tools::create_outgoing_rfc724_mid;
use crate::tools::{
buf_compress, buf_decompress, get_filebytes, get_filemeta, gm2local_offset, read_file,
sanitize_filename, time, timestamp_to_str,
buf_compress, buf_decompress, create_outgoing_rfc724_mid, get_filebytes, get_filemeta,
gm2local_offset, read_file, sanitize_filename, time, timestamp_to_str, truncate_msg_text,
};
/// Message ID, including reserved IDs.
@@ -431,7 +430,13 @@ pub struct Message {
pub(crate) timestamp_rcvd: i64,
pub(crate) ephemeral_timer: EphemeralTimer,
pub(crate) ephemeral_timestamp: i64,
/// Message text, possibly truncated if the message is large.
pub(crate) text: String,
/// Full text if the message text is truncated.
pub(crate) full_text: Option<String>,
/// Text that is added to the end of Message.text
///
/// Currently used for adding the download information on pre-messages
@@ -556,6 +561,7 @@ impl Message {
}
_ => String::new(),
};
let msg = Message {
id: row.get("id")?,
rfc724_mid: row.get::<_, String>("rfc724mid")?,
@@ -580,6 +586,7 @@ impl Message {
original_msg_id: row.get("original_msg_id")?,
mime_modified: row.get("mime_modified")?,
text,
full_text: None,
additional_text: String::new(),
subject: row.get("subject")?,
param: row.get::<_, String>("param")?.parse().unwrap_or_default(),
@@ -597,6 +604,15 @@ impl Message {
.with_context(|| format!("failed to load message {id} from the database"))?;
if let Some(msg) = &mut msg {
if !msg.mime_modified {
let (truncated_text, was_truncated) =
truncate_msg_text(context, msg.text.clone()).await?;
if was_truncated {
msg.full_text = Some(msg.text.clone());
msg.text = truncated_text;
}
}
msg.additional_text =
Self::get_additional_text(context, msg.download_state, &msg.param).await?;
}