Skip to content

Commit 846c8e7

Browse files
authored
Generate rfc724_mid when creating Message (#6704)
Set `rfc724_mid` in `Message::new()`, `Message::new_text()`, and `Message::default()` instead of when sending the message. This way the rfc724 mid can be read in the draft stage which makes it more consistent for bots. Tests had to be adjusted to create multiple messages to get unique mid, otherwise core would not send the messages out.
1 parent 98a1b9e commit 846c8e7

File tree

9 files changed

+57
-22
lines changed

9 files changed

+57
-22
lines changed

deltachat-ffi/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ use deltachat::*;
3737
use deltachat::{accounts::Accounts, log::LogExt};
3838
use deltachat_jsonrpc::api::CommandApi;
3939
use deltachat_jsonrpc::yerpc::{OutReceiver, RpcClient, RpcSession};
40+
use message::Viewtype;
4041
use num_traits::{FromPrimitive, ToPrimitive};
4142
use rand::Rng;
4243
use tokio::runtime::Runtime;
@@ -2076,7 +2077,7 @@ pub unsafe extern "C" fn dc_get_msg(context: *mut dc_context_t, msg_id: u32) ->
20762077
ctx,
20772078
"dc_get_msg called with special msg_id={msg_id}, returning empty msg"
20782079
);
2079-
message::Message::default()
2080+
message::Message::new(Viewtype::default())
20802081
} else {
20812082
warn!(ctx, "dc_get_msg could not retrieve msg_id {msg_id}: {e:#}");
20822083
return ptr::null_mut();

src/chat.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2005,7 +2005,9 @@ impl Chat {
20052005
let mut to_id = 0;
20062006
let mut location_id = 0;
20072007

2008-
let new_rfc724_mid = create_outgoing_rfc724_mid();
2008+
if msg.rfc724_mid.is_empty() {
2009+
msg.rfc724_mid = create_outgoing_rfc724_mid();
2010+
}
20092011

20102012
if self.typ == Chattype::Single {
20112013
if let Some(id) = context
@@ -2103,7 +2105,7 @@ impl Chat {
21032105
if references_vec.is_empty() {
21042106
// As a fallback, use our Message-ID,
21052107
// same as in the case of top-level message.
2106-
new_references = new_rfc724_mid.clone();
2108+
new_references = msg.rfc724_mid.clone();
21072109
} else {
21082110
new_references = references_vec.join(" ");
21092111
}
@@ -2113,7 +2115,7 @@ impl Chat {
21132115
// This allows us to identify replies to our message even if
21142116
// email server such as Outlook changes `Message-ID:` header.
21152117
// MUAs usually keep the first Message-ID in `References:` header unchanged.
2116-
new_references = new_rfc724_mid.clone();
2118+
new_references = msg.rfc724_mid.clone();
21172119
}
21182120

21192121
// add independent location to database
@@ -2181,7 +2183,6 @@ impl Chat {
21812183

21822184
msg.chat_id = self.id;
21832185
msg.from_id = ContactId::SELF;
2184-
msg.rfc724_mid = new_rfc724_mid;
21852186
msg.timestamp_sort = timestamp;
21862187

21872188
// add message to the database
@@ -3846,7 +3847,7 @@ pub(crate) async fn add_contact_to_chat_ex(
38463847
) -> Result<bool> {
38473848
ensure!(!chat_id.is_special(), "can not add member to special chats");
38483849
let contact = Contact::get_by_id(context, contact_id).await?;
3849-
let mut msg = Message::default();
3850+
let mut msg = Message::new(Viewtype::default());
38503851

38513852
chat_id.reset_gossiped_timestamp(context).await?;
38523853

@@ -4077,7 +4078,7 @@ pub async fn remove_contact_from_chat(
40774078
"Cannot remove special contact"
40784079
);
40794080

4080-
let mut msg = Message::default();
4081+
let mut msg = Message::new(Viewtype::default());
40814082

40824083
let chat = Chat::load_from_db(context, chat_id).await?;
40834084
if chat.typ == Chattype::Group || chat.typ == Chattype::Broadcast {
@@ -4184,7 +4185,7 @@ async fn rename_ex(
41844185
ensure!(!chat_id.is_special(), "Invalid chat ID");
41854186

41864187
let chat = Chat::load_from_db(context, chat_id).await?;
4187-
let mut msg = Message::default();
4188+
let mut msg = Message::new(Viewtype::default());
41884189

41894190
if chat.typ == Chattype::Group
41904191
|| chat.typ == Chattype::Mailinglist
@@ -4347,9 +4348,11 @@ pub async fn forward_msgs(context: &Context, msg_ids: &[MsgId], chat_id: ChatId)
43474348
msg.subject = "".to_string();
43484349

43494350
msg.state = MessageState::OutPending;
4351+
msg.rfc724_mid = create_outgoing_rfc724_mid();
43504352
let new_msg_id = chat
43514353
.prepare_msg_raw(context, &mut msg, None, curr_timestamp)
43524354
.await?;
4355+
43534356
curr_timestamp += 1;
43544357
if !create_send_msg_jobs(context, &mut msg).await?.is_empty() {
43554358
context.scheduler.interrupt_smtp().await;

src/chat/chat_tests.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2040,20 +2040,28 @@ async fn test_sticker_forward() -> Result<()> {
20402040
}
20412041

20422042
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
2043-
async fn test_forward() -> Result<()> {
2043+
async fn test_forward_basic() -> Result<()> {
20442044
let alice = TestContext::new_alice().await;
20452045
let bob = TestContext::new_bob().await;
20462046
let alice_chat = alice.create_chat(&bob).await;
20472047
let bob_chat = bob.create_chat(&alice).await;
20482048

2049-
let mut msg = Message::new_text("Hi Bob".to_owned());
2050-
let sent_msg = alice.send_msg(alice_chat.get_id(), &mut msg).await;
2049+
let mut alice_msg = Message::new_text("Hi Bob".to_owned());
2050+
let sent_msg = alice.send_msg(alice_chat.get_id(), &mut alice_msg).await;
20512051
let msg = bob.recv_msg(&sent_msg).await;
2052+
assert_eq!(alice_msg.rfc724_mid, msg.rfc724_mid);
20522053

20532054
forward_msgs(&bob, &[msg.id], bob_chat.get_id()).await?;
20542055

20552056
let forwarded_msg = bob.pop_sent_msg().await;
2057+
assert_eq!(bob_chat.id.get_msg_cnt(&bob).await?, 2);
2058+
assert_ne!(
2059+
forwarded_msg.load_from_db().await.rfc724_mid,
2060+
msg.rfc724_mid,
2061+
);
2062+
let msg_bob = Message::load_from_db(&bob, forwarded_msg.sender_msg_id).await?;
20562063
let msg = alice.recv_msg(&forwarded_msg).await;
2064+
assert_eq!(msg.rfc724_mid(), msg_bob.rfc724_mid());
20572065
assert_eq!(msg.get_text(), "Hi Bob");
20582066
assert!(msg.is_forwarded());
20592067
Ok(())

src/imex/key_transfer.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,7 @@ pub async fn initiate_key_transfer(context: &Context) -> Result<String> {
3232
)?;
3333

3434
let chat_id = ChatId::create_for_contact(context, ContactId::SELF).await?;
35-
let mut msg = Message {
36-
viewtype: Viewtype::File,
37-
..Default::default()
38-
};
35+
let mut msg = Message::new(Viewtype::File);
3936
msg.param.set(Param::File, setup_file_blob.as_name());
4037
msg.param
4138
.set(Param::Filename, "autocrypt-setup-message.html");

src/message.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ use crate::reaction::get_msg_reactions;
3333
use crate::sql;
3434
use crate::summary::Summary;
3535
use crate::sync::SyncData;
36+
use crate::tools::create_outgoing_rfc724_mid;
3637
use crate::tools::{
3738
buf_compress, buf_decompress, get_filebytes, get_filemeta, gm2local_offset, read_file,
3839
sanitize_filename, time, timestamp_to_str,
@@ -470,6 +471,7 @@ impl Message {
470471
pub fn new(viewtype: Viewtype) -> Self {
471472
Message {
472473
viewtype,
474+
rfc724_mid: create_outgoing_rfc724_mid(),
473475
..Default::default()
474476
}
475477
}
@@ -479,6 +481,7 @@ impl Message {
479481
Message {
480482
viewtype: Viewtype::Text,
481483
text,
484+
rfc724_mid: create_outgoing_rfc724_mid(),
482485
..Default::default()
483486
}
484487
}

src/message/message_tests.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ async fn test_get_width_height() {
135135
}
136136

137137
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
138-
async fn test_quote() {
138+
async fn test_quote_basic() {
139139
let d = TestContext::new().await;
140140
let ctx = &d.ctx;
141141

@@ -144,13 +144,10 @@ async fn test_quote() {
144144
.unwrap();
145145

146146
let chat = d.create_chat_with_contact("", "dest@example.com").await;
147-
148147
let mut msg = Message::new_text("Quoted message".to_string());
149148

150-
// Send message, so it gets a Message-Id.
151-
assert!(msg.rfc724_mid.is_empty());
152-
let msg_id = chat::send_msg(ctx, chat.id, &mut msg).await.unwrap();
153-
let msg = Message::load_from_db(ctx, msg_id).await.unwrap();
149+
// Message has to be sent such that it gets saved to db.
150+
chat::send_msg(ctx, chat.id, &mut msg).await.unwrap();
154151
assert!(!msg.rfc724_mid.is_empty());
155152

156153
let mut msg2 = Message::new(Viewtype::Text);
@@ -358,6 +355,7 @@ async fn test_markseen_msgs() -> Result<()> {
358355
let sent1 = alice.send_msg(alice_chat.id, &mut msg).await;
359356
let msg1 = bob.recv_msg(&sent1).await;
360357
let bob_chat_id = msg1.chat_id;
358+
let mut msg = Message::new_text("this is the text!".to_string());
361359
let sent2 = alice.send_msg(alice_chat.id, &mut msg).await;
362360
let msg2 = bob.recv_msg(&sent2).await;
363361
assert_eq!(msg1.chat_id, msg2.chat_id);
@@ -380,9 +378,11 @@ async fn test_markseen_msgs() -> Result<()> {
380378

381379
// bob sends to alice,
382380
// alice knows bob and messages appear in normal chat
381+
let mut msg = Message::new_text("this is the text!".to_string());
383382
let msg1 = alice
384383
.recv_msg(&bob.send_msg(bob_chat_id, &mut msg).await)
385384
.await;
385+
let mut msg = Message::new_text("this is the text!".to_string());
386386
let msg2 = alice
387387
.recv_msg(&bob.send_msg(bob_chat_id, &mut msg).await)
388388
.await;

src/mimefactory/mimefactory_tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,7 @@ async fn test_selfavatar_unencrypted_signed() {
728728
.is_some());
729729

730730
// if another message is sent, that one must not contain the avatar
731+
let mut msg = Message::new_text("this is the text!".to_string());
731732
let sent_msg = t.send_msg(chat.id, &mut msg).await;
732733
let mut payload = sent_msg.payload().splitn(4, "\r\n\r\n");
733734

src/receive_imf/receive_imf_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5090,7 +5090,7 @@ async fn test_references() -> Result<()> {
50905090
alice.set_config_bool(Config::BccSelf, true).await?;
50915091

50925092
let alice_chat_id = create_group_chat(alice, ProtectionStatus::Unprotected, "Group").await?;
5093-
let _sent = alice
5093+
alice
50945094
.send_text(alice_chat_id, "Hi! I created a group.")
50955095
.await;
50965096

src/webxdc/webxdc_tests.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2195,3 +2195,25 @@ async fn test_webxdc_href() -> Result<()> {
21952195

21962196
Ok(())
21972197
}
2198+
2199+
/// Test that the draft `selfAddr` is equal to the `selfAddr` of the sent message.
2200+
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
2201+
async fn test_self_addr_consistency() -> Result<()> {
2202+
let mut tcm = TestContextManager::new();
2203+
let alice = &tcm.alice().await;
2204+
let alice_chat = alice
2205+
.create_group_with_members(ProtectionStatus::Unprotected, "No friends :(", &[])
2206+
.await;
2207+
let mut instance = create_webxdc_instance(
2208+
alice,
2209+
"minimal.xdc",
2210+
include_bytes!("../../test-data/webxdc/minimal.xdc"),
2211+
)?;
2212+
alice_chat.set_draft(alice, Some(&mut instance)).await?;
2213+
let self_addr = instance.get_webxdc_self_addr(alice).await?;
2214+
let sent = alice.send_msg(alice_chat, &mut instance).await;
2215+
let db_msg = Message::load_from_db(alice, sent.sender_msg_id).await?;
2216+
assert_eq!(db_msg.get_webxdc_self_addr(alice).await?, self_addr);
2217+
assert_eq!(alice_chat.get_msg_cnt(alice).await?, 1);
2218+
Ok(())
2219+
}

0 commit comments

Comments
 (0)