1
0
Fork 0
peertube/server/middlewares/validators/server.ts

79 lines
2.4 KiB
TypeScript
Raw Normal View History

import * as express from 'express'
import { logger } from '../../helpers/logger'
import { areValidationErrors } from './utils'
2019-01-09 09:14:29 -05:00
import { isHostValid, isValidContactBody } from '../../helpers/custom-validators/servers'
import { ServerModel } from '../../models/server/server'
2019-07-25 10:23:44 -04:00
import { body } from 'express-validator'
2019-01-09 09:14:29 -05:00
import { isUserDisplayNameValid } from '../../helpers/custom-validators/users'
import { Emailer } from '../../lib/emailer'
import { Redis } from '../../lib/redis'
2019-04-11 05:33:44 -04:00
import { CONFIG } from '../../initializers/config'
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) {
return res.status(404)
.send({ error: 'Server host not found.' })
.end()
}
res.locals.server = server
return next()
}
]
2019-01-09 09:14:29 -05: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
.status(409)
.send({ error: 'Contact form is not enabled on this instance.' })
.end()
}
2019-01-10 05:12:41 -05:00
if (Emailer.isEnabled() === false) {
2019-01-09 09:14:29 -05:00
return res
.status(409)
.send({ error: 'Emailer is not enabled on this instance.' })
.end()
}
2019-03-19 04:26:50 -04:00
if (await Redis.Instance.doesContactFormIpExist(req.ip)) {
2019-01-09 09:14:29 -05:00
logger.info('Refusing a contact form by %s: already sent one recently.', req.ip)
return res
.status(403)
.send({ error: 'You already sent a contact form recently.' })
.end()
}
return next()
}
]
// ---------------------------------------------------------------------------
export {
2019-01-09 09:14:29 -05:00
serverGetValidator,
contactAdministratorValidator
}