]>
Commit | Line | Data |
---|---|---|
72c7248b | 1 | import * as express from 'express' |
d4f1e94c | 2 | import { body, param } from 'express-validator/check' |
6e46de09 C |
3 | import { UserRight } from '../../../../shared' |
4 | import { isAccountNameWithHostExist } from '../../../helpers/custom-validators/accounts' | |
4e50b6a1 | 5 | import { |
06a05d5f | 6 | isLocalVideoChannelNameExist, |
7d8e778a | 7 | isVideoChannelDescriptionValid, |
7d8e778a | 8 | isVideoChannelNameValid, |
8a19bee1 | 9 | isVideoChannelNameWithHostExist, |
7d8e778a | 10 | isVideoChannelSupportValid |
6e46de09 C |
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' | |
72c7248b | 18 | |
38fa2065 | 19 | const listVideoAccountChannelsValidator = [ |
ad9e39fb | 20 | param('accountName').exists().withMessage('Should have a valid account name'), |
72c7248b | 21 | |
a2431b7d | 22 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
38fa2065 | 23 | logger.debug('Checking listVideoAccountChannelsValidator parameters', { parameters: req.body }) |
72c7248b | 24 | |
a2431b7d | 25 | if (areValidationErrors(req, res)) return |
ad9e39fb | 26 | if (!await isAccountNameWithHostExist(req.params.accountName, res)) return |
a2431b7d C |
27 | |
28 | return next() | |
72c7248b C |
29 | } |
30 | ] | |
31 | ||
32 | const videoChannelsAddValidator = [ | |
8a19bee1 | 33 | body('name').custom(isActorPreferredUsernameValid).withMessage('Should have a valid channel name'), |
08c1efbe | 34 | body('displayName').custom(isVideoChannelNameValid).withMessage('Should have a valid display name'), |
2422c46b C |
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'), | |
72c7248b | 37 | |
601527d7 | 38 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
72c7248b C |
39 | logger.debug('Checking videoChannelsAdd parameters', { parameters: req.body }) |
40 | ||
a2431b7d C |
41 | if (areValidationErrors(req, res)) return |
42 | ||
601527d7 C |
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 | ||
a2431b7d | 51 | return next() |
72c7248b C |
52 | } |
53 | ] | |
54 | ||
55 | const videoChannelsUpdateValidator = [ | |
8a19bee1 | 56 | param('nameWithHost').exists().withMessage('Should have an video channel name with host'), |
08c1efbe | 57 | body('displayName').optional().custom(isVideoChannelNameValid).withMessage('Should have a valid display name'), |
72c7248b | 58 | body('description').optional().custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'), |
2422c46b | 59 | body('support').optional().custom(isVideoChannelSupportValid).withMessage('Should have a valid support text'), |
72c7248b | 60 | |
a2431b7d | 61 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
72c7248b C |
62 | logger.debug('Checking videoChannelsUpdate parameters', { parameters: req.body }) |
63 | ||
a2431b7d | 64 | if (areValidationErrors(req, res)) return |
8a19bee1 | 65 | if (!await isVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return |
a2431b7d C |
66 | |
67 | // We need to make additional checks | |
d50acfab | 68 | if (res.locals.videoChannel.Actor.isOwned() === false) { |
a2431b7d C |
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() | |
72c7248b C |
81 | } |
82 | ] | |
83 | ||
84 | const videoChannelsRemoveValidator = [ | |
8a19bee1 | 85 | param('nameWithHost').exists().withMessage('Should have an video channel name with host'), |
72c7248b | 86 | |
a2431b7d | 87 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
72c7248b C |
88 | logger.debug('Checking videoChannelsRemove parameters', { parameters: req.params }) |
89 | ||
a2431b7d | 90 | if (areValidationErrors(req, res)) return |
8a19bee1 | 91 | if (!await isVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return |
a2431b7d | 92 | |
d48ff09d | 93 | if (!checkUserCanDeleteVideoChannel(res.locals.oauth.token.User, res.locals.videoChannel, res)) return |
a2431b7d C |
94 | if (!await checkVideoChannelIsNotTheLastOne(res)) return |
95 | ||
96 | return next() | |
72c7248b C |
97 | } |
98 | ] | |
99 | ||
8a19bee1 C |
100 | const videoChannelsNameWithHostValidator = [ |
101 | param('nameWithHost').exists().withMessage('Should have an video channel name with host'), | |
72c7248b | 102 | |
a2431b7d | 103 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
8a19bee1 | 104 | logger.debug('Checking videoChannelsNameWithHostValidator parameters', { parameters: req.params }) |
72c7248b | 105 | |
a2431b7d | 106 | if (areValidationErrors(req, res)) return |
6b738c7a | 107 | |
8a19bee1 | 108 | if (!await isVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return |
a2431b7d C |
109 | |
110 | return next() | |
72c7248b C |
111 | } |
112 | ] | |
113 | ||
06a05d5f C |
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 | ||
72c7248b C |
127 | // --------------------------------------------------------------------------- |
128 | ||
129 | export { | |
38fa2065 | 130 | listVideoAccountChannelsValidator, |
72c7248b C |
131 | videoChannelsAddValidator, |
132 | videoChannelsUpdateValidator, | |
133 | videoChannelsRemoveValidator, | |
8a19bee1 | 134 | videoChannelsNameWithHostValidator, |
06a05d5f | 135 | localVideoChannelValidator |
72c7248b C |
136 | } |
137 | ||
138 | // --------------------------------------------------------------------------- | |
139 | ||
3fd3ab2d | 140 | function checkUserCanDeleteVideoChannel (user: UserModel, videoChannel: VideoChannelModel, res: express.Response) { |
50d6de9c | 141 | if (videoChannel.Actor.isOwned() === false) { |
a2431b7d | 142 | res.status(403) |
60862425 | 143 | .json({ error: 'Cannot remove video channel of another server.' }) |
72c7248b | 144 | .end() |
a2431b7d C |
145 | |
146 | return false | |
72c7248b C |
147 | } |
148 | ||
149 | // Check if the user can delete the video channel | |
150 | // The user can delete it if s/he is an admin | |
38fa2065 | 151 | // Or if s/he is the video channel's account |
a2431b7d C |
152 | if (user.hasRight(UserRight.REMOVE_ANY_VIDEO_CHANNEL) === false && videoChannel.Account.userId !== user.id) { |
153 | res.status(403) | |
72c7248b C |
154 | .json({ error: 'Cannot remove video channel of another user' }) |
155 | .end() | |
a2431b7d C |
156 | |
157 | return false | |
72c7248b C |
158 | } |
159 | ||
a2431b7d | 160 | return true |
72c7248b C |
161 | } |
162 | ||
a2431b7d | 163 | async function checkVideoChannelIsNotTheLastOne (res: express.Response) { |
3fd3ab2d | 164 | const count = await VideoChannelModel.countByAccount(res.locals.oauth.token.User.Account.id) |
a2431b7d C |
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 | |
72c7248b | 175 | } |