]>
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, |
4e50b6a1 C |
8 | isVideoChannelNameValid |
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' |
72c7248b | 14 | |
38fa2065 C |
15 | const listVideoAccountChannelsValidator = [ |
16 | param('accountId').custom(isIdOrUUIDValid).withMessage('Should have a valid account id'), | |
72c7248b | 17 | |
a2431b7d | 18 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
38fa2065 | 19 | logger.debug('Checking listVideoAccountChannelsValidator parameters', { parameters: req.body }) |
72c7248b | 20 | |
a2431b7d C |
21 | if (areValidationErrors(req, res)) return |
22 | if (!await isAccountIdExist(req.params.accountId, res)) return | |
23 | ||
24 | return next() | |
72c7248b C |
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 | ||
a2431b7d C |
35 | if (areValidationErrors(req, res)) return |
36 | ||
37 | return next() | |
72c7248b C |
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 | ||
a2431b7d | 46 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
72c7248b C |
47 | logger.debug('Checking videoChannelsUpdate parameters', { parameters: req.body }) |
48 | ||
a2431b7d C |
49 | if (areValidationErrors(req, res)) return |
50 | if (!await isVideoChannelExist(req.params.id, res)) return | |
51 | ||
52 | // We need to make additional checks | |
d50acfab | 53 | if (res.locals.videoChannel.Actor.isOwned() === false) { |
a2431b7d C |
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() | |
72c7248b C |
66 | } |
67 | ] | |
68 | ||
69 | const videoChannelsRemoveValidator = [ | |
70 | param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'), | |
71 | ||
a2431b7d | 72 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
72c7248b C |
73 | logger.debug('Checking videoChannelsRemove parameters', { parameters: req.params }) |
74 | ||
a2431b7d C |
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 | |
d48ff09d | 79 | if (!checkUserCanDeleteVideoChannel(res.locals.oauth.token.User, res.locals.videoChannel, res)) return |
a2431b7d C |
80 | if (!await checkVideoChannelIsNotTheLastOne(res)) return |
81 | ||
82 | return next() | |
72c7248b C |
83 | } |
84 | ] | |
85 | ||
20494f12 | 86 | const videoChannelsGetValidator = [ |
72c7248b C |
87 | param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'), |
88 | ||
a2431b7d | 89 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
72c7248b C |
90 | logger.debug('Checking videoChannelsGet parameters', { parameters: req.params }) |
91 | ||
a2431b7d C |
92 | if (areValidationErrors(req, res)) return |
93 | if (!await isVideoChannelExist(req.params.id, res)) return | |
94 | ||
95 | return next() | |
72c7248b C |
96 | } |
97 | ] | |
98 | ||
99 | // --------------------------------------------------------------------------- | |
100 | ||
101 | export { | |
38fa2065 | 102 | listVideoAccountChannelsValidator, |
72c7248b C |
103 | videoChannelsAddValidator, |
104 | videoChannelsUpdateValidator, | |
105 | videoChannelsRemoveValidator, | |
50d6de9c | 106 | videoChannelsGetValidator |
72c7248b C |
107 | } |
108 | ||
109 | // --------------------------------------------------------------------------- | |
110 | ||
3fd3ab2d | 111 | function checkUserCanDeleteVideoChannel (user: UserModel, videoChannel: VideoChannelModel, res: express.Response) { |
50d6de9c | 112 | if (videoChannel.Actor.isOwned() === false) { |
a2431b7d | 113 | res.status(403) |
60862425 | 114 | .json({ error: 'Cannot remove video channel of another server.' }) |
72c7248b | 115 | .end() |
a2431b7d C |
116 | |
117 | return false | |
72c7248b C |
118 | } |
119 | ||
120 | // Check if the user can delete the video channel | |
121 | // The user can delete it if s/he is an admin | |
38fa2065 | 122 | // Or if s/he is the video channel's account |
a2431b7d C |
123 | if (user.hasRight(UserRight.REMOVE_ANY_VIDEO_CHANNEL) === false && videoChannel.Account.userId !== user.id) { |
124 | res.status(403) | |
72c7248b C |
125 | .json({ error: 'Cannot remove video channel of another user' }) |
126 | .end() | |
a2431b7d C |
127 | |
128 | return false | |
72c7248b C |
129 | } |
130 | ||
a2431b7d | 131 | return true |
72c7248b C |
132 | } |
133 | ||
a2431b7d | 134 | async function checkVideoChannelIsNotTheLastOne (res: express.Response) { |
3fd3ab2d | 135 | const count = await VideoChannelModel.countByAccount(res.locals.oauth.token.User.Account.id) |
a2431b7d C |
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 | |
72c7248b | 146 | } |