]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/video-channels.ts
e3a11a41b58aff00f221f571b93e8a8340367a87
[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 { isAccountIdExist } from '../../helpers/custom-validators/accounts'
5 import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc'
6 import {
7 isVideoChannelDescriptionValid, isVideoChannelExist,
8 isVideoChannelNameValid, isVideoChannelSupportValid
9 } from '../../helpers/custom-validators/video-channels'
10 import { logger } from '../../helpers/logger'
11 import { UserModel } from '../../models/account/user'
12 import { VideoChannelModel } from '../../models/video/video-channel'
13 import { areValidationErrors } from './utils'
14
15 const listVideoAccountChannelsValidator = [
16 param('accountId').custom(isIdOrUUIDValid).withMessage('Should have a valid account id'),
17
18 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
19 logger.debug('Checking listVideoAccountChannelsValidator parameters', { parameters: req.body })
20
21 if (areValidationErrors(req, res)) return
22 if (!await isAccountIdExist(req.params.accountId, res)) return
23
24 return next()
25 }
26 ]
27
28 const videoChannelsAddValidator = [
29 param('accountId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid account id'),
30 body('name').custom(isVideoChannelNameValid).withMessage('Should have a valid name'),
31 body('description').optional().custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'),
32 body('support').optional().custom(isVideoChannelSupportValid).withMessage('Should have a valid support text'),
33
34 (req: express.Request, res: express.Response, next: express.NextFunction) => {
35 logger.debug('Checking videoChannelsAdd parameters', { parameters: req.body })
36
37 if (areValidationErrors(req, res)) return
38
39 return next()
40 }
41 ]
42
43 const videoChannelsUpdateValidator = [
44 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
45 param('accountId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid account id'),
46 body('name').optional().custom(isVideoChannelNameValid).withMessage('Should have a valid name'),
47 body('description').optional().custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'),
48 body('support').optional().custom(isVideoChannelSupportValid).withMessage('Should have a valid support text'),
49
50 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
51 logger.debug('Checking videoChannelsUpdate parameters', { parameters: req.body })
52
53 if (areValidationErrors(req, res)) return
54 if (!await isAccountIdExist(req.params.accountId, 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 param('accountId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid account id'),
77
78 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
79 logger.debug('Checking videoChannelsRemove parameters', { parameters: req.params })
80
81 if (areValidationErrors(req, res)) return
82 if (!await isAccountIdExist(req.params.accountId, res)) return
83 if (!await isVideoChannelExist(req.params.id, res)) return
84
85 // Check if the user who did the request is able to delete the video
86 if (!checkUserCanDeleteVideoChannel(res.locals.oauth.token.User, res.locals.videoChannel, res)) return
87 if (!await checkVideoChannelIsNotTheLastOne(res)) return
88
89 return next()
90 }
91 ]
92
93 const videoChannelsGetValidator = [
94 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
95 param('accountId').optional().custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid account id'),
96
97 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
98 logger.debug('Checking videoChannelsGet parameters', { parameters: req.params })
99
100 if (areValidationErrors(req, res)) return
101 // On some routes, accountId is optional (for example in the ActivityPub route)
102 if (req.params.accountId && !await isAccountIdExist(req.params.accountId, res)) return
103 if (!await isVideoChannelExist(req.params.id, res)) return
104
105 return next()
106 }
107 ]
108
109 // ---------------------------------------------------------------------------
110
111 export {
112 listVideoAccountChannelsValidator,
113 videoChannelsAddValidator,
114 videoChannelsUpdateValidator,
115 videoChannelsRemoveValidator,
116 videoChannelsGetValidator
117 }
118
119 // ---------------------------------------------------------------------------
120
121 function checkUserCanDeleteVideoChannel (user: UserModel, videoChannel: VideoChannelModel, res: express.Response) {
122 if (videoChannel.Actor.isOwned() === false) {
123 res.status(403)
124 .json({ error: 'Cannot remove video channel of another server.' })
125 .end()
126
127 return false
128 }
129
130 // Check if the user can delete the video channel
131 // The user can delete it if s/he is an admin
132 // Or if s/he is the video channel's account
133 if (user.hasRight(UserRight.REMOVE_ANY_VIDEO_CHANNEL) === false && videoChannel.Account.userId !== user.id) {
134 res.status(403)
135 .json({ error: 'Cannot remove video channel of another user' })
136 .end()
137
138 return false
139 }
140
141 return true
142 }
143
144 async function checkVideoChannelIsNotTheLastOne (res: express.Response) {
145 const count = await VideoChannelModel.countByAccount(res.locals.oauth.token.User.Account.id)
146
147 if (count <= 1) {
148 res.status(409)
149 .json({ error: 'Cannot remove the last channel of this user' })
150 .end()
151
152 return false
153 }
154
155 return true
156 }