From 730c3703452135fa955cfaae6c6bbbc9a581b014 Mon Sep 17 00:00:00 2001 From: Chris Bobbe Date: Thu, 8 Dec 2022 16:29:37 -0800 Subject: [PATCH] model: Fix lots of reducers that weren't clearing data on LOGIN_SUCCESS Most of these state subtrees hold per-account data we get from the server. In the current pre-#5005 design, where we only store server data for one account, these should already have been clearing out on LOGIN_SUCCESS, like we do with messages, narrows, etc. Two of the subtrees, `outbox` and `drafts`, hold client-side per-account data. Just as with server data, we've only been keeping these around for the active account, and like the other reducers touched in this commit, we've forgotten to handle LOGIN_SUCCESS for them. So, do that. Now, all the server-data subtrees and `outbox` and `drafts` all reset on ACCOUNT_SWITCH, LOGIN_SUCCESS, and LOGOUT. That's part of Greg's proposed fix for #4446. Next, we'll do the next part: > * I think it'd be a useful refactor to consolidate one action type > like `CLEAR_ACCOUNT_DATA`. > * The action creators for these three (in `accountActions.js`) can > dispatch that first, then also a `LOGOUT` etc. > * Almost all the reducers would respond only to the > `CLEAR_ACCOUNT_DATA`. > * There are a couple of exceptions, like `accountReducers` and > `sessionReducers`, that actually want to treat the three cases > differently. Those would be the only ones to continue responding > to `LOGOUT` etc.; and they'd stand out better as a result. > * Then there are further changes we might make to those, but > that'll be easier to see after that. Fixes-partly: #4446 --- src/alertWords/alertWordsReducer.js | 9 ++++++++- src/drafts/draftsReducer.js | 3 ++- src/mute/muteModel.js | 9 ++++++++- src/outbox/outboxReducer.js | 2 ++ src/streams/streamsReducer.js | 9 ++++++++- src/topics/topicsReducer.js | 9 ++++++++- src/unread/unreadHuddlesReducer.js | 2 ++ src/unread/unreadMentionsReducer.js | 2 ++ src/unread/unreadModel.js | 3 ++- src/unread/unreadPmsReducer.js | 2 ++ 10 files changed, 44 insertions(+), 6 deletions(-) diff --git a/src/alertWords/alertWordsReducer.js b/src/alertWords/alertWordsReducer.js index 44a4f1ab906..9a466081e86 100644 --- a/src/alertWords/alertWordsReducer.js +++ b/src/alertWords/alertWordsReducer.js @@ -1,6 +1,12 @@ /* @flow strict-local */ import type { AlertWordsState, PerAccountApplicableAction } from '../types'; -import { REGISTER_COMPLETE, EVENT_ALERT_WORDS, ACCOUNT_SWITCH, LOGOUT } from '../actionConstants'; +import { + REGISTER_COMPLETE, + EVENT_ALERT_WORDS, + ACCOUNT_SWITCH, + LOGOUT, + LOGIN_SUCCESS, +} from '../actionConstants'; import { NULL_ARRAY } from '../nullObjects'; const initialState = NULL_ARRAY; @@ -12,6 +18,7 @@ export default ( switch (action.type) { case LOGOUT: case ACCOUNT_SWITCH: + case LOGIN_SUCCESS: return initialState; case REGISTER_COMPLETE: diff --git a/src/drafts/draftsReducer.js b/src/drafts/draftsReducer.js index 8a29287ec4f..e26a6946915 100644 --- a/src/drafts/draftsReducer.js +++ b/src/drafts/draftsReducer.js @@ -1,6 +1,6 @@ /* @flow strict-local */ import type { DraftsState, PerAccountApplicableAction } from '../types'; -import { DRAFT_UPDATE, LOGOUT, ACCOUNT_SWITCH } from '../actionConstants'; +import { DRAFT_UPDATE, LOGOUT, ACCOUNT_SWITCH, LOGIN_SUCCESS } from '../actionConstants'; import { NULL_OBJECT } from '../nullObjects'; import { keyFromNarrow } from '../utils/narrow'; @@ -29,6 +29,7 @@ export default ( switch (action.type) { case LOGOUT: case ACCOUNT_SWITCH: + case LOGIN_SUCCESS: return initialState; case DRAFT_UPDATE: diff --git a/src/mute/muteModel.js b/src/mute/muteModel.js index 2d4602c74e5..18e9635116b 100644 --- a/src/mute/muteModel.js +++ b/src/mute/muteModel.js @@ -1,7 +1,13 @@ /* @flow strict-local */ import type { MuteState, PerAccountApplicableAction, PerAccountState } from '../types'; -import { REGISTER_COMPLETE, LOGOUT, ACCOUNT_SWITCH, EVENT_MUTED_TOPICS } from '../actionConstants'; +import { + REGISTER_COMPLETE, + LOGOUT, + ACCOUNT_SWITCH, + EVENT_MUTED_TOPICS, + LOGIN_SUCCESS, +} from '../actionConstants'; import { getStreamsByName } from '../subscriptions/subscriptionSelectors'; import * as logging from '../utils/logging'; import DefaultMap from '../utils/DefaultMap'; @@ -49,6 +55,7 @@ export const reducer = ( switch (action.type) { case LOGOUT: case ACCOUNT_SWITCH: + case LOGIN_SUCCESS: return initialState; case REGISTER_COMPLETE: diff --git a/src/outbox/outboxReducer.js b/src/outbox/outboxReducer.js index 508d3cc5317..eca174dd611 100644 --- a/src/outbox/outboxReducer.js +++ b/src/outbox/outboxReducer.js @@ -8,6 +8,7 @@ import { ACCOUNT_SWITCH, DELETE_OUTBOX_MESSAGE, MESSAGE_SEND_COMPLETE, + LOGIN_SUCCESS, } from '../actionConstants'; import { NULL_ARRAY } from '../nullObjects'; import { filterArray } from '../utils/immutability'; @@ -44,6 +45,7 @@ export default ( case ACCOUNT_SWITCH: case LOGOUT: + case LOGIN_SUCCESS: return initialState; default: diff --git a/src/streams/streamsReducer.js b/src/streams/streamsReducer.js index 57d65ae38cb..60601305b39 100644 --- a/src/streams/streamsReducer.js +++ b/src/streams/streamsReducer.js @@ -3,7 +3,13 @@ import { EventTypes } from '../api/eventTypes'; import type { PerAccountApplicableAction, StreamsState, StreamUpdateEvent } from '../types'; import type { Stream, Subscription } from '../api/modelTypes'; import { ensureUnreachable } from '../types'; -import { LOGOUT, ACCOUNT_SWITCH, EVENT, REGISTER_COMPLETE } from '../actionConstants'; +import { + LOGOUT, + ACCOUNT_SWITCH, + EVENT, + REGISTER_COMPLETE, + LOGIN_SUCCESS, +} from '../actionConstants'; import { NULL_ARRAY } from '../nullObjects'; import { filterArray } from '../utils/immutability'; @@ -61,6 +67,7 @@ export default ( case LOGOUT: case ACCOUNT_SWITCH: + case LOGIN_SUCCESS: return initialState; case EVENT: { diff --git a/src/topics/topicsReducer.js b/src/topics/topicsReducer.js index 897c20b9775..17e08665198 100644 --- a/src/topics/topicsReducer.js +++ b/src/topics/topicsReducer.js @@ -1,6 +1,12 @@ /* @flow strict-local */ import type { TopicsState, PerAccountApplicableAction } from '../types'; -import { LOGOUT, ACCOUNT_SWITCH, INIT_TOPICS, EVENT_NEW_MESSAGE } from '../actionConstants'; +import { + LOGOUT, + ACCOUNT_SWITCH, + INIT_TOPICS, + EVENT_NEW_MESSAGE, + LOGIN_SUCCESS, +} from '../actionConstants'; import { NULL_OBJECT } from '../nullObjects'; import { replaceItemInArray } from '../utils/immutability'; @@ -43,6 +49,7 @@ export default ( switch (action.type) { case LOGOUT: case ACCOUNT_SWITCH: + case LOGIN_SUCCESS: return initialState; case INIT_TOPICS: diff --git a/src/unread/unreadHuddlesReducer.js b/src/unread/unreadHuddlesReducer.js index f192b0a5147..0b1eedf6129 100644 --- a/src/unread/unreadHuddlesReducer.js +++ b/src/unread/unreadHuddlesReducer.js @@ -10,6 +10,7 @@ import { EVENT_NEW_MESSAGE, EVENT_MESSAGE_DELETE, EVENT_UPDATE_MESSAGE_FLAGS, + LOGIN_SUCCESS, } from '../actionConstants'; import { pmUnreadsKeyFromMessage, @@ -93,6 +94,7 @@ export default ( switch (action.type) { case LOGOUT: case ACCOUNT_SWITCH: + case LOGIN_SUCCESS: return initialState; case REGISTER_COMPLETE: diff --git a/src/unread/unreadMentionsReducer.js b/src/unread/unreadMentionsReducer.js index f6651d09681..05b1b437b95 100644 --- a/src/unread/unreadMentionsReducer.js +++ b/src/unread/unreadMentionsReducer.js @@ -10,6 +10,7 @@ import { EVENT_NEW_MESSAGE, EVENT_MESSAGE_DELETE, EVENT_UPDATE_MESSAGE_FLAGS, + LOGIN_SUCCESS, } from '../actionConstants'; import { addItemsToArray, removeItemsFromArray } from '../utils/immutability'; import { NULL_ARRAY } from '../nullObjects'; @@ -51,6 +52,7 @@ export default ( switch (action.type) { case LOGOUT: case ACCOUNT_SWITCH: + case LOGIN_SUCCESS: return initialState; case REGISTER_COMPLETE: diff --git a/src/unread/unreadModel.js b/src/unread/unreadModel.js index ed9254c6ada..c2c2ef246b3 100644 --- a/src/unread/unreadModel.js +++ b/src/unread/unreadModel.js @@ -23,6 +23,7 @@ import { EVENT_NEW_MESSAGE, EVENT_UPDATE_MESSAGE, EVENT_UPDATE_MESSAGE_FLAGS, + LOGIN_SUCCESS, LOGOUT, MESSAGE_FETCH_COMPLETE, REGISTER_COMPLETE, @@ -226,7 +227,7 @@ function streamsReducer( switch (action.type) { case LOGOUT: case ACCOUNT_SWITCH: - // TODO(#4446) also LOGIN_SUCCESS, presumably + case LOGIN_SUCCESS: return initialStreamsState; case REGISTER_COMPLETE: { diff --git a/src/unread/unreadPmsReducer.js b/src/unread/unreadPmsReducer.js index 2b9ad8d6d3a..ab84f333afc 100644 --- a/src/unread/unreadPmsReducer.js +++ b/src/unread/unreadPmsReducer.js @@ -10,6 +10,7 @@ import { EVENT_NEW_MESSAGE, EVENT_MESSAGE_DELETE, EVENT_UPDATE_MESSAGE_FLAGS, + LOGIN_SUCCESS, } from '../actionConstants'; import { addItemsToPmArray, removeItemsDeeply } from './unreadHelpers'; import { NULL_ARRAY } from '../nullObjects'; @@ -93,6 +94,7 @@ export default ( switch (action.type) { case LOGOUT: case ACCOUNT_SWITCH: + case LOGIN_SUCCESS: return initialState; case REGISTER_COMPLETE: