Begin advanced search
[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, isAccountNameWithHostExist } from '../../helpers/custom-validators/accounts'
5 import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc'
6 import {
7   isVideoChannelDescriptionValid,
8   isVideoChannelExist,
9   isVideoChannelNameValid,
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 { isAvatarFile } from '../../helpers/custom-validators/users'
17 import { CONSTRAINTS_FIELDS } from '../../initializers'
18
19 const listVideoAccountChannelsValidator = [
20   param('accountName').exists().withMessage('Should have a valid account name'),
21
22   async (req: express.Request, res: express.Response, next: express.NextFunction) => {
23     logger.debug('Checking listVideoAccountChannelsValidator parameters', { parameters: req.body })
24
25     if (areValidationErrors(req, res)) return
26     if (!await isAccountNameWithHostExist(req.params.accountName, res)) return
27
28     return next()
29   }
30 ]
31
32 const videoChannelsAddValidator = [
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('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
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 isVideoChannelExist(req.params.id, 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('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
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 isVideoChannelExist(req.params.id, 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 videoChannelsGetValidator = [
92   param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
93
94   async (req: express.Request, res: express.Response, next: express.NextFunction) => {
95     logger.debug('Checking videoChannelsGet parameters', { parameters: req.params })
96
97     if (areValidationErrors(req, res)) return
98
99     if (!await isVideoChannelExist(req.params.id, res)) return
100
101     return next()
102   }
103 ]
104
105 // ---------------------------------------------------------------------------
106
107 export {
108   listVideoAccountChannelsValidator,
109   videoChannelsAddValidator,
110   videoChannelsUpdateValidator,
111   videoChannelsRemoveValidator,
112   videoChannelsGetValidator
113 }
114
115 // ---------------------------------------------------------------------------
116
117 function checkUserCanDeleteVideoChannel (user: UserModel, videoChannel: VideoChannelModel, res: express.Response) {
118   if (videoChannel.Actor.isOwned() === false) {
119     res.status(403)
120               .json({ error: 'Cannot remove video channel of another server.' })
121               .end()
122
123     return false
124   }
125
126   // Check if the user can delete the video channel
127   // The user can delete it if s/he is an admin
128   // Or if s/he is the video channel's account
129   if (user.hasRight(UserRight.REMOVE_ANY_VIDEO_CHANNEL) === false && videoChannel.Account.userId !== user.id) {
130     res.status(403)
131               .json({ error: 'Cannot remove video channel of another user' })
132               .end()
133
134     return false
135   }
136
137   return true
138 }
139
140 async function checkVideoChannelIsNotTheLastOne (res: express.Response) {
141   const count = await VideoChannelModel.countByAccount(res.locals.oauth.token.User.Account.id)
142
143   if (count <= 1) {
144     res.status(409)
145       .json({ error: 'Cannot remove the last channel of this user' })
146       .end()
147
148     return false
149   }
150
151   return true
152 }