]>
Commit | Line | Data |
---|---|---|
72c7248b C |
1 | import { body, param } from 'express-validator/check' |
2 | import * as express from 'express' | |
3 | ||
4 | import { checkErrors } from './utils' | |
5 | import { database as db } from '../../initializers' | |
6 | import { | |
7 | logger, | |
8 | isIdOrUUIDValid, | |
9 | isVideoChannelDescriptionValid, | |
10 | isVideoChannelNameValid, | |
11 | checkVideoChannelExists, | |
38fa2065 | 12 | checkVideoAccountExists |
72c7248b | 13 | } from '../../helpers' |
954605a8 C |
14 | import { UserInstance } from '../../models' |
15 | import { UserRight } from '../../../shared' | |
72c7248b | 16 | |
38fa2065 C |
17 | const listVideoAccountChannelsValidator = [ |
18 | param('accountId').custom(isIdOrUUIDValid).withMessage('Should have a valid account id'), | |
72c7248b C |
19 | |
20 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
38fa2065 | 21 | logger.debug('Checking listVideoAccountChannelsValidator parameters', { parameters: req.body }) |
72c7248b C |
22 | |
23 | checkErrors(req, res, () => { | |
38fa2065 | 24 | checkVideoAccountExists(req.params.accountId, res, next) |
72c7248b C |
25 | }) |
26 | } | |
27 | ] | |
28 | ||
29 | const videoChannelsAddValidator = [ | |
30 | body('name').custom(isVideoChannelNameValid).withMessage('Should have a valid name'), | |
31 | body('description').custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'), | |
32 | ||
33 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
34 | logger.debug('Checking videoChannelsAdd parameters', { parameters: req.body }) | |
35 | ||
36 | checkErrors(req, res, next) | |
37 | } | |
38 | ] | |
39 | ||
40 | const videoChannelsUpdateValidator = [ | |
41 | param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'), | |
42 | body('name').optional().custom(isVideoChannelNameValid).withMessage('Should have a valid name'), | |
43 | body('description').optional().custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'), | |
44 | ||
45 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
46 | logger.debug('Checking videoChannelsUpdate parameters', { parameters: req.body }) | |
47 | ||
48 | checkErrors(req, res, () => { | |
49 | checkVideoChannelExists(req.params.id, res, () => { | |
50 | // We need to make additional checks | |
51 | if (res.locals.videoChannel.isOwned() === false) { | |
52 | return res.status(403) | |
60862425 | 53 | .json({ error: 'Cannot update video channel of another server' }) |
72c7248b C |
54 | .end() |
55 | } | |
56 | ||
38fa2065 | 57 | if (res.locals.videoChannel.Account.userId !== res.locals.oauth.token.User.id) { |
72c7248b C |
58 | return res.status(403) |
59 | .json({ error: 'Cannot update video channel of another user' }) | |
60 | .end() | |
61 | } | |
62 | ||
63 | next() | |
64 | }) | |
65 | }) | |
66 | } | |
67 | ] | |
68 | ||
69 | const videoChannelsRemoveValidator = [ | |
70 | param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'), | |
71 | ||
72 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
73 | logger.debug('Checking videoChannelsRemove parameters', { parameters: req.params }) | |
74 | ||
75 | checkErrors(req, res, () => { | |
76 | checkVideoChannelExists(req.params.id, res, () => { | |
77 | // Check if the user who did the request is able to delete the video | |
78 | checkUserCanDeleteVideoChannel(res, () => { | |
79 | checkVideoChannelIsNotTheLastOne(res, next) | |
80 | }) | |
81 | }) | |
82 | }) | |
83 | } | |
84 | ] | |
85 | ||
86 | const videoChannelGetValidator = [ | |
87 | param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'), | |
88 | ||
89 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
90 | logger.debug('Checking videoChannelsGet parameters', { parameters: req.params }) | |
91 | ||
92 | checkErrors(req, res, () => { | |
93 | checkVideoChannelExists(req.params.id, res, next) | |
94 | }) | |
95 | } | |
96 | ] | |
97 | ||
98 | // --------------------------------------------------------------------------- | |
99 | ||
100 | export { | |
38fa2065 | 101 | listVideoAccountChannelsValidator, |
72c7248b C |
102 | videoChannelsAddValidator, |
103 | videoChannelsUpdateValidator, | |
104 | videoChannelsRemoveValidator, | |
105 | videoChannelGetValidator | |
106 | } | |
107 | ||
108 | // --------------------------------------------------------------------------- | |
109 | ||
110 | function checkUserCanDeleteVideoChannel (res: express.Response, callback: () => void) { | |
954605a8 | 111 | const user: UserInstance = res.locals.oauth.token.User |
72c7248b C |
112 | |
113 | // Retrieve the user who did the request | |
114 | if (res.locals.videoChannel.isOwned() === false) { | |
115 | return res.status(403) | |
60862425 | 116 | .json({ error: 'Cannot remove video channel of another server.' }) |
72c7248b C |
117 | .end() |
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 C |
122 | // Or if s/he is the video channel's account |
123 | if (user.hasRight(UserRight.REMOVE_ANY_VIDEO_CHANNEL) === false && res.locals.videoChannel.Account.userId !== user.id) { | |
72c7248b C |
124 | return res.status(403) |
125 | .json({ error: 'Cannot remove video channel of another user' }) | |
126 | .end() | |
127 | } | |
128 | ||
129 | // If we reach this comment, we can delete the video | |
130 | callback() | |
131 | } | |
132 | ||
133 | function checkVideoChannelIsNotTheLastOne (res: express.Response, callback: () => void) { | |
38fa2065 | 134 | db.VideoChannel.countByAccount(res.locals.oauth.token.User.Account.id) |
72c7248b C |
135 | .then(count => { |
136 | if (count <= 1) { | |
137 | return res.status(409) | |
138 | .json({ error: 'Cannot remove the last channel of this user' }) | |
139 | .end() | |
140 | } | |
141 | ||
142 | callback() | |
143 | }) | |
144 | } |