]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/videos/video-channels.ts
Merge branch 'master' into develop
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / videos / video-channels.ts
1 import * as express from 'express'
2 import { body, param, query } from 'express-validator'
3 import { UserRight } from '../../../../shared'
4 import {
5 isVideoChannelDescriptionValid,
6 isVideoChannelNameValid,
7 isVideoChannelSupportValid
8 } from '../../../helpers/custom-validators/video-channels'
9 import { logger } from '../../../helpers/logger'
10 import { VideoChannelModel } from '../../../models/video/video-channel'
11 import { areValidationErrors } from '../utils'
12 import { isActorPreferredUsernameValid } from '../../../helpers/custom-validators/activitypub/actor'
13 import { ActorModel } from '../../../models/activitypub/actor'
14 import { isBooleanValid } from '../../../helpers/custom-validators/misc'
15 import { doesLocalVideoChannelNameExist, doesVideoChannelNameWithHostExist } from '../../../helpers/middlewares'
16 import { MChannelAccountDefault, MUser } from '@server/typings/models'
17 import { VIDEO_CHANNELS } from '@server/initializers/constants'
18
19 const videoChannelsAddValidator = [
20 body('name').custom(isActorPreferredUsernameValid).withMessage('Should have a valid channel name'),
21 body('displayName').custom(isVideoChannelNameValid).withMessage('Should have a valid display name'),
22 body('description').optional().custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'),
23 body('support').optional().custom(isVideoChannelSupportValid).withMessage('Should have a valid support text'),
24
25 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
26 logger.debug('Checking videoChannelsAdd parameters', { parameters: req.body })
27
28 if (areValidationErrors(req, res)) return
29
30 const actor = await ActorModel.loadLocalByName(req.body.name)
31 if (actor) {
32 res.status(409)
33 .send({ error: 'Another actor (account/channel) with this name on this instance already exists or has already existed.' })
34 .end()
35 return false
36 }
37
38 const count = await VideoChannelModel.countByAccount(res.locals.oauth.token.User.Account.id)
39 if (count >= VIDEO_CHANNELS.MAX_PER_USER) {
40 res.status(400)
41 .send({ error: `You cannot create more than ${VIDEO_CHANNELS.MAX_PER_USER} channels` })
42 .end()
43 return false
44 }
45
46 return next()
47 }
48 ]
49
50 const videoChannelsUpdateValidator = [
51 param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
52 body('displayName')
53 .optional()
54 .custom(isVideoChannelNameValid).withMessage('Should have a valid display name'),
55 body('description')
56 .optional()
57 .custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'),
58 body('support')
59 .optional()
60 .custom(isVideoChannelSupportValid).withMessage('Should have a valid support text'),
61 body('bulkVideosSupportUpdate')
62 .optional()
63 .custom(isBooleanValid).withMessage('Should have a valid bulkVideosSupportUpdate boolean field'),
64
65 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
66 logger.debug('Checking videoChannelsUpdate parameters', { parameters: req.body })
67
68 if (areValidationErrors(req, res)) return
69 if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
70
71 // We need to make additional checks
72 if (res.locals.videoChannel.Actor.isOwned() === false) {
73 return res.status(403)
74 .json({ error: 'Cannot update video channel of another server' })
75 .end()
76 }
77
78 if (res.locals.videoChannel.Account.userId !== res.locals.oauth.token.User.id) {
79 return res.status(403)
80 .json({ error: 'Cannot update video channel of another user' })
81 .end()
82 }
83
84 return next()
85 }
86 ]
87
88 const videoChannelsRemoveValidator = [
89 param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
90
91 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
92 logger.debug('Checking videoChannelsRemove parameters', { parameters: req.params })
93
94 if (areValidationErrors(req, res)) return
95 if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
96
97 if (!checkUserCanDeleteVideoChannel(res.locals.oauth.token.User, res.locals.videoChannel, res)) return
98 if (!await checkVideoChannelIsNotTheLastOne(res)) return
99
100 return next()
101 }
102 ]
103
104 const videoChannelsNameWithHostValidator = [
105 param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
106
107 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
108 logger.debug('Checking videoChannelsNameWithHostValidator parameters', { parameters: req.params })
109
110 if (areValidationErrors(req, res)) return
111
112 if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
113
114 return next()
115 }
116 ]
117
118 const localVideoChannelValidator = [
119 param('name').custom(isVideoChannelNameValid).withMessage('Should have a valid video channel name'),
120
121 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
122 logger.debug('Checking localVideoChannelValidator parameters', { parameters: req.params })
123
124 if (areValidationErrors(req, res)) return
125 if (!await doesLocalVideoChannelNameExist(req.params.name, res)) return
126
127 return next()
128 }
129 ]
130
131 const videoChannelStatsValidator = [
132 query('withStats').optional().isBoolean().withMessage('Should have a valid stats flag'),
133
134 (req: express.Request, res: express.Response, next: express.NextFunction) => {
135 if (areValidationErrors(req, res)) return
136 return next()
137 }
138 ]
139
140 // ---------------------------------------------------------------------------
141
142 export {
143 videoChannelsAddValidator,
144 videoChannelsUpdateValidator,
145 videoChannelsRemoveValidator,
146 videoChannelsNameWithHostValidator,
147 localVideoChannelValidator,
148 videoChannelStatsValidator
149 }
150
151 // ---------------------------------------------------------------------------
152
153 function checkUserCanDeleteVideoChannel (user: MUser, videoChannel: MChannelAccountDefault, res: express.Response) {
154 if (videoChannel.Actor.isOwned() === false) {
155 res.status(403)
156 .json({ error: 'Cannot remove video channel of another server.' })
157 .end()
158
159 return false
160 }
161
162 // Check if the user can delete the video channel
163 // The user can delete it if s/he is an admin
164 // Or if s/he is the video channel's account
165 if (user.hasRight(UserRight.REMOVE_ANY_VIDEO_CHANNEL) === false && videoChannel.Account.userId !== user.id) {
166 res.status(403)
167 .json({ error: 'Cannot remove video channel of another user' })
168 .end()
169
170 return false
171 }
172
173 return true
174 }
175
176 async function checkVideoChannelIsNotTheLastOne (res: express.Response) {
177 const count = await VideoChannelModel.countByAccount(res.locals.oauth.token.User.Account.id)
178
179 if (count <= 1) {
180 res.status(409)
181 .json({ error: 'Cannot remove the last channel of this user' })
182 .end()
183
184 return false
185 }
186
187 return true
188 }