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