allow limiting video-comments rss feeds to an account or video channel
[oweals/peertube.git] / server / middlewares / validators / server.ts
1 import * as express from 'express'
2 import { logger } from '../../helpers/logger'
3 import { areValidationErrors } from './utils'
4 import { isHostValid, isValidContactBody } from '../../helpers/custom-validators/servers'
5 import { ServerModel } from '../../models/server/server'
6 import { body } from 'express-validator'
7 import { isUserDisplayNameValid } from '../../helpers/custom-validators/users'
8 import { Redis } from '../../lib/redis'
9 import { CONFIG, isEmailEnabled } from '../../initializers/config'
10
11 const serverGetValidator = [
12   body('host').custom(isHostValid).withMessage('Should have a valid host'),
13
14   async (req: express.Request, res: express.Response, next: express.NextFunction) => {
15     logger.debug('Checking serverGetValidator parameters', { parameters: req.body })
16
17     if (areValidationErrors(req, res)) return
18
19     const server = await ServerModel.loadByHost(req.body.host)
20     if (!server) {
21       return res.status(404)
22          .send({ error: 'Server host not found.' })
23          .end()
24     }
25
26     res.locals.server = server
27
28     return next()
29   }
30 ]
31
32 const contactAdministratorValidator = [
33   body('fromName')
34     .custom(isUserDisplayNameValid).withMessage('Should have a valid name'),
35   body('fromEmail')
36     .isEmail().withMessage('Should have a valid email'),
37   body('body')
38     .custom(isValidContactBody).withMessage('Should have a valid body'),
39
40   async (req: express.Request, res: express.Response, next: express.NextFunction) => {
41     logger.debug('Checking contactAdministratorValidator parameters', { parameters: req.body })
42
43     if (areValidationErrors(req, res)) return
44
45     if (CONFIG.CONTACT_FORM.ENABLED === false) {
46       return res
47         .status(409)
48         .send({ error: 'Contact form is not enabled on this instance.' })
49         .end()
50     }
51
52     if (isEmailEnabled() === false) {
53       return res
54         .status(409)
55         .send({ error: 'Emailer is not enabled on this instance.' })
56         .end()
57     }
58
59     if (await Redis.Instance.doesContactFormIpExist(req.ip)) {
60       logger.info('Refusing a contact form by %s: already sent one recently.', req.ip)
61
62       return res
63         .status(403)
64         .send({ error: 'You already sent a contact form recently.' })
65         .end()
66     }
67
68     return next()
69   }
70 ]
71
72 // ---------------------------------------------------------------------------
73
74 export {
75   serverGetValidator,
76   contactAdministratorValidator
77 }