]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/webfinger.ts
Translated using Weblate (Vietnamese)
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / webfinger.ts
1 import express from 'express'
2 import { query } from 'express-validator'
3 import { HttpStatusCode } from '../../../shared/models/http/http-error-codes'
4 import { isWebfingerLocalResourceValid } from '../../helpers/custom-validators/webfinger'
5 import { getHostWithPort } from '../../helpers/express-utils'
6 import { ActorModel } from '../../models/actor/actor'
7 import { areValidationErrors } from './shared'
8
9 const webfingerValidator = [
10 query('resource')
11 .custom(isWebfingerLocalResourceValid),
12
13 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
14 if (areValidationErrors(req, res)) return
15
16 // Remove 'acct:' from the beginning of the string
17 const nameWithHost = getHostWithPort(req.query.resource.substr(5))
18 const [ name ] = nameWithHost.split('@')
19
20 const actor = await ActorModel.loadLocalUrlByName(name)
21 if (!actor) {
22 return res.fail({
23 status: HttpStatusCode.NOT_FOUND_404,
24 message: 'Actor not found'
25 })
26 }
27
28 res.locals.actorUrl = actor
29 return next()
30 }
31 ]
32
33 // ---------------------------------------------------------------------------
34
35 export {
36 webfingerValidator
37 }