]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/logs.ts
Translated using Weblate (Persian)
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / logs.ts
CommitLineData
41fb13c3 1import express from 'express'
42b40636
C
2import { body, query } from 'express-validator'
3import { isUrlValid } from '@server/helpers/custom-validators/activitypub/misc'
64553e88 4import { isStringArray } from '@server/helpers/custom-validators/search'
42b40636 5import { CONFIG } from '@server/initializers/config'
86347717 6import { arrayify } from '@shared/core-utils'
42b40636
C
7import { HttpStatusCode } from '@shared/models'
8import {
9 isValidClientLogLevel,
10 isValidClientLogMessage,
11 isValidClientLogMeta,
12 isValidClientLogStackTrace,
13 isValidClientLogUserAgent,
14 isValidLogLevel
15} from '../../helpers/custom-validators/logs'
86347717 16import { isDateValid } from '../../helpers/custom-validators/misc'
10363c74 17import { areValidationErrors } from './shared'
fd8710b8 18
42b40636
C
19const createClientLogValidator = [
20 body('message')
396f6f01 21 .custom(isValidClientLogMessage),
42b40636
C
22
23 body('url')
396f6f01 24 .custom(isUrlValid),
42b40636
C
25
26 body('level')
396f6f01 27 .custom(isValidClientLogLevel),
42b40636
C
28
29 body('stackTrace')
30 .optional()
396f6f01 31 .custom(isValidClientLogStackTrace),
42b40636
C
32
33 body('meta')
34 .optional()
396f6f01 35 .custom(isValidClientLogMeta),
42b40636
C
36
37 body('userAgent')
38 .optional()
396f6f01 39 .custom(isValidClientLogUserAgent),
42b40636
C
40
41 (req: express.Request, res: express.Response, next: express.NextFunction) => {
42b40636
C
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
fd8710b8
C
52const getLogsValidator = [
53 query('startDate')
70330f63 54 .custom(isDateValid).withMessage('Should have a start date that conforms to ISO 8601'),
fd8710b8
C
55 query('level')
56 .optional()
396f6f01 57 .custom(isValidLogLevel),
64553e88
C
58 query('tagsOneOf')
59 .optional()
86347717 60 .customSanitizer(arrayify)
64553e88 61 .custom(isStringArray).withMessage('Should have a valid one of tags array'),
fd8710b8
C
62 query('endDate')
63 .optional()
70330f63 64 .custom(isDateValid).withMessage('Should have an end date that conforms to ISO 8601'),
fd8710b8
C
65
66 (req: express.Request, res: express.Response, next: express.NextFunction) => {
fd8710b8
C
67 if (areValidationErrors(req, res)) return
68
69 return next()
70 }
71]
72
566c125d
C
73const getAuditLogsValidator = [
74 query('startDate')
70330f63 75 .custom(isDateValid).withMessage('Should have a start date that conforms to ISO 8601'),
566c125d
C
76 query('endDate')
77 .optional()
70330f63 78 .custom(isDateValid).withMessage('Should have a end date that conforms to ISO 8601'),
566c125d
C
79
80 (req: express.Request, res: express.Response, next: express.NextFunction) => {
566c125d
C
81 if (areValidationErrors(req, res)) return
82
83 return next()
84 }
85]
86
fd8710b8
C
87// ---------------------------------------------------------------------------
88
89export {
566c125d 90 getLogsValidator,
42b40636
C
91 getAuditLogsValidator,
92 createClientLogValidator
fd8710b8 93}