@@ -600,16 +600,9 @@ class MessageHandler {
600600
601601 let thread ;
602602
603- try {
604- thread = await this . getThreadIdAsync ( mailboxData . user , subject , mimeTree ) ;
605- } catch ( err ) {
606- return rollback ( err ) ;
607- }
608-
609- // If referencing a message then use referenced message's thread
603+ // If referencing a message then use referenced message's thread (applies to any action)
610604 if (
611605 options ?. referencedMessage ?. thread &&
612- [ 'reply' , 'replyAll' ] . includes ( options ?. meta ?. reference ?. action ) &&
613606 this . normalizeSubject ( subject , {
614607 removePrefix : true
615608 } ) ===
@@ -621,6 +614,12 @@ class MessageHandler {
621614 thread = options . referencedMessage . thread ;
622615 }
623616
617+ try {
618+ thread = await this . getThreadIdAsync ( mailboxData . user , subject , mimeTree , thread ) ;
619+ } catch ( err ) {
620+ return rollback ( err ) ;
621+ }
622+
624623 messageData . thread = thread ;
625624
626625 let insertRes ;
@@ -1654,7 +1653,7 @@ class MessageHandler {
16541653 }
16551654
16561655 // resolves or generates new thread id for a message
1657- async getThreadIdAsync ( userId , subject , mimeTree ) {
1656+ async getThreadIdAsync ( userId , subject , mimeTree , referencedThreadId ) {
16581657 let referenceIds = new Set (
16591658 [
16601659 [ ] . concat ( mimeTree . parsedHeader [ 'message-id' ] || [ ] ) . pop ( ) || '' ,
@@ -1698,13 +1697,20 @@ class MessageHandler {
16981697 return existingThread . value . _id ;
16991698 }
17001699
1701- // otherwise if no existing thread
1702- const newThread = await this . database . collection ( 'threads' ) . insertOne ( {
1700+ const newThreadData = {
17031701 user : userId ,
17041702 subject,
17051703 ids : referenceIds ,
17061704 updated : new Date ( )
1707- } ) ;
1705+ } ;
1706+
1707+ // Thread does not exist, but the message is referencing a thread that existed before, recreate thread
1708+ if ( referencedThreadId ) {
1709+ newThreadData . _id = new ObjectId ( referencedThreadId ) ;
1710+ }
1711+
1712+ // otherwise if no existing thread
1713+ const newThread = await this . database . collection ( 'threads' ) . insertOne ( newThreadData ) ;
17081714
17091715 return newThread . insertedId ;
17101716 }
0 commit comments