api!: make Message.text non-optional

Message.set_text() and Message.get_text() are modified accordingly
to accept String and return String.

Messages which previously contained None text
are now represented as messages with empty text.
Use Message.set_text("".to_string())
instead of Message.set_text(None).
This commit is contained in:
link2xt
2023-07-03 13:34:13 +00:00
parent 8e17e400b3
commit 9c68fac4b6
33 changed files with 248 additions and 325 deletions

View File

@@ -250,7 +250,7 @@ async fn test_read_receipt_and_unarchive() -> Result<()> {
.await?;
let msg = get_chat_msg(&t, group_id, 0, 1).await;
assert_eq!(msg.is_dc_message, MessengerMessage::Yes);
assert_eq!(msg.text.unwrap(), "hello");
assert_eq!(msg.text, "hello");
assert_eq!(msg.state, MessageState::OutDelivered);
let group = Chat::load_from_db(&t, group_id).await?;
assert!(group.get_visibility() == ChatVisibility::Normal);
@@ -415,7 +415,7 @@ async fn test_escaped_from() {
);
let msg = get_chat_msg(&t, chat_id, 0, 1).await;
assert_eq!(msg.is_dc_message, MessengerMessage::Yes);
assert_eq!(msg.text.unwrap(), "hello");
assert_eq!(msg.text, "hello");
assert_eq!(msg.param.get_int(Param::WantsMdn).unwrap(), 1);
}
@@ -461,7 +461,7 @@ async fn test_escaped_recipients() {
.await
.unwrap();
assert_eq!(msg.is_dc_message, MessengerMessage::Yes);
assert_eq!(msg.text.unwrap(), "hello");
assert_eq!(msg.text, "hello");
assert_eq!(msg.param.get_int(Param::WantsMdn).unwrap(), 1);
}
@@ -713,7 +713,7 @@ async fn test_parse_ndn_group_msg() -> Result<()> {
assert_eq!(
last_msg.text,
Some(stock_str::failed_sending_to(&t, "assidhfaaspocwaeofi@gmail.com").await,)
stock_str::failed_sending_to(&t, "assidhfaaspocwaeofi@gmail.com").await
);
assert_eq!(last_msg.from_id, ContactId::INFO);
Ok(())
@@ -734,7 +734,7 @@ async fn load_imf_email(context: &Context, imf_raw: &[u8]) -> Message {
async fn test_html_only_mail() {
let t = TestContext::new_alice().await;
let msg = load_imf_email(&t, include_bytes!("../../test-data/message/wrong-html.eml")).await;
assert_eq!(msg.text.unwrap(), "Guten Abend,\n\nLots of text\n\ntext with Umlaut ä...\n\nMfG\n\n--------------------------------------\n\n[Camping ](https://example.com/)\n\nsomeaddress\n\nsometown");
assert_eq!(msg.text, "Guten Abend,\n\nLots of text\n\ntext with Umlaut ä...\n\nMfG\n\n--------------------------------------\n\n[Camping ](https://example.com/)\n\nsomeaddress\n\nsometown");
}
static GH_MAILINGLIST: &[u8] =
@@ -1158,10 +1158,7 @@ async fn test_dhl_mailing_list() -> Result<()> {
.await
.unwrap();
let msg = t.get_last_msg().await;
assert_eq!(
msg.text,
Some("Ihr Paket ist in der Packstation 123 bla bla".to_string())
);
assert_eq!(msg.text, "Ihr Paket ist in der Packstation 123 bla bla");
assert!(msg.has_html());
let chat = Chat::load_from_db(&t, msg.chat_id).await.unwrap();
assert_eq!(chat.typ, Chattype::Mailinglist);
@@ -1186,10 +1183,7 @@ async fn test_dpd_mailing_list() -> Result<()> {
.await
.unwrap();
let msg = t.get_last_msg().await;
assert_eq!(
msg.text,
Some("Bald ist Ihr DPD Paket da bla bla".to_string())
);
assert_eq!(msg.text, "Bald ist Ihr DPD Paket da bla bla");
assert!(msg.has_html());
let chat = Chat::load_from_db(&t, msg.chat_id).await.unwrap();
assert_eq!(chat.typ, Chattype::Mailinglist);
@@ -1294,10 +1288,7 @@ async fn test_mailing_list_with_mimepart_footer() {
.await
.unwrap();
let msg = t.get_last_msg().await;
assert_eq!(
msg.text,
Some("[Intern] important stuff Hi mr ... [text part]".to_string())
);
assert_eq!(msg.text, "[Intern] important stuff Hi mr ... [text part]");
assert!(msg.has_html());
let chat = Chat::load_from_db(&t, msg.chat_id).await.unwrap();
assert_eq!(get_chat_msgs(&t, msg.chat_id).await.unwrap().len(), 1);
@@ -1320,7 +1311,7 @@ async fn test_mailing_list_with_mimepart_footer_signed() {
.unwrap();
let msg = t.get_last_msg().await;
assert_eq!(get_chat_msgs(&t, msg.chat_id).await.unwrap().len(), 1);
let text = msg.text.clone().unwrap();
let text = msg.text.clone();
assert!(text.contains("content text"));
assert!(!text.contains("footer text"));
assert!(msg.has_html());
@@ -1384,7 +1375,7 @@ async fn test_mailing_list_chat_message() {
.await
.unwrap();
let msg = t.get_last_msg().await;
assert_eq!(msg.text, Some("hello, this is a test 👋\n\n_______________________________________________\nTest1 mailing list -- test1@example.net\nTo unsubscribe send an email to test1-leave@example.net".to_string()));
assert_eq!(msg.text, "hello, this is a test 👋\n\n_______________________________________________\nTest1 mailing list -- test1@example.net\nTo unsubscribe send an email to test1-leave@example.net".to_string());
assert!(!msg.has_html());
let chat = Chat::load_from_db(&t, msg.chat_id).await.unwrap();
assert_eq!(chat.typ, Chattype::Mailinglist);
@@ -1464,7 +1455,7 @@ async fn test_pdf_filename_simple() {
)
.await;
assert_eq!(msg.viewtype, Viewtype::File);
assert_eq!(msg.text.unwrap(), "mail body");
assert_eq!(msg.text, "mail body");
assert_eq!(msg.param.get(Param::File).unwrap(), "$BLOBDIR/simple.pdf");
}
@@ -1478,7 +1469,7 @@ async fn test_pdf_filename_continuation() {
)
.await;
assert_eq!(msg.viewtype, Viewtype::File);
assert_eq!(msg.text.unwrap(), "mail body");
assert_eq!(msg.text, "mail body");
assert_eq!(
msg.param.get(Param::File).unwrap(),
"$BLOBDIR/test pdf äöüß.pdf"
@@ -1557,7 +1548,7 @@ async fn test_in_reply_to() {
.unwrap();
let msg = t.get_last_msg().await;
assert_eq!(msg.get_text().unwrap(), "reply foo");
assert_eq!(msg.get_text(), "reply foo");
// Load the first message from the same chat.
let msgs = chat::get_chat_msgs(&t, msg.chat_id).await.unwrap();
@@ -1568,7 +1559,7 @@ async fn test_in_reply_to() {
};
let reply_msg = Message::load_from_db(&t, *msg_id).await.unwrap();
assert_eq!(reply_msg.get_text().unwrap(), "hello foo");
assert_eq!(reply_msg.get_text(), "hello foo");
// Check that reply got into the same chat as the original message.
assert_eq!(msg.chat_id, reply_msg.chat_id);
@@ -1626,7 +1617,7 @@ async fn test_in_reply_to_two_member_group() {
let msg = t.get_last_msg().await;
let chat = Chat::load_from_db(&t, msg.chat_id).await.unwrap();
assert_eq!(chat.typ, Chattype::Group);
assert_eq!(msg.get_text().unwrap(), "classic reply");
assert_eq!(msg.get_text(), "classic reply");
// Receive a Delta Chat reply from Alice.
// It is assigned to group chat, because it has a group ID.
@@ -1652,7 +1643,7 @@ async fn test_in_reply_to_two_member_group() {
let msg = t.get_last_msg().await;
let chat = Chat::load_from_db(&t, msg.chat_id).await.unwrap();
assert_eq!(chat.typ, Chattype::Group);
assert_eq!(msg.get_text().unwrap(), "chat reply");
assert_eq!(msg.get_text(), "chat reply");
// Receive a private Delta Chat reply from Alice.
// It is assigned to 1:1 chat, because it has no group ID,
@@ -1679,7 +1670,7 @@ async fn test_in_reply_to_two_member_group() {
let msg = t.get_last_msg().await;
let chat = Chat::load_from_db(&t, msg.chat_id).await.unwrap();
assert_eq!(chat.typ, Chattype::Single);
assert_eq!(msg.get_text().unwrap(), "private reply");
assert_eq!(msg.get_text(), "private reply");
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
@@ -1690,7 +1681,7 @@ async fn test_save_mime_headers_off() -> anyhow::Result<()> {
chat::send_text_msg(&alice, chat_alice.id, "hi!".to_string()).await?;
let msg = bob.recv_msg(&alice.pop_sent_msg().await).await;
assert_eq!(msg.get_text(), Some("hi!".to_string()));
assert_eq!(msg.get_text(), "hi!");
assert!(!msg.get_showpadlock());
let mime = message::get_mime_headers(&bob, msg.id).await?;
assert!(mime.is_empty());
@@ -1709,7 +1700,7 @@ async fn test_save_mime_headers_on() -> anyhow::Result<()> {
chat::send_text_msg(&alice, chat_alice.id, "hi!".to_string()).await?;
let msg = bob.recv_msg(&alice.pop_sent_msg().await).await;
assert_eq!(msg.get_text(), Some("hi!".to_string()));
assert_eq!(msg.get_text(), "hi!");
assert!(!msg.get_showpadlock());
let mime = message::get_mime_headers(&bob, msg.id).await?;
let mime_str = String::from_utf8_lossy(&mime);
@@ -1720,7 +1711,7 @@ async fn test_save_mime_headers_on() -> anyhow::Result<()> {
let chat_bob = bob.create_chat(&alice).await;
chat::send_text_msg(&bob, chat_bob.id, "ho!".to_string()).await?;
let msg = alice.recv_msg(&bob.pop_sent_msg().await).await;
assert_eq!(msg.get_text(), Some("ho!".to_string()));
assert_eq!(msg.get_text(), "ho!");
assert!(msg.get_showpadlock());
let mime = message::get_mime_headers(&alice, msg.id).await?;
let mime_str = String::from_utf8_lossy(&mime);
@@ -1780,7 +1771,7 @@ async fn create_test_alias(chat_request: bool, group_request: bool) -> (TestCont
let msg = alice.get_last_msg().await;
assert_eq!(msg.get_subject(), "i have a question");
assert!(msg.get_text().unwrap().contains("hi support!"));
assert!(msg.get_text().contains("hi support!"));
let chat = Chat::load_from_db(&alice, msg.chat_id).await.unwrap();
assert_eq!(chat.typ, Chattype::Group);
assert_eq!(get_chat_msgs(&alice, chat.id).await.unwrap().len(), 1);
@@ -1805,7 +1796,7 @@ async fn create_test_alias(chat_request: bool, group_request: bool) -> (TestCont
let msg = Message::load_from_db(&claire, msg_id).await.unwrap();
msg.chat_id.accept(&claire).await.unwrap();
assert_eq!(msg.get_subject(), "i have a question");
assert!(msg.get_text().unwrap().contains("hi support!"));
assert!(msg.get_text().contains("hi support!"));
let chat = Chat::load_from_db(&claire, msg.chat_id).await.unwrap();
if group_request {
assert_eq!(chat.typ, Chattype::Group);
@@ -1826,7 +1817,7 @@ async fn check_alias_reply(reply: &[u8], chat_request: bool, group_request: bool
receive_imf(&alice, reply, false).await.unwrap();
let answer = alice.get_last_msg().await;
assert_eq!(answer.get_subject(), "Re: i have a question");
assert!(answer.get_text().unwrap().contains("the version is 1.0"));
assert!(answer.get_text().contains("the version is 1.0"));
assert_eq!(answer.chat_id, request.chat_id);
let chat_contacts = get_chat_contacts(&alice, answer.chat_id)
.await
@@ -1849,7 +1840,7 @@ async fn check_alias_reply(reply: &[u8], chat_request: bool, group_request: bool
receive_imf(&claire, reply, false).await.unwrap();
let answer = claire.get_last_msg().await;
assert_eq!(answer.get_subject(), "Re: i have a question");
assert!(answer.get_text().unwrap().contains("the version is 1.0"));
assert!(answer.get_text().contains("the version is 1.0"));
assert_eq!(answer.chat_id, request.chat_id);
assert_eq!(
answer.get_override_sender_name().unwrap(),
@@ -1921,7 +1912,7 @@ async fn test_dont_assign_to_trash_by_parent() {
chat_id.accept(&t).await.unwrap();
let msg = get_chat_msg(&t, chat_id, 0, 1).await; // Make sure that the message is actually in the chat
assert!(!msg.chat_id.is_special());
assert_eq!(msg.text.unwrap(), "Hi hello");
assert_eq!(msg.text, "Hi hello");
println!("\n========= Delete the message ==========");
msg.id.trash(&t).await.unwrap();
@@ -1945,7 +1936,7 @@ async fn test_dont_assign_to_trash_by_parent() {
.unwrap();
let msg = t.get_last_msg().await;
assert!(!msg.chat_id.is_special()); // Esp. check that the chat_id is not TRASH
assert_eq!(msg.text.unwrap(), "Reply");
assert_eq!(msg.text, "Reply");
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
@@ -1996,7 +1987,7 @@ Message content",
// Outgoing email should create a chat.
let msg = alice.get_last_msg().await;
assert_eq!(msg.get_text().unwrap(), "Subj Message content");
assert_eq!(msg.get_text(), "Subj Message content");
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
@@ -2243,7 +2234,7 @@ Message-ID: <Gr.eJ_llQIXf0K.buxmrnMmG0Y@gmx.de>"
let group_msg = t.get_last_msg().await;
assert_eq!(
group_msg.text.unwrap(),
group_msg.text,
if *outgoing_is_classical {
"single reply-to Hello, I\'ve just created the group \"single reply-to\" for us."
} else {
@@ -2283,7 +2274,7 @@ Private reply"#,
.unwrap();
let private_msg = t.get_last_msg().await;
assert_eq!(private_msg.text.unwrap(), "Private reply");
assert_eq!(private_msg.text, "Private reply");
let private_chat = Chat::load_from_db(&t, private_msg.chat_id).await.unwrap();
assert_eq!(private_chat.typ, Chattype::Single);
assert_ne!(private_msg.chat_id, group_msg.chat_id);
@@ -2332,7 +2323,7 @@ Message-ID: <Gr.iy1KCE2y65_.mH2TM52miv9@testrun.org>"
.unwrap();
let group_msg = t.get_last_msg().await;
assert_eq!(
group_msg.text.unwrap(),
group_msg.text,
if *outgoing_is_classical {
"single reply-to Hello, I\'ve just created the group \"single reply-to\" for us."
} else {
@@ -2378,7 +2369,7 @@ Sent with my Delta Chat Messenger: https://delta.chat
.unwrap();
let private_msg = t.get_last_msg().await;
assert_eq!(private_msg.text.unwrap(), "Private reply");
assert_eq!(private_msg.text, "Private reply");
let private_chat = Chat::load_from_db(&t, private_msg.chat_id).await.unwrap();
assert_eq!(private_chat.typ, Chattype::Single);
assert_ne!(private_msg.chat_id, group_msg.chat_id);
@@ -2420,7 +2411,7 @@ Message-ID: <Gr.eJ_llQIXf0K.buxmrnMmG0Y@gmx.de>"
let group_msg = t.get_last_msg().await;
assert_eq!(
group_msg.text.unwrap(),
group_msg.text,
if *outgoing_is_classical {
"single reply-to Hello, I\'ve just created the group \"single reply-to\" for us."
} else {
@@ -2457,7 +2448,7 @@ Outgoing reply to all"#,
.unwrap();
let reply = t.get_last_msg().await;
assert_eq!(reply.text.unwrap(), "Out subj Outgoing reply to all");
assert_eq!(reply.text, "Out subj Outgoing reply to all");
let reply_chat = Chat::load_from_db(&t, reply.chat_id).await.unwrap();
assert_eq!(reply_chat.typ, Chattype::Group);
assert_eq!(reply.chat_id, group_msg.chat_id);
@@ -2480,7 +2471,7 @@ Reply to all"#,
.unwrap();
let reply = t.get_last_msg().await;
assert_eq!(reply.text.unwrap(), "In subj Reply to all");
assert_eq!(reply.text, "In subj Reply to all");
let reply_chat = Chat::load_from_db(&t, reply.chat_id).await.unwrap();
assert_eq!(reply_chat.typ, Chattype::Group);
assert_eq!(reply.chat_id, group_msg.chat_id);
@@ -2766,7 +2757,7 @@ Hi, I created a group"#,
.await?;
let msg_out = t.get_last_msg().await;
assert_eq!(msg_out.from_id, ContactId::SELF);
assert_eq!(msg_out.text.unwrap(), "Hi, I created a group");
assert_eq!(msg_out.text, "Hi, I created a group");
assert_eq!(msg_out.in_reply_to, None);
// Bob replies from a different address
@@ -2790,7 +2781,7 @@ Reply from different address
.await?;
let msg_in = t.get_last_msg().await;
assert_eq!(msg_in.to_id, ContactId::SELF);
assert_eq!(msg_in.text.unwrap(), "Reply from different address");
assert_eq!(msg_in.text, "Reply from different address");
assert_eq!(
msg_in.in_reply_to.unwrap(),
"Gr.qetqsutor7a.Aresxresy-4@deltachat.de"
@@ -2868,22 +2859,22 @@ async fn test_accept_outgoing() -> Result<()> {
alice1.recv_msg(&sent).await;
alice2.recv_msg(&sent).await;
let alice1_msg = bob2.recv_msg(&sent).await;
assert_eq!(alice1_msg.text.unwrap(), "Hello!");
assert_eq!(alice1_msg.text, "Hello!");
let alice1_chat = chat::Chat::load_from_db(&alice1, alice1_msg.chat_id).await?;
assert!(alice1_chat.is_contact_request());
let alice2_msg = alice2.get_last_msg().await;
assert_eq!(alice2_msg.text.unwrap(), "Hello!");
assert_eq!(alice2_msg.text, "Hello!");
let alice2_chat = chat::Chat::load_from_db(&alice2, alice2_msg.chat_id).await?;
assert!(alice2_chat.is_contact_request());
let bob1_msg = bob1.get_last_msg().await;
assert_eq!(bob1_msg.text.unwrap(), "Hello!");
assert_eq!(bob1_msg.text, "Hello!");
let bob1_chat = chat::Chat::load_from_db(&bob1, bob1_msg.chat_id).await?;
assert!(!bob1_chat.is_contact_request());
let bob2_msg = bob2.get_last_msg().await;
assert_eq!(bob2_msg.text.unwrap(), "Hello!");
assert_eq!(bob2_msg.text, "Hello!");
let bob2_chat = chat::Chat::load_from_db(&bob2, bob2_msg.chat_id).await?;
assert!(!bob2_chat.is_contact_request());
@@ -2929,7 +2920,7 @@ async fn test_outgoing_private_reply_multidevice() -> Result<()> {
assert_eq!(received.from_id, alice1_bob_contact.id);
assert_eq!(received.to_id, ContactId::SELF);
assert!(!received.hidden);
assert_eq!(received.text, Some("Hello all!".to_string()));
assert_eq!(received.text, "Hello all!");
assert_eq!(received.in_reply_to, None);
assert_eq!(received.chat_blocked, Blocked::Request);
@@ -2939,7 +2930,7 @@ async fn test_outgoing_private_reply_multidevice() -> Result<()> {
assert_eq!(received_group.can_send(&alice1).await?, false); // Can't send because it's Blocked::Request
let mut msg_out = Message::new(Viewtype::Text);
msg_out.set_text(Some("Private reply".to_string()));
msg_out.set_text("Private reply".to_string());
assert_eq!(received_group.blocked, Blocked::Request);
msg_out.set_quote(&alice1, Some(&received)).await?;
@@ -2957,10 +2948,10 @@ async fn test_outgoing_private_reply_multidevice() -> Result<()> {
assert_eq!(received.from_id, ContactId::SELF);
assert_eq!(received.to_id, alice2_bob_contact.id);
assert!(!received.hidden);
assert_eq!(received.text, Some("Private reply".to_string()));
assert_eq!(received.text, "Private reply");
assert_eq!(
received.parent(&alice2).await?.unwrap().text,
Some("Hello all!".to_string())
"Hello all!".to_string()
);
assert_eq!(received.chat_blocked, Blocked::Not);
@@ -3021,13 +3012,13 @@ async fn test_no_private_reply_to_blocked_account() -> Result<()> {
// =============== Alice replies private to Bob ==============
let received = alice.get_last_msg().await;
assert_eq!(received.text, Some("Hello all!".to_string()));
assert_eq!(received.text, "Hello all!");
let received_group = Chat::load_from_db(&alice, received.chat_id).await?;
assert_eq!(received_group.typ, Chattype::Group);
let mut msg_out = Message::new(Viewtype::Text);
msg_out.set_text(Some("Private reply".to_string()));
msg_out.set_text("Private reply".to_string());
msg_out.set_quote(&alice, Some(&received)).await?;
let alice_bob_chat = alice.create_chat(&bob).await;
@@ -3043,7 +3034,7 @@ async fn test_no_private_reply_to_blocked_account() -> Result<()> {
// since only chat is a group, no new open chat has been created
assert_eq!(chat.typ, Chattype::Group);
let received = bob.get_last_msg().await;
assert_eq!(received.text, Some("Hello all!".to_string()));
assert_eq!(received.text, "Hello all!");
// =============== Bob unblocks Alice ================
// test if the blocked chat is restored correctly
@@ -3054,7 +3045,7 @@ async fn test_no_private_reply_to_blocked_account() -> Result<()> {
let chat = Chat::load_from_db(&bob, chat_id).await.unwrap();
assert_eq!(chat.typ, Chattype::Single);
let received = bob.get_last_msg().await;
assert_eq!(received.text, Some("Private reply".to_string()));
assert_eq!(received.text, "Private reply");
Ok(())
}