Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Onboarding: Handle legacy invites #4487

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions apps/tlon-mobile/src/fixtures/Onboarding.fixture.tsx
Original file line number Diff line number Diff line change
@@ -2,8 +2,8 @@ import { NavigationContainer } from '@react-navigation/native';
import { Context as BranchContext } from '@tloncorp/app/contexts/branch';
import { exampleContacts } from '@tloncorp/app/fixtures/contentHelpers';
import { group } from '@tloncorp/app/fixtures/fakeData';
import { AppInvite, QueryClientProvider, queryClient } from '@tloncorp/shared';
import { Theme } from '@tloncorp/app/ui';
import { AppInvite, QueryClientProvider, queryClient } from '@tloncorp/shared';
import { PropsWithChildren, useState } from 'react';
import { useFixtureSelect } from 'react-cosmos/client';

@@ -45,7 +45,6 @@ function OnboardingFixture({
hasGroupInvite
? {
id: group.id,
shouldAutoJoin: true,
inviterUserId: exampleContacts.ed.id,
inviterNickname: exampleContacts.ed.nickname,
invitedGroupId: group.id,
2 changes: 1 addition & 1 deletion apps/tlon-mobile/src/hooks/useDeepLinkListener.ts
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@ export const useDeepLinkListener = () => {
lure: lure.id,
});
try {
if (lure.shouldAutoJoin || !ship) {
if (!ship) {
// if the lure was clicked prior to authenticating, no-op for now.
// Hosting will handle once the user signs up.
} else {
18 changes: 7 additions & 11 deletions apps/tlon-mobile/src/screens/Onboarding/PasteInviteLinkScreen.tsx
Original file line number Diff line number Diff line change
@@ -6,30 +6,28 @@ import {
DEFAULT_INVITE_LINK_URL,
} from '@tloncorp/app/constants';
import { useBranch, useLureMetadata } from '@tloncorp/app/contexts/branch';
import { trackError, trackOnboardingAction } from '@tloncorp/app/utils/posthog';
import {
createInviteLinkRegex,
extractNormalizedInviteLink,
getInviteLinkMeta,
} from '@tloncorp/shared';
import {
Button,
Field,
OnboardingButton,
Pressable,
ScreenHeader,
TextInput,
TlonText,
View,
YStack,
} from '@tloncorp/app/ui';
import { trackError, trackOnboardingAction } from '@tloncorp/app/utils/posthog';
import {
createInviteLinkRegex,
extractNormalizedInviteLink,
getInviteLinkMeta,
} from '@tloncorp/shared';
import { useCallback, useEffect, useState } from 'react';
import { Controller, useForm } from 'react-hook-form';
import { Keyboard } from 'react-native';

import type { OnboardingStackParamList } from '../../types';

const INVITE_LINK_REGEX = createInviteLinkRegex(BRANCH_DOMAIN);
const INVITE_LINK_REGEX = createInviteLinkRegex();

type Props = NativeStackScreenProps<
OnboardingStackParamList,
@@ -68,8 +66,6 @@ export const PasteInviteLinkScreen = ({ navigation }: Props) => {
try {
const appInvite = await getInviteLinkMeta({
inviteLink: extractedLink,
branchDomain: BRANCH_DOMAIN,
branchKey: BRANCH_KEY,
});
if (appInvite) {
setLure(appInvite);
75 changes: 50 additions & 25 deletions packages/app/contexts/branch.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { AnalyticsEvent, createDevLogger } from '@tloncorp/shared';
import { storage } from '@tloncorp/shared/db';
import { AppInvite, Lure, extractLureMetadata } from '@tloncorp/shared/logic';
import {
AppInvite,
Lure,
extractLureMetadata,
getInviteLinkMeta,
} from '@tloncorp/shared/logic';
import {
type ReactNode,
createContext,
@@ -106,6 +111,36 @@ export const BranchProvider = ({ children }: { children: ReactNode }) => {
}
}
}

if (
asUrl.hostname === 'join.tlon.io' ||
asUrl.hostname === 'tlon.network'
) {
// potential invite link that's not branch wrapped
console.log(`got join url with missing branch link`, asUrl);
getInviteLinkMeta({ inviteLink: nonBranchLink })
.then((inviteMeta) => {
if (inviteMeta) {
const nextLure: Lure = {
lure: inviteMeta,
priorityToken: undefined,
};

setState({
...nextLure,
deepLinkPath: undefined,
});
storage.invitation.setValue(nextLure);
}
})
.catch((e) => {
logger.trackError(AnalyticsEvent.InviteError, {
context: 'Failed to extract lure metadata',
url: nonBranchLink,
errorMessage: e?.message,
});
});
}
return;
}

@@ -120,12 +155,7 @@ export const BranchProvider = ({ children }: { children: ReactNode }) => {
logger.log('detected lure link:', params.lure);
try {
const nextLure: Lure = {
lure: {
...extractLureMetadata(params),
id: params.lure as string,
// if not already authenticated, we should run Lure's invite auto-join capability after signing in
shouldAutoJoin: !isAuthenticated,
},
lure: extractLureMetadata(params) ?? undefined,
priorityToken: params.token as string | undefined,
};
console.log(`setting deeplink lure`, nextLure);
@@ -173,24 +203,19 @@ export const BranchProvider = ({ children }: { children: ReactNode }) => {
};
}, [goToChannel, isAuthenticated]);

const setLure = useCallback(
(invite: AppInvite) => {
const nextLure: Lure = {
lure: {
...invite,
// if not already authenticated, we should run Lure's invite auto-join capability after signing in
shouldAutoJoin: !isAuthenticated,
},
priorityToken: undefined,
};
setState({
...nextLure,
deepLinkPath: undefined,
});
storage.invitation.setValue(nextLure);
},
[isAuthenticated]
);
const setLure = useCallback((invite: AppInvite) => {
const nextLure: Lure = {
lure: {
...invite,
},
priorityToken: undefined,
};
setState({
...nextLure,
deepLinkPath: undefined,
});
storage.invitation.setValue(nextLure);
}, []);

const clearLure = useCallback(() => {
console.debug('[branch] Clearing lure state');
9 changes: 6 additions & 3 deletions packages/app/hooks/useBootSequence.ts
Original file line number Diff line number Diff line change
@@ -8,7 +8,6 @@ import { useLureMetadata } from '../contexts/branch';
import { useShip } from '../contexts/ship';
import { BootPhaseNames, NodeBootPhase } from '../lib/bootHelpers';
import BootHelpers from '../lib/bootHelpers';
import { getShipFromCookie } from '../utils/ship';
import { useConfigureUrbitClient } from './useConfigureUrbitClient';
import { usePosthog } from './usePosthog';

@@ -160,7 +159,9 @@ export function useBootSequence() {
});

const requiredInvites =
lureMeta?.inviteType === 'user' ? invitedDm : invitedGroup && invitedDm;
lureMeta?.inviteType === 'user'
? invitedDm
: invitedGroup && (lureMeta?.isLegacy ? true : invitedDm); // old style lures will not receive a DM invite

if (requiredInvites) {
logger.trackEvent(AnalyticsEvent.InviteDebug, {
@@ -241,7 +242,9 @@ export function useBootSequence() {
updatedGroup.channels.length > 0;

const hadSuccess =
lureMeta?.inviteType === 'user' ? dmIsGood : groupIsGood && dmIsGood;
lureMeta?.inviteType === 'user'
? dmIsGood
: groupIsGood && (lureMeta?.isLegacy ? true : dmIsGood); // old style lures will not receive a DM invite

if (hadSuccess) {
logger.crumb('successfully accepted invites');
4 changes: 2 additions & 2 deletions packages/app/ui/components/Onboarding/OnboardingInvite.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DeepLinkMetadata } from '@tloncorp/shared';
import { AppInvite } from '@tloncorp/shared';
import * as db from '@tloncorp/shared/db';
import React, { ComponentProps } from 'react';

@@ -16,7 +16,7 @@ interface GroupShim {
export const OnboardingInviteBlock = React.memo(function OnboardingInviteBlock({
metadata,
...rest
}: { metadata: DeepLinkMetadata } & ComponentProps<typeof ListItem>) {
}: { metadata: AppInvite } & ComponentProps<typeof ListItem>) {
const {
inviterUserId,
invitedGroupId,
18 changes: 9 additions & 9 deletions packages/shared/src/logic/branch.ts
Original file line number Diff line number Diff line change
@@ -34,13 +34,11 @@ export const getDeepLink = async (
return url;
};

export const getBranchLinkMeta = async (
branchUrl: string,
branchKey: string
) => {
export const getBranchLinkMeta = async (branchUrl: string) => {
const env = getConstants();
const params = new URLSearchParams();
params.set('url', branchUrl);
params.set('branch_key', branchKey);
params.set('branch_key', env.BRANCH_KEY);
const response = await fetchBranchApi(`/v1/url?${params}`);
if (!response.ok) {
const badRequestInfo = await response.json();
@@ -86,7 +84,7 @@ export interface DeepLinkMetadata {

export interface AppInvite extends DeepLinkMetadata {
id: string;
shouldAutoJoin: boolean;
isLegacy?: boolean;
}

export type Lure = {
@@ -101,12 +99,13 @@ export interface DeepLinkData extends DeepLinkMetadata {
lure?: string;
}

export function extractLureMetadata(branchParams: any) {
export function extractLureMetadata(branchParams: any): AppInvite | null {
if (!branchParams || typeof branchParams !== 'object') {
return {};
return null;
}

const extracted = {
const extracted: AppInvite = {
id: branchParams.lure,
inviterUserId: branchParams.inviterUserId || branchParams.inviter,
inviterNickname: branchParams.inviterNickname,
inviterAvatarImage: branchParams.inviterAvatarImage,
@@ -131,6 +130,7 @@ export function extractLureMetadata(branchParams: any) {
if (isValidPatp(ship)) {
extracted.inviterUserId = ship;
extracted.invitedGroupId = branchParams.lure;
extracted.isLegacy = true;
}
}

Loading