Skip to content

Commit

Permalink
[lib] Invalidate UserIdentityCache when changing FID
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Ashoat committed Oct 17, 2024
1 parent 3c1f9f7 commit 8086a05
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
9 changes: 8 additions & 1 deletion lib/components/user-identity-cache.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type UserIdentityCache = {
userIDs: $ReadOnlyArray<string>,
) => Promise<UserIdentitiesResponse>,
+getCachedUserIdentity: (userID: string) => ?UserIdentityResult,
+invalidateCacheForUser: (userID: string) => void,
};

type UserIdentityResult =
Expand Down Expand Up @@ -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 (
Expand Down
8 changes: 7 additions & 1 deletion lib/utils/farcaster-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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
Expand All @@ -44,8 +47,11 @@ function useSetLocalFID(): (fid: ?string) => void {
data: fidToSet,
},
});
if (currentUserID) {
invalidateCacheForUser(currentUserID);
}
},
[dispatch],
[dispatch, currentUserID, invalidateCacheForUser],
);
}

Expand Down

0 comments on commit 8086a05

Please sign in to comment.