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 * as Notifications 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 {PUBLIC_APPVIEW_DID, PUBLIC_STAGING_APPVIEW_DID} from '#/lib/constants'
import {logger as notyLogger} from '#/lib/notifications/util'
import {isNative} from '#/platform/detection'
import {useAgeAssuranceContext} from '#/state/ageAssurance'
import {type SessionAccount, useAgent, useSession} from '#/state/session'
import BackgroundNotificationHandler from '#/../modules/expo-background-notification-handler'
@@ -19,25 +20,30 @@ async function _registerPushToken({
agent,
currentAccount,
token,
extra = {},
}: {
agent: AtpAgent
currentAccount: SessionAccount
token: Notifications.DevicePushToken
extra?: {
ageRestricted?: boolean
}
}) {
try {
await agent.app.bsky.notification.registerPush({
const payload: AppBskyNotificationRegisterPush.InputSchema = {
serviceDid: currentAccount.service?.includes('staging')
? PUBLIC_STAGING_APPVIEW_DID
: PUBLIC_APPVIEW_DID,
platform: Platform.OS,
token: token.data,
appId: 'xyz.blueskyweb.app',
})
// @ts-ignore
ageRestricted: extra.ageRestricted ?? false,
}
notyLogger.debug(`registerPushToken: success`, {
tokenType: token.type,
token: token.data,
})
await agent.app.bsky.notification.registerPush(payload)
notyLogger.debug(`registerPushToken: success`, {...payload})
} catch (error) {
notyLogger.error(`registerPushToken: failed`, {safeMessage: error})
}
@@ -59,6 +65,7 @@ const _registerPushTokenDebounced = debounce(_registerPushToken, 100)
export function useRegisterPushToken() {
const agent = useAgent()
const {currentAccount} = useSession()
const {isAgeRestricted} = useAgeAssuranceContext()
return useCallback(
({token}: {token: Notifications.DevicePushToken}) => {
@@ -67,9 +74,12 @@ export function useRegisterPushToken() {
agent,
currentAccount,
token,
extra: {
ageRestricted: isAgeRestricted,
},
})
},
[agent, currentAccount],
[agent, currentAccount, isAgeRestricted],
)
}
+3 -3
View File
@@ -14,7 +14,7 @@ type TempAgeAssuranceState = {
}
export type AgeAssuranceContextType = {
required: boolean
isAgeRestricted: boolean
status: TempAgeAssuranceState['status']
}
@@ -23,7 +23,7 @@ export type AgeAssuranceAPIContextType = {
}
const AgeAssuranceContext = createContext<AgeAssuranceContextType>({
required: false,
isAgeRestricted: false,
status: 'unknown',
})
@@ -64,7 +64,7 @@ export function Provider({children}: {children: React.ReactNode}) {
const ageAssuranceContext = useMemo<AgeAssuranceContextType>(() => {
const {status} = ageAssuranceState
const ctx: AgeAssuranceContextType = {
required: Boolean(
isAgeRestricted: Boolean(
geolocation?.isAgeRestrictedGeo && status !== 'assured',
),
status,