]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/logs.ts
Fix filters on playlists
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / logs.ts
1 import express from 'express'
2 import { body, query } from 'express-validator'
3 import { isUrlValid } from '@server/helpers/custom-validators/activitypub/misc'
4 import { isStringArray } from '@server/helpers/custom-validators/search'
5 import { CONFIG } from '@server/initializers/config'
6 import { arrayify } from '@shared/core-utils'
7 import { HttpStatusCode } from '@shared/models'
8 import {
9 isValidClientLogLevel,
10 isValidClientLogMessage,
11 isValidClientLogMeta,
12 isValidClientLogStackTrace,
13 isValidClientLogUserAgent,
14 isValidLogLevel
15 } from '../../helpers/custom-validators/logs'
16 import { isDateValid } from '../../helpers/custom-validators/misc'
17 import { areValidationErrors } from './shared'
18
19 const createClientLogValidator = [
20 body('message')
21 .custom(isValidClientLogMessage),
22
23 body('url')
24 .custom(isUrlValid),
25
26 body('level')
27 .custom(isValidClientLogLevel),
28
29 body('stackTrace')
30 .optional()
31 .custom(isValidClientLogStackTrace),
32
33 body('meta')
34 .optional()
35 .custom(isValidClientLogMeta),
36
37 body('userAgent')
38 .optional()
39 .custom(isValidClientLogUserAgent),
40
41 (req: express.Request, res: express.Response, next: express.NextFunction) => {
42 if (CONFIG.LOG.ACCEPT_CLIENT_LOG !== true) {
43 return res.sendStatus(HttpStatusCode.FORBIDDEN_403)
44 }
45
46 if (areValidationErrors(req, res)) return
47
48 return next()
49 }
50 ]
51
52 const getLogsValidator = [
53 query('startDate')
54 .custom(isDateValid).withMessage('Should have a start date that conforms to ISO 8601'),
55 query('level')
56 .optional()
57 .custom(isValidLogLevel),
58 query('tagsOneOf')
59 .optional()
60 .customSanitizer(arrayify)
61 .custom(isStringArray).withMessage('Should have a valid one of tags array'),
62 query('endDate')
63 .optional()
64 .custom(isDateValid).withMessage('Should have an end date that conforms to ISO 8601'),
65
66 (req: express.Request, res: express.Response, next: express.NextFunction) => {
67 if (areValidationErrors(req, res)) return
68
69 return next()
70 }
71 ]
72
73 const getAuditLogsValidator = [
74 query('startDate')
75 .custom(isDateValid).withMessage('Should have a start date that conforms to ISO 8601'),
76 query('endDate')
77 .optional()
78 .custom(isDateValid).withMessage('Should have a end date that conforms to ISO 8601'),
79
80 (req: express.Request, res: express.Response, next: express.NextFunction) => {
81 if (areValidationErrors(req, res)) return
82
83 return next()
84 }
85 ]
86
87 // ---------------------------------------------------------------------------
88
89 export {
90 getLogsValidator,
91 getAuditLogsValidator,
92 createClientLogValidator
93 }