import { DAY, HOUR } from '@atproto/common' import { ForbiddenError, InvalidRequestError } from '@atproto/xrpc-server' import { ACCESS_FULL } from '../../../../auth-scope' import { AppContext } from '../../../../context' import { Server } from '../../../../lexicon' import { ids } from '../../../../lexicon/lexicons' export default function (server: Server, ctx: AppContext) { server.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.entrywayAgent) { await ctx.entrywayAgent.com.atproto.server.requestAccountDelete( undefined, await ctx.entrywayAuthHeaders( req, auth.credentials.did, ids.ComAtprotoServerRequestAccountDelete, ), ) 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 }) }, }) }