migrate the verification and chat declaration records to the pds client
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -7,6 +7,7 @@ import {
|
|||||||
AtpAgent,
|
AtpAgent,
|
||||||
type ChatBskyActorDeclaration,
|
type ChatBskyActorDeclaration,
|
||||||
} from '@atproto/api'
|
} from '@atproto/api'
|
||||||
|
import {type Client} from '@atproto/lex'
|
||||||
import {createAsyncStoragePersister} from '@tanstack/query-async-storage-persister'
|
import {createAsyncStoragePersister} from '@tanstack/query-async-storage-persister'
|
||||||
import {focusManager, QueryClient, useQuery} from '@tanstack/react-query'
|
import {focusManager, QueryClient, useQuery} from '@tanstack/react-query'
|
||||||
import {persistQueryClient} from '@tanstack/react-query-persist-client'
|
import {persistQueryClient} from '@tanstack/react-query-persist-client'
|
||||||
@@ -69,6 +70,17 @@ export function getDidFromAgentSession(agent: AtpAgent) {
|
|||||||
return sessionManager.did
|
return sessionManager.did
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reads the active account did from a lex {@link Client}. Logged-out clients
|
||||||
|
* expose `did: undefined`, so this returns undefined in that case.
|
||||||
|
*
|
||||||
|
* Lives alongside {@link getDidFromAgentSession} rather than replacing it: the
|
||||||
|
* rest of this file is still agent-shaped and blocked on `getPreferences`.
|
||||||
|
*/
|
||||||
|
export function getDidFromClient(client: Client) {
|
||||||
|
return client.did
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Optimistic data
|
* Optimistic data
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import {useMutation, useQueryClient} from '@tanstack/react-query'
|
|||||||
|
|
||||||
import {restrictChatSettings} from '#/state/queries/messages/restrictChatSettings'
|
import {restrictChatSettings} from '#/state/queries/messages/restrictChatSettings'
|
||||||
import {preferencesQueryKey} from '#/state/queries/preferences'
|
import {preferencesQueryKey} from '#/state/queries/preferences'
|
||||||
import {useAgent, useSession} from '#/state/session'
|
import {useAgent, usePdsClient, useSession} from '#/state/session'
|
||||||
import {usePatchAgeAssuranceOtherRequiredData} from '#/ageAssurance'
|
import {usePatchAgeAssuranceOtherRequiredData} from '#/ageAssurance'
|
||||||
import {isUnderAge} from '#/ageAssurance/util'
|
import {isUnderAge} from '#/ageAssurance/util'
|
||||||
import {IS_DEV} from '#/env'
|
import {IS_DEV} from '#/env'
|
||||||
@@ -55,6 +55,7 @@ export function useIsBirthdateUpdateAllowed() {
|
|||||||
export function useBirthdateMutation() {
|
export function useBirthdateMutation() {
|
||||||
const queryClient = useQueryClient()
|
const queryClient = useQueryClient()
|
||||||
const agent = useAgent()
|
const agent = useAgent()
|
||||||
|
const pdsClient = usePdsClient()
|
||||||
const patchOtherRequiredData = usePatchAgeAssuranceOtherRequiredData()
|
const patchOtherRequiredData = usePatchAgeAssuranceOtherRequiredData()
|
||||||
|
|
||||||
return useMutation<void, unknown, {birthDate: Date}>({
|
return useMutation<void, unknown, {birthDate: Date}>({
|
||||||
@@ -68,7 +69,7 @@ export function useBirthdateMutation() {
|
|||||||
|
|
||||||
if (isUnderAge(birthDate.toISOString(), 18)) {
|
if (isUnderAge(birthDate.toISOString(), 18)) {
|
||||||
await restrictChatSettings({
|
await restrictChatSettings({
|
||||||
agent,
|
client: pdsClient,
|
||||||
restrictIncoming: true,
|
restrictIncoming: true,
|
||||||
restrictGroupInvites: true,
|
restrictGroupInvites: true,
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,13 +1,13 @@
|
|||||||
import type AtpAgent from '@atproto/api'
|
import {type Client} from '@atproto/lex'
|
||||||
import {type ChatBskyActorDeclaration} from '@atproto/api'
|
|
||||||
|
|
||||||
import {networkRetry} from '#/lib/async/retry'
|
import {networkRetry} from '#/lib/async/retry'
|
||||||
import {logger} from '#/logger'
|
import {logger} from '#/logger'
|
||||||
import {
|
import {
|
||||||
getDidFromAgentSession,
|
getDidFromClient,
|
||||||
getOtherRequiredDataFromCache,
|
getOtherRequiredDataFromCache,
|
||||||
setOtherRequiredDataActorDeclarationCache,
|
setOtherRequiredDataActorDeclarationCache,
|
||||||
} from '#/ageAssurance/data'
|
} from '#/ageAssurance/data'
|
||||||
|
import {chat} from '#/lexicons'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Updates the chat actor declaration record to restrict who can contact the
|
* Updates the chat actor declaration record to restrict who can contact the
|
||||||
@@ -24,15 +24,15 @@ import {
|
|||||||
* back to the lexicon defaults when the cache is empty.
|
* back to the lexicon defaults when the cache is empty.
|
||||||
*/
|
*/
|
||||||
export async function restrictChatSettings({
|
export async function restrictChatSettings({
|
||||||
agent,
|
client,
|
||||||
restrictIncoming = false,
|
restrictIncoming = false,
|
||||||
restrictGroupInvites = false,
|
restrictGroupInvites = false,
|
||||||
}: {
|
}: {
|
||||||
agent: AtpAgent
|
client: Client
|
||||||
restrictIncoming?: boolean
|
restrictIncoming?: boolean
|
||||||
restrictGroupInvites?: boolean
|
restrictGroupInvites?: boolean
|
||||||
}): Promise<void> {
|
}): Promise<void> {
|
||||||
const did = getDidFromAgentSession(agent)
|
const did = getDidFromClient(client)
|
||||||
if (!did) return
|
if (!did) return
|
||||||
|
|
||||||
const cached = getOtherRequiredDataFromCache({did})?.actorDeclaration
|
const cached = getOtherRequiredDataFromCache({did})?.actorDeclaration
|
||||||
@@ -49,7 +49,7 @@ export async function restrictChatSettings({
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
const record: ChatBskyActorDeclaration.Main = {
|
const record: chat.bsky.actor.declaration.Main = {
|
||||||
$type: 'chat.bsky.actor.declaration',
|
$type: 'chat.bsky.actor.declaration',
|
||||||
allowIncoming: restrictIncoming
|
allowIncoming: restrictIncoming
|
||||||
? 'none'
|
? 'none'
|
||||||
@@ -69,11 +69,14 @@ export async function restrictChatSettings({
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
await networkRetry(3, () =>
|
await networkRetry(3, () =>
|
||||||
agent.com.atproto.repo.putRecord({
|
/*
|
||||||
|
* A record helper, not a raw `com.atproto.repo.putRecord`: lex forces
|
||||||
|
* `service: null` on record helpers, so the write lands on the account's
|
||||||
|
* PDS even though the collection is `chat.bsky.*`.
|
||||||
|
*/
|
||||||
|
client.put(chat.bsky.actor.declaration, record, {
|
||||||
repo: did,
|
repo: did,
|
||||||
collection: 'chat.bsky.actor.declaration',
|
|
||||||
rkey: 'self',
|
rkey: 'self',
|
||||||
record,
|
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
// important, update local cache to avoid running this again
|
// important, update local cache to avoid running this again
|
||||||
|
|||||||
@@ -1,15 +1,22 @@
|
|||||||
import {type AppBskyActorGetProfile} from '@atproto/api'
|
import {
|
||||||
|
type AtIdentifierString,
|
||||||
|
type DidString,
|
||||||
|
type HandleString,
|
||||||
|
toDatetimeString,
|
||||||
|
} from '@atproto/syntax'
|
||||||
import {useMutation} from '@tanstack/react-query'
|
import {useMutation} from '@tanstack/react-query'
|
||||||
|
|
||||||
import {until} from '#/lib/async/until'
|
import {until} from '#/lib/async/until'
|
||||||
import {useUpdateProfileVerificationCache} from '#/state/queries/verification/useUpdateProfileVerificationCache'
|
import {useUpdateProfileVerificationCache} from '#/state/queries/verification/useUpdateProfileVerificationCache'
|
||||||
import {useAgent, useSession} from '#/state/session'
|
import {useAppviewClient, usePdsClient, useSession} from '#/state/session'
|
||||||
import {useAnalytics} from '#/analytics'
|
import {useAnalytics} from '#/analytics'
|
||||||
|
import {app} from '#/lexicons'
|
||||||
import type * as bsky from '#/types/bsky'
|
import type * as bsky from '#/types/bsky'
|
||||||
|
|
||||||
export function useVerificationCreateMutation() {
|
export function useVerificationCreateMutation() {
|
||||||
const ax = useAnalytics()
|
const ax = useAnalytics()
|
||||||
const agent = useAgent()
|
const appviewClient = useAppviewClient()
|
||||||
|
const pdsClient = usePdsClient()
|
||||||
const {currentAccount} = useSession()
|
const {currentAccount} = useSession()
|
||||||
const updateProfileVerificationCache = useUpdateProfileVerificationCache()
|
const updateProfileVerificationCache = useUpdateProfileVerificationCache()
|
||||||
|
|
||||||
@@ -19,20 +26,18 @@ export function useVerificationCreateMutation() {
|
|||||||
throw new Error('User not logged in')
|
throw new Error('User not logged in')
|
||||||
}
|
}
|
||||||
|
|
||||||
const {uri} = await agent.app.bsky.graph.verification.create(
|
const {uri} = await pdsClient.create(app.bsky.graph.verification, {
|
||||||
{repo: currentAccount.did},
|
// the profile view is still legacy-typed, so its strings are unbranded
|
||||||
{
|
subject: profile.did as DidString,
|
||||||
subject: profile.did,
|
createdAt: toDatetimeString(new Date()),
|
||||||
createdAt: new Date().toISOString(),
|
handle: profile.handle as HandleString,
|
||||||
handle: profile.handle,
|
displayName: profile.displayName || '',
|
||||||
displayName: profile.displayName || '',
|
})
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
await until(
|
await until(
|
||||||
5,
|
5,
|
||||||
1e3,
|
1e3,
|
||||||
({data: profile}: AppBskyActorGetProfile.Response) => {
|
(profile: app.bsky.actor.getProfile.$OutputBody) => {
|
||||||
if (
|
if (
|
||||||
profile.verification &&
|
profile.verification &&
|
||||||
profile.verification.verifications.find(v => v.uri === uri)
|
profile.verification.verifications.find(v => v.uri === uri)
|
||||||
@@ -42,7 +47,9 @@ export function useVerificationCreateMutation() {
|
|||||||
return false
|
return false
|
||||||
},
|
},
|
||||||
() => {
|
() => {
|
||||||
return agent.getProfile({actor: profile.did ?? ''})
|
return appviewClient.call(app.bsky.actor.getProfile, {
|
||||||
|
actor: (profile.did ?? '') as AtIdentifierString,
|
||||||
|
})
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,19 +1,18 @@
|
|||||||
import {
|
import {type AppBskyActorDefs} from '@atproto/api'
|
||||||
type AppBskyActorDefs,
|
import {type AtIdentifierString, AtUri} from '@atproto/syntax'
|
||||||
type AppBskyActorGetProfile,
|
|
||||||
AtUri,
|
|
||||||
} from '@atproto/api'
|
|
||||||
import {useMutation} from '@tanstack/react-query'
|
import {useMutation} from '@tanstack/react-query'
|
||||||
|
|
||||||
import {until} from '#/lib/async/until'
|
import {until} from '#/lib/async/until'
|
||||||
import {useUpdateProfileVerificationCache} from '#/state/queries/verification/useUpdateProfileVerificationCache'
|
import {useUpdateProfileVerificationCache} from '#/state/queries/verification/useUpdateProfileVerificationCache'
|
||||||
import {useAgent, useSession} from '#/state/session'
|
import {useAppviewClient, usePdsClient, useSession} from '#/state/session'
|
||||||
import {useAnalytics} from '#/analytics'
|
import {useAnalytics} from '#/analytics'
|
||||||
|
import {app} from '#/lexicons'
|
||||||
import type * as bsky from '#/types/bsky'
|
import type * as bsky from '#/types/bsky'
|
||||||
|
|
||||||
export function useVerificationsRemoveMutation() {
|
export function useVerificationsRemoveMutation() {
|
||||||
const ax = useAnalytics()
|
const ax = useAnalytics()
|
||||||
const agent = useAgent()
|
const appviewClient = useAppviewClient()
|
||||||
|
const pdsClient = usePdsClient()
|
||||||
const {currentAccount} = useSession()
|
const {currentAccount} = useSession()
|
||||||
const updateProfileVerificationCache = useUpdateProfileVerificationCache()
|
const updateProfileVerificationCache = useUpdateProfileVerificationCache()
|
||||||
|
|
||||||
@@ -33,9 +32,8 @@ export function useVerificationsRemoveMutation() {
|
|||||||
|
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
uris.map(uri => {
|
uris.map(uri => {
|
||||||
return agent.app.bsky.graph.verification.delete({
|
return pdsClient.delete(app.bsky.graph.verification, {
|
||||||
repo: currentAccount.did,
|
rkey: new AtUri(uri).rkeySafe,
|
||||||
rkey: new AtUri(uri).rkey,
|
|
||||||
})
|
})
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
@@ -43,7 +41,7 @@ export function useVerificationsRemoveMutation() {
|
|||||||
await until(
|
await until(
|
||||||
5,
|
5,
|
||||||
1e3,
|
1e3,
|
||||||
({data: profile}: AppBskyActorGetProfile.Response) => {
|
(profile: app.bsky.actor.getProfile.$OutputBody) => {
|
||||||
if (
|
if (
|
||||||
!profile.verification?.verifications.some(v => uris.includes(v.uri))
|
!profile.verification?.verifications.some(v => uris.includes(v.uri))
|
||||||
) {
|
) {
|
||||||
@@ -52,7 +50,10 @@ export function useVerificationsRemoveMutation() {
|
|||||||
return false
|
return false
|
||||||
},
|
},
|
||||||
() => {
|
() => {
|
||||||
return agent.getProfile({actor: profile.did ?? ''})
|
return appviewClient.call(app.bsky.actor.getProfile, {
|
||||||
|
// the profile view is still legacy-typed, so its did is unbranded
|
||||||
|
actor: (profile.did ?? '') as AtIdentifierString,
|
||||||
|
})
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import {type AppBskyActorProfile, type Un$Typed} from '@atproto/api'
|
import {type AppBskyActorProfile, type Un$Typed} from '@atproto/api'
|
||||||
import {TID} from '@atproto/common-web'
|
import {TID} from '@atproto/common-web'
|
||||||
|
import {type Client} from '@atproto/lex'
|
||||||
import {PasswordSession} from '@atproto/lex-password-session'
|
import {PasswordSession} from '@atproto/lex-password-session'
|
||||||
|
|
||||||
import {networkRetry} from '#/lib/async/retry'
|
import {networkRetry} from '#/lib/async/retry'
|
||||||
@@ -21,6 +22,7 @@ import {
|
|||||||
import {unsafeGetAndComputeAgeAssurance} from '#/ageAssurance/state'
|
import {unsafeGetAndComputeAgeAssurance} from '#/ageAssurance/state'
|
||||||
import {features} from '#/analytics'
|
import {features} from '#/analytics'
|
||||||
import {type BskyAppAgent} from './bridge-agent'
|
import {type BskyAppAgent} from './bridge-agent'
|
||||||
|
import {agentToPdsClient} from './clients'
|
||||||
import {configureModerationForAccount} from './moderation'
|
import {configureModerationForAccount} from './moderation'
|
||||||
import {
|
import {
|
||||||
buildBundle,
|
buildBundle,
|
||||||
@@ -109,7 +111,11 @@ export async function createSessionBundleAndCreateAccount(
|
|||||||
if (isProd) {
|
if (isProd) {
|
||||||
postSignupTasks.push(
|
postSignupTasks.push(
|
||||||
initializeSavedFeeds(bundle.agent),
|
initializeSavedFeeds(bundle.agent),
|
||||||
restrictChatAfterAgeAssurance(aa, bundle.agent, earlyAccount.did),
|
restrictChatAfterAgeAssurance(
|
||||||
|
aa,
|
||||||
|
agentToPdsClient(bundle.agent),
|
||||||
|
earlyAccount.did,
|
||||||
|
),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
// Post-signup writes are not required to enter onboarding.
|
// Post-signup writes are not required to enter onboarding.
|
||||||
@@ -207,14 +213,14 @@ function initializeSavedFeeds(agent: BskyAppAgent) {
|
|||||||
|
|
||||||
function restrictChatAfterAgeAssurance(
|
function restrictChatAfterAgeAssurance(
|
||||||
ageAssurance: Promise<unknown>,
|
ageAssurance: Promise<unknown>,
|
||||||
agent: BskyAppAgent,
|
client: Client,
|
||||||
did: string,
|
did: string,
|
||||||
) {
|
) {
|
||||||
return ageAssurance.then(() => {
|
return ageAssurance.then(() => {
|
||||||
const {flags} = unsafeGetAndComputeAgeAssurance({did})
|
const {flags} = unsafeGetAndComputeAgeAssurance({did})
|
||||||
if (flags?.chatDisabled || flags?.groupChatDisabled) {
|
if (flags?.chatDisabled || flags?.groupChatDisabled) {
|
||||||
void restrictChatSettings({
|
void restrictChatSettings({
|
||||||
agent,
|
client,
|
||||||
restrictIncoming: flags.chatDisabled,
|
restrictIncoming: flags.chatDisabled,
|
||||||
restrictGroupInvites: flags.groupChatDisabled,
|
restrictGroupInvites: flags.groupChatDisabled,
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user