]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/video-channels.ts
Decrease log file max size
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / video-channels.ts
CommitLineData
72c7248b 1import * as express from 'express'
d4f1e94c
C
2import { body, param } from 'express-validator/check'
3import { UserRight } from '../../../shared'
cf7a61b5 4import { isAccountNameWithHostExist } from '../../helpers/custom-validators/accounts'
4e50b6a1 5import {
06a05d5f 6 isLocalVideoChannelNameExist,
7d8e778a 7 isVideoChannelDescriptionValid,
7d8e778a 8 isVideoChannelNameValid,
8a19bee1 9 isVideoChannelNameWithHostExist,
7d8e778a 10 isVideoChannelSupportValid
4e50b6a1 11} from '../../helpers/custom-validators/video-channels'
da854ddd 12import { logger } from '../../helpers/logger'
3fd3ab2d
C
13import { UserModel } from '../../models/account/user'
14import { VideoChannelModel } from '../../models/video/video-channel'
a2431b7d 15import { areValidationErrors } from './utils'
8a19bee1 16import { isActorPreferredUsernameValid } from '../../helpers/custom-validators/activitypub/actor'
72c7248b 17
38fa2065 18const listVideoAccountChannelsValidator = [
ad9e39fb 19 param('accountName').exists().withMessage('Should have a valid account name'),
72c7248b 20
a2431b7d 21 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
38fa2065 22 logger.debug('Checking listVideoAccountChannelsValidator parameters', { parameters: req.body })
72c7248b 23
a2431b7d 24 if (areValidationErrors(req, res)) return
ad9e39fb 25 if (!await isAccountNameWithHostExist(req.params.accountName, res)) return
a2431b7d
C
26
27 return next()
72c7248b
C
28 }
29]
30
31const videoChannelsAddValidator = [
8a19bee1 32 body('name').custom(isActorPreferredUsernameValid).withMessage('Should have a valid channel name'),
08c1efbe 33 body('displayName').custom(isVideoChannelNameValid).withMessage('Should have a valid display name'),
2422c46b
C
34 body('description').optional().custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'),
35 body('support').optional().custom(isVideoChannelSupportValid).withMessage('Should have a valid support text'),
72c7248b
C
36
37 (req: express.Request, res: express.Response, next: express.NextFunction) => {
38 logger.debug('Checking videoChannelsAdd parameters', { parameters: req.body })
39
a2431b7d
C
40 if (areValidationErrors(req, res)) return
41
42 return next()
72c7248b
C
43 }
44]
45
46const videoChannelsUpdateValidator = [
8a19bee1 47 param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
08c1efbe 48 body('displayName').optional().custom(isVideoChannelNameValid).withMessage('Should have a valid display name'),
72c7248b 49 body('description').optional().custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'),
2422c46b 50 body('support').optional().custom(isVideoChannelSupportValid).withMessage('Should have a valid support text'),
72c7248b 51
a2431b7d 52 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
72c7248b
C
53 logger.debug('Checking videoChannelsUpdate parameters', { parameters: req.body })
54
a2431b7d 55 if (areValidationErrors(req, res)) return
8a19bee1 56 if (!await isVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
a2431b7d
C
57
58 // We need to make additional checks
d50acfab 59 if (res.locals.videoChannel.Actor.isOwned() === false) {
a2431b7d
C
60 return res.status(403)
61 .json({ error: 'Cannot update video channel of another server' })
62 .end()
63 }
64
65 if (res.locals.videoChannel.Account.userId !== res.locals.oauth.token.User.id) {
66 return res.status(403)
67 .json({ error: 'Cannot update video channel of another user' })
68 .end()
69 }
70
71 return next()
72c7248b
C
72 }
73]
74
75const videoChannelsRemoveValidator = [
8a19bee1 76 param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
72c7248b 77
a2431b7d 78 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
72c7248b
C
79 logger.debug('Checking videoChannelsRemove parameters', { parameters: req.params })
80
a2431b7d 81 if (areValidationErrors(req, res)) return
8a19bee1 82 if (!await isVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
a2431b7d 83
d48ff09d 84 if (!checkUserCanDeleteVideoChannel(res.locals.oauth.token.User, res.locals.videoChannel, res)) return
a2431b7d
C
85 if (!await checkVideoChannelIsNotTheLastOne(res)) return
86
87 return next()
72c7248b
C
88 }
89]
90
8a19bee1
C
91const videoChannelsNameWithHostValidator = [
92 param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
72c7248b 93
a2431b7d 94 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
8a19bee1 95 logger.debug('Checking videoChannelsNameWithHostValidator parameters', { parameters: req.params })
72c7248b 96
a2431b7d 97 if (areValidationErrors(req, res)) return
6b738c7a 98
8a19bee1 99 if (!await isVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
a2431b7d
C
100
101 return next()
72c7248b
C
102 }
103]
104
06a05d5f
C
105const localVideoChannelValidator = [
106 param('name').custom(isVideoChannelNameValid).withMessage('Should have a valid video channel name'),
107
108 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
109 logger.debug('Checking localVideoChannelValidator parameters', { parameters: req.params })
110
111 if (areValidationErrors(req, res)) return
112 if (!await isLocalVideoChannelNameExist(req.params.name, res)) return
113
114 return next()
115 }
116]
117
72c7248b
C
118// ---------------------------------------------------------------------------
119
120export {
38fa2065 121 listVideoAccountChannelsValidator,
72c7248b
C
122 videoChannelsAddValidator,
123 videoChannelsUpdateValidator,
124 videoChannelsRemoveValidator,
8a19bee1 125 videoChannelsNameWithHostValidator,
06a05d5f 126 localVideoChannelValidator
72c7248b
C
127}
128
129// ---------------------------------------------------------------------------
130
3fd3ab2d 131function checkUserCanDeleteVideoChannel (user: UserModel, videoChannel: VideoChannelModel, res: express.Response) {
50d6de9c 132 if (videoChannel.Actor.isOwned() === false) {
a2431b7d 133 res.status(403)
60862425 134 .json({ error: 'Cannot remove video channel of another server.' })
72c7248b 135 .end()
a2431b7d
C
136
137 return false
72c7248b
C
138 }
139
140 // Check if the user can delete the video channel
141 // The user can delete it if s/he is an admin
38fa2065 142 // Or if s/he is the video channel's account
a2431b7d
C
143 if (user.hasRight(UserRight.REMOVE_ANY_VIDEO_CHANNEL) === false && videoChannel.Account.userId !== user.id) {
144 res.status(403)
72c7248b
C
145 .json({ error: 'Cannot remove video channel of another user' })
146 .end()
a2431b7d
C
147
148 return false
72c7248b
C
149 }
150
a2431b7d 151 return true
72c7248b
C
152}
153
a2431b7d 154async function checkVideoChannelIsNotTheLastOne (res: express.Response) {
3fd3ab2d 155 const count = await VideoChannelModel.countByAccount(res.locals.oauth.token.User.Account.id)
a2431b7d
C
156
157 if (count <= 1) {
158 res.status(409)
159 .json({ error: 'Cannot remove the last channel of this user' })
160 .end()
161
162 return false
163 }
164
165 return true
72c7248b 166}