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