1 import * as express from 'express'
2 import { logger } from '../../helpers/logger'
3 import { areValidationErrors } from './utils'
4 import { isHostValid, isValidContactBody } from '../../helpers/custom-validators/servers'
5 import { ServerModel } from '../../models/server/server'
6 import { body } from 'express-validator'
7 import { isUserDisplayNameValid } from '../../helpers/custom-validators/users'
8 import { Redis } from '../../lib/redis'
9 import { CONFIG, isEmailEnabled } from '../../initializers/config'
11 const serverGetValidator = [
12 body('host').custom(isHostValid).withMessage('Should have a valid host'),
14 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
15 logger.debug('Checking serverGetValidator parameters', { parameters: req.body })
17 if (areValidationErrors(req, res)) return
19 const server = await ServerModel.loadByHost(req.body.host)
21 return res.status(404)
22 .send({ error: 'Server host not found.' })
26 res.locals.server = server
32 const contactAdministratorValidator = [
34 .custom(isUserDisplayNameValid).withMessage('Should have a valid name'),
36 .isEmail().withMessage('Should have a valid email'),
38 .custom(isValidContactBody).withMessage('Should have a valid body'),
40 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
41 logger.debug('Checking contactAdministratorValidator parameters', { parameters: req.body })
43 if (areValidationErrors(req, res)) return
45 if (CONFIG.CONTACT_FORM.ENABLED === false) {
48 .send({ error: 'Contact form is not enabled on this instance.' })
52 if (isEmailEnabled() === false) {
55 .send({ error: 'Emailer is not enabled on this instance.' })
59 if (await Redis.Instance.doesContactFormIpExist(req.ip)) {
60 logger.info('Refusing a contact form by %s: already sent one recently.', req.ip)
64 .send({ error: 'You already sent a contact form recently.' })
72 // ---------------------------------------------------------------------------
76 contactAdministratorValidator