]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/video-channels.ts
Redirect to uuid video route after upload
[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'
3fd3ab2d
C
4import { logger } from '../../helpers'
5import { isAccountIdExist } from '../../helpers/custom-validators/accounts'
50d6de9c 6import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc'
4e50b6a1 7import {
4e50b6a1 8 isVideoChannelDescriptionValid,
a2431b7d 9 isVideoChannelExist,
4e50b6a1
C
10 isVideoChannelNameValid
11} from '../../helpers/custom-validators/video-channels'
3fd3ab2d
C
12import { UserModel } from '../../models/account/user'
13import { VideoChannelModel } from '../../models/video/video-channel'
a2431b7d 14import { areValidationErrors } from './utils'
72c7248b 15
38fa2065
C
16const listVideoAccountChannelsValidator = [
17 param('accountId').custom(isIdOrUUIDValid).withMessage('Should have a valid account id'),
72c7248b 18
a2431b7d 19 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
38fa2065 20 logger.debug('Checking listVideoAccountChannelsValidator parameters', { parameters: req.body })
72c7248b 21
a2431b7d
C
22 if (areValidationErrors(req, res)) return
23 if (!await isAccountIdExist(req.params.accountId, res)) return
24
25 return next()
72c7248b
C
26 }
27]
28
29const videoChannelsAddValidator = [
30 body('name').custom(isVideoChannelNameValid).withMessage('Should have a valid name'),
31 body('description').custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'),
32
33 (req: express.Request, res: express.Response, next: express.NextFunction) => {
34 logger.debug('Checking videoChannelsAdd parameters', { parameters: req.body })
35
a2431b7d
C
36 if (areValidationErrors(req, res)) return
37
38 return next()
72c7248b
C
39 }
40]
41
42const videoChannelsUpdateValidator = [
43 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
44 body('name').optional().custom(isVideoChannelNameValid).withMessage('Should have a valid name'),
45 body('description').optional().custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'),
46
a2431b7d 47 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
72c7248b
C
48 logger.debug('Checking videoChannelsUpdate parameters', { parameters: req.body })
49
a2431b7d
C
50 if (areValidationErrors(req, res)) return
51 if (!await isVideoChannelExist(req.params.id, res)) return
52
53 // We need to make additional checks
54 if (res.locals.videoChannel.isOwned() === false) {
55 return res.status(403)
56 .json({ error: 'Cannot update video channel of another server' })
57 .end()
58 }
59
60 if (res.locals.videoChannel.Account.userId !== res.locals.oauth.token.User.id) {
61 return res.status(403)
62 .json({ error: 'Cannot update video channel of another user' })
63 .end()
64 }
65
66 return next()
72c7248b
C
67 }
68]
69
70const videoChannelsRemoveValidator = [
71 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
72
a2431b7d 73 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
72c7248b
C
74 logger.debug('Checking videoChannelsRemove parameters', { parameters: req.params })
75
a2431b7d
C
76 if (areValidationErrors(req, res)) return
77 if (!await isVideoChannelExist(req.params.id, res)) return
78
79 // Check if the user who did the request is able to delete the video
d48ff09d 80 if (!checkUserCanDeleteVideoChannel(res.locals.oauth.token.User, res.locals.videoChannel, res)) return
a2431b7d
C
81 if (!await checkVideoChannelIsNotTheLastOne(res)) return
82
83 return next()
72c7248b
C
84 }
85]
86
20494f12 87const videoChannelsGetValidator = [
72c7248b
C
88 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
89
a2431b7d 90 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
72c7248b
C
91 logger.debug('Checking videoChannelsGet parameters', { parameters: req.params })
92
a2431b7d
C
93 if (areValidationErrors(req, res)) return
94 if (!await isVideoChannelExist(req.params.id, res)) return
95
96 return next()
72c7248b
C
97 }
98]
99
100// ---------------------------------------------------------------------------
101
102export {
38fa2065 103 listVideoAccountChannelsValidator,
72c7248b
C
104 videoChannelsAddValidator,
105 videoChannelsUpdateValidator,
106 videoChannelsRemoveValidator,
50d6de9c 107 videoChannelsGetValidator
72c7248b
C
108}
109
110// ---------------------------------------------------------------------------
111
3fd3ab2d 112function checkUserCanDeleteVideoChannel (user: UserModel, videoChannel: VideoChannelModel, res: express.Response) {
50d6de9c 113 if (videoChannel.Actor.isOwned() === false) {
a2431b7d 114 res.status(403)
60862425 115 .json({ error: 'Cannot remove video channel of another server.' })
72c7248b 116 .end()
a2431b7d
C
117
118 return false
72c7248b
C
119 }
120
121 // Check if the user can delete the video channel
122 // The user can delete it if s/he is an admin
38fa2065 123 // Or if s/he is the video channel's account
a2431b7d
C
124 if (user.hasRight(UserRight.REMOVE_ANY_VIDEO_CHANNEL) === false && videoChannel.Account.userId !== user.id) {
125 res.status(403)
72c7248b
C
126 .json({ error: 'Cannot remove video channel of another user' })
127 .end()
a2431b7d
C
128
129 return false
72c7248b
C
130 }
131
a2431b7d 132 return true
72c7248b
C
133}
134
a2431b7d 135async function checkVideoChannelIsNotTheLastOne (res: express.Response) {
3fd3ab2d 136 const count = await VideoChannelModel.countByAccount(res.locals.oauth.token.User.Account.id)
a2431b7d
C
137
138 if (count <= 1) {
139 res.status(409)
140 .json({ error: 'Cannot remove the last channel of this user' })
141 .end()
142
143 return false
144 }
145
146 return true
72c7248b 147}