[SDK] Migrate agent sugar to @bsky.app/sdk actions (#11382)
This commit is contained in:
@@ -2,24 +2,21 @@ import {useContext} from 'react'
|
||||
import {Alert, View} from 'react-native'
|
||||
import {useSafeAreaInsets} from 'react-native-safe-area-context'
|
||||
import * as Contacts from 'expo-contacts'
|
||||
import type AtpAgent from '@atproto/api'
|
||||
import {
|
||||
type AppBskyActorProfile,
|
||||
AppBskyContactImportContacts,
|
||||
type Un$Typed,
|
||||
} from '@atproto/api'
|
||||
import {type Un$Typed} from '@atproto/lex'
|
||||
import {type Client} from '@atproto/lex'
|
||||
import {toDatetimeString} from '@atproto/syntax'
|
||||
import {upsertProfile} from '@bsky.app/sdk'
|
||||
import {msg, t} from '@lingui/core/macro'
|
||||
import {useLingui} from '@lingui/react'
|
||||
import {Trans} from '@lingui/react/macro'
|
||||
import {useMutation, useQueryClient} from '@tanstack/react-query'
|
||||
|
||||
import {uploadBlob} from '#/lib/api'
|
||||
import {toLegacyBlobRef} from '#/lib/api/legacy-blob'
|
||||
import {cleanError, isNetworkError} from '#/lib/strings/errors'
|
||||
import {matchXrpcError} from '#/lib/xrpc-error'
|
||||
import {logger} from '#/logger'
|
||||
import {findContactsStatusQueryKey} from '#/state/queries/find-contacts'
|
||||
import {useAgent, usePdsClient} from '#/state/session'
|
||||
import {useAppviewClient, usePdsClient} from '#/state/session'
|
||||
import {
|
||||
Context as OnboardingContext,
|
||||
type OnboardingAction,
|
||||
@@ -32,6 +29,7 @@ import {Loader} from '#/components/Loader'
|
||||
import * as Toast from '#/components/Toast'
|
||||
import {Text} from '#/components/Typography'
|
||||
import {useAnalytics} from '#/analytics'
|
||||
import {app} from '#/lexicons'
|
||||
import {
|
||||
contactsWithPhoneNumbersOnly,
|
||||
filterMatchedNumbers,
|
||||
@@ -56,8 +54,8 @@ export function GetContacts({
|
||||
}) {
|
||||
const {_} = useLingui()
|
||||
const ax = useAnalytics()
|
||||
const agent = useAgent()
|
||||
const pdsClient = usePdsClient()
|
||||
const appviewClient = useAppviewClient()
|
||||
const insets = useSafeAreaInsets()
|
||||
const gutters = useGutters([0, 'wide'])
|
||||
const queryClient = useQueryClient()
|
||||
@@ -75,7 +73,7 @@ export function GetContacts({
|
||||
*/
|
||||
if (context === 'Onboarding' && maybeOnboardingContext) {
|
||||
try {
|
||||
await createProfileRecord(agent, pdsClient, maybeOnboardingContext)
|
||||
await createProfileRecord(pdsClient, maybeOnboardingContext)
|
||||
} catch (error) {
|
||||
logger.debug('Error creating profile record:', {safeMessage: error})
|
||||
}
|
||||
@@ -88,13 +86,13 @@ export function GetContacts({
|
||||
)
|
||||
|
||||
if (phoneNumbers.length > 0) {
|
||||
const res = await agent.app.bsky.contact.importContacts({
|
||||
const res = await appviewClient.call(app.bsky.contact.importContacts, {
|
||||
token: state.token,
|
||||
contacts: phoneNumbers.slice(0, MAX_UPLOAD_COUNT),
|
||||
})
|
||||
|
||||
return {
|
||||
matches: res.data.matchesAndContactIndexes,
|
||||
matches: res.matchesAndContactIndexes,
|
||||
indexToContactId,
|
||||
}
|
||||
} else {
|
||||
@@ -151,29 +149,30 @@ export function GetContacts({
|
||||
),
|
||||
{type: 'error'},
|
||||
)
|
||||
} else if (
|
||||
err instanceof AppBskyContactImportContacts.TooManyContactsError
|
||||
) {
|
||||
Toast.show(
|
||||
_(
|
||||
msg`Too many contacts - you've exceeded the number of contacts you can import to find your friends`,
|
||||
),
|
||||
{type: 'error'},
|
||||
)
|
||||
} else if (
|
||||
err instanceof AppBskyContactImportContacts.InvalidTokenError
|
||||
) {
|
||||
Toast.show(
|
||||
_(
|
||||
msg`Could not upload contacts. You need to re-verify your phone number to proceed`,
|
||||
),
|
||||
{type: 'error'},
|
||||
)
|
||||
} else {
|
||||
logger.error('Error uploading contacts', {safeMessage: err})
|
||||
Toast.show(_(msg`Could not upload contacts. ${cleanError(err)}`), {
|
||||
type: 'error',
|
||||
})
|
||||
return
|
||||
}
|
||||
switch (matchXrpcError(err, app.bsky.contact.importContacts)) {
|
||||
case 'TooManyContacts':
|
||||
Toast.show(
|
||||
_(
|
||||
msg`Too many contacts - you've exceeded the number of contacts you can import to find your friends`,
|
||||
),
|
||||
{type: 'error'},
|
||||
)
|
||||
break
|
||||
case 'InvalidToken':
|
||||
Toast.show(
|
||||
_(
|
||||
msg`Could not upload contacts. You need to re-verify your phone number to proceed`,
|
||||
),
|
||||
{type: 'error'},
|
||||
)
|
||||
break
|
||||
default:
|
||||
logger.error('Error uploading contacts', {safeMessage: err})
|
||||
Toast.show(_(msg`Could not upload contacts. ${cleanError(err)}`), {
|
||||
type: 'error',
|
||||
})
|
||||
}
|
||||
},
|
||||
})
|
||||
@@ -328,7 +327,6 @@ function showPermissionDeniedAlert() {
|
||||
* Copied from `#/screens/Onboarding/StepFinished/index.tsx`
|
||||
*/
|
||||
async function createProfileRecord(
|
||||
agent: AtpAgent,
|
||||
pdsClient: Client,
|
||||
onboardingContext: {
|
||||
state: OnboardingState
|
||||
@@ -342,19 +340,21 @@ async function createProfileRecord(
|
||||
? uploadBlob(pdsClient, imageUri, imageMime)
|
||||
: undefined
|
||||
|
||||
await agent.upsertProfile(async existing => {
|
||||
let next: Un$Typed<AppBskyActorProfile.Record> = existing ?? {}
|
||||
await pdsClient.call(upsertProfile, async existing => {
|
||||
let next: Un$Typed<app.bsky.actor.profile.Main> = existing ?? {}
|
||||
|
||||
if (blobPromise) {
|
||||
const res = await blobPromise
|
||||
if (res.blob) {
|
||||
next.avatar = toLegacyBlobRef(res.blob)
|
||||
next.avatar = res.blob
|
||||
}
|
||||
}
|
||||
|
||||
next.displayName = ''
|
||||
|
||||
next.createdAt = new Date().toISOString()
|
||||
if (!next.createdAt) {
|
||||
next.createdAt = toDatetimeString(new Date())
|
||||
}
|
||||
return next
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user