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