mirror of
https://github.com/chatmail/core.git
synced 2026-04-28 19:06:35 +03:00
feat: Make summary for pre-messages look like summary for fully downloaded messages (#7775)
This is not possible for webxdcs and vCards currently however, so add workarounds for them:
- Use translated "Mini App" as the webxdc name.
- Use just "👤" instead of the vCard summary (i.e. the vCard contact name).
This commit is contained in:
@@ -4374,10 +4374,7 @@ pub async fn forward_msgs_2ctx(
|
||||
if msg.get_viewtype() == Viewtype::Call {
|
||||
msg.viewtype = Viewtype::Text;
|
||||
}
|
||||
|
||||
if msg.download_state != DownloadState::Done {
|
||||
msg.text += &msg.additional_text;
|
||||
}
|
||||
msg.text += &msg.additional_text;
|
||||
|
||||
let param = &mut param;
|
||||
|
||||
@@ -4465,9 +4462,7 @@ pub(crate) async fn save_copy_in_self_talk(
|
||||
msg.param.remove(Param::PostMessageFileBytes);
|
||||
msg.param.remove(Param::PostMessageViewtype);
|
||||
|
||||
if msg.download_state != DownloadState::Done {
|
||||
msg.text += &msg.additional_text;
|
||||
}
|
||||
msg.text += &msg.additional_text;
|
||||
|
||||
if !msg.original_msg_id.is_unset() {
|
||||
bail!("message already saved.");
|
||||
|
||||
@@ -4,6 +4,9 @@ use std::borrow::Cow;
|
||||
use std::fmt;
|
||||
use std::str;
|
||||
|
||||
use anyhow::Result;
|
||||
use num_traits::FromPrimitive;
|
||||
|
||||
use crate::calls::{CallState, call_state};
|
||||
use crate::chat::Chat;
|
||||
use crate::constants::Chattype;
|
||||
@@ -15,7 +18,6 @@ use crate::param::Param;
|
||||
use crate::stock_str;
|
||||
use crate::stock_str::msg_reacted;
|
||||
use crate::tools::truncate;
|
||||
use anyhow::Result;
|
||||
|
||||
/// Prefix displayed before message and separated by ":" in the chatlist.
|
||||
#[derive(Debug)]
|
||||
@@ -167,7 +169,15 @@ impl Message {
|
||||
/// Returns a summary text without "Forwarded:" prefix.
|
||||
async fn get_summary_text_without_prefix(&self, context: &Context) -> String {
|
||||
let (emoji, type_name, type_file, append_text);
|
||||
match self.viewtype {
|
||||
let viewtype = match self
|
||||
.param
|
||||
.get_i64(Param::PostMessageViewtype)
|
||||
.and_then(Viewtype::from_i64)
|
||||
{
|
||||
Some(vt) => vt,
|
||||
None => self.viewtype,
|
||||
};
|
||||
match viewtype {
|
||||
Viewtype::Image => {
|
||||
emoji = Some("📷");
|
||||
type_name = Some(stock_str::image(context).await);
|
||||
@@ -211,20 +221,29 @@ impl Message {
|
||||
append_text = true
|
||||
}
|
||||
Viewtype::Webxdc => {
|
||||
emoji = None;
|
||||
type_name = None;
|
||||
type_file = Some(
|
||||
self.get_webxdc_info(context)
|
||||
.await
|
||||
.map(|info| info.name)
|
||||
.unwrap_or_else(|_| "ErrWebxdcName".to_string()),
|
||||
);
|
||||
if self.viewtype == Viewtype::Webxdc {
|
||||
emoji = None;
|
||||
type_file = Some(
|
||||
self.get_webxdc_info(context)
|
||||
.await
|
||||
.map(|info| info.name)
|
||||
.unwrap_or_else(|_| "ErrWebxdcName".to_string()),
|
||||
);
|
||||
} else {
|
||||
emoji = Some("📱");
|
||||
type_file = Some(viewtype.to_locale_string(context).await);
|
||||
}
|
||||
append_text = true;
|
||||
}
|
||||
Viewtype::Vcard => {
|
||||
emoji = Some("👤");
|
||||
type_name = None;
|
||||
type_file = self.param.get(Param::Summary1).map(|s| s.to_string());
|
||||
if self.viewtype == Viewtype::Vcard {
|
||||
type_file = self.param.get(Param::Summary1).map(|s| s.to_string());
|
||||
} else {
|
||||
type_file = None;
|
||||
}
|
||||
append_text = true;
|
||||
}
|
||||
Viewtype::Call => {
|
||||
@@ -261,7 +280,7 @@ impl Message {
|
||||
}
|
||||
};
|
||||
|
||||
let text = self.text.clone() + &self.additional_text;
|
||||
let text = self.text.clone();
|
||||
|
||||
let summary = if let Some(type_file) = type_file {
|
||||
if append_text && !text.is_empty() {
|
||||
@@ -293,6 +312,13 @@ impl Message {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
/// Asserts that the summary text with and w/o prefix is `expected`.
|
||||
pub async fn assert_summary_texts(msg: &Message, ctx: &Context, expected: &str) {
|
||||
assert_eq!(msg.get_summary_text(ctx).await, expected);
|
||||
assert_eq!(msg.get_summary_text_without_prefix(ctx).await, expected);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::path::PathBuf;
|
||||
@@ -302,11 +328,6 @@ mod tests {
|
||||
use crate::param::Param;
|
||||
use crate::test_utils::TestContext;
|
||||
|
||||
async fn assert_summary_texts(msg: &Message, ctx: &Context, expected: &str) {
|
||||
assert_eq!(msg.get_summary_text(ctx).await, expected);
|
||||
assert_eq!(msg.get_summary_text_without_prefix(ctx).await, expected);
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn test_get_summary_text() {
|
||||
let d = TestContext::new_alice().await;
|
||||
|
||||
@@ -10,6 +10,7 @@ use crate::message::{Message, MessageState, Viewtype, delete_msgs, markseen_msgs
|
||||
use crate::mimeparser::MimeMessage;
|
||||
use crate::param::Param;
|
||||
use crate::reaction::{get_msg_reactions, send_reaction};
|
||||
use crate::summary::assert_summary_texts;
|
||||
use crate::test_utils::TestContextManager;
|
||||
use crate::tests::pre_messages::util::{
|
||||
send_large_file_message, send_large_image_message, send_large_webxdc_message,
|
||||
@@ -76,6 +77,39 @@ async fn test_receive_pre_message() -> Result<()> {
|
||||
assert_eq!(msg.get_filebytes(bob).await?, Some(1_000_000));
|
||||
assert_eq!(msg.get_post_message_viewtype(), Some(Viewtype::File));
|
||||
assert_eq!(msg.get_filename(), Some("test.bin".to_owned()));
|
||||
assert_summary_texts(&msg, bob, "📎 test.bin – test").await;
|
||||
|
||||
// Some viewtypes are displayed unwell currently, still test them.
|
||||
|
||||
let (pre_message, ..) = send_large_webxdc_message(alice, alice_group_id).await?;
|
||||
let msg = bob.recv_msg(&pre_message).await;
|
||||
assert_eq!(msg.download_state, DownloadState::Available);
|
||||
assert_summary_texts(
|
||||
&msg,
|
||||
bob,
|
||||
&format!("📱 {} – test", Viewtype::Webxdc.to_locale_string(bob).await),
|
||||
)
|
||||
.await;
|
||||
|
||||
let (pre_message, ..) = send_large_file_message(
|
||||
alice,
|
||||
alice_group_id,
|
||||
Viewtype::Vcard,
|
||||
format!(
|
||||
"BEGIN:VCARD\r\n\
|
||||
VERSION:4.0\r\n\
|
||||
EMAIL:alice@example.org\r\n\
|
||||
FN:Alice\r\n\
|
||||
NOTE:{}\r\n\
|
||||
END:VCARD\r\n",
|
||||
String::from_utf8(vec![97u8; 1_000_000])?,
|
||||
)
|
||||
.as_bytes(),
|
||||
)
|
||||
.await?;
|
||||
let msg = bob.recv_msg(&pre_message).await;
|
||||
assert_eq!(msg.download_state, DownloadState::Available);
|
||||
assert_summary_texts(&msg, bob, "👤 test").await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -17,10 +17,10 @@ pub async fn send_large_file_message<'a>(
|
||||
content: &[u8],
|
||||
) -> Result<(SentMessage<'a>, SentMessage<'a>, MsgId)> {
|
||||
let mut msg = Message::new(view_type);
|
||||
let file_name = if view_type == Viewtype::Webxdc {
|
||||
"test.xdc"
|
||||
} else {
|
||||
"test.bin"
|
||||
let file_name = match view_type {
|
||||
Viewtype::Webxdc => "test.xdc",
|
||||
Viewtype::Vcard => "test.vcf",
|
||||
_ => "test.bin",
|
||||
};
|
||||
msg.set_file_from_bytes(sender, file_name, content, None)?;
|
||||
msg.set_text("test".to_owned());
|
||||
|
||||
Reference in New Issue
Block a user