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