From bf12fe45f3d90ed6a79e174c3327182972ec2664 Mon Sep 17 00:00:00 2001 From: DoodleBear Date: Sun, 18 Oct 2026 14:31:29 +0900 Subject: [PATCH 1/6] Join scoped cloud invitations from the mobile create flow Issue: #2161 Agent-Task: codex/local/01a148ae-ec7f-74b2-8157-961411ce54c9 --- apps/mobile/app/(tabs)/(home)/index.tsx | 2 +- apps/mobile/app/+native-intent.tsx | 27 +- apps/mobile/app/_layout.tsx | 1 + apps/mobile/app/cloud-join.tsx | 1 + apps/mobile/src/cloud/client.ts | 48 ++++ apps/mobile/src/cloud/invitation-link.ts | 28 ++ apps/mobile/src/cloud/native-intent.ts | 20 +- apps/mobile/src/cloud/native.ts | 8 + apps/mobile/src/ui/cloud/channels-screen.tsx | 43 ++- .../src/ui/cloud/invitations-screen.tsx | 31 ++- apps/mobile/src/ui/cloud/join-screen.tsx | 260 ++++++++++++++++++ apps/mobile/src/ui/shell/create-button.tsx | 14 +- apps/mobile/src/ui/strings.ts | 56 ++++ apps/mobile/test/cloud-client.test.ts | 90 ++++++ apps/mobile/test/create-button.test.tsx | 25 +- apps/mobile/test/home-search.test.tsx | 14 +- changes/2161-invitation-journey.md | 11 + docs/architecture/cloud-navigation.md | 10 +- 18 files changed, 665 insertions(+), 24 deletions(-) create mode 100644 apps/mobile/app/cloud-join.tsx create mode 100644 apps/mobile/src/cloud/invitation-link.ts create mode 100644 apps/mobile/src/ui/cloud/join-screen.tsx create mode 100644 changes/2161-invitation-journey.md diff --git a/apps/mobile/app/(tabs)/(home)/index.tsx b/apps/mobile/app/(tabs)/(home)/index.tsx index 703518d3ad..95313a2a48 100644 --- a/apps/mobile/app/(tabs)/(home)/index.tsx +++ b/apps/mobile/app/(tabs)/(home)/index.tsx @@ -68,7 +68,7 @@ export default function HomeScreen() { ); - if (saved === null) return ; + if (saved === null) return ; const create = (target: CreateTarget) => router.push({ pathname: '/full-client', params: fullClientParams(target) }); diff --git a/apps/mobile/app/+native-intent.tsx b/apps/mobile/app/+native-intent.tsx index 49bd002b13..96aa727284 100644 --- a/apps/mobile/app/+native-intent.tsx +++ b/apps/mobile/app/+native-intent.tsx @@ -1,4 +1,27 @@ +import * as SecureStore from 'expo-secure-store'; import { cloudNativeIntent } from '../src/cloud/native-intent'; -export function redirectSystemPath({ path }: { path: string; initial: boolean }) { - return cloudNativeIntent(path); +import { parseInvitationLink } from '../src/cloud/invitation-link'; +export async function redirectSystemPath({ path }: { path: string; initial: boolean }) { + const destination = cloudNativeIntent(path); + if (destination.startsWith('/cloud-join?link=')) { + try { + const link = new URL(destination, 'https://native.invalid').searchParams.get('link')!; + parseInvitationLink(link); + await SecureStore.setItemAsync('tob.cloud.pending-join.v1', link); + } catch { + /* The route still carries the target if local persistence is unavailable. */ + } + return destination; + } + if (destination !== '/cloud-services') return destination; + try { + const pending = await SecureStore.getItemAsync('tob.cloud.pending-join.v1'); + if (pending) { + parseInvitationLink(pending); + return '/cloud-join'; + } + } catch { + /* Existing login can still return to its neutral service screen. */ + } + return destination; } diff --git a/apps/mobile/app/_layout.tsx b/apps/mobile/app/_layout.tsx index 82772f7530..c74d0d4adf 100644 --- a/apps/mobile/app/_layout.tsx +++ b/apps/mobile/app/_layout.tsx @@ -60,6 +60,7 @@ export default function RootLayout() { /> + + this.lock(async () => { + const target = parseInvitationLink(link); + const response = await this.json(target.server + '/.well-known/open-cloud-groups'); + if (!response.ok) return fail('INSTANCE_UNAVAILABLE'); + const config = parseInstance(response.value); + if (config.server !== target.server) return fail('INVITATION_TARGET_MISMATCH'); + return config; + }); + previewInvitation = (id: string, link: string) => + this.lock(async () => { + const target = parseInvitationLink(link); + const saved = this.entry(id); + if (saved.config.server !== target.server) return fail('INVITATION_TARGET_MISMATCH'); + const entry = await this.current(saved); + const value = parseInvitation( + await this.orgRequest( + entry, + '/' + target.invitationId, + {}, + target.guest ? '/api/channel-invitations' : '/api/invitations', + ), + ); + if (value.invitationId !== target.invitationId || (value.role === 'guest') !== target.guest) + return fail('INVALID_RESPONSE'); + return value; + }); + openChannel = (id: string, organizationId: string, channelId: string, guest = false) => + this.lock(async () => { + if (!validChannelId(channelId)) return fail('INVALID_CHANNEL'); + const entry = guest + ? await this.current(this.entry(id)) + : await this.channelEntry(id, organizationId); + const raw = await this.orgRequest( + entry, + (guest ? '' : '/' + organizationId + '/channels') + '/' + channelId, + {}, + guest ? '/api/guest-channels' : '/api/organizations', + ); + const channel = parseChannel(raw); + if ( + channel.channelId !== channelId || + (guest && (raw.access !== 'guest' || channel.organizationId !== organizationId)) + ) + return fail('INVALID_RESPONSE'); + return channel; + }); importConfig = (text: string, replace = false) => this.lock(async () => { const config = parseCloudQr(text); diff --git a/apps/mobile/src/cloud/invitation-link.ts b/apps/mobile/src/cloud/invitation-link.ts new file mode 100644 index 0000000000..ef26f09a4f --- /dev/null +++ b/apps/mobile/src/cloud/invitation-link.ts @@ -0,0 +1,28 @@ +import { ClientError, serverOrigin } from './instance-contract'; + +// A locator for a recipient-bound invitation. Possession never grants membership. +export function invitationLink(server: string, invitationId: string): string { + serverOrigin(server); + if (!/^(?:invitation_[A-Za-z0-9]{1,120}|ci_[a-f0-9]{64})$/.test(invitationId)) + throw new ClientError('INVALID_INVITATION_LINK'); + return server + '/join?invitation=' + invitationId; +} +export function parseInvitationLink(raw: string) { + try { + if (raw.length > 2048 || raw !== raw.trim()) throw Error(); + const url = new URL(raw); + const id = url.searchParams.get('invitation') ?? ''; + if ( + url.username || + url.password || + url.hash || + url.pathname !== '/join' || + [...url.searchParams.keys()].length !== 1 || + invitationLink(url.origin, id) !== raw + ) + throw Error(); + return { server: url.origin, invitationId: id, guest: id.startsWith('ci_') }; + } catch { + throw new ClientError('INVALID_INVITATION_LINK'); + } +} diff --git a/apps/mobile/src/cloud/native-intent.ts b/apps/mobile/src/cloud/native-intent.ts index 2facac619b..caadea468d 100644 --- a/apps/mobile/src/cloud/native-intent.ts +++ b/apps/mobile/src/cloud/native-intent.ts @@ -1,3 +1,21 @@ +import { parseInvitationLink } from './invitation-link'; export function cloudNativeIntent(path: string): string { - return /^ai\.botharness\.deepseekbot\.dev:/i.test(path) ? '/cloud-services' : path; + if (!/^ai\.botharness\.deepseekbot\.dev:/i.test(path)) return path; + try { + const url = new URL(path); + const link = url.searchParams.get('link'); + if ( + !url.host && + url.pathname === '/cloud-join' && + !url.hash && + [...url.searchParams.keys()].length === 1 && + link + ) { + parseInvitationLink(link); + return '/cloud-join?link=' + encodeURIComponent(link); + } + } catch { + /* Invalid external input opens a neutral entry point. */ + } + return '/cloud-services'; } diff --git a/apps/mobile/src/cloud/native.ts b/apps/mobile/src/cloud/native.ts index 661da3f0b0..fd00951c62 100644 --- a/apps/mobile/src/cloud/native.ts +++ b/apps/mobile/src/cloud/native.ts @@ -44,3 +44,11 @@ const subscribe = (listener: () => void) => { export function useCloudSnapshot() { return useSyncExternalStore(subscribe, cloudClient.getSnapshot, cloudClient.getSnapshot); } + +// A navigation draft, never an access grant. Keep it through browser return/restart. +const JOIN_KEY = 'tob.cloud.pending-join.v1'; +export const pendingCloudJoin = { + read: () => SecureStore.getItemAsync(JOIN_KEY), + save: (link: string) => SecureStore.setItemAsync(JOIN_KEY, link), + clear: () => SecureStore.deleteItemAsync(JOIN_KEY), +}; diff --git a/apps/mobile/src/ui/cloud/channels-screen.tsx b/apps/mobile/src/ui/cloud/channels-screen.tsx index b82c669137..664398f476 100644 --- a/apps/mobile/src/ui/cloud/channels-screen.tsx +++ b/apps/mobile/src/ui/cloud/channels-screen.tsx @@ -1,5 +1,5 @@ import { router, useLocalSearchParams } from 'expo-router'; -import { useState } from 'react'; +import { useEffect, useRef, useState } from 'react'; import { FlatList, View, type ScrollViewProps } from 'react-native'; import { useSafeAreaInsets } from 'react-native-safe-area-context'; import { cloudError } from '../../cloud/client'; @@ -37,17 +37,19 @@ function renderGuestChannelFormScroll(props: ScrollViewProps) { } export function CloudChannelsScreen() { - const { instanceId, organizationId, guest } = useLocalSearchParams<{ + const { instanceId, organizationId, guest, channelId } = useLocalSearchParams<{ instanceId: string; organizationId?: string; guest?: string; + channelId?: string; }>(); return ( ); } @@ -55,10 +57,12 @@ function ChannelWorkspace({ instanceId, organizationId, guest, + initialChannelId, }: { instanceId: string; organizationId: string; guest: boolean; + initialChannelId?: string | undefined; }) { const snapshot = useCloudSnapshot(), theme = useDesignTheme(), @@ -77,6 +81,21 @@ function ChannelWorkspace({ [error, setError] = useState(), [notice, setNotice] = useState(); const busy = working || snapshot.busy; + const opened = useRef(false); + useEffect(() => { + if (!initialChannelId || !snapshot.ready || snapshot.busy || opened.current) return; + opened.current = true; + void run(async () => { + const channel = await cloudClient.openChannel( + instanceId, + organizationId, + initialChannelId, + guest, + ); + await history(channel); + setSelected(channel); + }); + }, [initialChannelId, instanceId, organizationId, guest, snapshot.ready, snapshot.busy]); const pending = row?.pendingChannelAction; const pendingHere = ( guest @@ -163,6 +182,24 @@ function ChannelWorkspace({ )} {!!notice && {notice}} + {!!initialChannelId && !selected && ( +