Merge branch 'release/v1.0.0' into develop
[oweals/peertube.git] / server / middlewares / validators / videos / 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 {
6   isLocalVideoChannelNameExist,
7   isVideoChannelDescriptionValid,
8   isVideoChannelNameValid,
9   isVideoChannelNameWithHostExist,
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 { isActorPreferredUsernameValid } from '../../../helpers/custom-validators/activitypub/actor'
17 import { ActorModel } from '../../../models/activitypub/actor'
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('name').custom(isActorPreferredUsernameValid).withMessage('Should have a valid channel name'),
34   body('displayName').custom(isVideoChannelNameValid).withMessage('Should have a valid display name'),
35   body('description').optional().custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'),
36   body('support').optional().custom(isVideoChannelSupportValid).withMessage('Should have a valid support text'),
37
38   async (req: express.Request, res: express.Response, next: express.NextFunction) => {
39     logger.debug('Checking videoChannelsAdd parameters', { parameters: req.body })
40
41     if (areValidationErrors(req, res)) return
42
43     const actor = await ActorModel.loadLocalByName(req.body.name)
44     if (actor) {
45       res.status(409)
46          .send({ error: 'Another actor (account/channel) with this name on this instance already exists or has already existed.' })
47          .end()
48       return false
49     }
50
51     return next()
52   }
53 ]
54
55 const videoChannelsUpdateValidator = [
56   param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
57   body('displayName').optional().custom(isVideoChannelNameValid).withMessage('Should have a valid display name'),
58   body('description').optional().custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'),
59   body('support').optional().custom(isVideoChannelSupportValid).withMessage('Should have a valid support text'),
60
61   async (req: express.Request, res: express.Response, next: express.NextFunction) => {
62     logger.debug('Checking videoChannelsUpdate parameters', { parameters: req.body })
63
64     if (areValidationErrors(req, res)) return
65     if (!await isVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
66
67     // We need to make additional checks
68     if (res.locals.videoChannel.Actor.isOwned() === false) {
69       return res.status(403)
70         .json({ error: 'Cannot update video channel of another server' })
71         .end()
72     }
73
74     if (res.locals.videoChannel.Account.userId !== res.locals.oauth.token.User.id) {
75       return res.status(403)
76         .json({ error: 'Cannot update video channel of another user' })
77         .end()
78     }
79
80     return next()
81   }
82 ]
83
84 const videoChannelsRemoveValidator = [
85   param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
86
87   async (req: express.Request, res: express.Response, next: express.NextFunction) => {
88     logger.debug('Checking videoChannelsRemove parameters', { parameters: req.params })
89
90     if (areValidationErrors(req, res)) return
91     if (!await isVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
92
93     if (!checkUserCanDeleteVideoChannel(res.locals.oauth.token.User, res.locals.videoChannel, res)) return
94     if (!await checkVideoChannelIsNotTheLastOne(res)) return
95
96     return next()
97   }
98 ]
99
100 const videoChannelsNameWithHostValidator = [
101   param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
102
103   async (req: express.Request, res: express.Response, next: express.NextFunction) => {
104     logger.debug('Checking videoChannelsNameWithHostValidator parameters', { parameters: req.params })
105
106     if (areValidationErrors(req, res)) return
107
108     if (!await isVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
109
110     return next()
111   }
112 ]
113
114 const localVideoChannelValidator = [
115   param('name').custom(isVideoChannelNameValid).withMessage('Should have a valid video channel name'),
116
117   async (req: express.Request, res: express.Response, next: express.NextFunction) => {
118     logger.debug('Checking localVideoChannelValidator parameters', { parameters: req.params })
119
120     if (areValidationErrors(req, res)) return
121     if (!await isLocalVideoChannelNameExist(req.params.name, res)) return
122
123     return next()
124   }
125 ]
126
127 // ---------------------------------------------------------------------------
128
129 export {
130   listVideoAccountChannelsValidator,
131   videoChannelsAddValidator,
132   videoChannelsUpdateValidator,
133   videoChannelsRemoveValidator,
134   videoChannelsNameWithHostValidator,
135   localVideoChannelValidator
136 }
137
138 // ---------------------------------------------------------------------------
139
140 function checkUserCanDeleteVideoChannel (user: UserModel, videoChannel: VideoChannelModel, res: express.Response) {
141   if (videoChannel.Actor.isOwned() === false) {
142     res.status(403)
143               .json({ error: 'Cannot remove video channel of another server.' })
144               .end()
145
146     return false
147   }
148
149   // Check if the user can delete the video channel
150   // The user can delete it if s/he is an admin
151   // Or if s/he is the video channel's account
152   if (user.hasRight(UserRight.REMOVE_ANY_VIDEO_CHANNEL) === false && videoChannel.Account.userId !== user.id) {
153     res.status(403)
154               .json({ error: 'Cannot remove video channel of another user' })
155               .end()
156
157     return false
158   }
159
160   return true
161 }
162
163 async function checkVideoChannelIsNotTheLastOne (res: express.Response) {
164   const count = await VideoChannelModel.countByAccount(res.locals.oauth.token.User.Account.id)
165
166   if (count <= 1) {
167     res.status(409)
168       .json({ error: 'Cannot remove the last channel of this user' })
169       .end()
170
171     return false
172   }
173
174   return true
175 }