mirror of
https://github.com/chatmail/core.git
synced 2026-05-07 08:56:30 +03:00
feat: do not cut incoming text if "bot" config is set
This commit is contained in:
@@ -461,8 +461,9 @@ char* dc_get_blobdir (const dc_context_t* context);
|
|||||||
* If no type is prefixed, the videochat is handled completely in a browser.
|
* If no type is prefixed, the videochat is handled completely in a browser.
|
||||||
* - `bot` = Set to "1" if this is a bot.
|
* - `bot` = Set to "1" if this is a bot.
|
||||||
* Prevents adding the "Device messages" and "Saved messages" chats,
|
* Prevents adding the "Device messages" and "Saved messages" chats,
|
||||||
* adds Auto-Submitted header to outgoing messages
|
* adds Auto-Submitted header to outgoing messages,
|
||||||
* and accepts contact requests automatically (calling dc_accept_chat() is not needed for bots).
|
* accepts contact requests automatically (calling dc_accept_chat() is not needed for bots)
|
||||||
|
* and does not cut large incoming text messages.
|
||||||
* - `last_msg_id` = database ID of the last message processed by the bot.
|
* - `last_msg_id` = database ID of the last message processed by the bot.
|
||||||
* This ID and IDs below it are guaranteed not to be returned
|
* This ID and IDs below it are guaranteed not to be returned
|
||||||
* by dc_get_next_msgs() and dc_wait_next_msgs().
|
* by dc_get_next_msgs() and dc_wait_next_msgs().
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ use once_cell::sync::Lazy;
|
|||||||
|
|
||||||
use crate::aheader::{Aheader, EncryptPreference};
|
use crate::aheader::{Aheader, EncryptPreference};
|
||||||
use crate::blob::BlobObject;
|
use crate::blob::BlobObject;
|
||||||
|
use crate::config::Config;
|
||||||
use crate::constants::{DC_DESIRED_TEXT_LINES, DC_DESIRED_TEXT_LINE_LEN};
|
use crate::constants::{DC_DESIRED_TEXT_LINES, DC_DESIRED_TEXT_LINE_LEN};
|
||||||
use crate::contact::{addr_cmp, addr_normalize, ContactId};
|
use crate::contact::{addr_cmp, addr_normalize, ContactId};
|
||||||
use crate::context::Context;
|
use crate::context::Context;
|
||||||
@@ -1114,15 +1115,22 @@ impl MimeMessage {
|
|||||||
(simplified_txt, top_quote)
|
(simplified_txt, top_quote)
|
||||||
};
|
};
|
||||||
|
|
||||||
// Truncate text if it has too many lines
|
let is_bot = context.get_config_bool(Config::Bot).await?;
|
||||||
let (simplified_txt, was_truncated) = truncate_by_lines(
|
|
||||||
simplified_txt,
|
let simplified_txt = if is_bot {
|
||||||
DC_DESIRED_TEXT_LINES,
|
simplified_txt
|
||||||
DC_DESIRED_TEXT_LINE_LEN,
|
} else {
|
||||||
);
|
// Truncate text if it has too many lines
|
||||||
if was_truncated {
|
let (simplified_txt, was_truncated) = truncate_by_lines(
|
||||||
self.is_mime_modified = was_truncated;
|
simplified_txt,
|
||||||
}
|
DC_DESIRED_TEXT_LINES,
|
||||||
|
DC_DESIRED_TEXT_LINE_LEN,
|
||||||
|
);
|
||||||
|
if was_truncated {
|
||||||
|
self.is_mime_modified = was_truncated;
|
||||||
|
}
|
||||||
|
simplified_txt
|
||||||
|
};
|
||||||
|
|
||||||
if !simplified_txt.is_empty() || simplified_quote.is_some() {
|
if !simplified_txt.is_empty() || simplified_quote.is_some() {
|
||||||
let mut part = Part {
|
let mut part = Part {
|
||||||
@@ -3290,24 +3298,37 @@ On 2020-10-25, Bob wrote:
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||||
async fn test_mime_modified_large_plain() {
|
async fn test_mime_modified_large_plain() -> Result<()> {
|
||||||
let t = TestContext::new_alice().await;
|
let t = TestContext::new_alice().await;
|
||||||
|
|
||||||
static REPEAT_TXT: &str = "this text with 42 chars is just repeated.\n";
|
static REPEAT_TXT: &str = "this text with 42 chars is just repeated.\n";
|
||||||
static REPEAT_CNT: usize = 2000; // results in a text of 84k, should be more than DC_DESIRED_TEXT_LEN
|
static REPEAT_CNT: usize = 2000; // results in a text of 84k, should be more than DC_DESIRED_TEXT_LEN
|
||||||
let long_txt = format!("From: alice@c.de\n\n{}", REPEAT_TXT.repeat(REPEAT_CNT));
|
let long_txt = format!("From: alice@c.de\n\n{}", REPEAT_TXT.repeat(REPEAT_CNT));
|
||||||
|
|
||||||
let mimemsg = MimeMessage::from_bytes(&t, long_txt.as_ref(), None)
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
assert_eq!(long_txt.matches("just repeated").count(), REPEAT_CNT);
|
assert_eq!(long_txt.matches("just repeated").count(), REPEAT_CNT);
|
||||||
assert!(long_txt.len() > DC_DESIRED_TEXT_LEN);
|
assert!(long_txt.len() > DC_DESIRED_TEXT_LEN);
|
||||||
assert!(mimemsg.is_mime_modified);
|
|
||||||
assert!(
|
{
|
||||||
mimemsg.parts[0].msg.matches("just repeated").count()
|
let mimemsg = MimeMessage::from_bytes(&t, long_txt.as_ref(), None).await?;
|
||||||
<= DC_DESIRED_TEXT_LEN / REPEAT_TXT.len()
|
assert!(mimemsg.is_mime_modified);
|
||||||
);
|
assert!(
|
||||||
assert!(mimemsg.parts[0].msg.len() <= DC_DESIRED_TEXT_LEN + DC_ELLIPSIS.len());
|
mimemsg.parts[0].msg.matches("just repeated").count()
|
||||||
|
<= DC_DESIRED_TEXT_LEN / REPEAT_TXT.len()
|
||||||
|
);
|
||||||
|
assert!(mimemsg.parts[0].msg.len() <= DC_DESIRED_TEXT_LEN + DC_ELLIPSIS.len());
|
||||||
|
}
|
||||||
|
|
||||||
|
t.set_config(Config::Bot, Some("1")).await?;
|
||||||
|
|
||||||
|
{
|
||||||
|
let mimemsg = MimeMessage::from_bytes(&t, long_txt.as_ref(), None).await?;
|
||||||
|
assert!(!mimemsg.is_mime_modified);
|
||||||
|
assert_eq!(
|
||||||
|
format!("{}\n", mimemsg.parts[0].msg),
|
||||||
|
REPEAT_TXT.repeat(REPEAT_CNT)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||||
|
|||||||
Reference in New Issue
Block a user