@@ -1044,6 +1044,28 @@ impl MimeMessage {
1044
1044
message:: handle_ndn ( context, failure_report, error) . await
1045
1045
}
1046
1046
}
1047
+
1048
+ /// Returns timestamp of the parent message.
1049
+ ///
1050
+ /// If there is no parent message or it is not found in the
1051
+ /// database, returns None.
1052
+ pub async fn get_parent_timestamp ( & self , context : & Context ) -> Result < Option < i64 > > {
1053
+ let parent_timestamp = if let Some ( field) = self
1054
+ . get ( HeaderDef :: InReplyTo )
1055
+ . and_then ( |msgid| parse_message_id ( msgid) . ok ( ) )
1056
+ {
1057
+ context
1058
+ . sql
1059
+ . query_get_value_result (
1060
+ "SELECT timestamp FROM msgs WHERE rfc724_mid=?" ,
1061
+ paramsv ! [ field] ,
1062
+ )
1063
+ . await ?
1064
+ } else {
1065
+ None
1066
+ } ;
1067
+ Ok ( parent_timestamp)
1068
+ }
1047
1069
}
1048
1070
1049
1071
async fn update_gossip_peerstates (
@@ -1413,6 +1435,39 @@ mod tests {
1413
1435
assert ! ( mimeparser. chat_disposition_notification_to. is_none( ) ) ;
1414
1436
}
1415
1437
1438
+ #[ async_std:: test]
1439
+ async fn test_get_parent_timestamp ( ) {
1440
+ let context = TestContext :: new ( ) . await ;
1441
+ let raw =
b"From: [email protected] \n \
1442
+ Content-Type: text/plain\n \
1443
+ Chat-Version: 1.0\n \
1444
+ In-Reply-To: <[email protected] >\n \
1445
+ \n \
1446
+ Some reply\n \
1447
+ ";
1448
+ let mimeparser = MimeMessage :: from_bytes ( & context. ctx , & raw [ ..] )
1449
+ . await
1450
+ . unwrap ( ) ;
1451
+ assert_eq ! (
1452
+ mimeparser. get_parent_timestamp( & context. ctx) . await . unwrap( ) ,
1453
+ None
1454
+ ) ;
1455
+ let timestamp = 1570435529 ;
1456
+ context
1457
+ . ctx
1458
+ . sql
1459
+ . execute (
1460
+ "INSERT INTO msgs (rfc724_mid, timestamp) VALUES(?,?)" ,
1461
+ paramsv ! [ "[email protected] " , timestamp
] ,
1462
+ )
1463
+ . await
1464
+ . expect ( "Failed to write to the database" ) ;
1465
+ assert_eq ! (
1466
+ mimeparser. get_parent_timestamp( & context. ctx) . await . unwrap( ) ,
1467
+ Some ( timestamp)
1468
+ ) ;
1469
+ }
1470
+
1416
1471
#[ async_std:: test]
1417
1472
async fn test_mimeparser_with_context ( ) {
1418
1473
let context = TestContext :: new ( ) . await ;
0 commit comments