]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/videos/video-channels.ts
View stats for channels
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / videos / video-channels.ts
CommitLineData
72c7248b 1import * as express from 'express'
c8861d5d 2import { body, param } from 'express-validator'
6e46de09 3import { UserRight } from '../../../../shared'
4e50b6a1 4import {
7d8e778a 5 isVideoChannelDescriptionValid,
7d8e778a
C
6 isVideoChannelNameValid,
7 isVideoChannelSupportValid
6e46de09
C
8} from '../../../helpers/custom-validators/video-channels'
9import { logger } from '../../../helpers/logger'
6e46de09
C
10import { VideoChannelModel } from '../../../models/video/video-channel'
11import { areValidationErrors } from '../utils'
12import { isActorPreferredUsernameValid } from '../../../helpers/custom-validators/activitypub/actor'
13import { ActorModel } from '../../../models/activitypub/actor'
7d14d4d2 14import { isBooleanValid } from '../../../helpers/custom-validators/misc'
3e753302 15import { doesLocalVideoChannelNameExist, doesVideoChannelNameWithHostExist } from '../../../helpers/middlewares'
0283eaac 16import { MChannelAccountDefault, MUser } from '@server/typings/models'
a3ce4ae8 17import { VIDEO_CHANNELS } from '@server/initializers/constants'
72c7248b 18
72c7248b 19const videoChannelsAddValidator = [
8a19bee1 20 body('name').custom(isActorPreferredUsernameValid).withMessage('Should have a valid channel name'),
08c1efbe 21 body('displayName').custom(isVideoChannelNameValid).withMessage('Should have a valid display name'),
2422c46b
C
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'),
72c7248b 24
601527d7 25 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
72c7248b
C
26 logger.debug('Checking videoChannelsAdd parameters', { parameters: req.body })
27
a2431b7d
C
28 if (areValidationErrors(req, res)) return
29
601527d7
C
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
a3ce4ae8 38 const count = await VideoChannelModel.countByAccount(res.locals.oauth.token.User.Account.id)
5197732b 39 if (count >= VIDEO_CHANNELS.MAX_PER_USER) {
a3ce4ae8
C
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
a2431b7d 46 return next()
72c7248b
C
47 }
48]
49
50const videoChannelsUpdateValidator = [
8a19bee1 51 param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
7d14d4d2
C
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'),
72c7248b 64
a2431b7d 65 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
72c7248b
C
66 logger.debug('Checking videoChannelsUpdate parameters', { parameters: req.body })
67
a2431b7d 68 if (areValidationErrors(req, res)) return
0f6acda1 69 if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
a2431b7d
C
70
71 // We need to make additional checks
d50acfab 72 if (res.locals.videoChannel.Actor.isOwned() === false) {
a2431b7d
C
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()
72c7248b
C
85 }
86]
87
88const videoChannelsRemoveValidator = [
8a19bee1 89 param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
72c7248b 90
a2431b7d 91 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
72c7248b
C
92 logger.debug('Checking videoChannelsRemove parameters', { parameters: req.params })
93
a2431b7d 94 if (areValidationErrors(req, res)) return
0f6acda1 95 if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
a2431b7d 96
d48ff09d 97 if (!checkUserCanDeleteVideoChannel(res.locals.oauth.token.User, res.locals.videoChannel, res)) return
a2431b7d
C
98 if (!await checkVideoChannelIsNotTheLastOne(res)) return
99
100 return next()
72c7248b
C
101 }
102]
103
8a19bee1
C
104const videoChannelsNameWithHostValidator = [
105 param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
72c7248b 106
a2431b7d 107 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
8a19bee1 108 logger.debug('Checking videoChannelsNameWithHostValidator parameters', { parameters: req.params })
72c7248b 109
a2431b7d 110 if (areValidationErrors(req, res)) return
6b738c7a 111
0f6acda1 112 if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
a2431b7d
C
113
114 return next()
72c7248b
C
115 }
116]
117
06a05d5f
C
118const 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
0f6acda1 125 if (!await doesLocalVideoChannelNameExist(req.params.name, res)) return
06a05d5f
C
126
127 return next()
128 }
129]
130
72c7248b
C
131// ---------------------------------------------------------------------------
132
133export {
72c7248b
C
134 videoChannelsAddValidator,
135 videoChannelsUpdateValidator,
136 videoChannelsRemoveValidator,
8a19bee1 137 videoChannelsNameWithHostValidator,
06a05d5f 138 localVideoChannelValidator
72c7248b
C
139}
140
141// ---------------------------------------------------------------------------
142
0283eaac 143function checkUserCanDeleteVideoChannel (user: MUser, videoChannel: MChannelAccountDefault, res: express.Response) {
50d6de9c 144 if (videoChannel.Actor.isOwned() === false) {
a2431b7d 145 res.status(403)
60862425 146 .json({ error: 'Cannot remove video channel of another server.' })
72c7248b 147 .end()
a2431b7d
C
148
149 return false
72c7248b
C
150 }
151
152 // Check if the user can delete the video channel
153 // The user can delete it if s/he is an admin
38fa2065 154 // Or if s/he is the video channel's account
a2431b7d
C
155 if (user.hasRight(UserRight.REMOVE_ANY_VIDEO_CHANNEL) === false && videoChannel.Account.userId !== user.id) {
156 res.status(403)
72c7248b
C
157 .json({ error: 'Cannot remove video channel of another user' })
158 .end()
a2431b7d
C
159
160 return false
72c7248b
C
161 }
162
a2431b7d 163 return true
72c7248b
C
164}
165
a2431b7d 166async function checkVideoChannelIsNotTheLastOne (res: express.Response) {
3fd3ab2d 167 const count = await VideoChannelModel.countByAccount(res.locals.oauth.token.User.Account.id)
a2431b7d
C
168
169 if (count <= 1) {
170 res.status(409)
171 .json({ error: 'Cannot remove the last channel of this user' })
172 .end()
173
174 return false
175 }
176
177 return true
72c7248b 178}