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