Pure reshuffle, no cleanup yet
This commit is contained in:
@@ -14,13 +14,13 @@ import {
|
|||||||
} from '#/ageAssurance/state'
|
} from '#/ageAssurance/state'
|
||||||
import {
|
import {
|
||||||
AgeAssuranceAccess,
|
AgeAssuranceAccess,
|
||||||
|
type AgeAssuranceFlags,
|
||||||
type AgeAssuranceState,
|
type AgeAssuranceState,
|
||||||
AgeAssuranceStatus,
|
AgeAssuranceStatus,
|
||||||
} from '#/ageAssurance/types'
|
} from '#/ageAssurance/types'
|
||||||
import {
|
import {
|
||||||
isUnderAge,
|
computeAgeAssuranceFlags,
|
||||||
maybeRestrictChatSettings,
|
maybeRestrictChatSettings,
|
||||||
MIN_ACCESS_AGE,
|
|
||||||
useAgeAssuranceRegionConfigWithFallback,
|
useAgeAssuranceRegionConfigWithFallback,
|
||||||
} from '#/ageAssurance/util'
|
} from '#/ageAssurance/util'
|
||||||
|
|
||||||
@@ -38,13 +38,7 @@ const AgeAssuranceStateContext = createContext<{
|
|||||||
Access: typeof AgeAssuranceAccess
|
Access: typeof AgeAssuranceAccess
|
||||||
Status: typeof AgeAssuranceStatus
|
Status: typeof AgeAssuranceStatus
|
||||||
state: AgeAssuranceState
|
state: AgeAssuranceState
|
||||||
flags: {
|
flags: AgeAssuranceFlags
|
||||||
adultContentDisabled: boolean
|
|
||||||
chatDisabled: boolean
|
|
||||||
isUnder18: boolean
|
|
||||||
isOverRegionMinAccessAge: boolean
|
|
||||||
isOverAppMinAccessAge: boolean
|
|
||||||
}
|
|
||||||
}>({
|
}>({
|
||||||
Access: AgeAssuranceAccess,
|
Access: AgeAssuranceAccess,
|
||||||
Status: AgeAssuranceStatus,
|
Status: AgeAssuranceStatus,
|
||||||
@@ -107,31 +101,15 @@ function InnerProvider({children}: {children: React.ReactNode}) {
|
|||||||
return (
|
return (
|
||||||
<AgeAssuranceStateContext.Provider
|
<AgeAssuranceStateContext.Provider
|
||||||
value={useMemo(() => {
|
value={useMemo(() => {
|
||||||
const chatDisabled = state.access !== AgeAssuranceAccess.Full
|
|
||||||
// Conservative default: if we don't yet know the birthdate, treat as
|
|
||||||
// under 18 so we don't briefly expose group-chat UI.
|
|
||||||
const isUnder18 = data?.birthdate
|
|
||||||
? isUnderAge(data.birthdate, 18)
|
|
||||||
: true
|
|
||||||
const isOverRegionMinAccessAge = data?.birthdate
|
|
||||||
? !isUnderAge(data.birthdate, config.minAccessAge)
|
|
||||||
: false
|
|
||||||
const isOverAppMinAccessAge = data?.birthdate
|
|
||||||
? !isUnderAge(data.birthdate, MIN_ACCESS_AGE)
|
|
||||||
: false
|
|
||||||
const adultContentDisabled =
|
|
||||||
state.access !== AgeAssuranceAccess.Full || isUnder18
|
|
||||||
return {
|
return {
|
||||||
Access: AgeAssuranceAccess,
|
Access: AgeAssuranceAccess,
|
||||||
Status: AgeAssuranceStatus,
|
Status: AgeAssuranceStatus,
|
||||||
state,
|
state,
|
||||||
flags: {
|
flags: computeAgeAssuranceFlags({
|
||||||
adultContentDisabled,
|
state,
|
||||||
chatDisabled,
|
config,
|
||||||
isUnder18,
|
data,
|
||||||
isOverRegionMinAccessAge,
|
}),
|
||||||
isOverAppMinAccessAge,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}, [state, data, config])}>
|
}, [state, data, config])}>
|
||||||
{children}
|
{children}
|
||||||
|
|||||||
+29
-13
@@ -18,7 +18,10 @@ import {
|
|||||||
parseAccessFromString,
|
parseAccessFromString,
|
||||||
parseStatusFromString,
|
parseStatusFromString,
|
||||||
} from '#/ageAssurance/types'
|
} from '#/ageAssurance/types'
|
||||||
import {getAgeAssuranceRegionConfigWithFallback} from '#/ageAssurance/util'
|
import {
|
||||||
|
computeAgeAssuranceFlags,
|
||||||
|
getAgeAssuranceRegionConfigWithFallback,
|
||||||
|
} from '#/ageAssurance/util'
|
||||||
import {type Geolocation, useGeolocation} from '#/geolocation'
|
import {type Geolocation, useGeolocation} from '#/geolocation'
|
||||||
import {device} from '#/storage'
|
import {device} from '#/storage'
|
||||||
|
|
||||||
@@ -116,29 +119,42 @@ export function computeAgeAssuranceState({
|
|||||||
export function getAndComputeAgeAssuranceState({did}: {did: string}) {
|
export function getAndComputeAgeAssuranceState({did}: {did: string}) {
|
||||||
const config = getConfigFromCache()
|
const config = getConfigFromCache()
|
||||||
const state = getServerStateFromCache({did})
|
const state = getServerStateFromCache({did})
|
||||||
const data = getOtherRequiredDataFromCache({did})
|
const requiredData = getOtherRequiredDataFromCache({did})
|
||||||
const geolocation = device.get(['mergedGeolocation'])
|
const geolocation = device.get(['mergedGeolocation'])
|
||||||
|
|
||||||
if (!geolocation || !config || !state || !data) {
|
if (!geolocation || !config || !state || !requiredData) {
|
||||||
return {
|
return {
|
||||||
status: AgeAssuranceStatus.Unknown,
|
state: {
|
||||||
access: AgeAssuranceAccess.Safe,
|
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,
|
hasSession: true,
|
||||||
config,
|
config,
|
||||||
geolocation,
|
geolocation,
|
||||||
state: state.state,
|
state: state.state,
|
||||||
data: {
|
data,
|
||||||
accountCreatedAt: state.metadata?.accountCreatedAt,
|
|
||||||
declaredAge: data?.birthdate
|
|
||||||
? getAge(new Date(data.birthdate))
|
|
||||||
: undefined,
|
|
||||||
birthdate: data?.birthdate,
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
return {
|
||||||
|
state: computed,
|
||||||
|
flags: computeAgeAssuranceFlags({
|
||||||
|
state: computed,
|
||||||
|
config: region,
|
||||||
|
data,
|
||||||
|
}),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function useAgeAssuranceState(): AgeAssuranceState {
|
export function useAgeAssuranceState(): AgeAssuranceState {
|
||||||
|
|||||||
@@ -21,6 +21,14 @@ export type AgeAssuranceState = {
|
|||||||
error?: 'config' // maybe other specific cases in the future
|
error?: 'config' // maybe other specific cases in the future
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type AgeAssuranceFlags = {
|
||||||
|
adultContentDisabled: boolean
|
||||||
|
chatDisabled: boolean
|
||||||
|
isUnder18: boolean
|
||||||
|
isOverRegionMinAccessAge: boolean
|
||||||
|
isOverAppMinAccessAge: boolean
|
||||||
|
}
|
||||||
|
|
||||||
export function parseStatusFromString(raw: string) {
|
export function parseStatusFromString(raw: string) {
|
||||||
switch (raw) {
|
switch (raw) {
|
||||||
case 'unknown':
|
case 'unknown':
|
||||||
|
|||||||
@@ -11,11 +11,16 @@ import {getAge} from '#/lib/strings/time'
|
|||||||
import {restrictChatSettings} from '#/state/queries/messages/restrictChatSettings'
|
import {restrictChatSettings} from '#/state/queries/messages/restrictChatSettings'
|
||||||
import {DEFAULT_LOGGED_OUT_LABEL_PREFERENCES} from '#/state/queries/preferences/moderation'
|
import {DEFAULT_LOGGED_OUT_LABEL_PREFERENCES} from '#/state/queries/preferences/moderation'
|
||||||
import {
|
import {
|
||||||
|
type AgeAssuranceData,
|
||||||
getDidFromAgentSession,
|
getDidFromAgentSession,
|
||||||
getOtherRequiredDataFromCache,
|
getOtherRequiredDataFromCache,
|
||||||
useAgeAssuranceDataContext,
|
useAgeAssuranceDataContext,
|
||||||
} from '#/ageAssurance/data'
|
} from '#/ageAssurance/data'
|
||||||
import {AgeAssuranceAccess} from '#/ageAssurance/types'
|
import {
|
||||||
|
AgeAssuranceAccess,
|
||||||
|
type AgeAssuranceFlags,
|
||||||
|
type AgeAssuranceState,
|
||||||
|
} from '#/ageAssurance/types'
|
||||||
import {type Geolocation, useGeolocation} from '#/geolocation'
|
import {type Geolocation, useGeolocation} from '#/geolocation'
|
||||||
|
|
||||||
export const MIN_ACCESS_AGE = 13
|
export const MIN_ACCESS_AGE = 13
|
||||||
@@ -128,3 +133,34 @@ export function maybeRestrictChatSettings({agent}: {agent: AtpAgent}) {
|
|||||||
if (data?.actorDeclaration?.allowIncoming === 'none') return
|
if (data?.actorDeclaration?.allowIncoming === 'none') return
|
||||||
restrictChatSettings({agent, did})
|
restrictChatSettings({agent, did})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function computeAgeAssuranceFlags({
|
||||||
|
state,
|
||||||
|
config,
|
||||||
|
data,
|
||||||
|
}: {
|
||||||
|
state: AgeAssuranceState
|
||||||
|
config: AppBskyAgeassuranceDefs.ConfigRegion
|
||||||
|
data: AgeAssuranceData['data']
|
||||||
|
}): AgeAssuranceFlags {
|
||||||
|
const chatDisabled = state.access !== AgeAssuranceAccess.Full
|
||||||
|
// Conservative default: if we don't yet know the birthdate, treat as
|
||||||
|
// under 18 so we don't briefly expose group-chat UI.
|
||||||
|
const isUnder18 = data?.birthdate ? isUnderAge(data.birthdate, 18) : true
|
||||||
|
const isOverRegionMinAccessAge = data?.birthdate
|
||||||
|
? !isUnderAge(data.birthdate, config.minAccessAge)
|
||||||
|
: false
|
||||||
|
const isOverAppMinAccessAge = data?.birthdate
|
||||||
|
? !isUnderAge(data.birthdate, MIN_ACCESS_AGE)
|
||||||
|
: false
|
||||||
|
const adultContentDisabled =
|
||||||
|
state.access !== AgeAssuranceAccess.Full || isUnder18
|
||||||
|
|
||||||
|
return {
|
||||||
|
adultContentDisabled,
|
||||||
|
chatDisabled,
|
||||||
|
isUnder18,
|
||||||
|
isOverRegionMinAccessAge,
|
||||||
|
isOverAppMinAccessAge,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -223,7 +223,7 @@ export async function createAgentAndCreateAccount(
|
|||||||
}),
|
}),
|
||||||
// wait for AA data to load first, then check state
|
// wait for AA data to load first, then check state
|
||||||
aa.then(async () => {
|
aa.then(async () => {
|
||||||
const state = getAndComputeAgeAssuranceState({did: account.did})
|
const {state} = getAndComputeAgeAssuranceState({did: account.did})
|
||||||
if (state.access !== AgeAssuranceAccess.Full) {
|
if (state.access !== AgeAssuranceAccess.Full) {
|
||||||
restrictChatSettings({agent, did: account.did})
|
restrictChatSettings({agent, did: account.did})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user