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