Skip to content

Commit

Permalink
[lib][web][native] Refactor cookieSelector
Browse files Browse the repository at this point in the history
Summary: issue: https://linear.app/comm/issue/ENG-5005/refactor-cookieselector

Test Plan: Tested that ther refactored selectors work

Reviewers: michal, kamil, ginsu

Reviewed By: kamil

Subscribers: ashoat, tomek, wyilio

Differential Revision: https://phab.comm.dev/D9733
  • Loading branch information
InkaAlicja committed Nov 9, 2023
1 parent aa4a1a0 commit 16501e4
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 18 deletions.
2 changes: 1 addition & 1 deletion lib/selectors/account-selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const basePreRequestUserStateForSingleKeyserverSelector: (
) => (state: AppState) => PreRequestUserState = keyserverID =>
createSelector(
(state: AppState) => state.currentUserInfo,
cookieSelector,
cookieSelector(keyserverID),
sessionIDSelector(keyserverID),
(
currentUserInfo: ?CurrentUserInfo,
Expand Down
10 changes: 7 additions & 3 deletions lib/selectors/keyserver-selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ import type {
import type { AppState } from '../types/redux-types.js';
import type { ConnectionInfo } from '../types/socket-types.js';
import type { UserInfos } from '../types/user-types.js';
import { ashoatKeyserverID } from '../utils/validation-utils.js';

const cookieSelector: (state: AppState) => ?string = (state: AppState) =>
state.keyserverStore.keyserverInfos[ashoatKeyserverID]?.cookie;
const baseCookieSelector: (
keyserverID: string,
) => (state: AppState) => ?string = keyserverID => (state: AppState) =>
state.keyserverStore.keyserverInfos[keyserverID]?.cookie;

const cookieSelector: (keyserverID: string) => (state: AppState) => ?string =
_memoize(baseCookieSelector);

const cookiesSelector: (state: AppState) => {
+[keyserverID: string]: string,
Expand Down
2 changes: 1 addition & 1 deletion lib/selectors/server-calls.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const baseServerCallStateSelector: (
keyserverID: string,
) => (state: AppState) => ServerCallState = keyserverID =>
createSelector(
cookieSelector,
cookieSelector(keyserverID),
urlPrefixSelector(keyserverID),
sessionIDSelector(keyserverID),
(state: AppState) => state.currentUserInfo,
Expand Down
2 changes: 1 addition & 1 deletion lib/shared/session-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function invalidSessionDowngrade(
preRequestUserState &&
(preRequestUserState.currentUserInfo?.id !== currentCurrentUserInfo.id ||
preRequestUserState.cookiesAndSessions[ashoatKeyserverID].cookie !==
cookieSelector(currentReduxState) ||
cookieSelector(ashoatKeyserverID)(currentReduxState) ||
preRequestUserState.cookiesAndSessions[ashoatKeyserverID].sessionID !==
sessionIDSelector(ashoatKeyserverID)(currentReduxState))
);
Expand Down
2 changes: 1 addition & 1 deletion native/account/logged-out-modal.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -809,7 +809,7 @@ const ConnectedLoggedOutModal: React.ComponentType<BaseProps> =
state => !!(state._persist && state._persist.rehydrated && navContext),
);
const persistedStateLoaded = usePersistedStateLoaded();
const cookie = useSelector(cookieSelector);
const cookie = useSelector(cookieSelector(ashoatKeyserverID));
const urlPrefix = useSelector(urlPrefixSelector(ashoatKeyserverID));
invariant(urlPrefix, "missing urlPrefix for ashoat's keyserver");
const loggedIn = useSelector(isLoggedIn);
Expand Down
2 changes: 1 addition & 1 deletion native/data/sqlite-data-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function SQLiteDataHandler(): React.Node {
const rehydrateConcluded = useSelector(
state => !!(state._persist && state._persist.rehydrated),
);
const cookie = useSelector(cookieSelector);
const cookie = useSelector(cookieSelector(ashoatKeyserverID));
const urlPrefix = useSelector(urlPrefixSelector(ashoatKeyserverID));
invariant(urlPrefix, "missing urlPrefix for ashoat's keyserver");
const staffCanSee = useStaffCanSee();
Expand Down
3 changes: 2 additions & 1 deletion native/navigation/navigation-handler.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as React from 'react';

import { cookieSelector } from 'lib/selectors/keyserver-selectors.js';
import { isLoggedIn } from 'lib/selectors/user-selectors.js';
import { ashoatKeyserverID } from 'lib/utils/validation-utils.js';

import { logInActionType, logOutActionType } from './action-types.js';
import ModalPruner from './modal-pruner.react.js';
Expand Down Expand Up @@ -60,7 +61,7 @@ const LogInHandler = React.memo<LogInHandlerProps>(function LogInHandler(

const hasCurrentUserInfo = useSelector(isLoggedIn);

const cookie = useSelector(cookieSelector);
const cookie = useSelector(cookieSelector(ashoatKeyserverID));
const hasUserCookie = !!(cookie && cookie.startsWith('user='));

const loggedIn = hasCurrentUserInfo && hasUserCookie;
Expand Down
17 changes: 12 additions & 5 deletions native/selectors/socket-selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const baseOpenSocketSelector: (
// the socket needs to be reopened. By including the cookie here,
// whenever the cookie changes this function will change,
// which tells the Socket component to restart the connection.
cookieSelector,
cookieSelector(keyserverID),
(urlPrefix: ?string) => {
if (!urlPrefix) {
return null;
Expand All @@ -51,11 +51,18 @@ const openSocketSelector: (
keyserverID: string,
) => (state: AppState) => ?() => WebSocket = _memoize(baseOpenSocketSelector);

const baseSessionIdentificationSelector: (
keyserverID: string,
) => (state: AppState) => SessionIdentification = keyserverID =>
createSelector(
cookieSelector(keyserverID),
(cookie: ?string): SessionIdentification => ({ cookie }),
);

const sessionIdentificationSelector: (
state: AppState,
) => SessionIdentification = createSelector(
cookieSelector,
(cookie: ?string): SessionIdentification => ({ cookie }),
keyserverID: string,
) => (state: AppState) => SessionIdentification = _memoize(
baseSessionIdentificationSelector,
);

function oneTimeKeyGenerator(inc: number): string {
Expand Down
6 changes: 4 additions & 2 deletions native/socket.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const NativeSocket: React.ComponentType<BaseSocketProps> =
const inputState = React.useContext(InputStateContext);
const navContext = React.useContext(NavContext);

const cookie = useSelector(cookieSelector);
const cookie = useSelector(cookieSelector(ashoatKeyserverID));
const urlPrefix = useSelector(urlPrefixSelector(ashoatKeyserverID));
invariant(urlPrefix, 'missing urlPrefix for given keyserver id');
const connection = useSelector(connectionSelector(ashoatKeyserverID));
Expand All @@ -61,7 +61,9 @@ const NativeSocket: React.ComponentType<BaseSocketProps> =

const openSocket = useSelector(openSocketSelector(ashoatKeyserverID));
invariant(openSocket, 'openSocket failed to be created');
const sessionIdentification = useSelector(sessionIdentificationSelector);
const sessionIdentification = useSelector(
sessionIdentificationSelector(ashoatKeyserverID),
);
const preRequestUserState = useSelector(
preRequestUserStateForSingleKeyserverSelector(ashoatKeyserverID),
);
Expand Down
2 changes: 1 addition & 1 deletion web/selectors/socket-selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const baseSessionIdentificationSelector: (
keyserverID: string,
) => (state: AppState) => SessionIdentification = keyserverID =>
createSelector(
cookieSelector,
cookieSelector(keyserverID),
sessionIDSelector(keyserverID),
(cookie: ?string, sessionID: ?string): SessionIdentification => ({
cookie,
Expand Down
2 changes: 1 addition & 1 deletion web/socket.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { decompressMessage } from './utils/decompress.js';

const WebSocket: React.ComponentType<BaseSocketProps> =
React.memo<BaseSocketProps>(function WebSocket(props) {
const cookie = useSelector(cookieSelector);
const cookie = useSelector(cookieSelector(ashoatKeyserverID));
const urlPrefix = useSelector(urlPrefixSelector(ashoatKeyserverID));
invariant(urlPrefix, 'missing urlPrefix for given keyserver id');
const connection = useSelector(connectionSelector(ashoatKeyserverID));
Expand Down

0 comments on commit 16501e4

Please sign in to comment.