diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-11-20 09:43:39 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-11-27 19:40:52 +0100 |
commit | 54141398354e6e7b94aa3065a705a1251390111c (patch) | |
tree | 8d30d1b9ea8acbe04f6d404125b04fc0c9897b70 /server/middlewares/validators | |
parent | eb8b27c93e61a896a08923dc1ca3c87ba8cf4948 (diff) | |
download | PeerTube-54141398354e6e7b94aa3065a705a1251390111c.tar.gz PeerTube-54141398354e6e7b94aa3065a705a1251390111c.tar.zst PeerTube-54141398354e6e7b94aa3065a705a1251390111c.zip |
Refractor activity pub lib/helpers
Diffstat (limited to 'server/middlewares/validators')
-rw-r--r-- | server/middlewares/validators/follows.ts | 62 | ||||
-rw-r--r-- | server/middlewares/validators/index.ts | 2 | ||||
-rw-r--r-- | server/middlewares/validators/servers.ts | 32 |
3 files changed, 63 insertions, 33 deletions
diff --git a/server/middlewares/validators/follows.ts b/server/middlewares/validators/follows.ts new file mode 100644 index 000000000..e22349726 --- /dev/null +++ b/server/middlewares/validators/follows.ts | |||
@@ -0,0 +1,62 @@ | |||
1 | import * as express from 'express' | ||
2 | import { body } from 'express-validator/check' | ||
3 | import { isTestInstance } from '../../helpers/core-utils' | ||
4 | import { isAccountIdValid } from '../../helpers/custom-validators/activitypub/account' | ||
5 | import { isEachUniqueHostValid } from '../../helpers/custom-validators/servers' | ||
6 | import { logger } from '../../helpers/logger' | ||
7 | import { CONFIG, database as db } from '../../initializers' | ||
8 | import { checkErrors } from './utils' | ||
9 | import { getServerAccount } from '../../helpers/utils' | ||
10 | |||
11 | const followValidator = [ | ||
12 | body('hosts').custom(isEachUniqueHostValid).withMessage('Should have an array of unique hosts'), | ||
13 | |||
14 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
15 | // Force https if the administrator wants to make friends | ||
16 | if (isTestInstance() === false && CONFIG.WEBSERVER.SCHEME === 'http') { | ||
17 | return res.status(400) | ||
18 | .json({ | ||
19 | error: 'Cannot follow non HTTPS web server.' | ||
20 | }) | ||
21 | .end() | ||
22 | } | ||
23 | |||
24 | logger.debug('Checking follow parameters', { parameters: req.body }) | ||
25 | |||
26 | checkErrors(req, res, next) | ||
27 | } | ||
28 | ] | ||
29 | |||
30 | const removeFollowingValidator = [ | ||
31 | body('accountId').custom(isAccountIdValid).withMessage('Should have a valid account id'), | ||
32 | |||
33 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
34 | logger.debug('Checking follow parameters', { parameters: req.body }) | ||
35 | |||
36 | checkErrors(req, res, async () => { | ||
37 | try { | ||
38 | const serverAccount = await getServerAccount() | ||
39 | const following = await db.AccountFollow.loadByAccountAndTarget(serverAccount.id, req.params.accountId) | ||
40 | |||
41 | if (!following) { | ||
42 | return res.status(404) | ||
43 | .end() | ||
44 | } | ||
45 | |||
46 | res.locals.following = following | ||
47 | |||
48 | return next() | ||
49 | } catch (err) { | ||
50 | logger.error('Error in remove following validator.', err) | ||
51 | return res.sendStatus(500) | ||
52 | } | ||
53 | }) | ||
54 | } | ||
55 | ] | ||
56 | |||
57 | // --------------------------------------------------------------------------- | ||
58 | |||
59 | export { | ||
60 | followValidator, | ||
61 | removeFollowingValidator | ||
62 | } | ||
diff --git a/server/middlewares/validators/index.ts b/server/middlewares/validators/index.ts index 3f5afe5b3..9840e8f65 100644 --- a/server/middlewares/validators/index.ts +++ b/server/middlewares/validators/index.ts | |||
@@ -2,7 +2,7 @@ export * from './account' | |||
2 | export * from './oembed' | 2 | export * from './oembed' |
3 | export * from './activitypub' | 3 | export * from './activitypub' |
4 | export * from './pagination' | 4 | export * from './pagination' |
5 | export * from './servers' | 5 | export * from './follows' |
6 | export * from './sort' | 6 | export * from './sort' |
7 | export * from './users' | 7 | export * from './users' |
8 | export * from './videos' | 8 | export * from './videos' |
diff --git a/server/middlewares/validators/servers.ts b/server/middlewares/validators/servers.ts deleted file mode 100644 index 95b69b789..000000000 --- a/server/middlewares/validators/servers.ts +++ /dev/null | |||
@@ -1,32 +0,0 @@ | |||
1 | import * as express from 'express' | ||
2 | import { body } from 'express-validator/check' | ||
3 | import { isEachUniqueHostValid } from '../../helpers/custom-validators/servers' | ||
4 | import { isTestInstance } from '../../helpers/core-utils' | ||
5 | import { CONFIG } from '../../initializers/constants' | ||
6 | import { logger } from '../../helpers/logger' | ||
7 | import { checkErrors } from './utils' | ||
8 | |||
9 | const followValidator = [ | ||
10 | body('hosts').custom(isEachUniqueHostValid).withMessage('Should have an array of unique hosts'), | ||
11 | |||
12 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
13 | // Force https if the administrator wants to make friends | ||
14 | if (isTestInstance() === false && CONFIG.WEBSERVER.SCHEME === 'http') { | ||
15 | return res.status(400) | ||
16 | .json({ | ||
17 | error: 'Cannot follow non HTTPS web server.' | ||
18 | }) | ||
19 | .end() | ||
20 | } | ||
21 | |||
22 | logger.debug('Checking follow parameters', { parameters: req.body }) | ||
23 | |||
24 | checkErrors(req, res, next) | ||
25 | } | ||
26 | ] | ||
27 | |||
28 | // --------------------------------------------------------------------------- | ||
29 | |||
30 | export { | ||
31 | followValidator | ||
32 | } | ||