2021-08-27 14:32:44 +02:00
|
|
|
import express from 'express'
|
2019-07-25 16:23:44 +02:00
|
|
|
import { query } from 'express-validator'
|
2021-07-16 10:42:24 +02:00
|
|
|
import { HttpStatusCode } from '../../../shared/models/http/http-error-codes'
|
2018-08-16 15:25:20 +02:00
|
|
|
import { isWebfingerLocalResourceValid } from '../../helpers/custom-validators/webfinger'
|
2021-05-11 11:15:29 +02:00
|
|
|
import { getHostWithPort } from '../../helpers/express-utils'
|
|
|
|
import { ActorModel } from '../../models/actor/actor'
|
2021-06-03 17:33:44 +02:00
|
|
|
import { areValidationErrors } from './shared'
|
2017-11-14 17:31:26 +01:00
|
|
|
|
|
|
|
const webfingerValidator = [
|
2022-08-17 14:27:04 +02:00
|
|
|
query('resource')
|
|
|
|
.custom(isWebfingerLocalResourceValid),
|
2017-11-14 17:31:26 +01:00
|
|
|
|
2017-11-27 17:30:46 +01:00
|
|
|
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
if (areValidationErrors(req, res)) return
|
|
|
|
|
|
|
|
// Remove 'acct:' from the beginning of the string
|
2017-12-19 14:21:14 +01:00
|
|
|
const nameWithHost = getHostWithPort(req.query.resource.substr(5))
|
2017-11-27 17:30:46 +01:00
|
|
|
const [ name ] = nameWithHost.split('@')
|
|
|
|
|
2020-01-28 14:45:17 +01:00
|
|
|
const actor = await ActorModel.loadLocalUrlByName(name)
|
2017-12-14 17:38:41 +01:00
|
|
|
if (!actor) {
|
2021-06-01 01:36:53 +02:00
|
|
|
return res.fail({
|
|
|
|
status: HttpStatusCode.NOT_FOUND_404,
|
|
|
|
message: 'Actor not found'
|
|
|
|
})
|
2017-11-27 17:30:46 +01:00
|
|
|
}
|
|
|
|
|
2020-01-28 14:45:17 +01:00
|
|
|
res.locals.actorUrl = actor
|
2017-11-27 17:30:46 +01:00
|
|
|
return next()
|
2017-11-14 17:31:26 +01:00
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
webfingerValidator
|
|
|
|
}
|