Refactor unsafeGetAndComputeAgeAssurance to expose flags as well
This commit is contained in:
+31
-15
@@ -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 {
|
||||
|
||||
@@ -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[]) {
|
||||
|
||||
@@ -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})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user