@@ -41,6 +41,7 @@ import {
4141 getLabelByUuid ,
4242 logoutApp ,
4343 openFilledComposerWindow ,
44+ reencryptEmail ,
4445 reportContentUnencrypted ,
4546 reportContentUnencryptedBob ,
4647 restartConnection ,
@@ -63,7 +64,8 @@ import {
6364 updateDeviceType ,
6465 initAutoBackupMonitor ,
6566 updateAccountDefaultAddress ,
66- createOrUpdateContact
67+ createOrUpdateContact ,
68+ reportUncaughtError
6769} from './ipc' ;
6870import {
6971 checkEmailIsTo ,
@@ -132,6 +134,8 @@ let profileHasChanged = false;
132134let newEmailNotificationList = [ ] ;
133135let deleteDataIntervalId = null ;
134136
137+ const FATAL_ERROR = 1 ;
138+
135139const stopGettingEvents = ( ) => {
136140 isGettingEvents = false ;
137141 emitter . emit ( Event . STOP_LOAD_SYNC , { } ) ;
@@ -615,7 +619,8 @@ const formEmailIfNotExists = async params => {
615619 isSpam,
616620 recipients,
617621 guestEncryption,
618- external
622+ external,
623+ rowid
619624 } = params ;
620625
621626 const labelIds = [ ] ;
@@ -624,11 +629,7 @@ const formEmailIfNotExists = async params => {
624629 myFileKeys ;
625630
626631 try {
627- const {
628- decryptedBody,
629- decryptedHeaders,
630- decryptedFileKeys
631- } = await signal . decryptEmail ( {
632+ const decryptResult = await signal . decryptEmail ( {
632633 accountRecipientId,
633634 optionalToken,
634635 bodyKey : metadataKey ,
@@ -637,37 +638,56 @@ const formEmailIfNotExists = async params => {
637638 messageType,
638639 fileKeys : fileKeys || ( fileKey ? [ fileKey ] : null )
639640 } ) ;
640- body = cleanEmailBody ( decryptedBody ) ;
641- headers = decryptedHeaders ;
642- myFileKeys = decryptedFileKeys
643- ? decryptedFileKeys . map ( fileKey => {
644- const fileKeySplit = fileKey . split ( ':' ) ;
645- return {
646- key : fileKeySplit [ 0 ] ,
647- iv : fileKeySplit [ 1 ]
648- } ;
649- } )
650- : null ;
651- } catch ( e ) {
652- if (
653- e . message === signal . ALICE_ERROR ||
654- e . message === signal . CONTENT_NOT_AVAILABLE ||
655- e instanceof TypeError
656- ) {
657- return {
658- error : 1
659- } ;
660- } else if ( e . message === signal . DUPLICATE_MESSAGE ) {
661- return {
662- error : 2
663- } ;
664- }
665- body = 'Content unencrypted' ;
666- if ( external ) {
667- reportContentUnencryptedBob ( e . stack ) ;
641+ if ( decryptResult . signalError ) {
642+ if ( external ) {
643+ const reencryptRes = await reencryptEmail ( {
644+ metadataKey,
645+ eventid : rowid ,
646+ recipientId : accountRecipientId
647+ } ) ;
648+ if ( ! reencryptRes ) return { error : FATAL_ERROR } ;
649+
650+ switch ( reencryptRes . status ) {
651+ case 200 :
652+ return {
653+ error : FATAL_ERROR
654+ } ;
655+ case 429 :
656+ reportContentUnencryptedBob ( decryptResult . signalError ) ;
657+ break ;
658+ default :
659+ return {
660+ error : FATAL_ERROR
661+ } ;
662+ }
663+ body = 'Content unencrypted' ;
664+ } else {
665+ reportContentUnencrypted ( decryptResult . signalError ) ;
666+ body = 'Content unencrypted' ;
667+ }
668668 } else {
669- reportContentUnencrypted ( e . stack ) ;
669+ const {
670+ decryptedBody,
671+ decryptedHeaders,
672+ decryptedFileKeys
673+ } = decryptResult ;
674+ body = cleanEmailBody ( decryptedBody ) ;
675+ headers = decryptedHeaders ;
676+ myFileKeys = decryptedFileKeys
677+ ? decryptedFileKeys . map ( fileKey => {
678+ const fileKeySplit = fileKey . split ( ':' ) ;
679+ return {
680+ key : fileKeySplit [ 0 ] ,
681+ iv : fileKeySplit [ 1 ]
682+ } ;
683+ } )
684+ : null ;
670685 }
686+ } catch ( e ) {
687+ reportUncaughtError ( e . stack ) ;
688+ return {
689+ error : FATAL_ERROR
690+ } ;
671691 }
672692
673693 if ( ! fileKeys && fileKey ) {
@@ -881,7 +901,8 @@ const handleNewMessageEvent = async (
881901 isSpam,
882902 recipients,
883903 guestEncryption,
884- external
904+ external,
905+ rowid
885906 } )
886907 : await formEmailIfExists ( {
887908 accountId,
@@ -894,7 +915,7 @@ const handleNewMessageEvent = async (
894915
895916 if ( error ) {
896917 return {
897- rowid : error === 1 ? null : rowid
918+ rowid : error === FATAL_ERROR ? null : rowid
898919 } ;
899920 }
900921
0 commit comments