import { DAY, HOUR } from '@atproto/common' import { ForbiddenError, InvalidRequestError, Server, } from '@atproto/xrpc-server' import { ACCESS_FULL } from '../../../../auth-scope' import { AppContext } from '../../../../context' import { com } from '../../../../lexicons/index.js' export default function (server: Server, ctx: AppContext) { server.add(com.atproto.server.requestAccountDelete, { rateLimit: [ { durationMs: DAY, points: 15, calcKey: ({ auth }) => auth.credentials.did, }, { durationMs: HOUR, points: 5, calcKey: ({ auth }) => auth.credentials.did, }, ], auth: ctx.authVerifier.authorization({ checkTakedown: true, scopes: ACCESS_FULL, authorize: () => { throw new ForbiddenError( 'OAuth credentials are not supported for this endpoint', ) }, }), handler: async ({ auth, req }) => { const did = auth.credentials.did const account = await ctx.accountManager.getAccount(did, { includeDeactivated: true, includeTakenDown: true, }) if (!account) { throw new InvalidRequestError('account not found') } if (ctx.entrywayClient) { const { headers } = await ctx.entrywayAuthHeaders( req, auth.credentials.did, com.atproto.server.requestAccountDelete.$lxm, ) await ctx.entrywayClient.xrpc(com.atproto.server.requestAccountDelete, { headers, }) return } if (!account.email) { throw new InvalidRequestError('account does not have an email address') } const token = await ctx.accountManager.createEmailToken( did, 'delete_account', ) await ctx.mailer.sendAccountDelete({ token }, { to: account.email }) }, }) }