]>
Commit | Line | Data |
---|---|---|
1 | import express from 'express' | |
2 | import { body } from 'express-validator' | |
3 | import { HttpStatusCode } from '../../../shared/models/http/http-error-codes' | |
4 | import { isHostValid, isValidContactBody } from '../../helpers/custom-validators/servers' | |
5 | import { isUserDisplayNameValid } from '../../helpers/custom-validators/users' | |
6 | import { logger } from '../../helpers/logger' | |
7 | import { CONFIG, isEmailEnabled } from '../../initializers/config' | |
8 | import { Redis } from '../../lib/redis' | |
9 | import { ServerModel } from '../../models/server/server' | |
10 | import { areValidationErrors } from './shared' | |
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) { | |
22 | return res.fail({ | |
23 | status: HttpStatusCode.NOT_FOUND_404, | |
24 | message: 'Server host not found.' | |
25 | }) | |
26 | } | |
27 | ||
28 | res.locals.server = server | |
29 | ||
30 | return next() | |
31 | } | |
32 | ] | |
33 | ||
34 | const contactAdministratorValidator = [ | |
35 | body('fromName') | |
36 | .custom(isUserDisplayNameValid).withMessage('Should have a valid name'), | |
37 | body('fromEmail') | |
38 | .isEmail().withMessage('Should have a valid email'), | |
39 | body('body') | |
40 | .custom(isValidContactBody).withMessage('Should have a valid body'), | |
41 | ||
42 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
43 | logger.debug('Checking contactAdministratorValidator parameters', { parameters: req.body }) | |
44 | ||
45 | if (areValidationErrors(req, res)) return | |
46 | ||
47 | if (CONFIG.CONTACT_FORM.ENABLED === false) { | |
48 | return res.fail({ | |
49 | status: HttpStatusCode.CONFLICT_409, | |
50 | message: 'Contact form is not enabled on this instance.' | |
51 | }) | |
52 | } | |
53 | ||
54 | if (isEmailEnabled() === false) { | |
55 | return res.fail({ | |
56 | status: HttpStatusCode.CONFLICT_409, | |
57 | message: 'Emailer is not enabled on this instance.' | |
58 | }) | |
59 | } | |
60 | ||
61 | if (await Redis.Instance.doesContactFormIpExist(req.ip)) { | |
62 | logger.info('Refusing a contact form by %s: already sent one recently.', req.ip) | |
63 | ||
64 | return res.fail({ | |
65 | status: HttpStatusCode.FORBIDDEN_403, | |
66 | message: 'You already sent a contact form recently.' | |
67 | }) | |
68 | } | |
69 | ||
70 | return next() | |
71 | } | |
72 | ] | |
73 | ||
74 | // --------------------------------------------------------------------------- | |
75 | ||
76 | export { | |
77 | serverGetValidator, | |
78 | contactAdministratorValidator | |
79 | } |