import { EVENT_ACTIONS } from '@proton/shared/lib/constants' ; import { canonizeEmail } from '@proton/shared/lib/helpers/email' ; import { Message } from '@proton/shared/lib/interfaces/mail/Message' ; import { isScheduledSend , isSent , isDraft as testIsDraft } from '@proton/shared/lib/mail/messages' ; import { PayloadAction } from '@reduxjs/toolkit' ; import { Draft } from 'immer' ; import { parseLabelIDsInEvent } from '../../../helpers/elements' ; import { LabelIDsChanges , MessageEvent } from '../../../models/event' ; import { getLocalID , getMessage } from '../helpers/messagesReducer' ; import { DocumentInitializeParams , LoadParams , MessageErrors , MessagesState , MessageState , VerificationParams , } from '../messagesTypes' ; export const reset = ( state ) => { Object . keys ( state ) . forEach ( ( ID ) => { delete state [ ID ] ; } ) ; } ; export const initialize = ( state , action ) => { state [ action . payload . localID ] = action . payload as any ; } ; export const errors = ( state , { payload : { ID , errors } } ) => { const messageState = getMessage ( state , ID ) ; if ( messageState ) { messageState . errors = errors ; } } ; export const event = ( state , action ) => { const { Action , Message } = action . payload ; const localID = getLocalID ( state , action . payload . ID ) ; if ( Action === EVENT_ACTIONS . DELETE ) { delete state [ localID ] ; } if ( Action === EVENT_ACTIONS . UPDATE_DRAFT || Action === EVENT_ACTIONS . UPDATE_FLAGS ) { const currentValue = state [ localID ] as MessageState | undefined ; const isSentDraft = isSent ( Message ) ; const isScheduled = isScheduledSend ( Message ) ; const isDraft = testIsDraft ( Message ) ; if ( currentValue ?. data ) { currentValue . data = parseLabelIDsInEvent ( currentValue . data , Message as Message & LabelIDsChanges ) ; if ( Action === EVENT_ACTIONS . UPDATE_DRAFT ) { if ( ! currentValue . draftFlags ?. sending ) { currentValue . messageDocument = undefined ; currentValue . data . Body = undefined ; } if ( isSentDraft && ! isScheduled ) { currentValue . draftFlags = { ... currentValue . draftFlags , isSentDraft : true , } ; } } if ( ! isDraft ) { delete ( currentValue . data as Partial < Message > ) . NumAttachments ; } } } } ; export const loadFulfilled = ( state , { payload } ) => { const messageState = getMessage ( state , payload . ID ) ; if ( messageState ) { messageState . data = payload ; messageState . loadRetry = messageState . loadRetry ? messageState . loadRetry + 1 : 1 ; } } ; export const loadRejected = ( state , { meta : { arg : { ID } , } , } ) => { const messageState = getMessage ( state , ID ) ; if ( messageState ) { messageState . loadRetry = messageState . loadRetry ? messageState . loadRetry + 1 : 1 ; } } ; export const documentInitializePending = ( state , { payload } ) => { const messageState = getMessage ( state , payload ) ; if ( messageState ) { messageState . messageDocument = { initialized : false } ; } } ; export const documentInitializeFulfilled = ( state , { payload : { ID , dataChanges , initialized , preparation , decryption , errors , messageImages } , } ) => { const messageState = getMessage ( state , ID ) ; if ( messageState ) { messageState . data = { ... ( messageState . data as Message ) , ... dataChanges , } ; if ( messageState . messageDocument ) { messageState . messageDocument . initialized = initialized ; messageState . messageDocument . document = preparation ?. document ; messageState . messageDocument . plainText = preparation ?. plainText ; } messageState . decryption = decryption ; messageState . errors = errors ; messageState . loadRetry = messageState . loadRetry ? messageState . loadRetry + 1 : 1 ; messageState . messageImages = messageImages ; } } ; export const verificationComplete = ( state , { payload : { ID , encryptionPreferences , verification , attachedPublicKeys , signingPublicKey , errors } , } ) => { const messageState = getMessage ( state , ID ) ; if ( messageState ) { messageState . verification = { senderPinnedKeys : encryptionPreferences ?. pinnedKeys , signingPublicKey , attachedPublicKeys , senderVerified : encryptionPreferences ?. isContactSignatureVerified , verificationStatus : verification ?. verified , verificationErrors : verification ?. verificationErrors , } ; messageState . errors = { ... messageState . errors , ... errors } ; } } ; export const resign = ( state , { payload : { ID , isContactSignatureVerified } } ) => { const messageState = getMessage ( state , ID ) ; if ( messageState && messageState . verification ) { messageState . verification . senderVerified = isContactSignatureVerified ; } } ; export const resetVerification = ( state , { payload : emails } ) => { Object . values ( state ) . forEach ( ( message ) => { const senderAddress = canonizeEmail ( message ?. data ?. Sender . Address || '' ) ; if ( message && emails . includes ( senderAddress ) ) { message . verification = undefined ; } } ) ; } ; export const applyDarkStyle = ( state , { payload : { ID , hasDarkStyle } } ) => { const messageState = getMessage ( state , ID ) ; if ( messageState && messageState . messageDocument ) { messageState . messageDocument . hasDarkStyle = hasDarkStyle ; } } ; export const removeDarkStyle = ( state , { payload : { ID , noDarkStyle } } ) => { const messageState = getMessage ( state , ID ) ; if ( messageState && messageState . messageDocument ) { messageState . messageDocument . noDarkStyle = noDarkStyle ; } } ;