Skip to content

Commit 3a976a8

Browse files
committed
fix: do not fail to load chatlist summary if the message got removed
1 parent e7a29f0 commit 3a976a8

File tree

1 file changed

+33
-7
lines changed

1 file changed

+33
-7
lines changed

src/chatlist.rs

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -394,25 +394,32 @@ impl Chatlist {
394394
&chat_loaded
395395
};
396396

397-
let (lastmsg, lastcontact) = if let Some(lastmsg_id) = lastmsg_id {
398-
let lastmsg = Message::load_from_db(context, lastmsg_id)
397+
let lastmsg = if let Some(lastmsg_id) = lastmsg_id {
398+
// Message may be deleted by the time we try to load it,
399+
// so use `load_from_db_optional` instead of `load_from_db`.
400+
Message::load_from_db_optional(context, lastmsg_id)
399401
.await
400-
.context("loading message failed")?;
402+
.context("Loading message failed")?
403+
} else {
404+
None
405+
};
406+
407+
let lastcontact = if let Some(lastmsg) = &lastmsg {
401408
if lastmsg.from_id == ContactId::SELF {
402-
(Some(lastmsg), None)
409+
None
403410
} else {
404411
match chat.typ {
405412
Chattype::Group | Chattype::Broadcast | Chattype::Mailinglist => {
406413
let lastcontact = Contact::get_by_id(context, lastmsg.from_id)
407414
.await
408415
.context("loading contact failed")?;
409-
(Some(lastmsg), Some(lastcontact))
416+
Some(lastcontact)
410417
}
411-
Chattype::Single => (Some(lastmsg), None),
418+
Chattype::Single => None,
412419
}
413420
}
414421
} else {
415-
(None, None)
422+
None
416423
};
417424

418425
if chat.id.is_archived_link() {
@@ -761,6 +768,25 @@ mod tests {
761768
assert_eq!(summary.text, "foo: bar test"); // the linebreak should be removed from summary
762769
}
763770

771+
/// Tests that summary does not fail to load
772+
/// if the draft was deleted after loading the chatlist.
773+
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
774+
async fn test_get_summary_deleted_draft() {
775+
let t = TestContext::new().await;
776+
777+
let chat_id = create_group_chat(&t, ProtectionStatus::Unprotected, "a chat")
778+
.await
779+
.unwrap();
780+
let mut msg = Message::new_text("Foobar".to_string());
781+
chat_id.set_draft(&t, Some(&mut msg)).await.unwrap();
782+
783+
let chats = Chatlist::try_load(&t, 0, None, None).await.unwrap();
784+
chat_id.set_draft(&t, None).await.unwrap();
785+
786+
let summary_res = chats.get_summary(&t, 0, None).await;
787+
assert!(summary_res.is_ok());
788+
}
789+
764790
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
765791
async fn test_load_broken() {
766792
let t = TestContext::new_bob().await;

0 commit comments

Comments
 (0)