1 import * as express from 'express'
2 import { query } from 'express-validator'
3 import { isWebfingerLocalResourceValid } from '../../helpers/custom-validators/webfinger'
4 import { logger } from '../../helpers/logger'
5 import { ActorModel } from '../../models/activitypub/actor'
6 import { areValidationErrors } from './utils'
7 import { getHostWithPort } from '../../helpers/express-utils'
8 import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
10 const webfingerValidator = [
11 query('resource').custom(isWebfingerLocalResourceValid).withMessage('Should have a valid webfinger resource'),
13 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
14 logger.debug('Checking webfinger parameters', { parameters: req.query })
16 if (areValidationErrors(req, res)) return
18 // Remove 'acct:' from the beginning of the string
19 const nameWithHost = getHostWithPort(req.query.resource.substr(5))
20 const [ name ] = nameWithHost.split('@')
22 const actor = await ActorModel.loadLocalUrlByName(name)
24 return res.status(HttpStatusCode.NOT_FOUND_404)
25 .send({ error: 'Actor not found' })
29 res.locals.actorUrl = actor
34 // ---------------------------------------------------------------------------