]>
Commit | Line | Data |
---|---|---|
72c7248b | 1 | import * as express from 'express' |
d4f1e94c C |
2 | import { body, param } from 'express-validator/check' |
3 | import { UserRight } from '../../../shared' | |
3fd3ab2d | 4 | import { isAccountIdExist } from '../../helpers/custom-validators/accounts' |
50d6de9c | 5 | import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc' |
4e50b6a1 | 6 | import { |
da854ddd | 7 | isVideoChannelDescriptionValid, isVideoChannelExist, |
2422c46b | 8 | isVideoChannelNameValid, isVideoChannelSupportValid |
4e50b6a1 | 9 | } from '../../helpers/custom-validators/video-channels' |
da854ddd | 10 | import { logger } from '../../helpers/logger' |
3fd3ab2d C |
11 | import { UserModel } from '../../models/account/user' |
12 | import { VideoChannelModel } from '../../models/video/video-channel' | |
a2431b7d | 13 | import { areValidationErrors } from './utils' |
6b738c7a | 14 | import { AccountModel } from '../../models/account/account' |
72c7248b | 15 | |
38fa2065 C |
16 | const listVideoAccountChannelsValidator = [ |
17 | param('accountId').custom(isIdOrUUIDValid).withMessage('Should have a valid account id'), | |
72c7248b | 18 | |
a2431b7d | 19 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
38fa2065 | 20 | logger.debug('Checking listVideoAccountChannelsValidator parameters', { parameters: req.body }) |
72c7248b | 21 | |
a2431b7d C |
22 | if (areValidationErrors(req, res)) return |
23 | if (!await isAccountIdExist(req.params.accountId, res)) return | |
24 | ||
25 | return next() | |
72c7248b C |
26 | } |
27 | ] | |
28 | ||
29 | const videoChannelsAddValidator = [ | |
30 | body('name').custom(isVideoChannelNameValid).withMessage('Should have a valid name'), | |
2422c46b C |
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'), | |
72c7248b C |
33 | |
34 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
35 | logger.debug('Checking videoChannelsAdd parameters', { parameters: req.body }) | |
36 | ||
a2431b7d C |
37 | if (areValidationErrors(req, res)) return |
38 | ||
39 | return next() | |
72c7248b C |
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'), | |
2422c46b | 47 | body('support').optional().custom(isVideoChannelSupportValid).withMessage('Should have a valid support text'), |
72c7248b | 48 | |
a2431b7d | 49 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
72c7248b C |
50 | logger.debug('Checking videoChannelsUpdate parameters', { parameters: req.body }) |
51 | ||
a2431b7d C |
52 | if (areValidationErrors(req, res)) return |
53 | if (!await isVideoChannelExist(req.params.id, res)) return | |
54 | ||
55 | // We need to make additional checks | |
d50acfab | 56 | if (res.locals.videoChannel.Actor.isOwned() === false) { |
a2431b7d C |
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() | |
72c7248b C |
69 | } |
70 | ] | |
71 | ||
72 | const videoChannelsRemoveValidator = [ | |
73 | param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'), | |
74 | ||
a2431b7d | 75 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
72c7248b C |
76 | logger.debug('Checking videoChannelsRemove parameters', { parameters: req.params }) |
77 | ||
a2431b7d C |
78 | if (areValidationErrors(req, res)) return |
79 | if (!await isVideoChannelExist(req.params.id, res)) return | |
80 | ||
d48ff09d | 81 | if (!checkUserCanDeleteVideoChannel(res.locals.oauth.token.User, res.locals.videoChannel, res)) return |
a2431b7d C |
82 | if (!await checkVideoChannelIsNotTheLastOne(res)) return |
83 | ||
84 | return next() | |
72c7248b C |
85 | } |
86 | ] | |
87 | ||
20494f12 | 88 | const videoChannelsGetValidator = [ |
72c7248b C |
89 | param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'), |
90 | ||
a2431b7d | 91 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
72c7248b C |
92 | logger.debug('Checking videoChannelsGet parameters', { parameters: req.params }) |
93 | ||
a2431b7d | 94 | if (areValidationErrors(req, res)) return |
6b738c7a | 95 | |
a2431b7d C |
96 | if (!await isVideoChannelExist(req.params.id, res)) return |
97 | ||
98 | return next() | |
72c7248b C |
99 | } |
100 | ] | |
101 | ||
102 | // --------------------------------------------------------------------------- | |
103 | ||
104 | export { | |
38fa2065 | 105 | listVideoAccountChannelsValidator, |
72c7248b C |
106 | videoChannelsAddValidator, |
107 | videoChannelsUpdateValidator, | |
108 | videoChannelsRemoveValidator, | |
50d6de9c | 109 | videoChannelsGetValidator |
72c7248b C |
110 | } |
111 | ||
112 | // --------------------------------------------------------------------------- | |
113 | ||
3fd3ab2d | 114 | function checkUserCanDeleteVideoChannel (user: UserModel, videoChannel: VideoChannelModel, res: express.Response) { |
50d6de9c | 115 | if (videoChannel.Actor.isOwned() === false) { |
a2431b7d | 116 | res.status(403) |
60862425 | 117 | .json({ error: 'Cannot remove video channel of another server.' }) |
72c7248b | 118 | .end() |
a2431b7d C |
119 | |
120 | return false | |
72c7248b C |
121 | } |
122 | ||
123 | // Check if the user can delete the video channel | |
124 | // The user can delete it if s/he is an admin | |
38fa2065 | 125 | // Or if s/he is the video channel's account |
a2431b7d C |
126 | if (user.hasRight(UserRight.REMOVE_ANY_VIDEO_CHANNEL) === false && videoChannel.Account.userId !== user.id) { |
127 | res.status(403) | |
72c7248b C |
128 | .json({ error: 'Cannot remove video channel of another user' }) |
129 | .end() | |
a2431b7d C |
130 | |
131 | return false | |
72c7248b C |
132 | } |
133 | ||
a2431b7d | 134 | return true |
72c7248b C |
135 | } |
136 | ||
a2431b7d | 137 | async function checkVideoChannelIsNotTheLastOne (res: express.Response) { |
3fd3ab2d | 138 | const count = await VideoChannelModel.countByAccount(res.locals.oauth.token.User.Account.id) |
a2431b7d C |
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 | |
72c7248b | 149 | } |