diff --git a/lib/components/farcaster-data-handler.react.js b/lib/components/farcaster-data-handler.react.js --- a/lib/components/farcaster-data-handler.react.js +++ b/lib/components/farcaster-data-handler.react.js @@ -2,6 +2,7 @@ import * as React from 'react'; +import { setAuxUserFIDsActionType } from '../actions/aux-user-actions.js'; import { updateRelationships as serverUpdateRelationships, updateRelationshipsActionTypes, @@ -14,7 +15,7 @@ import { authoritativeKeyserverID } from '../utils/authoritative-keyserver.js'; import { useCurrentUserFID } from '../utils/farcaster-utils.js'; import { useDispatchActionPromise } from '../utils/redux-promise-utils.js'; -import { useSelector } from '../utils/redux-utils.js'; +import { useSelector, useDispatch } from '../utils/redux-utils.js'; import { usingCommServicesAccessToken } from '../utils/services-utils.js'; function FarcasterDataHandler(): React.Node { @@ -30,7 +31,10 @@ const identityServiceClient = React.useContext(IdentityClientContext); const getFarcasterUsers = identityServiceClient?.identityClient.getFarcasterUsers; + const findUserIdentities = + identityServiceClient?.identityClient.findUserIdentities; + const dispatch = useDispatch(); const dispatchActionPromise = useDispatchActionPromise(); const updateRelationships = useLegacyAshoatKeyserverCall( serverUpdateRelationships, @@ -49,11 +53,8 @@ const fid = useCurrentUserFID(); const prevCanQueryRef = React.useRef(); - React.useEffect(() => { - if (!usingCommServicesAccessToken) { - return; - } + const handleFarcasterMutuals = React.useCallback(async () => { const canQuery = isActive && !!fid && loggedIn; if (canQuery === prevCanQueryRef.current) { return; @@ -71,35 +72,31 @@ return; } - void (async () => { - const followerFIDs = await neynarClient.fetchFriendFIDs(fid); - - const commFCUsers = await getFarcasterUsers(followerFIDs); - - const newCommUsers = commFCUsers.filter( - ({ userID }) => !userInfos[userID], - ); - - if (newCommUsers.length === 0) { - return; - } - - const userIDsToFID: { +[userID: string]: string } = Object.fromEntries( - newCommUsers.map(({ userID, farcasterID }) => [userID, farcasterID]), - ); - - const userIDsToFIDIncludingCurrentUser: { +[userID: string]: string } = { - ...userIDsToFID, - [(currentUserID: string)]: fid, - }; - - void dispatchActionPromise( - updateRelationshipsActionTypes, - createThreadsAndRobotextForFarcasterMutuals( - userIDsToFIDIncludingCurrentUser, - ), - ); - })(); + const followerFIDs = await neynarClient.fetchFriendFIDs(fid); + + const commFCUsers = await getFarcasterUsers(followerFIDs); + + const newCommUsers = commFCUsers.filter(({ userID }) => !userInfos[userID]); + + if (newCommUsers.length === 0) { + return; + } + + const userIDsToFID: { +[userID: string]: string } = Object.fromEntries( + newCommUsers.map(({ userID, farcasterID }) => [userID, farcasterID]), + ); + + const userIDsToFIDIncludingCurrentUser: { +[userID: string]: string } = { + ...userIDsToFID, + [(currentUserID: string)]: fid, + }; + + void dispatchActionPromise( + updateRelationshipsActionTypes, + createThreadsAndRobotextForFarcasterMutuals( + userIDsToFIDIncludingCurrentUser, + ), + ); }, [ isActive, fid, @@ -112,6 +109,42 @@ currentUserID, ]); + const handleUserStoreFIDs = React.useCallback(async () => { + if (!loggedIn || !isActive || !findUserIdentities) { + return; + } + + const userStoreIDs = Object.keys(userInfos); + + const userIdentities = await findUserIdentities(userStoreIDs); + + const userStoreFarcasterUsers = Object.entries(userIdentities) + .filter(([, identity]) => identity.farcasterID !== null) + .map(([userID, identity]) => ({ + userID, + username: identity.username, + farcasterID: identity.farcasterID, + })); + + dispatch({ + type: setAuxUserFIDsActionType, + payload: { + farcasterUsers: userStoreFarcasterUsers, + }, + }); + }, [loggedIn, isActive, findUserIdentities, userInfos, dispatch]); + + React.useEffect(() => { + if (!usingCommServicesAccessToken) { + return; + } + + void (async () => { + await Promise.all([handleFarcasterMutuals(), handleUserStoreFIDs()]); + }, + []); + }, [handleFarcasterMutuals, handleUserStoreFIDs]); + return null; }