phase 4: migrate ageAssurance unit and chat wrapper signatures to lex clients
This commit is contained in:
@@ -16,7 +16,7 @@ import {Trans} from '@lingui/react/macro'
|
||||
import {retry} from '#/lib/async/retry'
|
||||
import {wait} from '#/lib/async/wait'
|
||||
import {parseLinkingUrl} from '#/lib/parseLinkingUrl'
|
||||
import {useAgent, useSession} from '#/state/session'
|
||||
import {useAppviewClient, useSession} from '#/state/session'
|
||||
import {atoms as a, platform, useBreakpoints, useTheme} from '#/alf'
|
||||
import {AgeAssuranceBadge} from '#/components/ageAssurance/AgeAssuranceBadge'
|
||||
import {Button, ButtonText} from '#/components/Button'
|
||||
@@ -176,7 +176,8 @@ function Inner() {
|
||||
const t = useTheme()
|
||||
const ax = useAnalytics()
|
||||
const {_} = useLingui()
|
||||
const agent = useAgent()
|
||||
const {hasSession} = useSession()
|
||||
const appviewClient = useAppviewClient()
|
||||
const polling = useRef(false)
|
||||
const unmounted = useRef(false)
|
||||
const [error, setError] = useState(false)
|
||||
@@ -196,10 +197,10 @@ function Inner() {
|
||||
5,
|
||||
() => true,
|
||||
async () => {
|
||||
if (!agent.session) return
|
||||
if (!hasSession) return
|
||||
if (unmounted.current) return
|
||||
|
||||
const data = await refetchAgeAssuranceServerState({agent})
|
||||
const data = await refetchAgeAssuranceServerState({appviewClient})
|
||||
|
||||
if (data?.state.status !== 'assured') {
|
||||
throw new Error(
|
||||
@@ -214,7 +215,7 @@ function Inner() {
|
||||
)
|
||||
.then(async data => {
|
||||
if (!data) return
|
||||
if (!agent.session) return
|
||||
if (!hasSession) return
|
||||
if (unmounted.current) return
|
||||
|
||||
setSuccess(true)
|
||||
@@ -230,7 +231,7 @@ function Inner() {
|
||||
return () => {
|
||||
unmounted.current = true
|
||||
}
|
||||
}, [ax, agent])
|
||||
}, [ax, hasSession, appviewClient])
|
||||
|
||||
if (success) {
|
||||
return (
|
||||
|
||||
+71
-48
@@ -1,6 +1,7 @@
|
||||
import {createContext, useCallback, useContext, useEffect, useMemo} from 'react'
|
||||
import * as AgeRange from 'expo-age-range'
|
||||
import {Client} from '@atproto/lex-client'
|
||||
import {getPreferences} from '@bsky.app/sdk'
|
||||
import {createAsyncStoragePersister} from '@tanstack/query-async-storage-persister'
|
||||
import {focusManager, QueryClient, useQuery} from '@tanstack/react-query'
|
||||
import {persistQueryClient} from '@tanstack/react-query-persist-client'
|
||||
@@ -15,7 +16,7 @@ import {
|
||||
snoozeBirthdateUpdateAllowedForDid,
|
||||
} from '#/state/birthdate'
|
||||
import {fetchActorDeclarationRecord} from '#/state/queries/messages/actor-declaration'
|
||||
import {type SessionAgent, useAgent, useSession} from '#/state/session'
|
||||
import {useAppviewClient, usePdsClient, useSession} from '#/state/session'
|
||||
import {DEVICE_SIGNALS_SUPPORTED} from '#/ageAssurance/const'
|
||||
import * as debug from '#/ageAssurance/debug'
|
||||
import {logger} from '#/ageAssurance/logger'
|
||||
@@ -33,7 +34,6 @@ import {IS_DEV} from '#/env'
|
||||
import {useGeolocation} from '#/geolocation'
|
||||
import {app, type chat} from '#/lexicons'
|
||||
import {device} from '#/storage'
|
||||
import {toLex} from '#/types/bsky'
|
||||
|
||||
/**
|
||||
* Special query client for age assurance data so we can prefetch on app
|
||||
@@ -59,10 +59,12 @@ const [, cacheHydrationPromise] = persistQueryClient({
|
||||
persister,
|
||||
})
|
||||
|
||||
export function getDidFromAgentSession(agent: SessionAgent) {
|
||||
const sessionManager = agent.sessionManager
|
||||
if (!sessionManager || !sessionManager.did) return
|
||||
return sessionManager.did
|
||||
/**
|
||||
* Reads the active account did from a lex {@link Client}. Logged-out clients
|
||||
* expose `did: undefined`, so this returns undefined in that case.
|
||||
*/
|
||||
export function getDidFromClient(client: Client) {
|
||||
return client.did
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -183,9 +185,9 @@ export function createServerStateQueryKey({did}: {did: string}) {
|
||||
return ['serverState', did]
|
||||
}
|
||||
export async function getServerState({
|
||||
agent,
|
||||
appviewClient,
|
||||
}: {
|
||||
agent: SessionAgent
|
||||
appviewClient: Client
|
||||
}): Promise<app.bsky.ageassurance.getState.$OutputBody | null> {
|
||||
if (debug.enabled && debug.serverState)
|
||||
return debug.resolve(debug.serverState)
|
||||
@@ -194,24 +196,23 @@ export async function getServerState({
|
||||
logger.error(`getServerState: missing geolocation countryCode`)
|
||||
return null
|
||||
}
|
||||
const {data} = await agent.app.bsky.ageassurance.getState({
|
||||
const data = await appviewClient.call(app.bsky.ageassurance.getState, {
|
||||
countryCode: geolocation.countryCode,
|
||||
regionCode: geolocation.regionCode,
|
||||
})
|
||||
const did = getDidFromAgentSession(agent)
|
||||
const did = getDidFromClient(appviewClient)
|
||||
if (data && did && createdAtCache.has(did)) {
|
||||
/*
|
||||
* If account was just created, just use the local cache if available. On
|
||||
* subsequent reloads, the server should have the correct value.
|
||||
* subsequent reloads, the server should have the correct value. The cache
|
||||
* holds ISO datetime strings (written from `new Date().toISOString()`), so
|
||||
* assert the branded DatetimeString at this boundary.
|
||||
*/
|
||||
data.metadata.accountCreatedAt = createdAtCache.get(did)
|
||||
data.metadata.accountCreatedAt = createdAtCache.get(
|
||||
did,
|
||||
) as typeof data.metadata.accountCreatedAt
|
||||
}
|
||||
/*
|
||||
* TODO(phase4): the bridge agent still returns the old `@atproto/api`
|
||||
* getState output; `toLex` reconciles it with the `#/lexicons` shape at this
|
||||
* single boundary until the agent itself is migrated.
|
||||
*/
|
||||
return data ? toLex<app.bsky.ageassurance.getState.$OutputBody>(data) : null
|
||||
return data ?? null
|
||||
}
|
||||
export function getServerStateFromCache({
|
||||
did,
|
||||
@@ -222,8 +223,12 @@ export function getServerStateFromCache({
|
||||
createServerStateQueryKey({did}),
|
||||
)
|
||||
}
|
||||
export async function prefetchServerState({agent}: {agent: SessionAgent}) {
|
||||
const did = getDidFromAgentSession(agent)
|
||||
export async function prefetchServerState({
|
||||
appviewClient,
|
||||
}: {
|
||||
appviewClient: Client
|
||||
}) {
|
||||
const did = getDidFromClient(appviewClient)
|
||||
|
||||
if (!did) return
|
||||
|
||||
@@ -238,7 +243,7 @@ export async function prefetchServerState({agent}: {agent: SessionAgent}) {
|
||||
|
||||
try {
|
||||
logger.debug(`prefetchServerState: resolving...`)
|
||||
const res = await networkRetry(3, () => getServerState({agent}))
|
||||
const res = await networkRetry(3, () => getServerState({appviewClient}))
|
||||
if (res) {
|
||||
qc.setQueryData<app.bsky.ageassurance.getState.$OutputBody>(qk, res)
|
||||
}
|
||||
@@ -249,11 +254,15 @@ export async function prefetchServerState({agent}: {agent: SessionAgent}) {
|
||||
})
|
||||
}
|
||||
}
|
||||
export async function refetchServerState({agent}: {agent: SessionAgent}) {
|
||||
const did = getDidFromAgentSession(agent)
|
||||
export async function refetchServerState({
|
||||
appviewClient,
|
||||
}: {
|
||||
appviewClient: Client
|
||||
}) {
|
||||
const did = getDidFromClient(appviewClient)
|
||||
if (!did) return
|
||||
logger.debug(`refetchServerState: fetching...`)
|
||||
const res = await networkRetry(3, () => getServerState({agent}))
|
||||
const res = await networkRetry(3, () => getServerState({appviewClient}))
|
||||
if (res) {
|
||||
qc.setQueryData<app.bsky.ageassurance.getState.$OutputBody>(
|
||||
createServerStateQueryKey({did}),
|
||||
@@ -283,8 +292,8 @@ export function usePatchServerState() {
|
||||
)
|
||||
}
|
||||
export function useServerStateQuery() {
|
||||
const agent = useAgent()
|
||||
const did = getDidFromAgentSession(agent)
|
||||
const appviewClient = useAppviewClient()
|
||||
const did = getDidFromClient(appviewClient)
|
||||
const query = useQuery(
|
||||
{
|
||||
enabled: !!did,
|
||||
@@ -294,7 +303,7 @@ export function useServerStateQuery() {
|
||||
},
|
||||
queryKey: createServerStateQueryKey({did: did!}),
|
||||
async queryFn() {
|
||||
return getServerState({agent})
|
||||
return getServerState({appviewClient})
|
||||
},
|
||||
},
|
||||
qc,
|
||||
@@ -346,15 +355,15 @@ export function createOtherRequiredDataQueryKey({did}: {did: string}) {
|
||||
return ['otherRequiredData', did]
|
||||
}
|
||||
async function getOtherRequiredData({
|
||||
agent,
|
||||
accountClient,
|
||||
}: {
|
||||
agent: SessionAgent
|
||||
accountClient: Client
|
||||
}): Promise<OtherRequiredData> {
|
||||
if (debug.enabled) return debug.resolve(debug.otherRequiredData)
|
||||
const did = getDidFromAgentSession(agent)
|
||||
const did = getDidFromClient(accountClient)
|
||||
const [prefs, actorDeclaration] = await Promise.all([
|
||||
agent.getPreferences(),
|
||||
fetchActorDeclarationRecord({did, agent}),
|
||||
accountClient.call(getPreferences),
|
||||
fetchActorDeclarationRecord({did, client: accountClient}),
|
||||
])
|
||||
const data: OtherRequiredData = {
|
||||
birthdate: prefs.birthDate ? prefs.birthDate.toISOString() : undefined,
|
||||
@@ -431,11 +440,11 @@ export function setOtherRequiredDataActorDeclarationCache({
|
||||
)
|
||||
}
|
||||
export async function prefetchOtherRequiredData({
|
||||
agent,
|
||||
accountClient,
|
||||
}: {
|
||||
agent: SessionAgent
|
||||
accountClient: Client
|
||||
}) {
|
||||
const did = getDidFromAgentSession(agent)
|
||||
const did = getDidFromClient(accountClient)
|
||||
|
||||
if (!did) return
|
||||
|
||||
@@ -450,7 +459,9 @@ export async function prefetchOtherRequiredData({
|
||||
|
||||
try {
|
||||
logger.debug(`prefetchOtherRequiredData: resolving...`)
|
||||
const res = await networkRetry(3, () => getOtherRequiredData({agent}))
|
||||
const res = await networkRetry(3, () =>
|
||||
getOtherRequiredData({accountClient}),
|
||||
)
|
||||
qc.setQueryData<OtherRequiredData>(qk, res)
|
||||
} catch (err) {
|
||||
const e = err as Error
|
||||
@@ -479,8 +490,8 @@ export function usePatchOtherRequiredData() {
|
||||
)
|
||||
}
|
||||
export function useOtherRequiredDataQuery() {
|
||||
const agent = useAgent()
|
||||
const did = getDidFromAgentSession(agent)
|
||||
const accountClient = usePdsClient()
|
||||
const did = getDidFromClient(accountClient)
|
||||
return useQuery(
|
||||
{
|
||||
enabled: !!did,
|
||||
@@ -490,7 +501,7 @@ export function useOtherRequiredDataQuery() {
|
||||
},
|
||||
queryKey: createOtherRequiredDataQueryKey({did: did!}),
|
||||
async queryFn() {
|
||||
return getOtherRequiredData({agent})
|
||||
return getOtherRequiredData({accountClient})
|
||||
},
|
||||
},
|
||||
qc,
|
||||
@@ -585,8 +596,12 @@ export function setDeviceSignalsForRegion({
|
||||
prev => ({...prev, [regionKey]: signals}),
|
||||
)
|
||||
}
|
||||
export async function prefetchDeviceSignals({agent}: {agent: SessionAgent}) {
|
||||
const did = getDidFromAgentSession(agent)
|
||||
export async function prefetchDeviceSignals({
|
||||
appviewClient,
|
||||
}: {
|
||||
appviewClient: Client
|
||||
}) {
|
||||
const did = getDidFromClient(appviewClient)
|
||||
if (!did) return
|
||||
|
||||
/**
|
||||
@@ -621,8 +636,8 @@ export async function prefetchDeviceSignals({agent}: {agent: SessionAgent}) {
|
||||
*/
|
||||
}
|
||||
export function useDeviceSignalsQuery() {
|
||||
const agent = useAgent()
|
||||
const did = getDidFromAgentSession(agent)
|
||||
const appviewClient = useAppviewClient()
|
||||
const did = getDidFromClient(appviewClient)
|
||||
const {data: config} = useConfigQuery()
|
||||
const geolocation = useGeolocation()
|
||||
/*
|
||||
@@ -665,15 +680,23 @@ export function useDeviceSignalsQuery() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper to prefetch all age assurance data from the server.
|
||||
* Helper to prefetch all age assurance data from the server. Reads that hit
|
||||
* the appview (`getState`, device signals) take the appview client; the
|
||||
* preferences/actor-declaration read hits the PDS via the account client.
|
||||
*/
|
||||
export function prefetchAgeAssuranceServerData({agent}: {agent: SessionAgent}) {
|
||||
export function prefetchAgeAssuranceServerData({
|
||||
appviewClient,
|
||||
accountClient,
|
||||
}: {
|
||||
appviewClient: Client
|
||||
accountClient: Client
|
||||
}) {
|
||||
return Promise.allSettled([
|
||||
// config fetch initiated at the top of the App.platform.tsx files, awaited here
|
||||
configPrefetchPromise,
|
||||
prefetchServerState({agent}),
|
||||
prefetchOtherRequiredData({agent}),
|
||||
prefetchDeviceSignals({agent}),
|
||||
prefetchServerState({appviewClient}),
|
||||
prefetchOtherRequiredData({accountClient}),
|
||||
prefetchDeviceSignals({appviewClient}),
|
||||
])
|
||||
}
|
||||
|
||||
|
||||
@@ -9,20 +9,20 @@ import {
|
||||
PUBLIC_APPVIEW_DID,
|
||||
} from '#/lib/constants'
|
||||
import {isNetworkError} from '#/lib/hooks/useCleanError'
|
||||
import {useAgent} from '#/state/session'
|
||||
import {usePdsClient} from '#/state/session'
|
||||
import {usePatchAgeAssuranceServerState} from '#/ageAssurance'
|
||||
import {logger} from '#/ageAssurance/logger'
|
||||
import {useAnalytics} from '#/analytics'
|
||||
import {BLUESKY_PROXY_DID} from '#/env'
|
||||
import {useGeolocation} from '#/geolocation'
|
||||
import {app} from '#/lexicons'
|
||||
import {app, com} from '#/lexicons'
|
||||
|
||||
const IS_DEV_ENV = BLUESKY_PROXY_DID !== PUBLIC_APPVIEW_DID
|
||||
const APPVIEW = IS_DEV_ENV ? DEV_ENV_APPVIEW : PUBLIC_APPVIEW
|
||||
|
||||
export function useBeginAgeAssurance() {
|
||||
const ax = useAnalytics()
|
||||
const agent = useAgent()
|
||||
const pdsClient = usePdsClient()
|
||||
const geolocation = useGeolocation()
|
||||
const patchAgeAssuranceStateResponse = usePatchAgeAssuranceServerState()
|
||||
|
||||
@@ -39,9 +39,7 @@ export function useBeginAgeAssurance() {
|
||||
throw new Error(`Geolocation not available, cannot init age assurance.`)
|
||||
}
|
||||
|
||||
const {
|
||||
data: {token},
|
||||
} = await agent.com.atproto.server.getServiceAuth({
|
||||
const {token} = await pdsClient.call(com.atproto.server.getServiceAuth, {
|
||||
aud: BLUESKY_PROXY_DID,
|
||||
lxm: `app.bsky.ageassurance.begin`,
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user