Update video-channel routes (again)
[oweals/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 { isAccountIdExist } from '../../helpers/custom-validators/accounts'
5 import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc'
6 import {
7   isVideoChannelDescriptionValid, isVideoChannelExist,
8   isVideoChannelNameValid, isVideoChannelSupportValid
9 } from '../../helpers/custom-validators/video-channels'
10 import { logger } from '../../helpers/logger'
11 import { UserModel } from '../../models/account/user'
12 import { VideoChannelModel } from '../../models/video/video-channel'
13 import { areValidationErrors } from './utils'
14 import { AccountModel } from '../../models/account/account'
15
16 const listVideoAccountChannelsValidator = [
17   param('accountId').custom(isIdOrUUIDValid).withMessage('Should have a valid account id'),
18
19   async (req: express.Request, res: express.Response, next: express.NextFunction) => {
20     logger.debug('Checking listVideoAccountChannelsValidator parameters', { parameters: req.body })
21
22     if (areValidationErrors(req, res)) return
23     if (!await isAccountIdExist(req.params.accountId, res)) return
24
25     return next()
26   }
27 ]
28
29 const videoChannelsAddValidator = [
30   body('name').custom(isVideoChannelNameValid).withMessage('Should have a valid name'),
31   body('description').optional().custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'),
32   body('support').optional().custom(isVideoChannelSupportValid).withMessage('Should have a valid support text'),
33
34   (req: express.Request, res: express.Response, next: express.NextFunction) => {
35     logger.debug('Checking videoChannelsAdd parameters', { parameters: req.body })
36
37     if (areValidationErrors(req, res)) return
38
39     return next()
40   }
41 ]
42
43 const videoChannelsUpdateValidator = [
44   param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
45   body('name').optional().custom(isVideoChannelNameValid).withMessage('Should have a valid name'),
46   body('description').optional().custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'),
47   body('support').optional().custom(isVideoChannelSupportValid).withMessage('Should have a valid support text'),
48
49   async (req: express.Request, res: express.Response, next: express.NextFunction) => {
50     logger.debug('Checking videoChannelsUpdate parameters', { parameters: req.body })
51
52     if (areValidationErrors(req, res)) return
53     if (!await isVideoChannelExist(req.params.id, res)) return
54
55     // We need to make additional checks
56     if (res.locals.videoChannel.Actor.isOwned() === false) {
57       return res.status(403)
58         .json({ error: 'Cannot update video channel of another server' })
59         .end()
60     }
61
62     if (res.locals.videoChannel.Account.userId !== res.locals.oauth.token.User.id) {
63       return res.status(403)
64         .json({ error: 'Cannot update video channel of another user' })
65         .end()
66     }
67
68     return next()
69   }
70 ]
71
72 const videoChannelsRemoveValidator = [
73   param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
74
75   async (req: express.Request, res: express.Response, next: express.NextFunction) => {
76     logger.debug('Checking videoChannelsRemove parameters', { parameters: req.params })
77
78     if (areValidationErrors(req, res)) return
79     if (!await isVideoChannelExist(req.params.id, res)) return
80
81     if (!checkUserCanDeleteVideoChannel(res.locals.oauth.token.User, res.locals.videoChannel, res)) return
82     if (!await checkVideoChannelIsNotTheLastOne(res)) return
83
84     return next()
85   }
86 ]
87
88 const videoChannelsGetValidator = [
89   param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
90
91   async (req: express.Request, res: express.Response, next: express.NextFunction) => {
92     logger.debug('Checking videoChannelsGet parameters', { parameters: req.params })
93
94     if (areValidationErrors(req, res)) return
95
96     if (!await isVideoChannelExist(req.params.id, res)) return
97
98     return next()
99   }
100 ]
101
102 // ---------------------------------------------------------------------------
103
104 export {
105   listVideoAccountChannelsValidator,
106   videoChannelsAddValidator,
107   videoChannelsUpdateValidator,
108   videoChannelsRemoveValidator,
109   videoChannelsGetValidator
110 }
111
112 // ---------------------------------------------------------------------------
113
114 function checkUserCanDeleteVideoChannel (user: UserModel, videoChannel: VideoChannelModel, res: express.Response) {
115   if (videoChannel.Actor.isOwned() === false) {
116     res.status(403)
117               .json({ error: 'Cannot remove video channel of another server.' })
118               .end()
119
120     return false
121   }
122
123   // Check if the user can delete the video channel
124   // The user can delete it if s/he is an admin
125   // Or if s/he is the video channel's account
126   if (user.hasRight(UserRight.REMOVE_ANY_VIDEO_CHANNEL) === false && videoChannel.Account.userId !== user.id) {
127     res.status(403)
128               .json({ error: 'Cannot remove video channel of another user' })
129               .end()
130
131     return false
132   }
133
134   return true
135 }
136
137 async function checkVideoChannelIsNotTheLastOne (res: express.Response) {
138   const count = await VideoChannelModel.countByAccount(res.locals.oauth.token.User.Account.id)
139
140   if (count <= 1) {
141     res.status(409)
142       .json({ error: 'Cannot remove the last channel of this user' })
143       .end()
144
145     return false
146   }
147
148   return true
149 }