mirror of
https://github.com/chatmail/core.git
synced 2026-05-04 22:06:29 +03:00
feat: receive pre-messages and adapt download on demand
fix python lint errors receive pre-mesages, start with changes to imap loop. refactor: move download code from `scheduler.rs` to `download.rs`, also move `get_msg_id_by_rfc724_mid` to `MsgId::get_by_rfc724_mid` `MAX_FETCH_MSG_SIZE` is no longer unused Parse if it is a pre-message or full-message start with receiving logic get rid of `MsgId::get_by_rfc724_mid` because it was a duplicate of `message::rfc724_mid_exists` docs: add hint to `MimeMessage::from_bytes` stating that it has side-effects. receiving full message send and receive `attachment_size` and set viewtype to text in pre_message metadata as struct in pre-message in header. And fill params that we can already fill from the metadata. Also add a new api to check what viewtype the message will have once downloaded. api: jsonrpc: add `full_message_view_type` to `Message` and `MessageInfo` make PreMsgMetadata.to_header_value not consume self/PreMsgMetadata add api to merge params on download full message: merge new params into old params and remove full-message metadata params move tests to `src/tests/pre_messages.rs` dynamically allocate test attachment bytes fix detection of pre-messages. (it looked for the ChatFullMessageId header in the unencrypted headers before) fix setting dl state to avaiable on pre-messages fix: save pre message with rfc724_mid of full message als disable replacement for full messages add some receiving tests and update test todo for premessage metadata test: process full message before pre-message test receive normal message some serialization tests for PreMsgMetadata remove outdated todo comment test that pre-message contains message text PreMsgMetadata: test_build_from_file_msg and test_build_from_file_msg test: test_receive_pre_message_image Test receiving the full message after receiving an edit after receiving the pre-message test_reaction_on_pre_message test_full_download_after_trashed test_webxdc_update_for_not_downloaded_instance simplify fake webxdc generation in test_webxdc_update_for_not_downloaded_instance test_markseen_pre_msg test_pre_msg_can_start_chat and test_full_msg_can_start_chat test_download_later_keeps_message_order test_chatlist_event_on_full_msg_download fix download not working log splitting into pre-message add pre-message info to text when loading from db. this can be disabled with config key `hide_pre_message_metadata_text` if ui wants to display it in a prettier way. update `download_limit` documentation more logging: log size of pre and post messages rename full message to Post-Message split up the pre-message tests into multiple files dedup test code by extracting code to create test messages into util methods remove post_message_view_type from api, now it is only used internally for tests remove `hide_pre_message_metadata_text` config option, as there currently is no way to get the full message viewtype anymore Update src/download.rs resolve comment use `parse_message_id` instead of removing `<>`parenthesis it manually fix available_post_msgs gets no entries handle forwarding and add a test for it. convert comment to log warning event on unexpected download failure add doc comment to `simple_imap_loop` more logging handle saving pre-message to self messages and test.
This commit is contained in:
559
src/download.rs
559
src/download.rs
@@ -8,9 +8,12 @@ use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::context::Context;
|
||||
use crate::imap::session::Session;
|
||||
use crate::message::{Message, MsgId};
|
||||
use crate::log::warn;
|
||||
use crate::message::{self, Message, MsgId, rfc724_mid_exists};
|
||||
use crate::{EventType, chatlist_events};
|
||||
|
||||
pub(crate) mod pre_msg_metadata;
|
||||
|
||||
/// If a message is downloaded only partially
|
||||
/// and `delete_server_after` is set to small timeouts (eg. "at once"),
|
||||
/// the user might have no chance to actually download that message.
|
||||
@@ -18,16 +21,15 @@ use crate::{EventType, chatlist_events};
|
||||
pub(crate) const MIN_DELETE_SERVER_AFTER: i64 = 48 * 60 * 60;
|
||||
|
||||
/// From this point onward outgoing messages are considered large
|
||||
/// and get a pre-message, which announces the full message.
|
||||
/// and get a Pre-Message, which announces the Post-Message.
|
||||
// this is only about sending so we can modify it any time.
|
||||
// current value is a bit less than the minimum auto download setting from the UIs (which is 160 KiB)
|
||||
pub(crate) const PRE_MSG_ATTACHMENT_SIZE_THRESHOLD: u64 = 140_000;
|
||||
|
||||
/// Max message size to be fetched in the background.
|
||||
/// This limit defines what messages are fully fetched in the background.
|
||||
/// This is for all messages that don't have the Chat-Is-Full-Message header.
|
||||
#[allow(unused)]
|
||||
pub(crate) const MAX_FETCH_MSG_SIZE: usize = 1_000_000;
|
||||
/// This is for all messages that don't have the Post-Message header.
|
||||
pub(crate) const MAX_FETCH_MSG_SIZE: u32 = 1_000_000;
|
||||
|
||||
/// Max size for pre messages. A warning is emitted when this is exceeded.
|
||||
/// Should be well below `MAX_FETCH_MSG_SIZE`
|
||||
@@ -69,7 +71,7 @@ pub enum DownloadState {
|
||||
}
|
||||
|
||||
impl MsgId {
|
||||
/// Schedules full message download for partially downloaded message.
|
||||
/// Schedules Post-Message download for partially downloaded message.
|
||||
pub async fn download_full(self, context: &Context) -> Result<()> {
|
||||
let msg = Message::load_from_db(context, self).await?;
|
||||
match msg.download_state() {
|
||||
@@ -78,11 +80,17 @@ impl MsgId {
|
||||
}
|
||||
DownloadState::InProgress => return Err(anyhow!("Download already in progress.")),
|
||||
DownloadState::Available | DownloadState::Failure => {
|
||||
if msg.rfc724_mid().is_empty() {
|
||||
return Err(anyhow!("Download not possible, message has no rfc724_mid"));
|
||||
}
|
||||
self.update_download_state(context, DownloadState::InProgress)
|
||||
.await?;
|
||||
context
|
||||
.sql
|
||||
.execute("INSERT INTO download (msg_id) VALUES (?)", (self,))
|
||||
.execute(
|
||||
"INSERT INTO download (rfc724_mid) VALUES (?)",
|
||||
(msg.rfc724_mid(),),
|
||||
)
|
||||
.await?;
|
||||
context.scheduler.interrupt_inbox().await;
|
||||
}
|
||||
@@ -131,25 +139,14 @@ impl Message {
|
||||
/// Most messages are downloaded automatically on fetch instead.
|
||||
pub(crate) async fn download_msg(
|
||||
context: &Context,
|
||||
msg_id: MsgId,
|
||||
rfc724_mid: String,
|
||||
session: &mut Session,
|
||||
) -> Result<()> {
|
||||
let Some(msg) = Message::load_from_db_optional(context, msg_id).await? else {
|
||||
// If partially downloaded message was already deleted
|
||||
// we do not know its Message-ID anymore
|
||||
// so cannot download it.
|
||||
//
|
||||
// Probably the message expired due to `delete_device_after`
|
||||
// setting or was otherwise removed from the device,
|
||||
// so we don't want it to reappear anyway.
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
let row = context
|
||||
.sql
|
||||
.query_row_optional(
|
||||
"SELECT uid, folder FROM imap WHERE rfc724_mid=? AND target!=''",
|
||||
(&msg.rfc724_mid,),
|
||||
(&rfc724_mid,),
|
||||
|row| {
|
||||
let server_uid: u32 = row.get(0)?;
|
||||
let server_folder: String = row.get(1)?;
|
||||
@@ -164,7 +161,7 @@ pub(crate) async fn download_msg(
|
||||
};
|
||||
|
||||
session
|
||||
.fetch_single_msg(context, &server_folder, server_uid, msg.rfc724_mid.clone())
|
||||
.fetch_single_msg(context, &server_folder, server_uid, rfc724_mid)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
@@ -206,19 +203,152 @@ impl Session {
|
||||
}
|
||||
}
|
||||
|
||||
async fn set_msg_state_to_failed(context: &Context, rfc724_mid: &str) -> Result<()> {
|
||||
if let Some(msg_id) = rfc724_mid_exists(context, rfc724_mid).await? {
|
||||
// Update download state to failure
|
||||
// so it can be retried.
|
||||
//
|
||||
// On success update_download_state() is not needed
|
||||
// as receive_imf() already
|
||||
// set the state and emitted the event.
|
||||
msg_id
|
||||
.update_download_state(context, DownloadState::Failure)
|
||||
.await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn available_post_msgs_contains_rfc724_mid(
|
||||
context: &Context,
|
||||
rfc724_mid: &str,
|
||||
) -> Result<bool> {
|
||||
Ok(context
|
||||
.sql
|
||||
.query_get_value::<String>(
|
||||
"SELECT rfc724_mid FROM available_post_msgs WHERE rfc724_mid=?",
|
||||
(&rfc724_mid,),
|
||||
)
|
||||
.await?
|
||||
.is_some())
|
||||
}
|
||||
|
||||
async fn remove_from_available_post_msgs_table(context: &Context, rfc724_mid: &str) -> Result<()> {
|
||||
context
|
||||
.sql
|
||||
.execute(
|
||||
"DELETE FROM available_post_msgs WHERE rfc724_mid=?",
|
||||
(&rfc724_mid,),
|
||||
)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn remove_from_download_table(context: &Context, rfc724_mid: &str) -> Result<()> {
|
||||
context
|
||||
.sql
|
||||
.execute("DELETE FROM download WHERE rfc724_mid=?", (&rfc724_mid,))
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// this is a dedicated method because it is used in multiple places.
|
||||
pub(crate) async fn premessage_is_downloaded_for(
|
||||
context: &Context,
|
||||
rfc724_mid: &str,
|
||||
) -> Result<bool> {
|
||||
Ok(message::rfc724_mid_exists(context, rfc724_mid)
|
||||
.await?
|
||||
.is_some())
|
||||
}
|
||||
|
||||
pub(crate) async fn download_msgs(context: &Context, session: &mut Session) -> Result<()> {
|
||||
let rfc724_mids = context
|
||||
.sql
|
||||
.query_map_vec("SELECT rfc724_mid FROM download", (), |row| {
|
||||
let rfc724_mid: String = row.get(0)?;
|
||||
Ok(rfc724_mid)
|
||||
})
|
||||
.await?;
|
||||
|
||||
for rfc724_mid in &rfc724_mids {
|
||||
let res = download_msg(context, rfc724_mid.clone(), session).await;
|
||||
if res.is_ok() {
|
||||
remove_from_download_table(context, rfc724_mid).await?;
|
||||
remove_from_available_post_msgs_table(context, rfc724_mid).await?;
|
||||
}
|
||||
if let Err(err) = res {
|
||||
warn!(
|
||||
context,
|
||||
"Failed to download message rfc724_mid={rfc724_mid}: {:#}.", err
|
||||
);
|
||||
if !premessage_is_downloaded_for(context, rfc724_mid).await? {
|
||||
// This is probably a classical email that vanished before we could download it
|
||||
warn!(
|
||||
context,
|
||||
"{rfc724_mid} is probably a classical email that vanished before we could download it"
|
||||
);
|
||||
remove_from_download_table(context, rfc724_mid).await?;
|
||||
} else if available_post_msgs_contains_rfc724_mid(context, rfc724_mid).await? {
|
||||
warn!(
|
||||
context,
|
||||
"{rfc724_mid} is in available_post_msgs table but we failed to fetch it,
|
||||
so set the message to DownloadState::Failure - probably it was deleted on the server in the meantime"
|
||||
);
|
||||
set_msg_state_to_failed(context, rfc724_mid).await?;
|
||||
remove_from_download_table(context, rfc724_mid).await?;
|
||||
remove_from_available_post_msgs_table(context, rfc724_mid).await?;
|
||||
} else {
|
||||
// leave the message in DownloadState::InProgress;
|
||||
// it will be downloaded once it arrives.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Download known post messages without pre_message
|
||||
/// in order to guard against lost pre-messages:
|
||||
pub(crate) async fn download_known_post_messages_without_pre_message(
|
||||
context: &Context,
|
||||
session: &mut Session,
|
||||
) -> Result<()> {
|
||||
let rfc724_mids = context
|
||||
.sql
|
||||
.query_map_vec("SELECT rfc724_mid FROM available_post_msgs", (), |row| {
|
||||
let rfc724_mid: String = row.get(0)?;
|
||||
Ok(rfc724_mid)
|
||||
})
|
||||
.await?;
|
||||
for rfc724_mid in &rfc724_mids {
|
||||
if !premessage_is_downloaded_for(context, rfc724_mid).await? {
|
||||
// Download the Post-Message unconditionally,
|
||||
// because the Pre-Message got lost.
|
||||
// The message may be in the wrong order,
|
||||
// but at least we have it at all.
|
||||
let res = download_msg(context, rfc724_mid.clone(), session).await;
|
||||
if res.is_ok() {
|
||||
remove_from_available_post_msgs_table(context, rfc724_mid).await?;
|
||||
}
|
||||
if let Err(err) = res {
|
||||
warn!(
|
||||
context,
|
||||
"download_known_post_messages_without_pre_message: Failed to download message rfc724_mid={rfc724_mid}: {:#}.",
|
||||
err
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use mailparse::MailHeaderMap;
|
||||
use num_traits::FromPrimitive;
|
||||
use tokio::fs;
|
||||
|
||||
use super::*;
|
||||
use crate::chat::{self, create_group, send_msg};
|
||||
use crate::config::Config;
|
||||
use crate::headerdef::{HeaderDef, HeaderDefMap};
|
||||
use crate::message::Viewtype;
|
||||
use crate::receive_imf::receive_imf_from_inbox;
|
||||
use crate::test_utils::{self, TestContext, TestContextManager};
|
||||
use crate::chat::send_msg;
|
||||
use crate::test_utils::TestContext;
|
||||
|
||||
#[test]
|
||||
fn test_downloadstate_values() {
|
||||
@@ -268,374 +398,5 @@ mod tests {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn test_download_stub_message() -> Result<()> {
|
||||
let t = TestContext::new_alice().await;
|
||||
|
||||
let header = "Received: (Postfix, from userid 1000); Mon, 4 Dec 2006 14:51:39 +0100 (CET)\n\
|
||||
From: bob@example.com\n\
|
||||
To: alice@example.org\n\
|
||||
Subject: foo\n\
|
||||
Message-ID: <Mr.12345678901@example.com>\n\
|
||||
Chat-Version: 1.0\n\
|
||||
Date: Sun, 22 Mar 2020 22:37:57 +0000\
|
||||
Content-Type: text/plain";
|
||||
|
||||
t.sql
|
||||
.execute(
|
||||
r#"INSERT INTO chats VALUES(
|
||||
11001,100,'bob@example.com',0,'',2,'',
|
||||
replace('C=1763151754\nt=foo','\n',char(10)),0,0,0,0,0,1763151754,0,NULL,0,'');
|
||||
"#,
|
||||
(),
|
||||
)
|
||||
.await?;
|
||||
t.sql.execute(r#"INSERT INTO msgs VALUES(
|
||||
11001,'Mr.12345678901@example.com','',0,
|
||||
11001,11001,1,1763151754,10,10,1,0,
|
||||
'[97.66 KiB message]','','',0,1763151754,1763151754,0,X'',
|
||||
'','',1,0,'',0,0,0,'foo',10,replace('Hop: From: userid; Date: Mon, 4 Dec 2006 13:51:39 +0000\n\nDKIM Results: Passed=true','\n',char(10)),1,NULL,0);
|
||||
"#, ()).await?;
|
||||
let msg = t.get_last_msg().await;
|
||||
assert_eq!(msg.download_state(), DownloadState::Available);
|
||||
assert_eq!(msg.get_subject(), "foo");
|
||||
assert!(msg.get_text().contains("[97.66 KiB message]"));
|
||||
|
||||
receive_imf_from_inbox(
|
||||
&t,
|
||||
"Mr.12345678901@example.com",
|
||||
format!("{header}\n\n100k text...").as_bytes(),
|
||||
false,
|
||||
)
|
||||
.await?;
|
||||
let msg = t.get_last_msg().await;
|
||||
assert_eq!(msg.download_state(), DownloadState::Done);
|
||||
assert_eq!(msg.get_subject(), "foo");
|
||||
assert_eq!(msg.get_text(), "100k text...");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
/// Tests that pre message is sent for attachment larger than `PRE_MSG_ATTACHMENT_SIZE_THRESHOLD`
|
||||
/// Also test that pre message is sent first, before the full message
|
||||
/// And that Autocrypt-gossip and selfavatar never go into full-messages
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn test_sending_pre_message() -> Result<()> {
|
||||
let mut tcm = TestContextManager::new();
|
||||
let alice = &tcm.alice().await;
|
||||
let bob = &tcm.bob().await;
|
||||
let fiona = &tcm.fiona().await;
|
||||
let group_id = alice
|
||||
.create_group_with_members("test group", &[bob, fiona])
|
||||
.await;
|
||||
|
||||
let mut msg = Message::new(Viewtype::File);
|
||||
msg.set_file_from_bytes(alice, "test.bin", &[0u8; 300_000], None)?;
|
||||
msg.set_text("test".to_owned());
|
||||
|
||||
// assert that test attachment is bigger than limit
|
||||
assert!(msg.get_filebytes(alice).await?.unwrap() > PRE_MSG_ATTACHMENT_SIZE_THRESHOLD);
|
||||
|
||||
let msg_id = chat::send_msg(alice, group_id, &mut msg).await?;
|
||||
let smtp_rows = alice.get_smtp_rows_for_msg(msg_id).await;
|
||||
|
||||
// pre-message and full message should be present
|
||||
// and test that correct headers are present on both messages
|
||||
assert_eq!(smtp_rows.len(), 2);
|
||||
let pre_message = smtp_rows.first().expect("first element exists");
|
||||
let pre_message_parsed = mailparse::parse_mail(pre_message.payload.as_bytes())?;
|
||||
let full_message = smtp_rows.get(1).expect("second element exists");
|
||||
let full_message_parsed = mailparse::parse_mail(full_message.payload.as_bytes())?;
|
||||
|
||||
assert!(
|
||||
pre_message_parsed
|
||||
.headers
|
||||
.get_first_header(HeaderDef::ChatIsFullMessage.get_headername())
|
||||
.is_none()
|
||||
);
|
||||
assert!(
|
||||
full_message_parsed
|
||||
.headers
|
||||
.get_first_header(HeaderDef::ChatIsFullMessage.get_headername())
|
||||
.is_some()
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
full_message_parsed
|
||||
.headers
|
||||
.get_header_value(HeaderDef::MessageId),
|
||||
Some(format!("<{}>", msg.rfc724_mid)),
|
||||
"full message should have the rfc message id of the database message"
|
||||
);
|
||||
|
||||
assert_ne!(
|
||||
pre_message_parsed
|
||||
.headers
|
||||
.get_header_value(HeaderDef::MessageId),
|
||||
full_message_parsed
|
||||
.headers
|
||||
.get_header_value(HeaderDef::MessageId),
|
||||
"message ids of pre message and full message should be different"
|
||||
);
|
||||
|
||||
let decrypted_full_message = bob.parse_msg(full_message).await;
|
||||
assert_eq!(decrypted_full_message.decrypting_failed, false);
|
||||
assert_eq!(
|
||||
decrypted_full_message.header_exists(HeaderDef::ChatFullMessageId),
|
||||
false
|
||||
);
|
||||
|
||||
let decrypted_pre_message = bob.parse_msg(pre_message).await;
|
||||
assert_eq!(
|
||||
decrypted_pre_message
|
||||
.get_header(HeaderDef::ChatFullMessageId)
|
||||
.map(String::from),
|
||||
full_message_parsed
|
||||
.headers
|
||||
.get_header_value(HeaderDef::MessageId)
|
||||
);
|
||||
assert!(
|
||||
pre_message_parsed
|
||||
.headers
|
||||
.get_header_value(HeaderDef::ChatFullMessageId)
|
||||
.is_none(),
|
||||
"no Chat-Full-Message-ID header in unprotected headers of Pre-Message"
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Tests that pre message has autocrypt gossip headers and self avatar
|
||||
/// and full message doesn't have these headers
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn test_selfavatar_and_autocrypt_gossip_goto_pre_message() -> Result<()> {
|
||||
let mut tcm = TestContextManager::new();
|
||||
let alice = &tcm.alice().await;
|
||||
let bob = &tcm.bob().await;
|
||||
let fiona = &tcm.fiona().await;
|
||||
let group_id = alice
|
||||
.create_group_with_members("test group", &[bob, fiona])
|
||||
.await;
|
||||
|
||||
let mut msg = Message::new(Viewtype::File);
|
||||
msg.set_file_from_bytes(alice, "test.bin", &[0u8; 300_000], None)?;
|
||||
msg.set_text("test".to_owned());
|
||||
|
||||
// assert that test attachment is bigger than limit
|
||||
assert!(msg.get_filebytes(alice).await?.unwrap() > PRE_MSG_ATTACHMENT_SIZE_THRESHOLD);
|
||||
|
||||
// simulate conditions for sending self avatar
|
||||
let avatar_src = alice.get_blobdir().join("avatar.png");
|
||||
fs::write(&avatar_src, test_utils::AVATAR_900x900_BYTES).await?;
|
||||
alice
|
||||
.set_config(Config::Selfavatar, Some(avatar_src.to_str().unwrap()))
|
||||
.await?;
|
||||
|
||||
let msg_id = chat::send_msg(alice, group_id, &mut msg).await?;
|
||||
let smtp_rows = alice.get_smtp_rows_for_msg(msg_id).await;
|
||||
|
||||
assert_eq!(smtp_rows.len(), 2);
|
||||
let pre_message = smtp_rows.first().expect("first element exists");
|
||||
let full_message = smtp_rows.get(1).expect("second element exists");
|
||||
let full_message_parsed = mailparse::parse_mail(full_message.payload.as_bytes())?;
|
||||
|
||||
let decrypted_pre_message = bob.parse_msg(pre_message).await;
|
||||
assert!(
|
||||
decrypted_pre_message
|
||||
.get_header(HeaderDef::ChatFullMessageId)
|
||||
.is_some(),
|
||||
"tested message is not a pre-message, sending order may be broken"
|
||||
);
|
||||
assert_ne!(decrypted_pre_message.gossiped_keys.len(), 0);
|
||||
assert_ne!(decrypted_pre_message.user_avatar, None);
|
||||
|
||||
let decrypted_full_message = bob.parse_msg(full_message).await;
|
||||
assert!(
|
||||
full_message_parsed
|
||||
.headers
|
||||
.get_first_header(HeaderDef::ChatIsFullMessage.get_headername())
|
||||
.is_some(),
|
||||
"tested message is not a full-message, sending order may be broken"
|
||||
);
|
||||
assert_eq!(decrypted_full_message.gossiped_keys.len(), 0);
|
||||
assert_eq!(decrypted_full_message.user_avatar, None);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn test_unecrypted_gets_no_pre_message() -> Result<()> {
|
||||
let mut tcm = TestContextManager::new();
|
||||
let alice = &tcm.alice().await;
|
||||
|
||||
let chat = alice
|
||||
.create_chat_with_contact("example", "email@example.org")
|
||||
.await;
|
||||
|
||||
let mut msg = Message::new(Viewtype::File);
|
||||
msg.set_file_from_bytes(alice, "test.bin", &[0u8; 300_000], None)?;
|
||||
msg.set_text("test".to_owned());
|
||||
|
||||
let msg_id = chat::send_msg(alice, chat.id, &mut msg).await?;
|
||||
let smtp_rows = alice.get_smtp_rows_for_msg(msg_id).await;
|
||||
|
||||
assert_eq!(smtp_rows.len(), 1);
|
||||
let message_bytes = smtp_rows
|
||||
.first()
|
||||
.expect("first element exists")
|
||||
.payload
|
||||
.as_bytes();
|
||||
let message = mailparse::parse_mail(message_bytes)?;
|
||||
assert!(
|
||||
message
|
||||
.headers
|
||||
.get_first_header(HeaderDef::ChatIsFullMessage.get_headername())
|
||||
.is_none(),
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Tests that no pre message is sent for normal message
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn test_not_sending_pre_message_no_attachment() -> Result<()> {
|
||||
let mut tcm = TestContextManager::new();
|
||||
let alice = &tcm.alice().await;
|
||||
let bob = &tcm.bob().await;
|
||||
let chat = alice.create_chat(bob).await;
|
||||
|
||||
// send normal text message
|
||||
let mut msg = Message::new(Viewtype::Text);
|
||||
msg.set_text("test".to_owned());
|
||||
let msg_id = chat::send_msg(alice, chat.id, &mut msg).await.unwrap();
|
||||
let smtp_rows = alice.get_smtp_rows_for_msg(msg_id).await;
|
||||
|
||||
assert_eq!(smtp_rows.len(), 1, "only one message should be sent");
|
||||
|
||||
let msg = smtp_rows.first().expect("first element exists");
|
||||
let mail = mailparse::parse_mail(msg.payload.as_bytes())?;
|
||||
|
||||
assert!(
|
||||
mail.headers
|
||||
.get_first_header(HeaderDef::ChatIsFullMessage.get_headername())
|
||||
.is_none(),
|
||||
"no 'Chat-Is-Full-Message'-header should be present"
|
||||
);
|
||||
assert!(
|
||||
mail.headers
|
||||
.get_first_header(HeaderDef::ChatFullMessageId.get_headername())
|
||||
.is_none(),
|
||||
"no 'Chat-Full-Message-ID'-header should be present in clear text headers"
|
||||
);
|
||||
let decrypted_message = bob.parse_msg(msg).await;
|
||||
assert!(
|
||||
!decrypted_message.header_exists(HeaderDef::ChatFullMessageId),
|
||||
"no 'Chat-Full-Message-ID'-header should be present"
|
||||
);
|
||||
|
||||
// test that pre message is not send for large large text
|
||||
let mut msg = Message::new(Viewtype::Text);
|
||||
let long_text = String::from_utf8(vec![b'a'; 300_000])?;
|
||||
assert!(long_text.len() > PRE_MSG_ATTACHMENT_SIZE_THRESHOLD.try_into().unwrap());
|
||||
msg.set_text(long_text);
|
||||
let msg_id = chat::send_msg(alice, chat.id, &mut msg).await.unwrap();
|
||||
let smtp_rows = alice.get_smtp_rows_for_msg(msg_id).await;
|
||||
|
||||
assert_eq!(smtp_rows.len(), 1, "only one message should be sent");
|
||||
|
||||
let msg = smtp_rows.first().expect("first element exists");
|
||||
let mail = mailparse::parse_mail(msg.payload.as_bytes())?;
|
||||
|
||||
assert!(
|
||||
mail.headers
|
||||
.get_first_header(HeaderDef::ChatIsFullMessage.get_headername())
|
||||
.is_none()
|
||||
);
|
||||
assert!(
|
||||
mail.headers
|
||||
.get_first_header(HeaderDef::ChatFullMessageId.get_headername())
|
||||
.is_none(),
|
||||
"no 'Chat-Full-Message-ID'-header should be present in clear text headers"
|
||||
);
|
||||
let decrypted_message = bob.parse_msg(msg).await;
|
||||
assert!(
|
||||
!decrypted_message.header_exists(HeaderDef::ChatFullMessageId),
|
||||
"no 'Chat-Full-Message-ID'-header should be present"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Tests that no pre message is sent for attachment smaller than `PRE_MSG_ATTACHMENT_SIZE_THRESHOLD`
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn test_not_sending_pre_message_for_small_attachment() -> Result<()> {
|
||||
let mut tcm = TestContextManager::new();
|
||||
let alice = &tcm.alice().await;
|
||||
let bob = &tcm.bob().await;
|
||||
let chat = alice.create_chat(bob).await;
|
||||
|
||||
let mut msg = Message::new(Viewtype::File);
|
||||
msg.set_file_from_bytes(alice, "test.bin", &[0u8; 100_000], None)?;
|
||||
msg.set_text("test".to_owned());
|
||||
|
||||
// assert that test attachment is smaller than limit
|
||||
assert!(msg.get_filebytes(alice).await?.unwrap() < PRE_MSG_ATTACHMENT_SIZE_THRESHOLD);
|
||||
|
||||
let msg_id = chat::send_msg(alice, chat.id, &mut msg).await.unwrap();
|
||||
let smtp_rows = alice.get_smtp_rows_for_msg(msg_id).await;
|
||||
|
||||
// only one message and no "is full message" header should be present
|
||||
assert_eq!(smtp_rows.len(), 1);
|
||||
|
||||
let msg = smtp_rows.first().expect("first element exists");
|
||||
let mail = mailparse::parse_mail(msg.payload.as_bytes())?;
|
||||
|
||||
assert!(
|
||||
mail.headers
|
||||
.get_first_header(HeaderDef::ChatIsFullMessage.get_headername())
|
||||
.is_none()
|
||||
);
|
||||
assert!(
|
||||
mail.headers
|
||||
.get_first_header(HeaderDef::ChatFullMessageId.get_headername())
|
||||
.is_none(),
|
||||
"no 'Chat-Full-Message-ID'-header should be present in clear text headers"
|
||||
);
|
||||
let decrypted_message = bob.parse_msg(msg).await;
|
||||
assert!(
|
||||
!decrypted_message.header_exists(HeaderDef::ChatFullMessageId),
|
||||
"no 'Chat-Full-Message-ID'-header should be present"
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Tests that pre message is not send for large webxdc updates
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn test_render_webxdc_status_update_object_range() -> Result<()> {
|
||||
let t = TestContext::new_alice().await;
|
||||
let chat_id = create_group(&t, "a chat").await?;
|
||||
|
||||
let instance = {
|
||||
let mut instance = Message::new(Viewtype::File);
|
||||
instance.set_file_from_bytes(
|
||||
&t,
|
||||
"minimal.xdc",
|
||||
include_bytes!("../test-data/webxdc/minimal.xdc"),
|
||||
None,
|
||||
)?;
|
||||
let instance_msg_id = send_msg(&t, chat_id, &mut instance).await?;
|
||||
assert_eq!(instance.viewtype, Viewtype::Webxdc);
|
||||
Message::load_from_db(&t, instance_msg_id).await
|
||||
}
|
||||
.unwrap();
|
||||
|
||||
t.pop_sent_msg().await;
|
||||
assert_eq!(t.sql.count("SELECT COUNT(*) FROM smtp", ()).await?, 0);
|
||||
|
||||
let long_text = String::from_utf8(vec![b'a'; 300_000])?;
|
||||
assert!(long_text.len() > PRE_MSG_ATTACHMENT_SIZE_THRESHOLD.try_into().unwrap());
|
||||
t.send_webxdc_status_update(instance.id, &format!("{{\"payload\": \"{long_text}\"}}"))
|
||||
.await?;
|
||||
t.flush_status_updates().await?;
|
||||
|
||||
assert_eq!(t.sql.count("SELECT COUNT(*) FROM smtp", ()).await?, 1);
|
||||
Ok(())
|
||||
}
|
||||
// NOTE: The download tests for pre-messages are in src/tests/pre_messages.rs
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user