diff --git a/lib/tunnelbroker/use-peer-to-peer-message-handler.js b/lib/tunnelbroker/use-peer-to-peer-message-handler.js --- a/lib/tunnelbroker/use-peer-to-peer-message-handler.js +++ b/lib/tunnelbroker/use-peer-to-peer-message-handler.js @@ -55,6 +55,7 @@ import { getContentSigningKey } from '../utils/crypto-utils.js'; import { getMessageForException } from '../utils/errors.js'; import { + useClearFarcasterThreads, useSetLocalCurrentUserSupportsDCs, useSetLocalFID, } from '../utils/farcaster-utils.js'; @@ -108,6 +109,7 @@ const setLocalFID = useSetLocalFID(); const setLocalDCsSupport = useSetLocalCurrentUserSupportsDCs(); + const clearFarcasterThreads = useClearFarcasterThreads(); const fullBackupSupport = useFullBackupSupportEnabled(); @@ -238,6 +240,9 @@ ) { setLocalFID(userActionMessage.farcasterID); setLocalDCsSupport(userActionMessage.hasDCsToken); + if (!userActionMessage.hasDCsToken) { + clearFarcasterThreads(); + } } else { console.warn( 'Unsupported P2P user action message:', @@ -258,6 +263,7 @@ setLocalFID, userDataRestore, fullBackupSupport, + clearFarcasterThreads, ], ); } diff --git a/lib/utils/farcaster-utils.js b/lib/utils/farcaster-utils.js --- a/lib/utils/farcaster-utils.js +++ b/lib/utils/farcaster-utils.js @@ -17,6 +17,7 @@ } from '../permissions/thread-permissions.js'; import { getOwnPeerDevices } from '../selectors/user-selectors.js'; import { generatePendingThreadColor } from '../shared/color-utils.js'; +import { processFarcasterOpsActionType } from '../shared/farcaster/farcaster-actions.js'; import type { FarcasterConversation } from '../shared/farcaster/farcaster-conversation-types.js'; import type { FarcasterMessage } from '../shared/farcaster/farcaster-messages-types.js'; import { farcasterThreadIDFromConversationID } from '../shared/id-utils.js'; @@ -42,6 +43,7 @@ import { farcasterThreadTypes } from '../types/thread-types-enum.js'; import type { FarcasterThreadType } from '../types/thread-types-enum.js'; import type { FarcasterConnectionUpdated } from '../types/tunnelbroker/user-actions-peer-to-peer-message-types.js'; +import { updateTypes } from '../types/update-types-enum.js'; const DISABLE_CONNECT_FARCASTER_ALERT = false; const NO_FID_METADATA = 'NONE'; @@ -155,6 +157,30 @@ ); } +function useClearFarcasterThreads(): () => void { + const threads = useSelector(state => state.threadStore.threadInfos); + const dispatch = useDispatch(); + return React.useCallback(() => { + const farcasterThreadIDs = Object.values(threads) + .filter(thread => thread.farcaster) + .map(thread => thread.id); + const time = Date.now(); + const updates = farcasterThreadIDs.map(threadID => ({ + type: updateTypes.DELETE_THREAD, + id: uuid.v4(), + time, + threadID, + })); + dispatch({ + type: processFarcasterOpsActionType, + payload: { + rawMessageInfos: [], + updateInfos: updates, + }, + }); + }, [dispatch, threads]); +} + function useUnlinkFID(): () => Promise { const identityClientContext = React.useContext(IdentityClientContext); invariant(identityClientContext, 'identityClientContext should be set'); @@ -166,17 +192,20 @@ const setLocalDCsSupport = useSetLocalCurrentUserSupportsDCs(); const broadcastConnectionStatus = useBroadcastUpdateFarcasterConnectionStatus(); + const clearFarcasterThreads = useClearFarcasterThreads(); return React.useCallback(async () => { await unlinkFarcasterAccount(); setLocalFID(null); setLocalDCsSupport(null); await broadcastConnectionStatus(null, null); + clearFarcasterThreads(); }, [ unlinkFarcasterAccount, setLocalFID, setLocalDCsSupport, broadcastConnectionStatus, + clearFarcasterThreads, ]); } @@ -474,4 +503,5 @@ createFarcasterDCsAuthMessage, createFarcasterRawThreadInfo, convertFarcasterMessageToCommMessages, + useClearFarcasterThreads, };