]>
Commit | Line | Data |
---|---|---|
7ad9b984 C |
1 | import * as express from 'express' |
2 | import { logger } from '../../helpers/logger' | |
3 | import { areValidationErrors } from './utils' | |
a4101923 | 4 | import { isHostValid, isValidContactBody } from '../../helpers/custom-validators/servers' |
7ad9b984 | 5 | import { ServerModel } from '../../models/server/server' |
c8861d5d | 6 | import { body } from 'express-validator' |
a4101923 | 7 | import { isUserDisplayNameValid } from '../../helpers/custom-validators/users' |
a4101923 | 8 | import { Redis } from '../../lib/redis' |
4c1c1709 | 9 | import { CONFIG, isEmailEnabled } from '../../initializers/config' |
7ad9b984 C |
10 | |
11 | const serverGetValidator = [ | |
12 | body('host').custom(isHostValid).withMessage('Should have a valid host'), | |
13 | ||
14 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
15 | logger.debug('Checking serverGetValidator parameters', { parameters: req.body }) | |
16 | ||
17 | if (areValidationErrors(req, res)) return | |
18 | ||
19 | const server = await ServerModel.loadByHost(req.body.host) | |
20 | if (!server) { | |
21 | return res.status(404) | |
22 | .send({ error: 'Server host not found.' }) | |
23 | .end() | |
24 | } | |
25 | ||
26 | res.locals.server = server | |
27 | ||
28 | return next() | |
29 | } | |
30 | ] | |
31 | ||
a4101923 C |
32 | const contactAdministratorValidator = [ |
33 | body('fromName') | |
34 | .custom(isUserDisplayNameValid).withMessage('Should have a valid name'), | |
35 | body('fromEmail') | |
36 | .isEmail().withMessage('Should have a valid email'), | |
37 | body('body') | |
38 | .custom(isValidContactBody).withMessage('Should have a valid body'), | |
39 | ||
40 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
41 | logger.debug('Checking contactAdministratorValidator parameters', { parameters: req.body }) | |
42 | ||
43 | if (areValidationErrors(req, res)) return | |
44 | ||
45 | if (CONFIG.CONTACT_FORM.ENABLED === false) { | |
46 | return res | |
47 | .status(409) | |
48 | .send({ error: 'Contact form is not enabled on this instance.' }) | |
49 | .end() | |
50 | } | |
51 | ||
4c1c1709 | 52 | if (isEmailEnabled() === false) { |
a4101923 C |
53 | return res |
54 | .status(409) | |
55 | .send({ error: 'Emailer is not enabled on this instance.' }) | |
56 | .end() | |
57 | } | |
58 | ||
0f6acda1 | 59 | if (await Redis.Instance.doesContactFormIpExist(req.ip)) { |
a4101923 C |
60 | logger.info('Refusing a contact form by %s: already sent one recently.', req.ip) |
61 | ||
62 | return res | |
63 | .status(403) | |
64 | .send({ error: 'You already sent a contact form recently.' }) | |
65 | .end() | |
66 | } | |
67 | ||
68 | return next() | |
69 | } | |
70 | ] | |
71 | ||
7ad9b984 C |
72 | // --------------------------------------------------------------------------- |
73 | ||
74 | export { | |
a4101923 C |
75 | serverGetValidator, |
76 | contactAdministratorValidator | |
7ad9b984 | 77 | } |