diff --git a/lib/reducers/keyserver-reducer.js b/lib/reducers/keyserver-reducer.js index bd7ec85e4..d4b7cdaf0 100644 --- a/lib/reducers/keyserver-reducer.js +++ b/lib/reducers/keyserver-reducer.js @@ -1,543 +1,542 @@ // @flow import { unsupervisedBackgroundActionType } from './lifecycle-state-reducer.js'; import { updateActivityActionTypes } from '../actions/activity-actions.js'; import { updateLastCommunicatedPlatformDetailsActionType, setDeviceTokenActionTypes, } from '../actions/device-actions.js'; import { addKeyserverActionType, removeKeyserverActionType, } from '../actions/keyserver-actions.js'; import { siweAuthActionTypes } from '../actions/siwe-actions.js'; import { keyserverAuthActionTypes, logOutActionTypes, deleteKeyserverAccountActionTypes, deleteAccountActionTypes, keyserverRegisterActionTypes, logInActionTypes, resetUserStateActionType, } from '../actions/user-actions.js'; import { setNewSessionActionType } from '../keyserver-conn/keyserver-conn-types.js'; import { keyserverStoreOpsHandlers, type ReplaceKeyserverOperation, type RemoveKeyserversOperation, type KeyserverStoreOperation, } from '../ops/keyserver-store-ops.js'; import { queueActivityUpdatesActionType } from '../types/activity-types.js'; -import type { KeyserverStore, KeyserverInfos } from '../types/keyserver-types'; +import type { KeyserverStore } from '../types/keyserver-types.js'; import type { BaseAction } from '../types/redux-types.js'; import { fullStateSyncActionType, incrementalStateSyncActionType, updateConnectionStatusActionType, setLateResponseActionType, updateDisconnectedBarActionType, setConnectionIssueActionType, } from '../types/socket-types.js'; import { updateTypes } from '../types/update-types-enum.js'; import { processUpdatesActionType } from '../types/update-types.js'; import { getConfig } from '../utils/config.js'; import { setURLPrefix } from '../utils/url-utils.js'; import { ashoatKeyserverID } from '../utils/validation-utils.js'; const { processStoreOperations: processStoreOps } = keyserverStoreOpsHandlers; export default function reduceKeyserverStore( state: KeyserverStore, action: BaseAction, ): KeyserverStore { if (action.type === addKeyserverActionType) { const replaceOperation: ReplaceKeyserverOperation = { type: 'replace_keyserver', payload: { id: action.payload.keyserverAdminUserID, keyserverInfo: { ...action.payload.newKeyserverInfo, }, }, }; return { ...state, keyserverInfos: processStoreOps(state.keyserverInfos, [replaceOperation]), }; } else if (action.type === removeKeyserverActionType) { const removeOperation: RemoveKeyserversOperation = { type: 'remove_keyservers', payload: { ids: [action.payload.keyserverAdminUserID], }, }; return { ...state, keyserverInfos: processStoreOps(state.keyserverInfos, [removeOperation]), }; } else if (action.type === resetUserStateActionType) { // this action is only dispatched on native const replaceOperations: ReplaceKeyserverOperation[] = []; for (const keyserverID in state.keyserverInfos) { const stateCookie = state.keyserverInfos[keyserverID]?.cookie; if (stateCookie && stateCookie.startsWith('anonymous=')) { continue; } replaceOperations.push({ type: 'replace_keyserver', payload: { id: keyserverID, keyserverInfo: { ...state.keyserverInfos[keyserverID], cookie: null, }, }, }); } return { ...state, keyserverInfos: processStoreOps(state.keyserverInfos, replaceOperations), }; } else if (action.type === setNewSessionActionType) { const { keyserverID, sessionChange } = action.payload; if (!state.keyserverInfos[keyserverID]) { if (sessionChange.cookie?.startsWith('user=')) { console.log( 'received sessionChange with user cookie, ' + `but keyserver ${keyserverID} is not in KeyserverStore!`, ); } return state; } let newKeyserverInfo = { ...state.keyserverInfos[keyserverID], }; let keyserverUpdated = false; if (sessionChange.cookie !== undefined) { newKeyserverInfo = { ...newKeyserverInfo, cookie: sessionChange.cookie, }; keyserverUpdated = true; } if (sessionChange.cookieInvalidated) { newKeyserverInfo = { ...newKeyserverInfo, connection: { ...newKeyserverInfo.connection, queuedActivityUpdates: [], }, }; keyserverUpdated = true; } const operations: ReplaceKeyserverOperation[] = []; if (keyserverUpdated) { operations.push({ type: 'replace_keyserver', payload: { id: keyserverID, keyserverInfo: newKeyserverInfo, }, }); } return { ...state, keyserverInfos: processStoreOps(state.keyserverInfos, operations), }; } else if ( action.type === logInActionTypes.success || action.type === siweAuthActionTypes.success || action.type === keyserverAuthActionTypes.success ) { const { updatesCurrentAsOf } = action.payload; const operations: ReplaceKeyserverOperation[] = []; for (const keyserverID in updatesCurrentAsOf) { operations.push({ type: 'replace_keyserver', payload: { id: keyserverID, keyserverInfo: { ...state.keyserverInfos[keyserverID], updatesCurrentAsOf: updatesCurrentAsOf[keyserverID], lastCommunicatedPlatformDetails: getConfig().platformDetails, }, }, }); } return { ...state, keyserverInfos: processStoreOps(state.keyserverInfos, operations), }; } else if (action.type === fullStateSyncActionType) { const { keyserverID } = action.payload; const operation: ReplaceKeyserverOperation = { type: 'replace_keyserver', payload: { id: keyserverID, keyserverInfo: { ...state.keyserverInfos[keyserverID], updatesCurrentAsOf: action.payload.updatesCurrentAsOf, }, }, }; return { ...state, keyserverInfos: processStoreOps(state.keyserverInfos, [operation]), }; } else if (action.type === incrementalStateSyncActionType) { const { keyserverID } = action.payload; let { deviceToken } = state.keyserverInfos[keyserverID]; for (const update of action.payload.updatesResult.newUpdates) { if ( update.type === updateTypes.BAD_DEVICE_TOKEN && update.deviceToken === state.keyserverInfos[keyserverID].deviceToken ) { deviceToken = null; break; } } const operation: ReplaceKeyserverOperation = { type: 'replace_keyserver', payload: { id: keyserverID, keyserverInfo: { ...state.keyserverInfos[keyserverID], updatesCurrentAsOf: action.payload.updatesResult.currentAsOf, deviceToken, }, }, }; return { ...state, keyserverInfos: processStoreOps(state.keyserverInfos, [operation]), }; } else if (action.type === processUpdatesActionType) { const { keyserverID } = action.payload; const updatesCurrentAsOf = Math.max( action.payload.updatesResult.currentAsOf, state.keyserverInfos[keyserverID].updatesCurrentAsOf, ); const operation: ReplaceKeyserverOperation = { type: 'replace_keyserver', payload: { id: keyserverID, keyserverInfo: { ...state.keyserverInfos[keyserverID], updatesCurrentAsOf, }, }, }; return { ...state, keyserverInfos: processStoreOps(state.keyserverInfos, [operation]), }; } else if (action.type === setURLPrefix) { const operation: ReplaceKeyserverOperation = { type: 'replace_keyserver', payload: { id: ashoatKeyserverID, keyserverInfo: { ...state.keyserverInfos[ashoatKeyserverID], urlPrefix: action.payload, }, }, }; return { ...state, keyserverInfos: processStoreOps(state.keyserverInfos, [operation]), }; } else if (action.type === updateLastCommunicatedPlatformDetailsActionType) { const { keyserverID, platformDetails } = action.payload; const operation: ReplaceKeyserverOperation = { type: 'replace_keyserver', payload: { id: keyserverID, keyserverInfo: { ...state.keyserverInfos[keyserverID], lastCommunicatedPlatformDetails: platformDetails, }, }, }; return { ...state, keyserverInfos: processStoreOps(state.keyserverInfos, [operation]), }; } else if (action.type === keyserverRegisterActionTypes.success) { const operation: ReplaceKeyserverOperation = { type: 'replace_keyserver', payload: { id: ashoatKeyserverID, keyserverInfo: { ...state.keyserverInfos[ashoatKeyserverID], lastCommunicatedPlatformDetails: getConfig().platformDetails, }, }, }; return { ...state, keyserverInfos: processStoreOps(state.keyserverInfos, [operation]), }; } else if (action.type === updateConnectionStatusActionType) { const { keyserverID, status } = action.payload; const operation: ReplaceKeyserverOperation = { type: 'replace_keyserver', payload: { id: keyserverID, keyserverInfo: { ...state.keyserverInfos[keyserverID], connection: { ...state.keyserverInfos[keyserverID].connection, status, lateResponses: [], }, }, }, }; return { ...state, keyserverInfos: processStoreOps(state.keyserverInfos, [operation]), }; } else if (action.type === unsupervisedBackgroundActionType) { const { keyserverID } = action.payload; const operation: ReplaceKeyserverOperation = { type: 'replace_keyserver', payload: { id: keyserverID, keyserverInfo: { ...state.keyserverInfos[keyserverID], connection: { ...state.keyserverInfos[keyserverID].connection, status: 'disconnected', lateResponses: [], }, }, }, }; return { ...state, keyserverInfos: processStoreOps(state.keyserverInfos, [operation]), }; } else if (action.type === queueActivityUpdatesActionType) { const { activityUpdates, keyserverID } = action.payload; const oldConnection = state.keyserverInfos[keyserverID].connection; const connection = { ...oldConnection, queuedActivityUpdates: [ ...oldConnection.queuedActivityUpdates.filter(existingUpdate => { for (const activityUpdate of activityUpdates) { if ( ((existingUpdate.focus && activityUpdate.focus) || (existingUpdate.focus === false && activityUpdate.focus !== undefined)) && existingUpdate.threadID === activityUpdate.threadID ) { return false; } } return true; }), ...activityUpdates, ], }; const operation: ReplaceKeyserverOperation = { type: 'replace_keyserver', payload: { id: keyserverID, keyserverInfo: { ...state.keyserverInfos[keyserverID], connection, }, }, }; return { ...state, keyserverInfos: processStoreOps(state.keyserverInfos, [operation]), }; } else if (action.type === updateActivityActionTypes.success) { const { activityUpdates } = action.payload; const operations: ReplaceKeyserverOperation[] = []; for (const keyserverID in activityUpdates) { const oldConnection = state.keyserverInfos[keyserverID].connection; const queuedActivityUpdates = oldConnection.queuedActivityUpdates.filter( activityUpdate => !activityUpdates[keyserverID].includes(activityUpdate), ); operations.push({ type: 'replace_keyserver', payload: { id: keyserverID, keyserverInfo: { ...state.keyserverInfos[keyserverID], connection: { ...oldConnection, queuedActivityUpdates }, }, }, }); } return { ...state, keyserverInfos: processStoreOps(state.keyserverInfos, operations), }; } else if ( action.type === logOutActionTypes.success || action.type === deleteAccountActionTypes.success ) { // We want to remove all keyservers but Ashoat's keyserver const oldConnection = state.keyserverInfos[ashoatKeyserverID].connection; const operations: KeyserverStoreOperation[] = [ { type: 'remove_all_keyservers' }, ]; operations.push({ type: 'replace_keyserver', payload: { id: ashoatKeyserverID, keyserverInfo: { ...state.keyserverInfos[ashoatKeyserverID], connection: { ...oldConnection, connectionIssue: null, queuedActivityUpdates: [], }, cookie: null, }, }, }); return { ...state, keyserverInfos: processStoreOps(state.keyserverInfos, operations), }; } else if (action.type === deleteKeyserverAccountActionTypes.success) { - const keyserverIDsToRemove = new Set(action.payload.keyserverIDs); - const newKeyserverInfos: { ...KeyserverInfos } = {}; - - const { keyserverInfos } = state; - - for (const keyserverID in keyserverInfos) { - if (!keyserverIDsToRemove.has(keyserverID)) { - newKeyserverInfos[keyserverID] = keyserverInfos[keyserverID]; - continue; - } - if (keyserverID === ashoatKeyserverID) { - const oldConnection = - state.keyserverInfos[ashoatKeyserverID].connection; - - newKeyserverInfos[ashoatKeyserverID] = { - ...state.keyserverInfos[ashoatKeyserverID], - connection: { - ...oldConnection, - connectionIssue: null, - queuedActivityUpdates: [], - lateResponses: [], + const operations: KeyserverStoreOperation[] = [ + { + type: 'remove_keyservers', + payload: { ids: action.payload.keyserverIDs }, + }, + ]; + if (action.payload.keyserverIDs.includes(ashoatKeyserverID)) { + const oldConnection = state.keyserverInfos[ashoatKeyserverID].connection; + operations.push({ + type: 'replace_keyserver', + payload: { + id: ashoatKeyserverID, + keyserverInfo: { + ...state.keyserverInfos[ashoatKeyserverID], + connection: { + ...oldConnection, + connectionIssue: null, + queuedActivityUpdates: [], + lateResponses: [], + }, + cookie: null, }, - cookie: null, - }; - } + }, + }); } return { ...state, - keyserverInfos: newKeyserverInfos, + keyserverInfos: processStoreOps(state.keyserverInfos, operations), }; } else if (action.type === setLateResponseActionType) { const { messageID, isLate, keyserverID } = action.payload; const lateResponsesSet = new Set( state.keyserverInfos[keyserverID].connection.lateResponses, ); if (isLate) { lateResponsesSet.add(messageID); } else { lateResponsesSet.delete(messageID); } const operation: ReplaceKeyserverOperation = { type: 'replace_keyserver', payload: { id: keyserverID, keyserverInfo: { ...state.keyserverInfos[keyserverID], connection: { ...state.keyserverInfos[keyserverID].connection, lateResponses: [...lateResponsesSet], }, }, }, }; return { ...state, keyserverInfos: processStoreOps(state.keyserverInfos, [operation]), }; } else if (action.type === updateDisconnectedBarActionType) { const { keyserverID } = action.payload; const operation: ReplaceKeyserverOperation = { type: 'replace_keyserver', payload: { id: keyserverID, keyserverInfo: { ...state.keyserverInfos[keyserverID], connection: { ...state.keyserverInfos[keyserverID].connection, showDisconnectedBar: action.payload.visible, }, }, }, }; return { ...state, keyserverInfos: processStoreOps(state.keyserverInfos, [operation]), }; } else if (action.type === setDeviceTokenActionTypes.success) { const { deviceTokens } = action.payload; const operations: ReplaceKeyserverOperation[] = []; for (const keyserverID in deviceTokens) { operations.push({ type: 'replace_keyserver', payload: { id: keyserverID, keyserverInfo: { ...state.keyserverInfos[keyserverID], deviceToken: deviceTokens[keyserverID], }, }, }); } return { ...state, keyserverInfos: processStoreOps(state.keyserverInfos, operations), }; } else if (action.type === setConnectionIssueActionType) { const { connectionIssue, keyserverID } = action.payload; const operation: ReplaceKeyserverOperation = { type: 'replace_keyserver', payload: { id: keyserverID, keyserverInfo: { ...state.keyserverInfos[keyserverID], connection: { ...state.keyserverInfos[keyserverID].connection, connectionIssue, }, }, }, }; return { ...state, keyserverInfos: processStoreOps(state.keyserverInfos, [operation]), }; } return state; } diff --git a/lib/reducers/keyserver-reducer.test.js b/lib/reducers/keyserver-reducer.test.js index 9bc977ecb..fd9aa65b9 100644 --- a/lib/reducers/keyserver-reducer.test.js +++ b/lib/reducers/keyserver-reducer.test.js @@ -1,41 +1,121 @@ // @flow import reduceKeyserverStore from './keyserver-reducer.js'; import { deleteKeyserverAccountActionTypes } from '../actions/user-actions.js'; import { defaultKeyserverInfo } from '../types/keyserver-types.js'; +import { ashoatKeyserverID } from '../utils/validation-utils.js'; describe('reduceKeyserverStore', () => { it('removes from the store keyservers the user has disconnected from', () => { const oldKeyserverStore = { keyserverInfos: { ['0']: defaultKeyserverInfo('url1'), ['100']: defaultKeyserverInfo('url2'), ['200']: defaultKeyserverInfo('url3'), }, }; const deleteAccountAction = { type: deleteKeyserverAccountActionTypes.success, payload: { currentUserInfo: { anonymous: true }, preRequestUserState: { cookiesAndSessions: {}, currentUserInfo: { id: '1000', username: 'test', }, }, keyserverIDs: ['100', '200'], }, loadingInfo: { fetchIndex: 1, trackMultipleRequests: false, customKeyName: undefined, }, }; expect( reduceKeyserverStore(oldKeyserverStore, deleteAccountAction), ).toEqual({ keyserverInfos: { ['0']: defaultKeyserverInfo('url1') } }); }); + it('update keyserverInfo with ashoatKeyserverID', () => { + const defaultAshoatKeyserverInfo = defaultKeyserverInfo('url1'); + const oldKeyserverStore = { + keyserverInfos: { + [ashoatKeyserverID]: { + ...defaultAshoatKeyserverInfo, + connection: { + ...defaultAshoatKeyserverInfo.connection, + connectionIssue: 'not_logged_in_error', + }, + }, + }, + }; + + const deleteAccountAction = { + type: deleteKeyserverAccountActionTypes.success, + payload: { + currentUserInfo: { anonymous: true }, + preRequestUserState: { + cookiesAndSessions: {}, + currentUserInfo: { + id: '1000', + username: 'test', + }, + }, + keyserverIDs: [ashoatKeyserverID], + }, + loadingInfo: { + fetchIndex: 1, + trackMultipleRequests: false, + customKeyName: undefined, + }, + }; + + expect( + reduceKeyserverStore(oldKeyserverStore, deleteAccountAction) + .keyserverInfos[ashoatKeyserverID].connection.connectionIssue, + ).toEqual(null); + }); + + it('return the same keyserverInfo with ashoatKeyserverID', () => { + const defaultAshoatKeyserverInfo = defaultKeyserverInfo('url1'); + const oldKeyserverStore = { + keyserverInfos: { + [ashoatKeyserverID]: { + ...defaultAshoatKeyserverInfo, + connection: { + ...defaultAshoatKeyserverInfo.connection, + connectionIssue: 'not_logged_in_error', + }, + }, + }, + }; + + const deleteAccountAction = { + type: deleteKeyserverAccountActionTypes.success, + payload: { + currentUserInfo: { anonymous: true }, + preRequestUserState: { + cookiesAndSessions: {}, + currentUserInfo: { + id: '1000', + username: 'test', + }, + }, + keyserverIDs: ['100', '200'], + }, + loadingInfo: { + fetchIndex: 1, + trackMultipleRequests: false, + customKeyName: undefined, + }, + }; + + expect( + reduceKeyserverStore(oldKeyserverStore, deleteAccountAction) + .keyserverInfos[ashoatKeyserverID].connection.connectionIssue, + ).toEqual('not_logged_in_error'); + }); });