]>
Commit | Line | Data |
---|---|---|
72c7248b | 1 | import * as express from 'express' |
d4f1e94c | 2 | import { body, param } from 'express-validator/check' |
6e46de09 | 3 | import { UserRight } from '../../../../shared' |
4e50b6a1 | 4 | import { |
0f6acda1 | 5 | doesLocalVideoChannelNameExist, |
74dc3bca | 6 | doesVideoChannelNameWithHostExist, |
7d8e778a | 7 | isVideoChannelDescriptionValid, |
7d8e778a C |
8 | isVideoChannelNameValid, |
9 | isVideoChannelSupportValid | |
6e46de09 C |
10 | } from '../../../helpers/custom-validators/video-channels' |
11 | import { logger } from '../../../helpers/logger' | |
12 | import { UserModel } from '../../../models/account/user' | |
13 | import { VideoChannelModel } from '../../../models/video/video-channel' | |
14 | import { areValidationErrors } from '../utils' | |
15 | import { isActorPreferredUsernameValid } from '../../../helpers/custom-validators/activitypub/actor' | |
16 | import { ActorModel } from '../../../models/activitypub/actor' | |
72c7248b | 17 | |
72c7248b | 18 | const videoChannelsAddValidator = [ |
8a19bee1 | 19 | body('name').custom(isActorPreferredUsernameValid).withMessage('Should have a valid channel name'), |
08c1efbe | 20 | body('displayName').custom(isVideoChannelNameValid).withMessage('Should have a valid display name'), |
2422c46b C |
21 | body('description').optional().custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'), |
22 | body('support').optional().custom(isVideoChannelSupportValid).withMessage('Should have a valid support text'), | |
72c7248b | 23 | |
601527d7 | 24 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
72c7248b C |
25 | logger.debug('Checking videoChannelsAdd parameters', { parameters: req.body }) |
26 | ||
a2431b7d C |
27 | if (areValidationErrors(req, res)) return |
28 | ||
601527d7 C |
29 | const actor = await ActorModel.loadLocalByName(req.body.name) |
30 | if (actor) { | |
31 | res.status(409) | |
32 | .send({ error: 'Another actor (account/channel) with this name on this instance already exists or has already existed.' }) | |
33 | .end() | |
34 | return false | |
35 | } | |
36 | ||
a2431b7d | 37 | return next() |
72c7248b C |
38 | } |
39 | ] | |
40 | ||
41 | const videoChannelsUpdateValidator = [ | |
8a19bee1 | 42 | param('nameWithHost').exists().withMessage('Should have an video channel name with host'), |
08c1efbe | 43 | body('displayName').optional().custom(isVideoChannelNameValid).withMessage('Should have a valid display name'), |
72c7248b | 44 | body('description').optional().custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'), |
2422c46b | 45 | body('support').optional().custom(isVideoChannelSupportValid).withMessage('Should have a valid support text'), |
72c7248b | 46 | |
a2431b7d | 47 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
72c7248b C |
48 | logger.debug('Checking videoChannelsUpdate parameters', { parameters: req.body }) |
49 | ||
a2431b7d | 50 | if (areValidationErrors(req, res)) return |
0f6acda1 | 51 | if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return |
a2431b7d C |
52 | |
53 | // We need to make additional checks | |
d50acfab | 54 | if (res.locals.videoChannel.Actor.isOwned() === false) { |
a2431b7d C |
55 | return res.status(403) |
56 | .json({ error: 'Cannot update video channel of another server' }) | |
57 | .end() | |
58 | } | |
59 | ||
60 | if (res.locals.videoChannel.Account.userId !== res.locals.oauth.token.User.id) { | |
61 | return res.status(403) | |
62 | .json({ error: 'Cannot update video channel of another user' }) | |
63 | .end() | |
64 | } | |
65 | ||
66 | return next() | |
72c7248b C |
67 | } |
68 | ] | |
69 | ||
70 | const videoChannelsRemoveValidator = [ | |
8a19bee1 | 71 | param('nameWithHost').exists().withMessage('Should have an video channel name with host'), |
72c7248b | 72 | |
a2431b7d | 73 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
72c7248b C |
74 | logger.debug('Checking videoChannelsRemove parameters', { parameters: req.params }) |
75 | ||
a2431b7d | 76 | if (areValidationErrors(req, res)) return |
0f6acda1 | 77 | if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return |
a2431b7d | 78 | |
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 | ||
8a19bee1 C |
86 | const videoChannelsNameWithHostValidator = [ |
87 | param('nameWithHost').exists().withMessage('Should have an video channel name with host'), | |
72c7248b | 88 | |
a2431b7d | 89 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
8a19bee1 | 90 | logger.debug('Checking videoChannelsNameWithHostValidator parameters', { parameters: req.params }) |
72c7248b | 91 | |
a2431b7d | 92 | if (areValidationErrors(req, res)) return |
6b738c7a | 93 | |
0f6acda1 | 94 | if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return |
a2431b7d C |
95 | |
96 | return next() | |
72c7248b C |
97 | } |
98 | ] | |
99 | ||
06a05d5f C |
100 | const localVideoChannelValidator = [ |
101 | param('name').custom(isVideoChannelNameValid).withMessage('Should have a valid video channel name'), | |
102 | ||
103 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
104 | logger.debug('Checking localVideoChannelValidator parameters', { parameters: req.params }) | |
105 | ||
106 | if (areValidationErrors(req, res)) return | |
0f6acda1 | 107 | if (!await doesLocalVideoChannelNameExist(req.params.name, res)) return |
06a05d5f C |
108 | |
109 | return next() | |
110 | } | |
111 | ] | |
112 | ||
72c7248b C |
113 | // --------------------------------------------------------------------------- |
114 | ||
115 | export { | |
72c7248b C |
116 | videoChannelsAddValidator, |
117 | videoChannelsUpdateValidator, | |
118 | videoChannelsRemoveValidator, | |
8a19bee1 | 119 | videoChannelsNameWithHostValidator, |
06a05d5f | 120 | localVideoChannelValidator |
72c7248b C |
121 | } |
122 | ||
123 | // --------------------------------------------------------------------------- | |
124 | ||
3fd3ab2d | 125 | function checkUserCanDeleteVideoChannel (user: UserModel, videoChannel: VideoChannelModel, res: express.Response) { |
50d6de9c | 126 | if (videoChannel.Actor.isOwned() === false) { |
a2431b7d | 127 | res.status(403) |
60862425 | 128 | .json({ error: 'Cannot remove video channel of another server.' }) |
72c7248b | 129 | .end() |
a2431b7d C |
130 | |
131 | return false | |
72c7248b C |
132 | } |
133 | ||
134 | // Check if the user can delete the video channel | |
135 | // The user can delete it if s/he is an admin | |
38fa2065 | 136 | // Or if s/he is the video channel's account |
a2431b7d C |
137 | if (user.hasRight(UserRight.REMOVE_ANY_VIDEO_CHANNEL) === false && videoChannel.Account.userId !== user.id) { |
138 | res.status(403) | |
72c7248b C |
139 | .json({ error: 'Cannot remove video channel of another user' }) |
140 | .end() | |
a2431b7d C |
141 | |
142 | return false | |
72c7248b C |
143 | } |
144 | ||
a2431b7d | 145 | return true |
72c7248b C |
146 | } |
147 | ||
a2431b7d | 148 | async function checkVideoChannelIsNotTheLastOne (res: express.Response) { |
3fd3ab2d | 149 | const count = await VideoChannelModel.countByAccount(res.locals.oauth.token.User.Account.id) |
a2431b7d C |
150 | |
151 | if (count <= 1) { | |
152 | res.status(409) | |
153 | .json({ error: 'Cannot remove the last channel of this user' }) | |
154 | .end() | |
155 | ||
156 | return false | |
157 | } | |
158 | ||
159 | return true | |
72c7248b | 160 | } |