]>
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' |
2d53be02 | 10 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' |
7ad9b984 C |
11 | |
12 | const serverGetValidator = [ | |
13 | body('host').custom(isHostValid).withMessage('Should have a valid host'), | |
14 | ||
15 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
16 | logger.debug('Checking serverGetValidator parameters', { parameters: req.body }) | |
17 | ||
18 | if (areValidationErrors(req, res)) return | |
19 | ||
20 | const server = await ServerModel.loadByHost(req.body.host) | |
21 | if (!server) { | |
2d53be02 RK |
22 | return res.status(HttpStatusCode.NOT_FOUND_404) |
23 | .send({ error: 'Server host not found.' }) | |
24 | .end() | |
7ad9b984 C |
25 | } |
26 | ||
27 | res.locals.server = server | |
28 | ||
29 | return next() | |
30 | } | |
31 | ] | |
32 | ||
a4101923 C |
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 | |
2d53be02 | 48 | .status(HttpStatusCode.CONFLICT_409) |
a4101923 C |
49 | .send({ error: 'Contact form is not enabled on this instance.' }) |
50 | .end() | |
51 | } | |
52 | ||
4c1c1709 | 53 | if (isEmailEnabled() === false) { |
a4101923 | 54 | return res |
2d53be02 | 55 | .status(HttpStatusCode.CONFLICT_409) |
a4101923 C |
56 | .send({ error: 'Emailer is not enabled on this instance.' }) |
57 | .end() | |
58 | } | |
59 | ||
0f6acda1 | 60 | if (await Redis.Instance.doesContactFormIpExist(req.ip)) { |
a4101923 C |
61 | logger.info('Refusing a contact form by %s: already sent one recently.', req.ip) |
62 | ||
63 | return res | |
2d53be02 | 64 | .status(HttpStatusCode.FORBIDDEN_403) |
a4101923 C |
65 | .send({ error: 'You already sent a contact form recently.' }) |
66 | .end() | |
67 | } | |
68 | ||
69 | return next() | |
70 | } | |
71 | ] | |
72 | ||
7ad9b984 C |
73 | // --------------------------------------------------------------------------- |
74 | ||
75 | export { | |
a4101923 C |
76 | serverGetValidator, |
77 | contactAdministratorValidator | |
7ad9b984 | 78 | } |