]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/videos/video-channels.ts
Add ability to unpublish video/playlist
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / videos / video-channels.ts
1 import * as express from 'express'
2 import { body, param } from 'express-validator/check'
3 import { UserRight } from '../../../../shared'
4 import {
5 doesLocalVideoChannelNameExist,
6 doesVideoChannelNameWithHostExist,
7 isVideoChannelDescriptionValid,
8 isVideoChannelNameValid,
9 isVideoChannelSupportValid
10 } from '../../../helpers/custom-validators/video-channels'
11 import { logger } from '../../../helpers/logger'
12 import { UserModel } from '../../../models/account/user'
13 import { VideoChannelModel } from '../../../models/video/video-channel'
14 import { areValidationErrors } from '../utils'
15 import { isActorPreferredUsernameValid } from '../../../helpers/custom-validators/activitypub/actor'
16 import { ActorModel } from '../../../models/activitypub/actor'
17 import { isBooleanValid } from '../../../helpers/custom-validators/misc'
18
19 const videoChannelsAddValidator = [
20 body('name').custom(isActorPreferredUsernameValid).withMessage('Should have a valid channel name'),
21 body('displayName').custom(isVideoChannelNameValid).withMessage('Should have a valid display name'),
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'),
24
25 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
26 logger.debug('Checking videoChannelsAdd parameters', { parameters: req.body })
27
28 if (areValidationErrors(req, res)) return
29
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
38 return next()
39 }
40 ]
41
42 const videoChannelsUpdateValidator = [
43 param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
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'),
56
57 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
58 logger.debug('Checking videoChannelsUpdate parameters', { parameters: req.body })
59
60 if (areValidationErrors(req, res)) return
61 if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
62
63 // We need to make additional checks
64 if (res.locals.videoChannel.Actor.isOwned() === false) {
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()
77 }
78 ]
79
80 const videoChannelsRemoveValidator = [
81 param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
82
83 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
84 logger.debug('Checking videoChannelsRemove parameters', { parameters: req.params })
85
86 if (areValidationErrors(req, res)) return
87 if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
88
89 if (!checkUserCanDeleteVideoChannel(res.locals.oauth.token.User, res.locals.videoChannel, res)) return
90 if (!await checkVideoChannelIsNotTheLastOne(res)) return
91
92 return next()
93 }
94 ]
95
96 const videoChannelsNameWithHostValidator = [
97 param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
98
99 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
100 logger.debug('Checking videoChannelsNameWithHostValidator parameters', { parameters: req.params })
101
102 if (areValidationErrors(req, res)) return
103
104 if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
105
106 return next()
107 }
108 ]
109
110 const 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
117 if (!await doesLocalVideoChannelNameExist(req.params.name, res)) return
118
119 return next()
120 }
121 ]
122
123 // ---------------------------------------------------------------------------
124
125 export {
126 videoChannelsAddValidator,
127 videoChannelsUpdateValidator,
128 videoChannelsRemoveValidator,
129 videoChannelsNameWithHostValidator,
130 localVideoChannelValidator
131 }
132
133 // ---------------------------------------------------------------------------
134
135 function checkUserCanDeleteVideoChannel (user: UserModel, videoChannel: VideoChannelModel, res: express.Response) {
136 if (videoChannel.Actor.isOwned() === false) {
137 res.status(403)
138 .json({ error: 'Cannot remove video channel of another server.' })
139 .end()
140
141 return false
142 }
143
144 // Check if the user can delete the video channel
145 // The user can delete it if s/he is an admin
146 // Or if s/he is the video channel's account
147 if (user.hasRight(UserRight.REMOVE_ANY_VIDEO_CHANNEL) === false && videoChannel.Account.userId !== user.id) {
148 res.status(403)
149 .json({ error: 'Cannot remove video channel of another user' })
150 .end()
151
152 return false
153 }
154
155 return true
156 }
157
158 async function checkVideoChannelIsNotTheLastOne (res: express.Response) {
159 const count = await VideoChannelModel.countByAccount(res.locals.oauth.token.User.Account.id)
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
170 }