Remove get_summarytext_by_raw

Use `Summary.truncated_text()` instead.

Co-Authored-By: Floris Bruynooghe <flub@devork.be>
This commit is contained in:
link2xt
2021-10-09 10:39:44 +00:00
parent 49b07c1c6a
commit a3562c5940
5 changed files with 158 additions and 237 deletions

View File

@@ -7,15 +7,12 @@ use crate::context::Context;
use crate::dc_tools::dc_truncate;
use crate::message::{Message, MessageState};
use crate::mimeparser::SystemMessage;
use crate::param::{Param, Params};
use crate::param::Param;
use crate::stock_str;
use itertools::Itertools;
use std::borrow::Cow;
use std::fmt;
// In practice, the user additionally cuts the string themselves
// pixel-accurate.
const SUMMARY_CHARACTERS: usize = 160;
/// Prefix displayed before message and separated by ":" in the chatlist.
#[derive(Debug)]
pub enum SummaryPrefix {
@@ -85,15 +82,7 @@ impl Summary {
}
};
let mut text = get_summarytext_by_raw(
msg.viewtype,
msg.text.as_ref(),
msg.is_forwarded(),
&msg.param,
SUMMARY_CHARACTERS,
context,
)
.await;
let mut text = msg.get_summary_text(context).await;
if text.is_empty() && msg.quoted_text().is_some() {
text = stock_str::reply_noun(context).await
@@ -106,88 +95,87 @@ impl Summary {
state: msg.state,
}
}
/// Returns the [`Summary::text`] attribute truncated to an approximate length.
pub fn truncated_text(&self, approx_chars: usize) -> Cow<str> {
dc_truncate(&self.text, approx_chars)
}
}
/// Returns a summary text.
pub async fn get_summarytext_by_raw(
viewtype: Viewtype,
text: Option<impl AsRef<str>>,
was_forwarded: bool,
param: &Params,
approx_characters: usize,
context: &Context,
) -> String {
let mut append_text = true;
let prefix = match viewtype {
Viewtype::Image => stock_str::image(context).await,
Viewtype::Gif => stock_str::gif(context).await,
Viewtype::Sticker => stock_str::sticker(context).await,
Viewtype::Video => stock_str::video(context).await,
Viewtype::Voice => stock_str::voice_message(context).await,
Viewtype::Audio | Viewtype::File => {
if param.get_cmd() == SystemMessage::AutocryptSetupMessage {
append_text = false;
stock_str::ac_setup_msg_subject(context).await
} else {
let file_name: String = param
.get_path(Param::File, context)
.unwrap_or(None)
.and_then(|path| {
path.file_name()
.map(|fname| fname.to_string_lossy().into_owned())
})
.unwrap_or_else(|| String::from("ErrFileName"));
let label = if viewtype == Viewtype::Audio {
stock_str::audio(context).await
impl Message {
/// Returns a summary text.
async fn get_summary_text(&self, context: &Context) -> String {
let mut append_text = true;
let prefix = match self.viewtype {
Viewtype::Image => stock_str::image(context).await,
Viewtype::Gif => stock_str::gif(context).await,
Viewtype::Sticker => stock_str::sticker(context).await,
Viewtype::Video => stock_str::video(context).await,
Viewtype::Voice => stock_str::voice_message(context).await,
Viewtype::Audio | Viewtype::File => {
if self.param.get_cmd() == SystemMessage::AutocryptSetupMessage {
append_text = false;
stock_str::ac_setup_msg_subject(context).await
} else {
stock_str::file(context).await
};
format!("{} {}", label, file_name)
let file_name: String = self
.param
.get_path(Param::File, context)
.unwrap_or(None)
.and_then(|path| {
path.file_name()
.map(|fname| fname.to_string_lossy().into_owned())
})
.unwrap_or_else(|| String::from("ErrFileName"));
let label = if self.viewtype == Viewtype::Audio {
stock_str::audio(context).await
} else {
stock_str::file(context).await
};
format!("{} {}", label, file_name)
}
}
}
Viewtype::VideochatInvitation => {
append_text = false;
stock_str::videochat_invitation(context).await
}
_ => {
if param.get_cmd() != SystemMessage::LocationOnly {
"".to_string()
} else {
Viewtype::VideochatInvitation => {
append_text = false;
stock_str::location(context).await
stock_str::videochat_invitation(context).await
}
_ => {
if self.param.get_cmd() != SystemMessage::LocationOnly {
"".to_string()
} else {
append_text = false;
stock_str::location(context).await
}
}
};
if !append_text {
return prefix;
}
};
if !append_text {
return prefix;
}
let summary_content = if let Some(text) = text {
if text.as_ref().is_empty() {
prefix
} else if prefix.is_empty() {
dc_truncate(text.as_ref(), approx_characters).to_string()
let summary_content = if let Some(text) = &self.text {
if text.is_empty() {
prefix
} else if prefix.is_empty() {
text.to_string()
} else {
format!("{} {}", prefix, text)
}
} else {
let tmp = format!("{} {}", prefix, text.as_ref());
dc_truncate(&tmp, approx_characters).to_string()
}
} else {
prefix
};
prefix
};
let summary = if was_forwarded {
let tmp = format!(
"{}: {}",
stock_str::forwarded(context).await,
let summary = if self.is_forwarded() {
format!(
"{}: {}",
stock_str::forwarded(context).await,
summary_content
)
} else {
summary_content
);
dc_truncate(&tmp, approx_characters).to_string()
} else {
summary_content
};
};
summary.split_whitespace().join(" ")
summary.split_whitespace().join(" ")
}
}
#[cfg(test)]
@@ -196,7 +184,7 @@ mod tests {
use crate::test_utils as test;
#[async_std::test]
async fn test_get_summarytext_by_raw() {
async fn test_get_summary_text() {
let d = test::TestContext::new().await;
let ctx = &d.ctx;
@@ -204,190 +192,125 @@ mod tests {
let empty_text = Some("".to_string());
let no_text: Option<String> = None;
let mut some_file = Params::new();
some_file.set(Param::File, "foo.bar");
let mut msg = Message::new(Viewtype::Text);
msg.set_text(some_text.clone());
assert_eq!(
get_summarytext_by_raw(
Viewtype::Text,
some_text.as_ref(),
false,
&Params::new(),
50,
ctx
)
.await,
msg.get_summary_text(ctx).await,
"bla bla" // for simple text, the type is not added to the summary
);
let mut msg = Message::new(Viewtype::Image);
msg.set_text(no_text.clone());
msg.set_file("foo.bar", None);
assert_eq!(
get_summarytext_by_raw(
Viewtype::Image,
no_text.as_ref(),
false,
&some_file,
50,
ctx
)
.await,
msg.get_summary_text(ctx).await,
"Image" // file names are not added for images
);
let mut msg = Message::new(Viewtype::Video);
msg.set_text(no_text.clone());
msg.set_file("foo.bar", None);
assert_eq!(
get_summarytext_by_raw(
Viewtype::Video,
no_text.as_ref(),
false,
&some_file,
50,
ctx
)
.await,
msg.get_summary_text(ctx).await,
"Video" // file names are not added for videos
);
let mut msg = Message::new(Viewtype::Gif);
msg.set_text(no_text.clone());
msg.set_file("foo.bar", None);
assert_eq!(
get_summarytext_by_raw(Viewtype::Gif, no_text.as_ref(), false, &some_file, 50, ctx,)
.await,
msg.get_summary_text(ctx).await,
"GIF" // file names are not added for GIFs
);
let mut msg = Message::new(Viewtype::Sticker);
msg.set_text(no_text.clone());
msg.set_file("foo.bar", None);
assert_eq!(
get_summarytext_by_raw(
Viewtype::Sticker,
no_text.as_ref(),
false,
&some_file,
50,
ctx,
)
.await,
msg.get_summary_text(ctx).await,
"Sticker" // file names are not added for stickers
);
let mut msg = Message::new(Viewtype::Voice);
msg.set_text(empty_text.clone());
msg.set_file("foo.bar", None);
assert_eq!(
get_summarytext_by_raw(
Viewtype::Voice,
empty_text.as_ref(),
false,
&some_file,
50,
ctx,
)
.await,
msg.get_summary_text(ctx).await,
"Voice message" // file names are not added for voice messages, empty text is skipped
);
let mut msg = Message::new(Viewtype::Voice);
msg.set_text(no_text.clone());
msg.set_file("foo.bar", None);
assert_eq!(
get_summarytext_by_raw(
Viewtype::Voice,
no_text.as_ref(),
false,
&some_file,
50,
ctx
)
.await,
msg.get_summary_text(ctx).await,
"Voice message" // file names are not added for voice messages
);
let mut msg = Message::new(Viewtype::Voice);
msg.set_text(some_text.clone());
msg.set_file("foo.bar", None);
assert_eq!(
get_summarytext_by_raw(
Viewtype::Voice,
some_text.as_ref(),
false,
&some_file,
50,
ctx
)
.await,
msg.get_summary_text(ctx).await,
"Voice message \u{2013} bla bla" // `\u{2013}` explicitly checks for "EN DASH"
);
let mut msg = Message::new(Viewtype::Audio);
msg.set_text(no_text.clone());
msg.set_file("foo.bar", None);
assert_eq!(
get_summarytext_by_raw(
Viewtype::Audio,
no_text.as_ref(),
false,
&some_file,
50,
ctx
)
.await,
msg.get_summary_text(ctx).await,
"Audio \u{2013} foo.bar" // file name is added for audio
);
let mut msg = Message::new(Viewtype::Audio);
msg.set_text(empty_text.clone());
msg.set_file("foo.bar", None);
assert_eq!(
get_summarytext_by_raw(
Viewtype::Audio,
empty_text.as_ref(),
false,
&some_file,
50,
ctx,
)
.await,
msg.get_summary_text(ctx).await,
"Audio \u{2013} foo.bar" // file name is added for audio, empty text is not added
);
let mut msg = Message::new(Viewtype::Audio);
msg.set_text(some_text.clone());
msg.set_file("foo.bar", None);
assert_eq!(
get_summarytext_by_raw(
Viewtype::Audio,
some_text.as_ref(),
false,
&some_file,
50,
ctx
)
.await,
msg.get_summary_text(ctx).await,
"Audio \u{2013} foo.bar \u{2013} bla bla" // file name and text added for audio
);
let mut msg = Message::new(Viewtype::File);
msg.set_text(some_text.clone());
msg.set_file("foo.bar", None);
assert_eq!(
get_summarytext_by_raw(
Viewtype::File,
some_text.as_ref(),
false,
&some_file,
50,
ctx
)
.await,
msg.get_summary_text(ctx).await,
"File \u{2013} foo.bar \u{2013} bla bla" // file name is added for files
);
// Forwarded
let mut msg = Message::new(Viewtype::Text);
msg.set_text(some_text.clone());
msg.param.set_int(Param::Forwarded, 1);
assert_eq!(
get_summarytext_by_raw(
Viewtype::Text,
some_text.as_ref(),
true,
&Params::new(),
50,
ctx
)
.await,
msg.get_summary_text(ctx).await,
"Forwarded: bla bla" // for simple text, the type is not added to the summary
);
let mut msg = Message::new(Viewtype::File);
msg.set_text(some_text.clone());
msg.set_file("foo.bar", None);
msg.param.set_int(Param::Forwarded, 1);
assert_eq!(
get_summarytext_by_raw(
Viewtype::File,
some_text.as_ref(),
true,
&some_file,
50,
ctx
)
.await,
msg.get_summary_text(ctx).await,
"Forwarded: File \u{2013} foo.bar \u{2013} bla bla"
);
let mut asm_file = Params::new();
asm_file.set(Param::File, "foo.bar");
asm_file.set_cmd(SystemMessage::AutocryptSetupMessage);
let mut msg = Message::new(Viewtype::File);
msg.set_text(no_text.clone());
msg.param.set(Param::File, "foo.bar");
msg.param.set_cmd(SystemMessage::AutocryptSetupMessage);
assert_eq!(
get_summarytext_by_raw(Viewtype::File, no_text.as_ref(), false, &asm_file, 50, ctx)
.await,
msg.get_summary_text(ctx).await,
"Autocrypt Setup Message" // file name is not added for autocrypt setup messages
);
}