Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use public api to resolve handle #7560

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/lib/api/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ export async function resolveLink(
throw new Error('getPost: post not found')
}

// Forked from useFetchDid. TODO: move into RQ.
// TODO: move into RQ.
async function fetchDid(handleOrDid: string) {
let identifier = handleOrDid
if (!identifier.startsWith('did:')) {
Expand Down
8 changes: 4 additions & 4 deletions src/screens/Settings/components/ChangeHandleDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {HITSLOP_10} from '#/lib/constants'
import {cleanError} from '#/lib/strings/errors'
import {sanitizeHandle} from '#/lib/strings/handles'
import {createFullHandle, validateHandle} from '#/lib/strings/handles'
import {useFetchDid, useUpdateHandleMutation} from '#/state/queries/handle'
import {useResolveHandle, useUpdateHandleMutation} from '#/state/queries/handle'
import {RQKEY as RQKEY_PROFILE} from '#/state/queries/profile'
import {useServiceQuery} from '#/state/queries/service'
import {useAgent, useSession} from '#/state/session'
Expand Down Expand Up @@ -282,7 +282,7 @@ function OwnHandlePage({goToServiceHandle}: {goToServiceHandle: () => void}) {
const [domain, setDomain] = useState('')
const agent = useAgent()
const control = Dialog.useDialogContext()
const fetchDid = useFetchDid()
const resolveHandle = useResolveHandle()
const queryClient = useQueryClient()

const {
Expand All @@ -307,10 +307,10 @@ function OwnHandlePage({goToServiceHandle}: {goToServiceHandle: () => void}) {
isSuccess: isVerified,
error: verifyError,
reset: resetVerification,
} = useMutation<true, Error | DidMismatchError>({
} = useMutation<true>({
mutationKey: ['verify-handle', domain],
mutationFn: async () => {
const did = await fetchDid(domain)
const did = await resolveHandle(domain)
if (did !== currentAccount?.did) {
throw new DidMismatchError(did)
}
Expand Down
33 changes: 19 additions & 14 deletions src/state/queries/handle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ import {useMutation, useQueryClient} from '@tanstack/react-query'

import {STALE} from '#/state/queries'
import {useAgent} from '#/state/session'
import {createPublicAgent} from '#/state/session/agent'

const handleQueryKeyRoot = 'handle'
const fetchHandleQueryKey = (handleOrDid: string) => [
handleQueryKeyRoot,
handleOrDid,
]
const didQueryKeyRoot = 'did'
const fetchDidQueryKey = (handleOrDid: string) => [didQueryKeyRoot, handleOrDid]
const fetchDidQueryKey = (handle: string) => [didQueryKeyRoot, handle]

export function useFetchHandle() {
const queryClient = useQueryClient()
Expand Down Expand Up @@ -51,25 +52,29 @@ export function useUpdateHandleMutation(opts?: {
})
}

export function useFetchDid() {
export function useResolveHandle() {
const queryClient = useQueryClient()
const agent = useAgent()

// @NOTE: We are *not* using the logged in agent (from `useAgent()`) here.
// Using the public API rather than the user's PDS ensures that the handle
// properly resolves.
const publicAgent = React.useMemo(() => createPublicAgent(), [])

return React.useCallback(
async (handleOrDid: string) => {
async (handle: string) => {
return queryClient.fetchQuery({
staleTime: STALE.INFINITY,
queryKey: fetchDidQueryKey(handleOrDid),
queryFn: async () => {
let identifier = handleOrDid
if (!identifier.startsWith('did:')) {
const res = await agent.resolveHandle({handle: identifier})
identifier = res.data.did
}
return identifier
staleTime: STALE.MINUTES.ONE,
queryKey: fetchDidQueryKey(handle),
queryFn: async ({signal}) => {
const res = await publicAgent.resolveHandle(
{handle},
// Retries should force a fresh resolution; avoid cache.
{signal, headers: {'cache-control': 'no-cache'}},
)
return res.data.did
},
})
},
[queryClient, agent],
[queryClient, publicAgent],
)
}
Loading