diff --git a/src/lib/notifications/notifications.ts b/src/lib/notifications/notifications.ts index 94b3f6de39..c6e6f4c4fe 100644 --- a/src/lib/notifications/notifications.ts +++ b/src/lib/notifications/notifications.ts @@ -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], ) } diff --git a/src/state/ageAssurance.tsx b/src/state/ageAssurance.tsx index fad7475a18..b3af652f72 100644 --- a/src/state/ageAssurance.tsx +++ b/src/state/ageAssurance.tsx @@ -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({ - required: false, + isAgeRestricted: false, status: 'unknown', }) @@ -64,7 +64,7 @@ export function Provider({children}: {children: React.ReactNode}) { const ageAssuranceContext = useMemo(() => { const {status} = ageAssuranceState const ctx: AgeAssuranceContextType = { - required: Boolean( + isAgeRestricted: Boolean( geolocation?.isAgeRestrictedGeo && status !== 'assured', ), status,