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