diff --git a/lib/reducers/keyserver-reducer.js b/lib/reducers/keyserver-reducer.js --- a/lib/reducers/keyserver-reducer.js +++ b/lib/reducers/keyserver-reducer.js @@ -28,7 +28,7 @@ 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, @@ -421,36 +421,35 @@ 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; diff --git a/lib/reducers/keyserver-reducer.test.js b/lib/reducers/keyserver-reducer.test.js --- a/lib/reducers/keyserver-reducer.test.js +++ b/lib/reducers/keyserver-reducer.test.js @@ -3,6 +3,7 @@ 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', () => { @@ -38,4 +39,83 @@ 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'); + }); });