]>
Commit | Line | Data |
---|---|---|
1 | import * as express from 'express' | |
2 | import { query } from 'express-validator' | |
3 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' | |
4 | import { isWebfingerLocalResourceValid } from '../../helpers/custom-validators/webfinger' | |
5 | import { getHostWithPort } from '../../helpers/express-utils' | |
6 | import { logger } from '../../helpers/logger' | |
7 | import { ActorModel } from '../../models/actor/actor' | |
8 | import { areValidationErrors } from './shared' | |
9 | ||
10 | const webfingerValidator = [ | |
11 | query('resource').custom(isWebfingerLocalResourceValid).withMessage('Should have a valid webfinger resource'), | |
12 | ||
13 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
14 | logger.debug('Checking webfinger parameters', { parameters: req.query }) | |
15 | ||
16 | if (areValidationErrors(req, res)) return | |
17 | ||
18 | // Remove 'acct:' from the beginning of the string | |
19 | const nameWithHost = getHostWithPort(req.query.resource.substr(5)) | |
20 | const [ name ] = nameWithHost.split('@') | |
21 | ||
22 | const actor = await ActorModel.loadLocalUrlByName(name) | |
23 | if (!actor) { | |
24 | return res.fail({ | |
25 | status: HttpStatusCode.NOT_FOUND_404, | |
26 | message: 'Actor not found' | |
27 | }) | |
28 | } | |
29 | ||
30 | res.locals.actorUrl = actor | |
31 | return next() | |
32 | } | |
33 | ] | |
34 | ||
35 | // --------------------------------------------------------------------------- | |
36 | ||
37 | export { | |
38 | webfingerValidator | |
39 | } |