mirror of
https://github.com/chatmail/core.git
synced 2026-05-07 08:56:30 +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 {
|
if msg.get_viewtype() == Viewtype::Call {
|
||||||
msg.viewtype = Viewtype::Text;
|
msg.viewtype = Viewtype::Text;
|
||||||
}
|
}
|
||||||
|
msg.text += &msg.additional_text;
|
||||||
if msg.download_state != DownloadState::Done {
|
|
||||||
msg.text += &msg.additional_text;
|
|
||||||
}
|
|
||||||
|
|
||||||
let param = &mut param;
|
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::PostMessageFileBytes);
|
||||||
msg.param.remove(Param::PostMessageViewtype);
|
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() {
|
if !msg.original_msg_id.is_unset() {
|
||||||
bail!("message already saved.");
|
bail!("message already saved.");
|
||||||
|
|||||||
@@ -4,6 +4,9 @@ use std::borrow::Cow;
|
|||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::str;
|
use std::str;
|
||||||
|
|
||||||
|
use anyhow::Result;
|
||||||
|
use num_traits::FromPrimitive;
|
||||||
|
|
||||||
use crate::calls::{CallState, call_state};
|
use crate::calls::{CallState, call_state};
|
||||||
use crate::chat::Chat;
|
use crate::chat::Chat;
|
||||||
use crate::constants::Chattype;
|
use crate::constants::Chattype;
|
||||||
@@ -15,7 +18,6 @@ use crate::param::Param;
|
|||||||
use crate::stock_str;
|
use crate::stock_str;
|
||||||
use crate::stock_str::msg_reacted;
|
use crate::stock_str::msg_reacted;
|
||||||
use crate::tools::truncate;
|
use crate::tools::truncate;
|
||||||
use anyhow::Result;
|
|
||||||
|
|
||||||
/// Prefix displayed before message and separated by ":" in the chatlist.
|
/// Prefix displayed before message and separated by ":" in the chatlist.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@@ -167,7 +169,15 @@ impl Message {
|
|||||||
/// Returns a summary text without "Forwarded:" prefix.
|
/// Returns a summary text without "Forwarded:" prefix.
|
||||||
async fn get_summary_text_without_prefix(&self, context: &Context) -> String {
|
async fn get_summary_text_without_prefix(&self, context: &Context) -> String {
|
||||||
let (emoji, type_name, type_file, append_text);
|
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 => {
|
Viewtype::Image => {
|
||||||
emoji = Some("📷");
|
emoji = Some("📷");
|
||||||
type_name = Some(stock_str::image(context).await);
|
type_name = Some(stock_str::image(context).await);
|
||||||
@@ -211,20 +221,29 @@ impl Message {
|
|||||||
append_text = true
|
append_text = true
|
||||||
}
|
}
|
||||||
Viewtype::Webxdc => {
|
Viewtype::Webxdc => {
|
||||||
emoji = None;
|
|
||||||
type_name = None;
|
type_name = None;
|
||||||
type_file = Some(
|
if self.viewtype == Viewtype::Webxdc {
|
||||||
self.get_webxdc_info(context)
|
emoji = None;
|
||||||
.await
|
type_file = Some(
|
||||||
.map(|info| info.name)
|
self.get_webxdc_info(context)
|
||||||
.unwrap_or_else(|_| "ErrWebxdcName".to_string()),
|
.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;
|
append_text = true;
|
||||||
}
|
}
|
||||||
Viewtype::Vcard => {
|
Viewtype::Vcard => {
|
||||||
emoji = Some("👤");
|
emoji = Some("👤");
|
||||||
type_name = None;
|
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;
|
append_text = true;
|
||||||
}
|
}
|
||||||
Viewtype::Call => {
|
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 {
|
let summary = if let Some(type_file) = type_file {
|
||||||
if append_text && !text.is_empty() {
|
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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
@@ -302,11 +328,6 @@ mod tests {
|
|||||||
use crate::param::Param;
|
use crate::param::Param;
|
||||||
use crate::test_utils::TestContext;
|
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)]
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||||
async fn test_get_summary_text() {
|
async fn test_get_summary_text() {
|
||||||
let d = TestContext::new_alice().await;
|
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::mimeparser::MimeMessage;
|
||||||
use crate::param::Param;
|
use crate::param::Param;
|
||||||
use crate::reaction::{get_msg_reactions, send_reaction};
|
use crate::reaction::{get_msg_reactions, send_reaction};
|
||||||
|
use crate::summary::assert_summary_texts;
|
||||||
use crate::test_utils::TestContextManager;
|
use crate::test_utils::TestContextManager;
|
||||||
use crate::tests::pre_messages::util::{
|
use crate::tests::pre_messages::util::{
|
||||||
send_large_file_message, send_large_image_message, send_large_webxdc_message,
|
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_filebytes(bob).await?, Some(1_000_000));
|
||||||
assert_eq!(msg.get_post_message_viewtype(), Some(Viewtype::File));
|
assert_eq!(msg.get_post_message_viewtype(), Some(Viewtype::File));
|
||||||
assert_eq!(msg.get_filename(), Some("test.bin".to_owned()));
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,10 +17,10 @@ pub async fn send_large_file_message<'a>(
|
|||||||
content: &[u8],
|
content: &[u8],
|
||||||
) -> Result<(SentMessage<'a>, SentMessage<'a>, MsgId)> {
|
) -> Result<(SentMessage<'a>, SentMessage<'a>, MsgId)> {
|
||||||
let mut msg = Message::new(view_type);
|
let mut msg = Message::new(view_type);
|
||||||
let file_name = if view_type == Viewtype::Webxdc {
|
let file_name = match view_type {
|
||||||
"test.xdc"
|
Viewtype::Webxdc => "test.xdc",
|
||||||
} else {
|
Viewtype::Vcard => "test.vcf",
|
||||||
"test.bin"
|
_ => "test.bin",
|
||||||
};
|
};
|
||||||
msg.set_file_from_bytes(sender, file_name, content, None)?;
|
msg.set_file_from_bytes(sender, file_name, content, None)?;
|
||||||
msg.set_text("test".to_owned());
|
msg.set_text("test".to_owned());
|
||||||
|
|||||||
Reference in New Issue
Block a user