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