]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/video-channels.ts
Move sort middleware utils in utils file
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / video-channels.ts
CommitLineData
72c7248b 1import * as express from 'express'
d4f1e94c
C
2import { body, param } from 'express-validator/check'
3import { UserRight } from '../../../shared'
3fd3ab2d 4import { isAccountIdExist } from '../../helpers/custom-validators/accounts'
50d6de9c 5import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc'
4e50b6a1 6import {
da854ddd 7 isVideoChannelDescriptionValid, isVideoChannelExist,
2422c46b 8 isVideoChannelNameValid, isVideoChannelSupportValid
4e50b6a1 9} from '../../helpers/custom-validators/video-channels'
da854ddd 10import { logger } from '../../helpers/logger'
3fd3ab2d
C
11import { UserModel } from '../../models/account/user'
12import { VideoChannelModel } from '../../models/video/video-channel'
a2431b7d 13import { areValidationErrors } from './utils'
72c7248b 14
38fa2065
C
15const listVideoAccountChannelsValidator = [
16 param('accountId').custom(isIdOrUUIDValid).withMessage('Should have a valid account id'),
72c7248b 17
a2431b7d 18 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
38fa2065 19 logger.debug('Checking listVideoAccountChannelsValidator parameters', { parameters: req.body })
72c7248b 20
a2431b7d
C
21 if (areValidationErrors(req, res)) return
22 if (!await isAccountIdExist(req.params.accountId, res)) return
23
24 return next()
72c7248b
C
25 }
26]
27
28const videoChannelsAddValidator = [
29 body('name').custom(isVideoChannelNameValid).withMessage('Should have a valid name'),
2422c46b
C
30 body('description').optional().custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'),
31 body('support').optional().custom(isVideoChannelSupportValid).withMessage('Should have a valid support text'),
72c7248b
C
32
33 (req: express.Request, res: express.Response, next: express.NextFunction) => {
34 logger.debug('Checking videoChannelsAdd parameters', { parameters: req.body })
35
a2431b7d
C
36 if (areValidationErrors(req, res)) return
37
38 return next()
72c7248b
C
39 }
40]
41
42const videoChannelsUpdateValidator = [
43 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
44 body('name').optional().custom(isVideoChannelNameValid).withMessage('Should have a valid name'),
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
C
51 if (areValidationErrors(req, res)) return
52 if (!await isVideoChannelExist(req.params.id, res)) return
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 = [
72 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
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
C
77 if (areValidationErrors(req, res)) return
78 if (!await isVideoChannelExist(req.params.id, res)) return
79
80 // Check if the user who did the request is able to delete the video
d48ff09d 81 if (!checkUserCanDeleteVideoChannel(res.locals.oauth.token.User, res.locals.videoChannel, res)) return
a2431b7d
C
82 if (!await checkVideoChannelIsNotTheLastOne(res)) return
83
84 return next()
72c7248b
C
85 }
86]
87
20494f12 88const videoChannelsGetValidator = [
72c7248b
C
89 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
90
a2431b7d 91 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
72c7248b
C
92 logger.debug('Checking videoChannelsGet parameters', { parameters: req.params })
93
a2431b7d
C
94 if (areValidationErrors(req, res)) return
95 if (!await isVideoChannelExist(req.params.id, res)) return
96
97 return next()
72c7248b
C
98 }
99]
100
101// ---------------------------------------------------------------------------
102
103export {
38fa2065 104 listVideoAccountChannelsValidator,
72c7248b
C
105 videoChannelsAddValidator,
106 videoChannelsUpdateValidator,
107 videoChannelsRemoveValidator,
50d6de9c 108 videoChannelsGetValidator
72c7248b
C
109}
110
111// ---------------------------------------------------------------------------
112
3fd3ab2d 113function checkUserCanDeleteVideoChannel (user: UserModel, videoChannel: VideoChannelModel, res: express.Response) {
50d6de9c 114 if (videoChannel.Actor.isOwned() === false) {
a2431b7d 115 res.status(403)
60862425 116 .json({ error: 'Cannot remove video channel of another server.' })
72c7248b 117 .end()
a2431b7d
C
118
119 return false
72c7248b
C
120 }
121
122 // Check if the user can delete the video channel
123 // The user can delete it if s/he is an admin
38fa2065 124 // Or if s/he is the video channel's account
a2431b7d
C
125 if (user.hasRight(UserRight.REMOVE_ANY_VIDEO_CHANNEL) === false && videoChannel.Account.userId !== user.id) {
126 res.status(403)
72c7248b
C
127 .json({ error: 'Cannot remove video channel of another user' })
128 .end()
a2431b7d
C
129
130 return false
72c7248b
C
131 }
132
a2431b7d 133 return true
72c7248b
C
134}
135
a2431b7d 136async function checkVideoChannelIsNotTheLastOne (res: express.Response) {
3fd3ab2d 137 const count = await VideoChannelModel.countByAccount(res.locals.oauth.token.User.Account.id)
a2431b7d
C
138
139 if (count <= 1) {
140 res.status(409)
141 .json({ error: 'Cannot remove the last channel of this user' })
142 .end()
143
144 return false
145 }
146
147 return true
72c7248b 148}