From 1e37d32f4bdc51045cd85d01ea1035fd53e0d32c Mon Sep 17 00:00:00 2001 From: Kimsible Date: Wed, 28 Apr 2021 17:33:55 +0200 Subject: Add server API actors route --- server/middlewares/validators/actor.ts | 59 ++++++++++++++++++++++++++++++++++ server/middlewares/validators/index.ts | 1 + 2 files changed, 60 insertions(+) create mode 100644 server/middlewares/validators/actor.ts (limited to 'server/middlewares') diff --git a/server/middlewares/validators/actor.ts b/server/middlewares/validators/actor.ts new file mode 100644 index 000000000..99b529dd6 --- /dev/null +++ b/server/middlewares/validators/actor.ts @@ -0,0 +1,59 @@ +import * as express from 'express' +import { param } from 'express-validator' +import { isActorNameValid } from '../../helpers/custom-validators/actor' +import { logger } from '../../helpers/logger' +import { areValidationErrors } from './utils' +import { + doesAccountNameWithHostExist, + doesLocalAccountNameExist, + doesVideoChannelNameWithHostExist, + doesLocalVideoChannelNameExist +} from '../../helpers/middlewares' +import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' + +const localActorValidator = [ + param('actorName').custom(isActorNameValid).withMessage('Should have a valid actor name'), + + async (req: express.Request, res: express.Response, next: express.NextFunction) => { + logger.debug('Checking localActorValidator parameters', { parameters: req.params }) + + if (areValidationErrors(req, res)) return + + const isAccount = await doesLocalAccountNameExist(req.params.actorName, res, false) + const isVideoChannel = await doesLocalVideoChannelNameExist(req.params.actorName, res, false) + + if (!isAccount || !isVideoChannel) { + res.status(HttpStatusCode.NOT_FOUND_404) + .json({ error: 'Actor not found' }) + } + + return next() + } +] + +const actorNameWithHostGetValidator = [ + param('actorName').exists().withMessage('Should have an actor name with host'), + + async (req: express.Request, res: express.Response, next: express.NextFunction) => { + logger.debug('Checking actorNameWithHostGetValidator parameters', { parameters: req.params }) + + if (areValidationErrors(req, res)) return + + const isAccount = await doesAccountNameWithHostExist(req.params.actorName, res, false) + const isVideoChannel = await doesVideoChannelNameWithHostExist(req.params.actorName, res, false) + + if (!isAccount && !isVideoChannel) { + res.status(HttpStatusCode.NOT_FOUND_404) + .json({ error: 'Actor not found' }) + } + + return next() + } +] + +// --------------------------------------------------------------------------- + +export { + localActorValidator, + actorNameWithHostGetValidator +} diff --git a/server/middlewares/validators/index.ts b/server/middlewares/validators/index.ts index 24faeea3e..3e1a1e5ce 100644 --- a/server/middlewares/validators/index.ts +++ b/server/middlewares/validators/index.ts @@ -1,5 +1,6 @@ export * from './abuse' export * from './account' +export * from './actor' export * from './actor-image' export * from './blocklist' export * from './oembed' -- cgit v1.2.3