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'
12 const serverGetValidator = [
13 body('host').custom(isHostValid).withMessage('Should have a valid host'),
15 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
16 logger.debug('Checking serverGetValidator parameters', { parameters: req.body })
18 if (areValidationErrors(req, res)) return
20 const server = await ServerModel.loadByHost(req.body.host)
23 status: HttpStatusCode.NOT_FOUND_404,
24 message: 'Server host not found.'
28 res.locals.server = server
34 const contactAdministratorValidator = [
36 .custom(isUserDisplayNameValid).withMessage('Should have a valid name'),
38 .isEmail().withMessage('Should have a valid email'),
40 .custom(isValidContactBody).withMessage('Should have a valid body'),
42 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
43 logger.debug('Checking contactAdministratorValidator parameters', { parameters: req.body })
45 if (areValidationErrors(req, res)) return
47 if (CONFIG.CONTACT_FORM.ENABLED === false) {
49 status: HttpStatusCode.CONFLICT_409,
50 message: 'Contact form is not enabled on this instance.'
54 if (isEmailEnabled() === false) {
56 status: HttpStatusCode.CONFLICT_409,
57 message: 'Emailer is not enabled on this instance.'
61 if (await Redis.Instance.doesContactFormIpExist(req.ip)) {
62 logger.info('Refusing a contact form by %s: already sent one recently.', req.ip)
65 status: HttpStatusCode.FORBIDDEN_403,
66 message: 'You already sent a contact form recently.'
74 // ---------------------------------------------------------------------------
78 contactAdministratorValidator