Write chat declaration record in response to different events (#10216)

Co-authored-by: Eric Bailey <git@esb.lol>
This commit is contained in:
DS Boyce
2026-04-20 13:39:12 -07:00
committed by GitHub
parent d58ff89441
commit 8c2e4c6fad
10 changed files with 298 additions and 125 deletions
+6
View File
@@ -4,6 +4,7 @@ import {useMutation, useQueryClient} from '@tanstack/react-query'
import {preferencesQueryKey} from '#/state/queries/preferences'
import {useAgent, useSession} from '#/state/session'
import {usePatchAgeAssuranceOtherRequiredData} from '#/ageAssurance'
import {isUnderAge, maybeRestrictChatSettings} from '#/ageAssurance/util'
import {IS_DEV} from '#/env'
import {account} from '#/storage'
@@ -63,6 +64,11 @@ export function useBirthdateMutation() {
await queryClient.invalidateQueries({
queryKey: preferencesQueryKey,
})
if (isUnderAge(birthDate.toISOString(), 18)) {
maybeRestrictChatSettings({agent})
}
/**
* Also patch the age assurance other required data with the new
* birthdate, which may change the user's age assurance access level.
@@ -1,4 +1,8 @@
import {type AppBskyActorDefs} from '@atproto/api'
import type AtpAgent from '@atproto/api'
import {
type AppBskyActorDefs,
type ChatBskyActorDeclaration,
} from '@atproto/api'
import {useMutation, useQueryClient} from '@tanstack/react-query'
import {logger} from '#/logger'
@@ -78,3 +82,21 @@ export function useDeleteActorDeclaration() {
},
})
}
export async function fetchActorDeclarationRecord({
agent,
did,
}: {
agent: AtpAgent
did?: string
}) {
if (!did) return
const res = await agent.com.atproto.repo
.getRecord({
repo: did,
collection: 'chat.bsky.actor.declaration',
rkey: 'self',
})
.catch(_e => undefined)
return res?.data.value as ChatBskyActorDeclaration.Main
}
@@ -0,0 +1,39 @@
import type AtpAgent from '@atproto/api'
import {type ChatBskyActorDeclaration} from '@atproto/api'
import {networkRetry} from '#/lib/async/retry'
import {logger} from '#/logger'
import {setOtherRequiredDataActorDeclarationCache} from '#/ageAssurance/data'
/**
* Helper to update the chat settings record.
*/
export async function restrictChatSettings({
agent,
did,
}: {
agent: AtpAgent
did: string
}): Promise<void> {
try {
const record: ChatBskyActorDeclaration.Main = {
$type: 'chat.bsky.actor.declaration',
allowIncoming: 'none',
}
await networkRetry(3, () =>
agent.com.atproto.repo.putRecord({
repo: did,
collection: 'chat.bsky.actor.declaration',
rkey: 'self',
record,
}),
)
// important, update local cache to avoid running this again
setOtherRequiredDataActorDeclarationCache({
did,
actorDeclaration: record,
})
} catch {
logger.error(`restrictChatSettings: failed to set chat declaration`)
}
}
@@ -12,6 +12,9 @@ jest.mock('jwt-decode', () => ({
jest.mock('../../birthdate')
jest.mock('../../../ageAssurance/data')
jest.mock('../../../ageAssurance/state', () => ({
getAndComputeAgeAssuranceState: () => ({}),
}))
jest.mock('#/lib/notifications/notifications', () => ({
unregisterPushToken(_agents: BskyAgent[]) {
return Promise.resolve()
+10 -21
View File
@@ -22,15 +22,17 @@ import {
PUBLIC_BSKY_SERVICE,
TIMELINE_SAVED_FEED,
} from '#/lib/constants'
import {getAge} from '#/lib/strings/time'
import {logger} from '#/logger'
import {snoozeBirthdateUpdateAllowedForDid} from '#/state/birthdate'
import {restrictChatSettings} from '#/state/queries/messages/restrictChatSettings'
import {snoozeEmailConfirmationPrompt} from '#/state/shell/reminders'
import {
prefetchAgeAssuranceData,
setBirthdateForDid,
setCreatedAtForDid,
} from '#/ageAssurance/data'
import {getAndComputeAgeAssuranceState} from '#/ageAssurance/state'
import {AgeAssuranceAccess} from '#/ageAssurance/types'
import {features} from '#/analytics'
import {emitNetworkConfirmed, emitNetworkLost} from '../events'
import {addSessionErrorLog} from './logging'
@@ -218,26 +220,13 @@ export async function createAgentAndCreateAccount(
logger.info(`createAgentAndCreateAccount: failed to set initial feeds`)
throw e
}),
...(getAge(birthDate) < 18
? [
networkRetry(3, () => {
return agent.com.atproto.repo.putRecord({
repo: account.did,
collection: 'chat.bsky.actor.declaration',
rkey: 'self',
record: {
$type: 'chat.bsky.actor.declaration',
allowIncoming: 'none',
},
})
}).catch(e => {
logger.info(
`createAgentAndCreateAccount: failed to set chat declaration`,
)
throw e
}),
]
: []),
// wait for AA data to load first, then check state
aa.then(async () => {
const state = getAndComputeAgeAssuranceState({did: account.did})
if (state.access !== AgeAssuranceAccess.Full) {
restrictChatSettings({agent, did: account.did})
}
}),
]).then(promises => {
const rejected = promises.filter(p => p.status === 'rejected')
if (rejected.length > 0) {