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

76 lines
2.2 KiB
TypeScript
Raw Normal View History

2021-08-27 08:32:44 -04:00
import express from 'express'
2019-07-25 10:23:44 -04:00
import { body } from 'express-validator'
2021-07-16 04:42:24 -04:00
import { HttpStatusCode } from '../../../shared/models/http/http-error-codes'
import { isHostValid, isValidContactBody } from '../../helpers/custom-validators/servers'
2019-01-09 09:14:29 -05:00
import { isUserDisplayNameValid } from '../../helpers/custom-validators/users'
import { logger } from '../../helpers/logger'
2020-02-17 04:27:00 -05:00
import { CONFIG, isEmailEnabled } from '../../initializers/config'
import { Redis } from '../../lib/redis'
import { ServerModel } from '../../models/server/server'
import { areValidationErrors } from './shared'
const serverGetValidator = [
body('host').custom(isHostValid).withMessage('Should have a valid host'),
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
if (areValidationErrors(req, res)) return
const server = await ServerModel.loadByHost(req.body.host)
if (!server) {
return res.fail({
status: HttpStatusCode.NOT_FOUND_404,
message: 'Server host not found.'
})
}
res.locals.server = server
return next()
}
]
2019-01-09 09:14:29 -05:00
const contactAdministratorValidator = [
body('fromName')
.custom(isUserDisplayNameValid),
2019-01-09 09:14:29 -05:00
body('fromEmail')
.isEmail(),
2019-01-09 09:14:29 -05:00
body('body')
.custom(isValidContactBody),
2019-01-09 09:14:29 -05:00
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
if (areValidationErrors(req, res)) return
if (CONFIG.CONTACT_FORM.ENABLED === false) {
return res.fail({
status: HttpStatusCode.CONFLICT_409,
message: 'Contact form is not enabled on this instance.'
})
2019-01-09 09:14:29 -05:00
}
2020-02-17 04:27:00 -05:00
if (isEmailEnabled() === false) {
return res.fail({
status: HttpStatusCode.CONFLICT_409,
message: 'Emailer is not enabled on this instance.'
})
2019-01-09 09:14:29 -05:00
}
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.fail({
status: HttpStatusCode.FORBIDDEN_403,
message: 'You already sent a contact form recently.'
})
2019-01-09 09:14:29 -05:00
}
return next()
}
]
// ---------------------------------------------------------------------------
export {
2019-01-09 09:14:29 -05:00
serverGetValidator,
contactAdministratorValidator
}