]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/videos/video-channels.ts
Stronger model typings
[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'
453e83ea
C
16import { MChannelActorAccountDefault } from '../../../typings/models/video'
17import { MUser } from '@server/typings/models'
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
a2431b7d 38 return next()
72c7248b
C
39 }
40]
41
42const videoChannelsUpdateValidator = [
8a19bee1 43 param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
7d14d4d2
C
44 body('displayName')
45 .optional()
46 .custom(isVideoChannelNameValid).withMessage('Should have a valid display name'),
47 body('description')
48 .optional()
49 .custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'),
50 body('support')
51 .optional()
52 .custom(isVideoChannelSupportValid).withMessage('Should have a valid support text'),
53 body('bulkVideosSupportUpdate')
54 .optional()
55 .custom(isBooleanValid).withMessage('Should have a valid bulkVideosSupportUpdate boolean field'),
72c7248b 56
a2431b7d 57 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
72c7248b
C
58 logger.debug('Checking videoChannelsUpdate parameters', { parameters: req.body })
59
a2431b7d 60 if (areValidationErrors(req, res)) return
0f6acda1 61 if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
a2431b7d
C
62
63 // We need to make additional checks
d50acfab 64 if (res.locals.videoChannel.Actor.isOwned() === false) {
a2431b7d
C
65 return res.status(403)
66 .json({ error: 'Cannot update video channel of another server' })
67 .end()
68 }
69
70 if (res.locals.videoChannel.Account.userId !== res.locals.oauth.token.User.id) {
71 return res.status(403)
72 .json({ error: 'Cannot update video channel of another user' })
73 .end()
74 }
75
76 return next()
72c7248b
C
77 }
78]
79
80const videoChannelsRemoveValidator = [
8a19bee1 81 param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
72c7248b 82
a2431b7d 83 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
72c7248b
C
84 logger.debug('Checking videoChannelsRemove parameters', { parameters: req.params })
85
a2431b7d 86 if (areValidationErrors(req, res)) return
0f6acda1 87 if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
a2431b7d 88
d48ff09d 89 if (!checkUserCanDeleteVideoChannel(res.locals.oauth.token.User, res.locals.videoChannel, res)) return
a2431b7d
C
90 if (!await checkVideoChannelIsNotTheLastOne(res)) return
91
92 return next()
72c7248b
C
93 }
94]
95
8a19bee1
C
96const videoChannelsNameWithHostValidator = [
97 param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
72c7248b 98
a2431b7d 99 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
8a19bee1 100 logger.debug('Checking videoChannelsNameWithHostValidator parameters', { parameters: req.params })
72c7248b 101
a2431b7d 102 if (areValidationErrors(req, res)) return
6b738c7a 103
0f6acda1 104 if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
a2431b7d
C
105
106 return next()
72c7248b
C
107 }
108]
109
06a05d5f
C
110const localVideoChannelValidator = [
111 param('name').custom(isVideoChannelNameValid).withMessage('Should have a valid video channel name'),
112
113 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
114 logger.debug('Checking localVideoChannelValidator parameters', { parameters: req.params })
115
116 if (areValidationErrors(req, res)) return
0f6acda1 117 if (!await doesLocalVideoChannelNameExist(req.params.name, res)) return
06a05d5f
C
118
119 return next()
120 }
121]
122
72c7248b
C
123// ---------------------------------------------------------------------------
124
125export {
72c7248b
C
126 videoChannelsAddValidator,
127 videoChannelsUpdateValidator,
128 videoChannelsRemoveValidator,
8a19bee1 129 videoChannelsNameWithHostValidator,
06a05d5f 130 localVideoChannelValidator
72c7248b
C
131}
132
133// ---------------------------------------------------------------------------
134
453e83ea 135function checkUserCanDeleteVideoChannel (user: MUser, videoChannel: MChannelActorAccountDefault, res: express.Response) {
50d6de9c 136 if (videoChannel.Actor.isOwned() === false) {
a2431b7d 137 res.status(403)
60862425 138 .json({ error: 'Cannot remove video channel of another server.' })
72c7248b 139 .end()
a2431b7d
C
140
141 return false
72c7248b
C
142 }
143
144 // Check if the user can delete the video channel
145 // The user can delete it if s/he is an admin
38fa2065 146 // Or if s/he is the video channel's account
a2431b7d
C
147 if (user.hasRight(UserRight.REMOVE_ANY_VIDEO_CHANNEL) === false && videoChannel.Account.userId !== user.id) {
148 res.status(403)
72c7248b
C
149 .json({ error: 'Cannot remove video channel of another user' })
150 .end()
a2431b7d
C
151
152 return false
72c7248b
C
153 }
154
a2431b7d 155 return true
72c7248b
C
156}
157
a2431b7d 158async function checkVideoChannelIsNotTheLastOne (res: express.Response) {
3fd3ab2d 159 const count = await VideoChannelModel.countByAccount(res.locals.oauth.token.User.Account.id)
a2431b7d
C
160
161 if (count <= 1) {
162 res.status(409)
163 .json({ error: 'Cannot remove the last channel of this user' })
164 .end()
165
166 return false
167 }
168
169 return true
72c7248b 170}