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 if (areValidationErrors(req, res)) return
18 const server = await ServerModel.loadByHost(req.body.host)
21 status: HttpStatusCode.NOT_FOUND_404,
22 message: 'Server host not found.'
26 res.locals.server = server
32 const contactAdministratorValidator = [
34 .custom(isUserDisplayNameValid),
38 .custom(isValidContactBody),
40 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
41 if (areValidationErrors(req, res)) return
43 if (CONFIG.CONTACT_FORM.ENABLED === false) {
45 status: HttpStatusCode.CONFLICT_409,
46 message: 'Contact form is not enabled on this instance.'
50 if (isEmailEnabled() === false) {
52 status: HttpStatusCode.CONFLICT_409,
53 message: 'Emailer is not enabled on this instance.'
57 if (await Redis.Instance.doesContactFormIpExist(req.ip)) {
58 logger.info('Refusing a contact form by %s: already sent one recently.', req.ip)
61 status: HttpStatusCode.FORBIDDEN_403,
62 message: 'You already sent a contact form recently.'
70 // ---------------------------------------------------------------------------
74 contactAdministratorValidator