@@ -394,25 +394,32 @@ impl Chatlist {
394
394
& chat_loaded
395
395
} ;
396
396
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)
399
401
. 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 {
401
408
if lastmsg. from_id == ContactId :: SELF {
402
- ( Some ( lastmsg ) , None )
409
+ None
403
410
} else {
404
411
match chat. typ {
405
412
Chattype :: Group | Chattype :: Broadcast | Chattype :: Mailinglist => {
406
413
let lastcontact = Contact :: get_by_id ( context, lastmsg. from_id )
407
414
. await
408
415
. context ( "loading contact failed" ) ?;
409
- ( Some ( lastmsg ) , Some ( lastcontact) )
416
+ Some ( lastcontact)
410
417
}
411
- Chattype :: Single => ( Some ( lastmsg ) , None ) ,
418
+ Chattype :: Single => None ,
412
419
}
413
420
}
414
421
} else {
415
- ( None , None )
422
+ None
416
423
} ;
417
424
418
425
if chat. id . is_archived_link ( ) {
@@ -761,6 +768,25 @@ mod tests {
761
768
assert_eq ! ( summary. text, "foo: bar test" ) ; // the linebreak should be removed from summary
762
769
}
763
770
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
+
764
790
#[ tokio:: test( flavor = "multi_thread" , worker_threads = 2 ) ]
765
791
async fn test_load_broken ( ) {
766
792
let t = TestContext :: new_bob ( ) . await ;
0 commit comments