diff --git a/src/message.rs b/src/message.rs index 92c5b5c16f..cf6adf94f2 100644 --- a/src/message.rs +++ b/src/message.rs @@ -2590,6 +2590,60 @@ mod tests { Ok(()) } + #[tokio::test(flavor = "multi_thread", worker_threads = 2)] + async fn test_markseen_not_downloaded_msg() -> Result<()> { + let mut tcm = TestContextManager::new(); + let alice = &tcm.alice().await; + alice.set_config(Config::DownloadLimit, Some("1")).await?; + let bob = &tcm.bob().await; + let bob_chat_id = tcm.send_recv_accept(alice, bob, "hi").await.chat_id; + + let file_bytes = include_bytes!("../test-data/image/screenshot.png"); + let mut msg = Message::new(Viewtype::Image); + msg.set_file_from_bytes(bob, "a.jpg", file_bytes, None) + .await?; + let sent_msg = bob.send_msg(bob_chat_id, &mut msg).await; + let msg = alice.recv_msg(&sent_msg).await; + assert_eq!(msg.download_state, DownloadState::Available); + assert!(!msg.param.get_bool(Param::WantsMdn).unwrap_or_default()); + assert_eq!(msg.state, MessageState::InFresh); + markseen_msgs(alice, vec![msg.id]).await?; + let msg = Message::load_from_db(alice, msg.id).await?; + assert_eq!(msg.state, MessageState::InSeen); + assert!( + !alice + .sql + .exists("SELECT COUNT(*) FROM smtp_mdns", ()) + .await? + ); + + alice.set_config(Config::DownloadLimit, None).await?; + // Simulate that the message is even marked as `\Seen` on IMAP. + let rcvd_msg = receive_imf(alice, sent_msg.payload().as_bytes(), true) + .await + .unwrap() + .unwrap(); + assert_eq!(rcvd_msg.chat_id, msg.chat_id); + let msg = Message::load_from_db(alice, *rcvd_msg.msg_ids.last().unwrap()) + .await + .unwrap(); + assert_eq!(msg.download_state, DownloadState::Done); + assert!(msg.param.get_bool(Param::WantsMdn).unwrap_or_default()); + assert!(msg.get_showpadlock()); + assert_eq!(msg.state, MessageState::InNoticed); + markseen_msgs(alice, vec![msg.id]).await?; + let msg = Message::load_from_db(alice, msg.id).await?; + assert_eq!(msg.state, MessageState::InSeen); + assert_eq!( + alice + .sql + .count("SELECT COUNT(*) FROM smtp_mdns", ()) + .await?, + 1 + ); + Ok(()) + } + #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn test_get_state() -> Result<()> { let alice = TestContext::new_alice().await; diff --git a/src/receive_imf.rs b/src/receive_imf.rs index e19696a284..b6f071b07c 100644 --- a/src/receive_imf.rs +++ b/src/receive_imf.rs @@ -1014,7 +1014,10 @@ async fn add_parts( } } - state = if seen + state = if replace_msg_id.is_some() { + // TODO a comment explaining why we're doing this would be nice + MessageState::InNoticed + } else if seen || fetching_existing_messages || is_mdn || is_reaction @@ -1562,7 +1565,7 @@ INSERT INTO msgs ON CONFLICT (id) DO UPDATE SET rfc724_mid=excluded.rfc724_mid, chat_id=excluded.chat_id, from_id=excluded.from_id, to_id=excluded.to_id, timestamp_sent=excluded.timestamp_sent, - type=excluded.type, msgrmsg=excluded.msgrmsg, + type=excluded.type, state=min(state,excluded.state), msgrmsg=excluded.msgrmsg, txt=excluded.txt, txt_normalized=excluded.txt_normalized, subject=excluded.subject, txt_raw=excluded.txt_raw, param=excluded.param, hidden=excluded.hidden,bytes=excluded.bytes, mime_headers=excluded.mime_headers, @@ -1613,7 +1616,7 @@ RETURNING id } else { DownloadState::Done }, - mime_parser.hop_info + mime_parser.hop_info, ], |row| { let msg_id: MsgId = row.get(0)?;