Pass in assurance state to notifications registration

This commit is contained in:
Eric Bailey
2025-06-25 18:02:06 -05:00
parent 8ec5d93e0d
commit 404f837248
2 changed files with 21 additions and 11 deletions
+18 -8
View File
@@ -2,12 +2,13 @@ import {useCallback, useEffect} from 'react'
import {Platform} from 'react-native' import {Platform} from 'react-native'
import * as Notifications from 'expo-notifications' import * as Notifications from 'expo-notifications'
import {getBadgeCountAsync, setBadgeCountAsync} from 'expo-notifications' import {getBadgeCountAsync, setBadgeCountAsync} from 'expo-notifications'
import {type AtpAgent} from '@atproto/api' import {type AppBskyNotificationRegisterPush, type AtpAgent} from '@atproto/api'
import debounce from 'lodash.debounce' import debounce from 'lodash.debounce'
import {PUBLIC_APPVIEW_DID, PUBLIC_STAGING_APPVIEW_DID} from '#/lib/constants' import {PUBLIC_APPVIEW_DID, PUBLIC_STAGING_APPVIEW_DID} from '#/lib/constants'
import {logger as notyLogger} from '#/lib/notifications/util' import {logger as notyLogger} from '#/lib/notifications/util'
import {isNative} from '#/platform/detection' import {isNative} from '#/platform/detection'
import {useAgeAssuranceContext} from '#/state/ageAssurance'
import {type SessionAccount, useAgent, useSession} from '#/state/session' import {type SessionAccount, useAgent, useSession} from '#/state/session'
import BackgroundNotificationHandler from '#/../modules/expo-background-notification-handler' import BackgroundNotificationHandler from '#/../modules/expo-background-notification-handler'
@@ -19,25 +20,30 @@ async function _registerPushToken({
agent, agent,
currentAccount, currentAccount,
token, token,
extra = {},
}: { }: {
agent: AtpAgent agent: AtpAgent
currentAccount: SessionAccount currentAccount: SessionAccount
token: Notifications.DevicePushToken token: Notifications.DevicePushToken
extra?: {
ageRestricted?: boolean
}
}) { }) {
try { try {
await agent.app.bsky.notification.registerPush({ const payload: AppBskyNotificationRegisterPush.InputSchema = {
serviceDid: currentAccount.service?.includes('staging') serviceDid: currentAccount.service?.includes('staging')
? PUBLIC_STAGING_APPVIEW_DID ? PUBLIC_STAGING_APPVIEW_DID
: PUBLIC_APPVIEW_DID, : PUBLIC_APPVIEW_DID,
platform: Platform.OS, platform: Platform.OS,
token: token.data, token: token.data,
appId: 'xyz.blueskyweb.app', appId: 'xyz.blueskyweb.app',
}) // @ts-ignore
ageRestricted: extra.ageRestricted ?? false,
}
notyLogger.debug(`registerPushToken: success`, { await agent.app.bsky.notification.registerPush(payload)
tokenType: token.type,
token: token.data, notyLogger.debug(`registerPushToken: success`, {...payload})
})
} catch (error) { } catch (error) {
notyLogger.error(`registerPushToken: failed`, {safeMessage: error}) notyLogger.error(`registerPushToken: failed`, {safeMessage: error})
} }
@@ -59,6 +65,7 @@ const _registerPushTokenDebounced = debounce(_registerPushToken, 100)
export function useRegisterPushToken() { export function useRegisterPushToken() {
const agent = useAgent() const agent = useAgent()
const {currentAccount} = useSession() const {currentAccount} = useSession()
const {isAgeRestricted} = useAgeAssuranceContext()
return useCallback( return useCallback(
({token}: {token: Notifications.DevicePushToken}) => { ({token}: {token: Notifications.DevicePushToken}) => {
@@ -67,9 +74,12 @@ export function useRegisterPushToken() {
agent, agent,
currentAccount, currentAccount,
token, token,
extra: {
ageRestricted: isAgeRestricted,
},
}) })
}, },
[agent, currentAccount], [agent, currentAccount, isAgeRestricted],
) )
} }
+3 -3
View File
@@ -14,7 +14,7 @@ type TempAgeAssuranceState = {
} }
export type AgeAssuranceContextType = { export type AgeAssuranceContextType = {
required: boolean isAgeRestricted: boolean
status: TempAgeAssuranceState['status'] status: TempAgeAssuranceState['status']
} }
@@ -23,7 +23,7 @@ export type AgeAssuranceAPIContextType = {
} }
const AgeAssuranceContext = createContext<AgeAssuranceContextType>({ const AgeAssuranceContext = createContext<AgeAssuranceContextType>({
required: false, isAgeRestricted: false,
status: 'unknown', status: 'unknown',
}) })
@@ -64,7 +64,7 @@ export function Provider({children}: {children: React.ReactNode}) {
const ageAssuranceContext = useMemo<AgeAssuranceContextType>(() => { const ageAssuranceContext = useMemo<AgeAssuranceContextType>(() => {
const {status} = ageAssuranceState const {status} = ageAssuranceState
const ctx: AgeAssuranceContextType = { const ctx: AgeAssuranceContextType = {
required: Boolean( isAgeRestricted: Boolean(
geolocation?.isAgeRestrictedGeo && status !== 'assured', geolocation?.isAgeRestrictedGeo && status !== 'assured',
), ),
status, status,