Small refactor of notifications registration

This commit is contained in:
Eric Bailey
2025-07-08 17:00:03 -05:00
parent c2c7138987
commit 37a52e8e84
+44 -29
View File
@@ -41,9 +41,11 @@ async function _registerPushToken({
ageRestricted: extra.ageRestricted ?? false, ageRestricted: extra.ageRestricted ?? false,
} }
notyLogger.debug(`registerPushToken: registering`, {...payload})
await agent.app.bsky.notification.registerPush(payload) await agent.app.bsky.notification.registerPush(payload)
notyLogger.debug(`registerPushToken: success`, {...payload}) notyLogger.debug(`registerPushToken: success`)
} catch (error) { } catch (error) {
notyLogger.error(`registerPushToken: failed`, {safeMessage: error}) notyLogger.error(`registerPushToken: failed`, {safeMessage: error})
} }
@@ -65,15 +67,15 @@ const _registerPushTokenDebounced = debounce(_registerPushToken, 100)
export function useRegisterPushToken() { export function useRegisterPushToken() {
const agent = useAgent() const agent = useAgent()
const {currentAccount} = useSession() const {currentAccount} = useSession()
/**
* At the time of writing, this data is guaranteed to be available here,
* since we await both geolocation and age assurance requirements before
* rendering `shell/index.tsx`.
*/
const {isAgeRestricted} = useAgeAssuranceContext()
return useCallback( return useCallback(
({token}: {token: Notifications.DevicePushToken}) => { ({
token,
isAgeRestricted,
}: {
token: Notifications.DevicePushToken
isAgeRestricted: boolean
}) => {
if (!currentAccount) return if (!currentAccount) return
return _registerPushTokenDebounced({ return _registerPushTokenDebounced({
agent, agent,
@@ -84,7 +86,7 @@ export function useRegisterPushToken() {
}, },
}) })
}, },
[agent, currentAccount, isAgeRestricted], [agent, currentAccount],
) )
} }
@@ -120,28 +122,39 @@ async function getPushToken() {
* @see https://github.com/bluesky-social/social-app/pull/4467 * @see https://github.com/bluesky-social/social-app/pull/4467
*/ */
export function useGetAndRegisterPushToken() { export function useGetAndRegisterPushToken() {
const {isAgeRestricted} = useAgeAssuranceContext()
const registerPushToken = useRegisterPushToken() const registerPushToken = useRegisterPushToken()
return useCallback(async () => { return useCallback(
/** async ({
* This will also fire the listener added via `addPushTokenListener`. That isAgeRestricted: isAgeRestrictedOverride,
* listener also handles registration. }: {
*/ isAgeRestricted?: boolean
const token = await getPushToken() } = {}) => {
notyLogger.debug(`useGetAndRegisterPushToken`, {
token: token ?? 'undefined',
})
if (token) {
/** /**
* The listener should have registered the token already, but just in * This will also fire the listener added via `addPushTokenListener`. That
* case, call the debounced function again. * listener also handles registration.
*/ */
registerPushToken({token}) const token = await getPushToken()
}
return token notyLogger.debug(`useGetAndRegisterPushToken`, {
}, [registerPushToken]) token: token ?? 'undefined',
})
if (token) {
/**
* The listener should have registered the token already, but just in
* case, call the debounced function again.
*/
registerPushToken({
token,
isAgeRestricted: isAgeRestrictedOverride ?? isAgeRestricted,
})
}
return token
},
[registerPushToken, isAgeRestricted],
)
} }
/** /**
@@ -155,7 +168,8 @@ export function useNotificationsRegistration() {
const {currentAccount} = useSession() const {currentAccount} = useSession()
const registerPushToken = useRegisterPushToken() const registerPushToken = useRegisterPushToken()
const getAndRegisterPushToken = useGetAndRegisterPushToken() const getAndRegisterPushToken = useGetAndRegisterPushToken()
const {isLoaded: isAgeAssuranceLoaded} = useAgeAssuranceContext() const {isLoaded: isAgeAssuranceLoaded, isAgeRestricted} =
useAgeAssuranceContext()
useEffect(() => { useEffect(() => {
/** /**
@@ -184,7 +198,7 @@ export function useNotificationsRegistration() {
* @see https://docs.expo.dev/versions/latest/sdk/notifications/#addpushtokenlistenerlistener * @see https://docs.expo.dev/versions/latest/sdk/notifications/#addpushtokenlistenerlistener
*/ */
const subscription = Notifications.addPushTokenListener(async token => { const subscription = Notifications.addPushTokenListener(async token => {
registerPushToken({token}) registerPushToken({token, isAgeRestricted})
notyLogger.debug(`addPushTokenListener callback`, {token}) notyLogger.debug(`addPushTokenListener callback`, {token})
}) })
@@ -196,6 +210,7 @@ export function useNotificationsRegistration() {
getAndRegisterPushToken, getAndRegisterPushToken,
registerPushToken, registerPushToken,
isAgeAssuranceLoaded, isAgeAssuranceLoaded,
isAgeRestricted,
]) ])
} }