Skip to content

Commit f347dca

Browse files
wesbillmanPinky
andauthored
Fix membership-denied community onboarding recovery (#2072)
Signed-off-by: Wes <[email protected]> Co-authored-by: Pinky <44b8e82baa6e0e254e0208d68f335c283c94e7b78dd1fa10d5a49d3f13dd0435@sprout-oss.stage.blox.sqprod.co>
1 parent 929d3aa commit f347dca

4 files changed

Lines changed: 146 additions & 3 deletions

File tree

‎desktop/src/features/communities/ui/CommunityChangeOverlay.tsx‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ import { CommunityEditForm } from "./CommunityEditForm";
55

66
type CommunityChangeOverlayProps = {
77
onClose: () => void;
8+
onUpdated?: (name: string, relayUrl: string) => void;
89
};
910

1011
export function CommunityChangeOverlay({
1112
onClose,
13+
onUpdated,
1214
}: CommunityChangeOverlayProps) {
1315
const { activeCommunity, updateCommunity } = useCommunities();
1416
const [error, setError] = React.useState<string | null>(null);
@@ -40,6 +42,7 @@ export function CommunityChangeOverlay({
4042
onClose();
4143
break;
4244
case "updated":
45+
onUpdated?.(name, relayUrl);
4346
// If reinit is needed, the communityKey change will trigger a remount.
4447
// If not (name-only), just close.
4548
if (!result.requiresReinit) {
@@ -55,7 +58,7 @@ export function CommunityChangeOverlay({
5558
break;
5659
}
5760
},
58-
[activeCommunity, onClose, updateCommunity],
61+
[activeCommunity, onClose, onUpdated, updateCommunity],
5962
);
6063

6164
if (!activeCommunity) return null;

‎desktop/src/features/onboarding/communityOnboarding.tsx‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,12 @@ export type CommunityOnboardingTransaction = {
5353
export type CommunityOnboardingTransactionPatch = Partial<
5454
Pick<
5555
CommunityOnboardingTransaction,
56-
"stage" | "communityId" | "communityName" | "error" | "acknowledged"
56+
| "stage"
57+
| "relayUrl"
58+
| "communityId"
59+
| "communityName"
60+
| "error"
61+
| "acknowledged"
5762
>
5863
>;
5964

‎desktop/src/features/onboarding/ui/CommunityOnboardingFlow.tsx‎

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,25 @@ import {
88
} from "@/features/onboarding/communityOnboarding";
99
import { initializeStarterChannels } from "@/features/onboarding/hooks";
1010
import { useClaimInvite } from "@/features/onboarding/useClaimInvite";
11+
import { CommunityChangeOverlay } from "@/features/communities/ui/CommunityChangeOverlay";
1112
import {
1213
takePendingWelcomeChannelForDirectEntry,
1314
WELCOME_SURFACE_READY_EVENT,
1415
} from "@/features/onboarding/welcome";
16+
import { profileQueryKey } from "@/features/profile/hooks";
1517
import { ProfileAvatar } from "@/features/profile/ui/ProfileAvatar";
1618
import {
1719
parseEmojiAvatarDataUrl,
1820
ProfileAvatarEditor,
1921
} from "@/features/profile/ui/ProfileAvatarEditor";
2022
import { updateProfile } from "@/shared/api/tauriProfiles";
21-
import { getIdentity } from "@/shared/api/tauriIdentity";
23+
import { getIdentity, importIdentity } from "@/shared/api/tauriIdentity";
2224
import { listPersonas } from "@/shared/api/tauriPersonas";
25+
import { relayClient } from "@/shared/api/relayClient";
2326
import type { AgentPersona } from "@/shared/api/types";
2427
import { cn } from "@/shared/lib/cn";
2528
import { Button } from "@/shared/ui/button";
29+
import { MembershipDenied } from "./MembershipDenied";
2630
import { StartupWindowDragRegion } from "@/shared/ui/StartupWindowDragRegion";
2731
import {
2832
ONBOARDING_PRIMARY_CTA_CLASS,
@@ -35,6 +39,16 @@ import {
3539
ONBOARDING_KEY_TEXT_CLASS,
3640
} from "./NsecMaskedDisplay";
3741

42+
function isRelayMembershipDeniedError(error: unknown): boolean {
43+
if (!(error instanceof Error)) return false;
44+
return (
45+
error.message.includes("You must be a relay member") ||
46+
error.message.includes("relay_membership_required") ||
47+
error.message.includes("restricted: not a relay member") ||
48+
error.message.includes("invalid: you are not a relay member")
49+
);
50+
}
51+
3852
const STARTER_PERSONA_ANIMATIONS: Record<string, string> = {
3953
Fizz: "/onboarding/starter-team/fizz.png",
4054
Honey: "/onboarding/starter-team/honey.png",
@@ -113,6 +127,10 @@ export function CommunityOnboardingFlow({
113127
[],
114128
);
115129
const [isPending, setIsPending] = React.useState(false);
130+
const [deniedPubkey, setDeniedPubkey] = React.useState("");
131+
const [isMembershipDenied, setIsMembershipDenied] = React.useState(false);
132+
const [isCommunityChangeOpen, setIsCommunityChangeOpen] =
133+
React.useState(false);
116134
const [isCurtainFading, setIsCurtainFading] = React.useState(false);
117135
const nameInputRef = React.useRef<HTMLInputElement | null>(null);
118136

@@ -228,6 +246,45 @@ export function CommunityOnboardingFlow({
228246

229247
if (!transaction) return null;
230248

249+
if (isMembershipDenied) {
250+
return (
251+
<>
252+
<MembershipDenied
253+
activeRelayUrl={transaction.relayUrl}
254+
onBack={() => setIsMembershipDenied(false)}
255+
onChangeCommunity={() => setIsCommunityChangeOpen(true)}
256+
onImportKey={async (nsec) => {
257+
const identity = await importIdentity(nsec);
258+
relayClient.disconnect();
259+
queryClient.setQueryData(["identity"], identity);
260+
queryClient.removeQueries({ queryKey: profileQueryKey });
261+
setIsMembershipDenied(false);
262+
update({ stage: "connecting", error: undefined });
263+
}}
264+
onRetry={() => {
265+
setIsMembershipDenied(false);
266+
update({ stage: "connecting", error: undefined });
267+
}}
268+
pubkey={deniedPubkey}
269+
/>
270+
{isCommunityChangeOpen ? (
271+
<CommunityChangeOverlay
272+
onClose={() => setIsCommunityChangeOpen(false)}
273+
onUpdated={(communityName, updatedRelayUrl) => {
274+
update({
275+
communityName,
276+
relayUrl: updatedRelayUrl,
277+
stage: "connecting",
278+
error: undefined,
279+
});
280+
setIsMembershipDenied(false);
281+
}}
282+
/>
283+
) : null}
284+
</>
285+
);
286+
}
287+
231288
const saveProfile = async () => {
232289
if (!displayName.trim()) return;
233290
setIsPending(true);
@@ -238,6 +295,16 @@ export function CommunityOnboardingFlow({
238295
});
239296
update({ stage: "team-intro", error: undefined });
240297
} catch (error) {
298+
if (isRelayMembershipDeniedError(error)) {
299+
try {
300+
const identity = await getIdentity();
301+
setDeniedPubkey(identity.pubkey);
302+
} catch {
303+
setDeniedPubkey("");
304+
}
305+
setIsMembershipDenied(true);
306+
return;
307+
}
241308
update({ error: error instanceof Error ? error.message : String(error) });
242309
} finally {
243310
setIsPending(false);

‎desktop/tests/e2e/onboarding.spec.ts‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,74 @@ test("connected first-community profile step cannot discard resumable onboarding
869869
.not.toBeNull();
870870
});
871871

872+
test("membership denial on community profile save offers recovery", async ({
873+
page,
874+
}) => {
875+
await seedActiveIdentity(page, BLANK_TYLER_IDENTITY);
876+
await page.addInitScript(
877+
({ pubkey, transactionStorageKey }) => {
878+
window.localStorage.setItem(
879+
`buzz-machine-onboarding-complete.v2:${pubkey}`,
880+
"true",
881+
);
882+
const timestamp = new Date().toISOString();
883+
window.localStorage.setItem(
884+
transactionStorageKey,
885+
JSON.stringify({
886+
id: "txn-membership-denied",
887+
source: "first-community",
888+
stage: "profile",
889+
relayUrl: "wss://denied.example.com",
890+
communityName: "Denied",
891+
communityId: "e2e-default-community",
892+
createdAt: timestamp,
893+
updatedAt: timestamp,
894+
}),
895+
);
896+
},
897+
{
898+
pubkey: BLANK_TYLER_IDENTITY.pubkey,
899+
transactionStorageKey: COMMUNITY_ONBOARDING_TRANSACTION_STORAGE_KEY,
900+
},
901+
);
902+
await installMockBridge(
903+
page,
904+
{
905+
profileUpdateError:
906+
"relay returned 403 Forbidden: You must be a relay member to access this relay",
907+
},
908+
{
909+
relayWsUrl: "wss://denied.example.com",
910+
skipOnboardingSeed: true,
911+
},
912+
);
913+
await page.goto("/");
914+
915+
await page.getByTestId("community-profile-name-key").fill("Kalvin");
916+
await page.getByTestId("community-profile-next").click();
917+
918+
await expect(page.getByTestId("membership-denied")).toBeVisible();
919+
await expect(
920+
page.getByRole("heading", { name: "Not a member yet" }),
921+
).toBeVisible();
922+
await page.getByRole("button", { name: "Change community" }).click();
923+
await expect(page.getByTestId("community-change-overlay")).toBeVisible();
924+
await page.getByLabel("Community URL").fill("wss://invited.example.com");
925+
await page.getByRole("button", { name: "Save changes" }).click();
926+
await page.getByRole("button", { name: "Use anyway" }).click();
927+
await expect(
928+
page.getByRole("heading", { name: "Build your profile" }),
929+
).toBeVisible();
930+
await expect
931+
.poll(() =>
932+
page.evaluate(
933+
(key) => window.localStorage.getItem(key),
934+
COMMUNITY_ONBOARDING_TRANSACTION_STORAGE_KEY,
935+
),
936+
)
937+
.toContain("wss://invited.example.com");
938+
});
939+
872940
test("identity fallback text does not count as a real onboarding name", async ({
873941
page,
874942
}) => {

0 commit comments

Comments
 (0)