diff --git a/lib/actions/aux-user-actions.js b/lib/actions/aux-user-actions.js --- a/lib/actions/aux-user-actions.js +++ b/lib/actions/aux-user-actions.js @@ -2,12 +2,15 @@ const setAuxUserFIDsActionType = 'SET_AUX_USER_FIDS'; +const addAuxUserFIDsActionType = 'ADD_AUX_USER_FIDS'; + const clearAuxUserFIDsActionType = 'CLEAR_AUX_USER_FIDS'; const setPeerDeviceListsActionType = 'SET_PEER_DEVICE_LISTS'; export { setAuxUserFIDsActionType, + addAuxUserFIDsActionType, clearAuxUserFIDsActionType, setPeerDeviceListsActionType, }; diff --git a/lib/reducers/aux-user-reducer.js b/lib/reducers/aux-user-reducer.js --- a/lib/reducers/aux-user-reducer.js +++ b/lib/reducers/aux-user-reducer.js @@ -2,6 +2,7 @@ import { setAuxUserFIDsActionType, + addAuxUserFIDsActionType, clearAuxUserFIDsActionType, setPeerDeviceListsActionType, } from '../actions/aux-user-actions.js'; @@ -24,6 +25,44 @@ +auxUserStoreOperations: $ReadOnlyArray, } { if (action.type === setAuxUserFIDsActionType) { + const toUpdateUserIDs = new Set( + action.payload.farcasterUsers.map(farcasterUser => farcasterUser.userID), + ); + const replaceOperations: ReplaceAuxUserInfoOperation[] = []; + for (const userID in state.auxUserInfos) { + if ( + state.auxUserInfos[userID].fid !== null && + !toUpdateUserIDs.has(userID) + ) { + replaceOperations.push({ + type: 'replace_aux_user_info', + payload: { + id: userID, + auxUserInfo: { + ...state.auxUserInfos[userID], + fid: null, + }, + }, + }); + } + } + for (const farcasterUser of action.payload.farcasterUsers) { + replaceOperations.push({ + type: 'replace_aux_user_info', + payload: { + id: farcasterUser.userID, + auxUserInfo: { + ...state.auxUserInfos[farcasterUser.userID], + fid: farcasterUser.farcasterID, + }, + }, + }); + } + return { + auxUserStore: processStoreOps(state, replaceOperations), + auxUserStoreOperations: replaceOperations, + }; + } else if (action.type === addAuxUserFIDsActionType) { const replaceOperations: ReplaceAuxUserInfoOperation[] = []; for (const farcasterUser of action.payload.farcasterUsers) { replaceOperations.push({ diff --git a/lib/reducers/aux-user-reducer.test.js b/lib/reducers/aux-user-reducer.test.js --- a/lib/reducers/aux-user-reducer.test.js +++ b/lib/reducers/aux-user-reducer.test.js @@ -2,6 +2,7 @@ import { reduceAuxUserStore } from './aux-user-reducer.js'; import { + addAuxUserFIDsActionType, setAuxUserFIDsActionType, setPeerDeviceListsActionType, } from '../actions/aux-user-actions.js'; @@ -10,19 +11,23 @@ jest.mock('../utils/config.js'); describe('reduceAuxUserStore', () => { - it('should update aux user store with farcaster data for user', () => { + it('should update aux user store with farcaster data for user2', () => { const oldAuxUserStore = { - auxUserInfos: {}, + auxUserInfos: { + userID_1: { + fid: 'farcasterID_1', + }, + }, }; const updateAuxUserInfosAction = { - type: setAuxUserFIDsActionType, + type: addAuxUserFIDsActionType, payload: { farcasterUsers: [ { - userID: 'userID_1', - username: 'username_1', - farcasterID: 'farcasterID_1', + userID: 'userID_2', + username: 'username_2', + farcasterID: 'farcasterID_2', }, ], }, @@ -36,6 +41,46 @@ userID_1: { fid: 'farcasterID_1', }, + userID_2: { + fid: 'farcasterID_2', + }, + }, + }); + }); + + it('should set aux user store user1 fid to null and add user2', () => { + const oldAuxUserStore = { + auxUserInfos: { + userID_1: { + fid: 'farcasterID_1', + }, + }, + }; + + const updateAuxUserInfosAction = { + type: setAuxUserFIDsActionType, + payload: { + farcasterUsers: [ + { + userID: 'userID_2', + username: 'username_2', + farcasterID: 'farcasterID_2', + }, + ], + }, + }; + + expect( + reduceAuxUserStore(oldAuxUserStore, updateAuxUserInfosAction) + .auxUserStore, + ).toEqual({ + auxUserInfos: { + userID_1: { + fid: null, + }, + userID_2: { + fid: 'farcasterID_2', + }, }, }); }); diff --git a/lib/types/aux-user-types.js b/lib/types/aux-user-types.js --- a/lib/types/aux-user-types.js +++ b/lib/types/aux-user-types.js @@ -18,6 +18,10 @@ +farcasterUsers: $ReadOnlyArray, }; +export type AddAuxUserFIDsPayload = { + +farcasterUsers: $ReadOnlyArray, +}; + export type SetPeerDeviceListsPayload = { +deviceLists: UsersRawDeviceLists, }; diff --git a/lib/types/redux-types.js b/lib/types/redux-types.js --- a/lib/types/redux-types.js +++ b/lib/types/redux-types.js @@ -19,6 +19,7 @@ import type { AuxUserStore, SetAuxUserFIDsPayload, + AddAuxUserFIDsPayload, SetPeerDeviceListsPayload, } from './aux-user-types.js'; import type { @@ -1390,6 +1391,10 @@ +type: 'SET_AUX_USER_FIDS', +payload: SetAuxUserFIDsPayload, } + | { + +type: 'ADD_AUX_USER_FIDS', + +payload: AddAuxUserFIDsPayload, + } | { +type: 'CLEAR_AUX_USER_FIDS', +payload?: void,