diff --git a/lib/reducers/thread-activity-reducer.js b/lib/reducers/thread-activity-reducer.js --- a/lib/reducers/thread-activity-reducer.js +++ b/lib/reducers/thread-activity-reducer.js @@ -18,6 +18,7 @@ logOutActionTypes, deleteKeyserverAccountActionTypes, } from '../actions/user-actions.js'; +import { extractKeyserverIDFromID } from '../keyserver-conn/keyserver-call-utils.js'; import { setNewSessionActionType } from '../keyserver-conn/keyserver-conn-types.js'; import type { BaseAction } from '../types/redux-types.js'; import { incrementalStateSyncActionType } from '../types/socket-types.js'; @@ -91,9 +92,23 @@ return updatedState; } else if ( action.type === logOutActionTypes.success || - action.type === deleteKeyserverAccountActionTypes.success || - (action.type === setNewSessionActionType && - action.payload.sessionChange.cookieInvalidated) + action.type === deleteKeyserverAccountActionTypes.success + ) { + let updatedState = { ...state }; + const keyserverIDsSet = new Set(action.payload.keyserverIDs); + + for (const threadID in state) { + if (!keyserverIDsSet.has(extractKeyserverIDFromID(threadID))) { + continue; + } + const { [threadID]: _, ...stateSansRemovedThread } = updatedState; + updatedState = stateSansRemovedThread; + } + + return updatedState; + } else if ( + action.type === setNewSessionActionType && + action.payload.sessionChange.cookieInvalidated ) { return {}; } diff --git a/lib/reducers/thread-activity-reducer.test.js b/lib/reducers/thread-activity-reducer.test.js --- a/lib/reducers/thread-activity-reducer.test.js +++ b/lib/reducers/thread-activity-reducer.test.js @@ -1,6 +1,7 @@ // @flow import { reduceThreadActivity } from './thread-activity-reducer.js'; +import { deleteKeyserverAccountActionTypes } from '../actions/user-actions.js'; import { updateThreadLastNavigatedActionType } from '../types/thread-activity-types.js'; // NOTE: These unit tests were generated by GitHub Copilot. @@ -66,4 +67,45 @@ const result = reduceThreadActivity(initialState, action); expect(result).toEqual(expectedState); }); + + test('removes threads of keyserver the user has disconnected from', () => { + const keyserver1 = '100'; + const keyserver2 = '200'; + const keyserver3 = '300'; + const threads1 = { + [keyserver1 + '|1']: { lastNavigatedTo: 1, lastPruned: 1 }, + [keyserver1 + '|2']: { lastNavigatedTo: 1, lastPruned: 1 }, + }; + const threads2 = { + [keyserver2 + '|1']: { lastNavigatedTo: 1, lastPruned: 1 }, + [keyserver2 + '|2']: { lastNavigatedTo: 1, lastPruned: 1 }, + }; + const threads3 = { + [keyserver3 + '|1']: { lastNavigatedTo: 1, lastPruned: 1 }, + [keyserver3 + '|2']: { lastNavigatedTo: 1, lastPruned: 1 }, + }; + + const threads = { ...threads1, ...threads2, ...threads3 }; + const action = { + type: deleteKeyserverAccountActionTypes.success, + payload: { + currentUserInfo: { anonymous: true }, + preRequestUserState: { + cookiesAndSessions: {}, + currentUserInfo: { + id: '83810', + username: 'user1', + }, + }, + keyserverIDs: [keyserver1, keyserver2], + }, + loadingInfo: { + fetchIndex: 1, + trackMultipleRequests: false, + customKeyName: undefined, + }, + }; + + expect(reduceThreadActivity(threads, action)).toEqual(threads3); + }); });