]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/server.ts
Add ability to list imports of a channel sync
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / server.ts
CommitLineData
41fb13c3 1import express from 'express'
c8861d5d 2import { body } from 'express-validator'
c0e8b12e 3import { HttpStatusCode } from '../../../shared/models/http/http-error-codes'
10363c74 4import { isHostValid, isValidContactBody } from '../../helpers/custom-validators/servers'
a4101923 5import { isUserDisplayNameValid } from '../../helpers/custom-validators/users'
10363c74 6import { logger } from '../../helpers/logger'
4c1c1709 7import { CONFIG, isEmailEnabled } from '../../initializers/config'
10363c74
C
8import { Redis } from '../../lib/redis'
9import { ServerModel } from '../../models/server/server'
10import { areValidationErrors } from './shared'
7ad9b984
C
11
12const serverGetValidator = [
13 body('host').custom(isHostValid).withMessage('Should have a valid host'),
14
15 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
16 logger.debug('Checking serverGetValidator parameters', { parameters: req.body })
17
18 if (areValidationErrors(req, res)) return
19
20 const server = await ServerModel.loadByHost(req.body.host)
21 if (!server) {
76148b27
RK
22 return res.fail({
23 status: HttpStatusCode.NOT_FOUND_404,
24 message: 'Server host not found.'
25 })
7ad9b984
C
26 }
27
28 res.locals.server = server
29
30 return next()
31 }
32]
33
a4101923
C
34const contactAdministratorValidator = [
35 body('fromName')
36 .custom(isUserDisplayNameValid).withMessage('Should have a valid name'),
37 body('fromEmail')
38 .isEmail().withMessage('Should have a valid email'),
39 body('body')
40 .custom(isValidContactBody).withMessage('Should have a valid body'),
41
42 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
43 logger.debug('Checking contactAdministratorValidator parameters', { parameters: req.body })
44
45 if (areValidationErrors(req, res)) return
46
47 if (CONFIG.CONTACT_FORM.ENABLED === false) {
76148b27
RK
48 return res.fail({
49 status: HttpStatusCode.CONFLICT_409,
50 message: 'Contact form is not enabled on this instance.'
51 })
a4101923
C
52 }
53
4c1c1709 54 if (isEmailEnabled() === false) {
76148b27
RK
55 return res.fail({
56 status: HttpStatusCode.CONFLICT_409,
57 message: 'Emailer is not enabled on this instance.'
58 })
a4101923
C
59 }
60
0f6acda1 61 if (await Redis.Instance.doesContactFormIpExist(req.ip)) {
a4101923
C
62 logger.info('Refusing a contact form by %s: already sent one recently.', req.ip)
63
76148b27
RK
64 return res.fail({
65 status: HttpStatusCode.FORBIDDEN_403,
66 message: 'You already sent a contact form recently.'
67 })
a4101923
C
68 }
69
70 return next()
71 }
72]
73
7ad9b984
C
74// ---------------------------------------------------------------------------
75
76export {
a4101923
C
77 serverGetValidator,
78 contactAdministratorValidator
7ad9b984 79}