From 8086a05f54b866752e3581714f81dfa3a4e8b765 Mon Sep 17 00:00:00 2001 From: Ashoat Tevosyan Date: Wed, 16 Oct 2024 23:25:53 -0400 Subject: [PATCH] [lib] Invalidate UserIdentityCache when changing FID Summary: When we change the current user's FID, we should invalidate their entry in the `UserIdentityCache`. Otherwise, `FarcasterDataHandler` will continue to use the old value, and can end up wiping the FID if `handleCurrentUserFID` is run again before the app is killed (this can happen if the app is backgrounded/foregrounded). This addresses [ENG-9704](https://linear.app/comm/issue/ENG-9704/farcaster-channel-books-linked-to-deleted-community). Depends on D13736 Test Plan: 1. Open mobile app when Farcaster account not linked 2. Link Farcaster account 3. Background the app 4. Foreground the app 5. Confirm that the Farcaster account is not unlinked Reviewers: will Reviewed By: will Subscribers: tomek Differential Revision: https://phab.comm.dev/D13737 --- lib/components/user-identity-cache.react.js | 9 ++++++++- lib/utils/farcaster-utils.js | 8 +++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/components/user-identity-cache.react.js b/lib/components/user-identity-cache.react.js index 0bb2ee7e31..3a0167d09f 100644 --- a/lib/components/user-identity-cache.react.js +++ b/lib/components/user-identity-cache.react.js @@ -51,6 +51,7 @@ type UserIdentityCache = { userIDs: $ReadOnlyArray, ) => Promise, +getCachedUserIdentity: (userID: string) => ?UserIdentityResult, + +invalidateCacheForUser: (userID: string) => void, }; type UserIdentityResult = @@ -225,12 +226,18 @@ function UserIdentityCacheProvider(props: Props): React.Node { [getCachedUserIdentityEntry, findUserIdentities], ); + const invalidateCacheForUser = React.useCallback((userID: string) => { + const cache = userIdentityCacheRef.current; + cache.delete(userID); + }, []); + const value = React.useMemo( () => ({ getUserIdentities, getCachedUserIdentity, + invalidateCacheForUser, }), - [getUserIdentities, getCachedUserIdentity], + [getUserIdentities, getCachedUserIdentity, invalidateCacheForUser], ); return ( diff --git a/lib/utils/farcaster-utils.js b/lib/utils/farcaster-utils.js index 72e338728e..84df68418a 100644 --- a/lib/utils/farcaster-utils.js +++ b/lib/utils/farcaster-utils.js @@ -4,6 +4,7 @@ import invariant from 'invariant'; import * as React from 'react'; import { setSyncedMetadataEntryActionType } from '../actions/synced-metadata-actions.js'; +import { useUserIdentityCache } from '../components/user-identity-cache.react.js'; import { IdentityClientContext } from '../shared/identity-client-context.js'; import { syncedMetadataNames } from '../types/synced-metadata-types.js'; import { useSelector, useDispatch } from '../utils/redux-utils.js'; @@ -32,6 +33,8 @@ function useCurrentUserFID(): ?string { function useSetLocalFID(): (fid: ?string) => void { const dispatch = useDispatch(); + const { invalidateCacheForUser } = useUserIdentityCache(); + const currentUserID = useSelector(state => state.currentUserInfo?.id); return React.useCallback( (fid: ?string) => { // If we're unsetting the FID, we should set it to NO_FID_METADATA to @@ -44,8 +47,11 @@ function useSetLocalFID(): (fid: ?string) => void { data: fidToSet, }, }); + if (currentUserID) { + invalidateCacheForUser(currentUserID); + } }, - [dispatch], + [dispatch, currentUserID, invalidateCacheForUser], ); }