diff options
author | Chocobozzz <me@florianbigard.com> | 2019-01-09 15:14:29 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2019-01-10 11:32:37 +0100 |
commit | a4101923e699e49ceb9ff36e971c75417fafc9f0 (patch) | |
tree | c098a87ac5a85e1bc7454facbb59ecbd6c7dac82 /server/middlewares/validators/server.ts | |
parent | 8d00889b6038c38d9c86cbeca88a9f3c23962c48 (diff) | |
download | PeerTube-a4101923e699e49ceb9ff36e971c75417fafc9f0.tar.gz PeerTube-a4101923e699e49ceb9ff36e971c75417fafc9f0.tar.zst PeerTube-a4101923e699e49ceb9ff36e971c75417fafc9f0.zip |
Implement contact form on server side
Diffstat (limited to 'server/middlewares/validators/server.ts')
-rw-r--r-- | server/middlewares/validators/server.ts | 49 |
1 files changed, 47 insertions, 2 deletions
diff --git a/server/middlewares/validators/server.ts b/server/middlewares/validators/server.ts index a491dfeb3..d82e19230 100644 --- a/server/middlewares/validators/server.ts +++ b/server/middlewares/validators/server.ts | |||
@@ -1,9 +1,13 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | import { logger } from '../../helpers/logger' | 2 | import { logger } from '../../helpers/logger' |
3 | import { areValidationErrors } from './utils' | 3 | import { areValidationErrors } from './utils' |
4 | import { isHostValid } from '../../helpers/custom-validators/servers' | 4 | import { isHostValid, isValidContactBody } from '../../helpers/custom-validators/servers' |
5 | import { ServerModel } from '../../models/server/server' | 5 | import { ServerModel } from '../../models/server/server' |
6 | import { body } from 'express-validator/check' | 6 | import { body } from 'express-validator/check' |
7 | import { isUserDisplayNameValid } from '../../helpers/custom-validators/users' | ||
8 | import { Emailer } from '../../lib/emailer' | ||
9 | import { Redis } from '../../lib/redis' | ||
10 | import { CONFIG } from '../../initializers/constants' | ||
7 | 11 | ||
8 | const serverGetValidator = [ | 12 | const serverGetValidator = [ |
9 | body('host').custom(isHostValid).withMessage('Should have a valid host'), | 13 | body('host').custom(isHostValid).withMessage('Should have a valid host'), |
@@ -26,8 +30,49 @@ const serverGetValidator = [ | |||
26 | } | 30 | } |
27 | ] | 31 | ] |
28 | 32 | ||
33 | const contactAdministratorValidator = [ | ||
34 | body('fromName') | ||
35 | .custom(isUserDisplayNameValid).withMessage('Should have a valid name'), | ||
36 | body('fromEmail') | ||
37 | .isEmail().withMessage('Should have a valid email'), | ||
38 | body('body') | ||
39 | .custom(isValidContactBody).withMessage('Should have a valid body'), | ||
40 | |||
41 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
42 | logger.debug('Checking contactAdministratorValidator parameters', { parameters: req.body }) | ||
43 | |||
44 | if (areValidationErrors(req, res)) return | ||
45 | |||
46 | if (CONFIG.CONTACT_FORM.ENABLED === false) { | ||
47 | return res | ||
48 | .status(409) | ||
49 | .send({ error: 'Contact form is not enabled on this instance.' }) | ||
50 | .end() | ||
51 | } | ||
52 | |||
53 | if (Emailer.Instance.isEnabled() === false) { | ||
54 | return res | ||
55 | .status(409) | ||
56 | .send({ error: 'Emailer is not enabled on this instance.' }) | ||
57 | .end() | ||
58 | } | ||
59 | |||
60 | if (await Redis.Instance.isContactFormIpExists(req.ip)) { | ||
61 | logger.info('Refusing a contact form by %s: already sent one recently.', req.ip) | ||
62 | |||
63 | return res | ||
64 | .status(403) | ||
65 | .send({ error: 'You already sent a contact form recently.' }) | ||
66 | .end() | ||
67 | } | ||
68 | |||
69 | return next() | ||
70 | } | ||
71 | ] | ||
72 | |||
29 | // --------------------------------------------------------------------------- | 73 | // --------------------------------------------------------------------------- |
30 | 74 | ||
31 | export { | 75 | export { |
32 | serverGetValidator | 76 | serverGetValidator, |
77 | contactAdministratorValidator | ||
33 | } | 78 | } |