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