2018-10-12 15:26:04 +02:00
|
|
|
import * as express from 'express'
|
|
|
|
import { logger } from '../../helpers/logger'
|
|
|
|
import { areValidationErrors } from './utils'
|
2019-01-09 15:14:29 +01:00
|
|
|
import { isHostValid, isValidContactBody } from '../../helpers/custom-validators/servers'
|
2018-10-12 15:26:04 +02:00
|
|
|
import { ServerModel } from '../../models/server/server'
|
2019-07-25 16:23:44 +02:00
|
|
|
import { body } from 'express-validator'
|
2019-01-09 15:14:29 +01:00
|
|
|
import { isUserDisplayNameValid } from '../../helpers/custom-validators/users'
|
|
|
|
import { Redis } from '../../lib/redis'
|
2020-02-17 10:27:00 +01:00
|
|
|
import { CONFIG, isEmailEnabled } from '../../initializers/config'
|
2020-12-07 14:32:36 +01:00
|
|
|
import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
|
2018-10-12 15:26:04 +02:00
|
|
|
|
|
|
|
const serverGetValidator = [
|
|
|
|
body('host').custom(isHostValid).withMessage('Should have a valid host'),
|
|
|
|
|
|
|
|
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
logger.debug('Checking serverGetValidator parameters', { parameters: req.body })
|
|
|
|
|
|
|
|
if (areValidationErrors(req, res)) return
|
|
|
|
|
|
|
|
const server = await ServerModel.loadByHost(req.body.host)
|
|
|
|
if (!server) {
|
2020-12-07 14:32:36 +01:00
|
|
|
return res.status(HttpStatusCode.NOT_FOUND_404)
|
|
|
|
.send({ error: 'Server host not found.' })
|
|
|
|
.end()
|
2018-10-12 15:26:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
res.locals.server = server
|
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2019-01-09 15:14:29 +01:00
|
|
|
const contactAdministratorValidator = [
|
|
|
|
body('fromName')
|
|
|
|
.custom(isUserDisplayNameValid).withMessage('Should have a valid name'),
|
|
|
|
body('fromEmail')
|
|
|
|
.isEmail().withMessage('Should have a valid email'),
|
|
|
|
body('body')
|
|
|
|
.custom(isValidContactBody).withMessage('Should have a valid body'),
|
|
|
|
|
|
|
|
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
logger.debug('Checking contactAdministratorValidator parameters', { parameters: req.body })
|
|
|
|
|
|
|
|
if (areValidationErrors(req, res)) return
|
|
|
|
|
|
|
|
if (CONFIG.CONTACT_FORM.ENABLED === false) {
|
|
|
|
return res
|
2020-12-07 14:32:36 +01:00
|
|
|
.status(HttpStatusCode.CONFLICT_409)
|
2019-01-09 15:14:29 +01:00
|
|
|
.send({ error: 'Contact form is not enabled on this instance.' })
|
|
|
|
.end()
|
|
|
|
}
|
|
|
|
|
2020-02-17 10:27:00 +01:00
|
|
|
if (isEmailEnabled() === false) {
|
2019-01-09 15:14:29 +01:00
|
|
|
return res
|
2020-12-07 14:32:36 +01:00
|
|
|
.status(HttpStatusCode.CONFLICT_409)
|
2019-01-09 15:14:29 +01:00
|
|
|
.send({ error: 'Emailer is not enabled on this instance.' })
|
|
|
|
.end()
|
|
|
|
}
|
|
|
|
|
2019-03-19 09:26:50 +01:00
|
|
|
if (await Redis.Instance.doesContactFormIpExist(req.ip)) {
|
2019-01-09 15:14:29 +01:00
|
|
|
logger.info('Refusing a contact form by %s: already sent one recently.', req.ip)
|
|
|
|
|
|
|
|
return res
|
2020-12-07 14:32:36 +01:00
|
|
|
.status(HttpStatusCode.FORBIDDEN_403)
|
2019-01-09 15:14:29 +01:00
|
|
|
.send({ error: 'You already sent a contact form recently.' })
|
|
|
|
.end()
|
|
|
|
}
|
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2018-10-12 15:26:04 +02:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
2019-01-09 15:14:29 +01:00
|
|
|
serverGetValidator,
|
|
|
|
contactAdministratorValidator
|
2018-10-12 15:26:04 +02:00
|
|
|
}
|