Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293import {useCallback, useEffect} from 'react'import {Alert} from 'react-native'import * as Linking from 'expo-linking'import * as WebBrowser from 'expo-web-browser'
import {useOpenComposer} from '#/lib/hooks/useOpenComposer'import {parseLinkingUrl} from '#/lib/parseLinkingUrl'import { CHAT_INVITE_CODE_REGEX, GROUP_INVITE_CODE_REGEX, shouldHandleIncomingIntentUrl,} from '#/lib/strings/url-helpers'import {usePrefetchGroupInvitePreview} from '#/state/queries/group-invites'import {usePrefetchJoinLinkPreviews} from '#/state/queries/join-links'import {useSession} from '#/state/session'import {useSetActiveLanding} from '#/state/shell/landing'import {useLoggedOutViewControls} from '#/state/shell/logged-out'import {useCloseAllActiveElements} from '#/state/util'import {useIntentDialogs} from '#/components/intents/IntentDialogs'import {useAnalytics} from '#/analytics'import {IS_IOS, IS_NATIVE} from '#/env'import {Referrer} from '../../../modules/expo-bluesky-swiss-army'import {useApplyPullRequestOTAUpdate} from './useOTAUpdates'
type IntentType = 'compose' | 'verify-email' | 'age-assurance' | 'apply-ota'
const VALID_IMAGE_REGEX = /^[\w.:\-_/]+\|\d+(\.\d+)?\|\d+(\.\d+)?$/
// This needs to stay outside of react to persist between account switcheslet previousIntentUrl = ''
export function useIntentHandler() { const incomingUrl = Linking.useLinkingURL() const ax = useAnalytics() const composeIntent = useComposeIntent() const verifyEmailIntent = useVerifyEmailIntent() const groupChatJoinIntent = useGroupChatJoinIntent() const groupInviteIntent = useGroupInviteIntent() const {currentAccount} = useSession() const {tryApplyUpdate} = useApplyPullRequestOTAUpdate()
useEffect(() => { const handleIncomingURL = async (url: string) => { if (IS_IOS) { // Close in-app browser if it's open (iOS only) await WebBrowser.dismissBrowser().catch(() => {}) }
const urlp = parseLinkingUrl(url) const chatInviteMatch = urlp.pathname.match(CHAT_INVITE_CODE_REGEX) if (chatInviteMatch) { groupChatJoinIntent(chatInviteMatch[1], url) return } const groupInviteMatch = urlp.pathname.match(GROUP_INVITE_CODE_REGEX) if (groupInviteMatch) { groupInviteIntent(groupInviteMatch[1]) return } const referrerInfo = Referrer.getReferrerInfo() if (referrerInfo && referrerInfo.hostname !== 'bsky.app') { ax.metric('deepLink:referrerReceived', { to: url, referrer: referrerInfo.referrer, hostname: referrerInfo.hostname, }) } const [, intent, intentType] = urlp.pathname.split('/')
// On native, our links look like blacksky://intent/SomeIntent, so we have to check the hostname for the // intent check. On web, we have to check the first part of the path since we have an actual hostname const isIntent = intent === 'intent' const params = urlp.searchParams
if (!isIntent) return
switch (intentType as IntentType) { case 'compose': { composeIntent({ text: params.get('text'), imageUrisStr: params.get('imageUris'), videoUri: params.get('videoUri'), }) return } case 'verify-email': { const code = params.get('code') if (!code) return verifyEmailIntent(code) return } case 'age-assurance': { // No-op: age assurance system has been removed return } case 'apply-ota': { const channel = params.get('channel') if (!channel) { Alert.alert('Error', 'No channel provided to look for.') } else { tryApplyUpdate(channel) } return } default: { return } } }
const processIncomingURL = (url: string, fromLinkEvent: boolean) => { if ( !shouldHandleIncomingIntentUrl(url, previousIntentUrl, fromLinkEvent) ) { return } previousIntentUrl = url void handleIncomingURL(url) }
const subscription = Linking.addEventListener('url', event => { processIncomingURL(event.url, true) }) if (incomingUrl) processIncomingURL(incomingUrl, false)
return () => subscription.remove() }, [ incomingUrl, ax, composeIntent, verifyEmailIntent, groupChatJoinIntent, groupInviteIntent, currentAccount, tryApplyUpdate, ])}
export function useGroupInviteIntent() { const closeAllActiveElements = useCloseAllActiveElements() const {hasSession} = useSession() const {groupInviteDialogControl: control, setGroupInviteState: setState} = useIntentDialogs() const {requestSwitchToAccount} = useLoggedOutViewControls() const setActiveLanding = useSetActiveLanding() const prefetchGroupInvitePreview = usePrefetchGroupInvitePreview()
return useCallback( (code: string) => { closeAllActiveElements() if (hasSession) { setActiveLanding(undefined) setState({code}) const prefetch = prefetchGroupInvitePreview(code) void Promise.race([ prefetch, new Promise(res => setTimeout(res, 200)), ]).finally(() => control.open()) } else { // The original URL contains the bearer code. It is not needed to // resume the landing flow and must not be retained in redirect state. setActiveLanding({type: 'groupinvite', uri: '', code}) requestSwitchToAccount({requestedAccount: 'groupinvite'}) } }, [ closeAllActiveElements, hasSession, setState, prefetchGroupInvitePreview, control, setActiveLanding, requestSwitchToAccount, ], )}
export function useComposeIntent() { const closeAllActiveElements = useCloseAllActiveElements() const {openComposer} = useOpenComposer() const {hasSession} = useSession()
return useCallback( ({ text, imageUrisStr, videoUri, }: { text: string | null imageUrisStr: string | null videoUri: string | null }) => { if (!hasSession) return closeAllActiveElements()
// Whenever a video URI is present, we don't support adding images right now. if (videoUri) { const [uri, width, height] = videoUri.split('|') openComposer({ text: text ?? undefined, videoUri: {uri, width: Number(width), height: Number(height)}, logContext: 'Deeplink', }) return }
const imageUris = imageUrisStr ?.split(',') .filter(part => { // For some security, we're going to filter out any image uri that is external. We don't want someone to // be able to provide some link like "blacksky://intent/compose?imageUris=https://IHaveYourIpNow.com/image.jpeg // and we load that image if (part.includes('https://') || part.includes('http://')) { return false } // We also should just filter out cases that don't have all the info we need return VALID_IMAGE_REGEX.test(part) }) .map(part => { const [uri, width, height] = part.split('|') return {uri, width: Number(width), height: Number(height)} })
setTimeout(() => { openComposer({ text: text ?? undefined, imageUris: IS_NATIVE ? imageUris : undefined, logContext: 'Deeplink', }) }, 500) }, [hasSession, closeAllActiveElements, openComposer], )}
export function useGroupChatJoinIntent() { const closeAllActiveElements = useCloseAllActiveElements() const {hasSession} = useSession() const {groupChatJoinDialogControl: control, setGroupChatJoinState: setState} = useIntentDialogs() const {requestSwitchToAccount} = useLoggedOutViewControls() const setActiveLanding = useSetActiveLanding() const prefetchJoinLinkPreviews = usePrefetchJoinLinkPreviews() return useCallback( (code: string, uri?: string) => { closeAllActiveElements() if (hasSession) { setState({code}) const prefetch = prefetchJoinLinkPreviews({ codes: [code], hasSession: true, }) void Promise.race([ prefetch, new Promise(res => setTimeout(res, 200)), ]).finally(() => { control.open() }) } else { setActiveLanding({type: 'groupchat', uri: uri ?? '', code}) requestSwitchToAccount({requestedAccount: 'groupchat'}) } }, [ closeAllActiveElements, hasSession, control, setState, prefetchJoinLinkPreviews, requestSwitchToAccount, setActiveLanding, ], )}
function useVerifyEmailIntent() { const closeAllActiveElements = useCloseAllActiveElements() const {verifyEmailDialogControl: control, setVerifyEmailState: setState} = useIntentDialogs() return useCallback( (code: string) => { closeAllActiveElements() setState({ code, }) setTimeout(() => { control.open() }, 1000) }, [closeAllActiveElements, control, setState], )}