diff options
author | Kimsible <kimsible@users.noreply.github.com> | 2021-04-28 17:33:55 +0200 |
---|---|---|
committer | Kimsible <kimsible@users.noreply.github.com> | 2021-05-05 11:47:03 +0200 |
commit | 1e37d32f4bdc51045cd85d01ea1035fd53e0d32c (patch) | |
tree | 2e7bcf053989fc5be9e3ef29558cf1fcf32f8453 /server/middlewares/validators | |
parent | ff8c5ccf09cfe6a469777d4789625f8fdb004408 (diff) | |
download | PeerTube-1e37d32f4bdc51045cd85d01ea1035fd53e0d32c.tar.gz PeerTube-1e37d32f4bdc51045cd85d01ea1035fd53e0d32c.tar.zst PeerTube-1e37d32f4bdc51045cd85d01ea1035fd53e0d32c.zip |
Add server API actors route
Diffstat (limited to 'server/middlewares/validators')
-rw-r--r-- | server/middlewares/validators/actor.ts | 59 | ||||
-rw-r--r-- | server/middlewares/validators/index.ts | 1 |
2 files changed, 60 insertions, 0 deletions
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 @@ | |||
1 | import * as express from 'express' | ||
2 | import { param } from 'express-validator' | ||
3 | import { isActorNameValid } from '../../helpers/custom-validators/actor' | ||
4 | import { logger } from '../../helpers/logger' | ||
5 | import { areValidationErrors } from './utils' | ||
6 | import { | ||
7 | doesAccountNameWithHostExist, | ||
8 | doesLocalAccountNameExist, | ||
9 | doesVideoChannelNameWithHostExist, | ||
10 | doesLocalVideoChannelNameExist | ||
11 | } from '../../helpers/middlewares' | ||
12 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' | ||
13 | |||
14 | const localActorValidator = [ | ||
15 | param('actorName').custom(isActorNameValid).withMessage('Should have a valid actor name'), | ||
16 | |||
17 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
18 | logger.debug('Checking localActorValidator parameters', { parameters: req.params }) | ||
19 | |||
20 | if (areValidationErrors(req, res)) return | ||
21 | |||
22 | const isAccount = await doesLocalAccountNameExist(req.params.actorName, res, false) | ||
23 | const isVideoChannel = await doesLocalVideoChannelNameExist(req.params.actorName, res, false) | ||
24 | |||
25 | if (!isAccount || !isVideoChannel) { | ||
26 | res.status(HttpStatusCode.NOT_FOUND_404) | ||
27 | .json({ error: 'Actor not found' }) | ||
28 | } | ||
29 | |||
30 | return next() | ||
31 | } | ||
32 | ] | ||
33 | |||
34 | const actorNameWithHostGetValidator = [ | ||
35 | param('actorName').exists().withMessage('Should have an actor name with host'), | ||
36 | |||
37 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
38 | logger.debug('Checking actorNameWithHostGetValidator parameters', { parameters: req.params }) | ||
39 | |||
40 | if (areValidationErrors(req, res)) return | ||
41 | |||
42 | const isAccount = await doesAccountNameWithHostExist(req.params.actorName, res, false) | ||
43 | const isVideoChannel = await doesVideoChannelNameWithHostExist(req.params.actorName, res, false) | ||
44 | |||
45 | if (!isAccount && !isVideoChannel) { | ||
46 | res.status(HttpStatusCode.NOT_FOUND_404) | ||
47 | .json({ error: 'Actor not found' }) | ||
48 | } | ||
49 | |||
50 | return next() | ||
51 | } | ||
52 | ] | ||
53 | |||
54 | // --------------------------------------------------------------------------- | ||
55 | |||
56 | export { | ||
57 | localActorValidator, | ||
58 | actorNameWithHostGetValidator | ||
59 | } | ||
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 @@ | |||
1 | export * from './abuse' | 1 | export * from './abuse' |
2 | export * from './account' | 2 | export * from './account' |
3 | export * from './actor' | ||
3 | export * from './actor-image' | 4 | export * from './actor-image' |
4 | export * from './blocklist' | 5 | export * from './blocklist' |
5 | export * from './oembed' | 6 | export * from './oembed' |