Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495import {type DidString, type HandleString} from '@atproto/syntax'
import {BSKY_SERVICE, PUBLIC_BSKY_SERVICE} from '#/lib/constants'import {createServiceClient} from '#/lib/lexClient'import {isNetworkError} from '#/lib/strings/errors'import {buildHandleCandidates} from '#/lib/strings/handles'import {getErrorStatus} from '#/lib/xrpc-error'import {com} from '#/lexicons'
export function isHandleResolutionError(e: unknown): boolean { if (e && typeof e === 'object' && 'name' in e) { const name = (e as {name?: unknown}).name if (name === 'IdentityResolverError' || name === 'HandleResolverError') { return true } } const s = String(e) return ( s.includes('Unable to resolve handle') || s.includes('does not resolve to a DID') || s.includes('Failed to resolve identity') )}
export async function resolveBareNameToHandles( name: string, availableUserDomains?: string[],): Promise<string[]> { const candidates = buildHandleCandidates(name, availableUserDomains) const appview = createServiceClient(PUBLIC_BSKY_SERVICE) const results = await Promise.allSettled( candidates.map(handle => appview.call(com.atproto.identity.resolveHandle, { handle: handle as HandleString, }), ), ) const matches = candidates.filter((_, i) => results[i].status === 'fulfilled') // only a definite not-found proves a candidate doesn't exist; a transient // failure on the user's real domain must not let a lookalike domain win const retryableFailure = results.find( r => r.status === 'rejected' && !isHandleResolutionError(r.reason), ) if (retryableFailure?.status === 'rejected') { throw retryableFailure.reason } return matches}
export async function resolveDeactivatedHandle( identifier: string,): Promise<string> { if (identifier.startsWith('did:')) { return identifier } const appview = createServiceClient(PUBLIC_BSKY_SERVICE) let did: string try { const res = await appview.call(com.atproto.identity.resolveHandle, { handle: identifier as HandleString, }) did = res.did } catch (e) { if (isNetworkError(e)) throw e throw new Error('Unable to resolve handle') }
const pds = createServiceClient(BSKY_SERVICE) let active: boolean | undefined let status: string | undefined try { const res = await pds.call(com.atproto.sync.getRepoStatus, { did: did as DidString, }) active = res.active status = res.status } catch (e) { if (getErrorStatus(e) === 404) { return did } if (isNetworkError(e)) throw e throw new Error('Unable to verify account status') } if (active || status === 'deactivated') { return did } if (status === 'takendown') { throw new Error('Account has been taken down') } if (status === 'suspended') { throw new Error('Account has been suspended') } throw new Error('Unable to verify account status')}