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