From d812e41fe45f579b08d8db01f7022462245104c2 Mon Sep 17 00:00:00 2001 From: Samuel Newman Date: Wed, 10 Jun 2026 09:57:00 +0300 Subject: [PATCH] revalidate join links on leave --- src/state/queries/join-links.ts | 29 +++++++++++++++++++ .../queries/messages/leave-conversation.ts | 4 +++ .../queries/messages/list-conversations.tsx | 6 ++++ 3 files changed, 39 insertions(+) diff --git a/src/state/queries/join-links.ts b/src/state/queries/join-links.ts index 01e2d3fdf6..2381f467f4 100644 --- a/src/state/queries/join-links.ts +++ b/src/state/queries/join-links.ts @@ -76,6 +76,35 @@ export function invalidateJoinLinkPreviewsForCode( }) } +/** + * Invalidate any join link preview queries that resolved to the given convo. + * The code isn't always known to the viewer (e.g. when they're a regular + * member), so we match on the convoId carried by the resolved preview instead. + * Use this when the viewer's membership changes (e.g. they leave or are removed) + * so cached previews refetch and reflect their new viewer state. + */ +export function invalidateJoinLinkPreviewsForConvo( + queryClient: QueryClient, + convoId: string, +) { + return queryClient.invalidateQueries({ + predicate: query => { + const [root] = query.queryKey + if (root !== joinLinkPreviewQueryKeyRoot) return false + const data = query.state.data as + | ChatBskyGroupGetJoinLinkPreviews.OutputSchema + | undefined + return ( + data?.joinLinkPreviews.some( + preview => + ChatBskyGroupDefs.isJoinLinkPreviewView(preview) && + preview.convoId === convoId, + ) ?? false + ) + }, + }) +} + async function fetchJoinLinkPreviews({ agent, codes, diff --git a/src/state/queries/messages/leave-conversation.ts b/src/state/queries/messages/leave-conversation.ts index 620ce7ee2c..a9614981e5 100644 --- a/src/state/queries/messages/leave-conversation.ts +++ b/src/state/queries/messages/leave-conversation.ts @@ -11,6 +11,7 @@ import { import {DM_SERVICE_HEADERS} from '#/lib/constants' import {logger} from '#/logger' +import {invalidateJoinLinkPreviewsForConvo} from '#/state/queries/join-links' import {useAgent} from '#/state/session' import {RQKEY_ROOT as CONVO_LIST_KEY} from './list-conversations' @@ -72,6 +73,9 @@ export function useLeaveConvo( }, onSuccess: data => { void queryClient.invalidateQueries({queryKey: [CONVO_LIST_KEY]}) + if (convoId) { + void invalidateJoinLinkPreviewsForConvo(queryClient, convoId) + } onSuccess?.(data) }, onError: (error, _, context) => { diff --git a/src/state/queries/messages/list-conversations.tsx b/src/state/queries/messages/list-conversations.tsx index f7ad22fbae..9cb4e59603 100644 --- a/src/state/queries/messages/list-conversations.tsx +++ b/src/state/queries/messages/list-conversations.tsx @@ -18,6 +18,7 @@ import {DM_SERVICE_HEADERS} from '#/lib/constants' import {useCurrentConvoId} from '#/state/messages/current-convo-id' import {useMessagesEventBus} from '#/state/messages/events' import {useModerationOpts} from '#/state/preferences/moderation-opts' +import {invalidateJoinLinkPreviewsForConvo} from '#/state/queries/join-links' import {useAgent, useSession} from '#/state/session' import {parseConvoView} from '#/components/dms/util' import {useAgeAssurance} from '#/ageAssurance' @@ -257,6 +258,11 @@ export function ListConvosProviderInner({ debouncedRefetch() } else if (ChatBskyConvoDefs.isLogLeaveConvo(log)) { deleteConvoFromAllLists(log.convoId) + // The viewer is no longer in this convo (they left on another + // device, or were removed - removed members receive a + // logLeaveConvo, not a logRemoveMember). Refetch any cached join + // link preview so its viewer state reflects the lost membership. + void invalidateJoinLinkPreviewsForConvo(queryClient, log.convoId) } else if (ChatBskyConvoDefs.isLogDeleteMessage(log)) { updateConvoInAllLists(log.convoId, convo => { if (