Refactor unsafeGetAndComputeAgeAssurance to expose flags as well

This commit is contained in:
Eric Bailey
2026-06-08 14:26:32 -05:00
parent a4eda59f48
commit bef23cc9f6
3 changed files with 34 additions and 18 deletions
+31 -15
View File
@@ -18,7 +18,10 @@ import {
parseAccessFromString,
parseStatusFromString,
} from '#/ageAssurance/types'
import {getAgeAssuranceRegionConfigWithFallback} from '#/ageAssurance/util'
import {
computeAgeAssuranceFlags,
getAgeAssuranceRegionConfigWithFallback,
} from '#/ageAssurance/util'
import {type Geolocation, useGeolocation} from '#/geolocation'
import {device} from '#/storage'
@@ -27,7 +30,7 @@ import {device} from '#/storage'
* server state before computing access based on AA config from the server +
* geolocation and other data.
*/
export function computeAgeAssuranceState({
function computeAgeAssuranceState({
hasSession,
config,
geolocation,
@@ -113,32 +116,45 @@ export function computeAgeAssuranceState({
* This is a last-ditch helper for out-of-band reads of the AA state, such as
* during account creation. Don't use it for anything else.
*/
export function getAndComputeAgeAssuranceState({did}: {did: string}) {
export function unsafeGetAndComputeAgeAssurance({did}: {did: string}) {
const config = getConfigFromCache()
const state = getServerStateFromCache({did})
const data = getOtherRequiredDataFromCache({did})
const requiredData = getOtherRequiredDataFromCache({did})
const geolocation = device.get(['mergedGeolocation'])
if (!geolocation || !config || !state || !data) {
if (!geolocation || !config || !state || !requiredData) {
return {
status: AgeAssuranceStatus.Unknown,
access: AgeAssuranceAccess.Safe,
state: {
status: AgeAssuranceStatus.Unknown,
access: AgeAssuranceAccess.Safe,
},
}
}
return computeAgeAssuranceState({
const region = getAgeAssuranceRegionConfigWithFallback(config, geolocation)
const data = {
accountCreatedAt: state.metadata?.accountCreatedAt,
declaredAge: requiredData?.birthdate
? getAge(new Date(requiredData.birthdate))
: undefined,
birthdate: requiredData?.birthdate,
}
const computed = computeAgeAssuranceState({
hasSession: true,
config,
geolocation,
state: state.state,
data: {
accountCreatedAt: state.metadata?.accountCreatedAt,
declaredAge: data?.birthdate
? getAge(new Date(data.birthdate))
: undefined,
birthdate: data?.birthdate,
},
data,
})
return {
state: computed,
flags: computeAgeAssuranceFlags({
state: computed,
config: region,
data,
}),
}
}
export function useAgeAssuranceState(): AgeAssuranceState {
+1 -1
View File
@@ -13,7 +13,7 @@ jest.mock('jwt-decode', () => ({
jest.mock('../../birthdate')
jest.mock('../../../ageAssurance/data')
jest.mock('../../../ageAssurance/state', () => ({
getAndComputeAgeAssuranceState: () => ({}),
unsafeGetAndComputeAgeAssurance: () => ({state: {}}),
}))
jest.mock('#/lib/notifications/notifications', () => ({
unregisterPushToken(_agents: BskyAgent[]) {
+2 -2
View File
@@ -28,7 +28,7 @@ import {
setBirthdateForDid,
setCreatedAtForDid,
} from '#/ageAssurance/data'
import {getAndComputeAgeAssuranceState} from '#/ageAssurance/state'
import {unsafeGetAndComputeAgeAssurance} from '#/ageAssurance/state'
import {AgeAssuranceAccess} from '#/ageAssurance/types'
import {features} from '#/analytics'
import {emitNetworkConfirmed, emitNetworkLost} from '../events'
@@ -219,7 +219,7 @@ export async function createAgentAndCreateAccount(
}),
// wait for AA data to load first, then check state
aa.then(async () => {
const state = getAndComputeAgeAssuranceState({did: account.did})
const {state} = unsafeGetAndComputeAgeAssurance({did: account.did})
if (state.access !== AgeAssuranceAccess.Full) {
restrictChatSettings({agent, did: account.did})
}