]>
Commit | Line | Data |
---|---|---|
72c7248b | 1 | import * as express from 'express' |
747c5628 | 2 | import { body, param, query } from 'express-validator' |
5a61ffbb | 3 | import { VIDEO_CHANNELS } from '@server/initializers/constants' |
26d6bf65 | 4 | import { MChannelAccountDefault, MUser } from '@server/types/models' |
6e46de09 | 5 | import { UserRight } from '../../../../shared' |
c0e8b12e | 6 | import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes' |
5a61ffbb C |
7 | import { isActorPreferredUsernameValid } from '../../../helpers/custom-validators/activitypub/actor' |
8 | import { isBooleanValid, toBooleanOrNull } from '../../../helpers/custom-validators/misc' | |
4e50b6a1 | 9 | import { |
7d8e778a | 10 | isVideoChannelDescriptionValid, |
7d8e778a C |
11 | isVideoChannelNameValid, |
12 | isVideoChannelSupportValid | |
6e46de09 C |
13 | } from '../../../helpers/custom-validators/video-channels' |
14 | import { logger } from '../../../helpers/logger' | |
7d9ba5c0 | 15 | import { ActorModel } from '../../../models/actor/actor' |
6e46de09 | 16 | import { VideoChannelModel } from '../../../models/video/video-channel' |
10363c74 | 17 | import { areValidationErrors, doesLocalVideoChannelNameExist, doesVideoChannelNameWithHostExist } from '../shared' |
72c7248b | 18 | |
72c7248b | 19 | const videoChannelsAddValidator = [ |
8a19bee1 | 20 | body('name').custom(isActorPreferredUsernameValid).withMessage('Should have a valid channel name'), |
08c1efbe | 21 | body('displayName').custom(isVideoChannelNameValid).withMessage('Should have a valid display name'), |
2422c46b C |
22 | body('description').optional().custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'), |
23 | body('support').optional().custom(isVideoChannelSupportValid).withMessage('Should have a valid support text'), | |
72c7248b | 24 | |
601527d7 | 25 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
72c7248b C |
26 | logger.debug('Checking videoChannelsAdd parameters', { parameters: req.body }) |
27 | ||
a2431b7d C |
28 | if (areValidationErrors(req, res)) return |
29 | ||
601527d7 C |
30 | const actor = await ActorModel.loadLocalByName(req.body.name) |
31 | if (actor) { | |
76148b27 RK |
32 | res.fail({ |
33 | status: HttpStatusCode.CONFLICT_409, | |
34 | message: 'Another actor (account/channel) with this name on this instance already exists or has already existed.' | |
35 | }) | |
601527d7 C |
36 | return false |
37 | } | |
38 | ||
a3ce4ae8 | 39 | const count = await VideoChannelModel.countByAccount(res.locals.oauth.token.User.Account.id) |
5197732b | 40 | if (count >= VIDEO_CHANNELS.MAX_PER_USER) { |
76148b27 | 41 | res.fail({ message: `You cannot create more than ${VIDEO_CHANNELS.MAX_PER_USER} channels` }) |
a3ce4ae8 C |
42 | return false |
43 | } | |
44 | ||
a2431b7d | 45 | return next() |
72c7248b C |
46 | } |
47 | ] | |
48 | ||
49 | const videoChannelsUpdateValidator = [ | |
8a19bee1 | 50 | param('nameWithHost').exists().withMessage('Should have an video channel name with host'), |
7d14d4d2 C |
51 | body('displayName') |
52 | .optional() | |
53 | .custom(isVideoChannelNameValid).withMessage('Should have a valid display name'), | |
54 | body('description') | |
55 | .optional() | |
56 | .custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'), | |
57 | body('support') | |
58 | .optional() | |
59 | .custom(isVideoChannelSupportValid).withMessage('Should have a valid support text'), | |
60 | body('bulkVideosSupportUpdate') | |
61 | .optional() | |
62 | .custom(isBooleanValid).withMessage('Should have a valid bulkVideosSupportUpdate boolean field'), | |
72c7248b | 63 | |
a2431b7d | 64 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
72c7248b C |
65 | logger.debug('Checking videoChannelsUpdate parameters', { parameters: req.body }) |
66 | ||
a2431b7d | 67 | if (areValidationErrors(req, res)) return |
0f6acda1 | 68 | if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return |
a2431b7d C |
69 | |
70 | // We need to make additional checks | |
d50acfab | 71 | if (res.locals.videoChannel.Actor.isOwned() === false) { |
76148b27 RK |
72 | return res.fail({ |
73 | status: HttpStatusCode.FORBIDDEN_403, | |
74 | message: 'Cannot update video channel of another server' | |
75 | }) | |
a2431b7d C |
76 | } |
77 | ||
78 | if (res.locals.videoChannel.Account.userId !== res.locals.oauth.token.User.id) { | |
76148b27 RK |
79 | return res.fail({ |
80 | status: HttpStatusCode.FORBIDDEN_403, | |
81 | message: 'Cannot update video channel of another user' | |
82 | }) | |
a2431b7d C |
83 | } |
84 | ||
85 | return next() | |
72c7248b C |
86 | } |
87 | ] | |
88 | ||
89 | const videoChannelsRemoveValidator = [ | |
8a19bee1 | 90 | param('nameWithHost').exists().withMessage('Should have an video channel name with host'), |
72c7248b | 91 | |
a2431b7d | 92 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
72c7248b C |
93 | logger.debug('Checking videoChannelsRemove parameters', { parameters: req.params }) |
94 | ||
a2431b7d | 95 | if (areValidationErrors(req, res)) return |
0f6acda1 | 96 | if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return |
a2431b7d | 97 | |
d48ff09d | 98 | if (!checkUserCanDeleteVideoChannel(res.locals.oauth.token.User, res.locals.videoChannel, res)) return |
a2431b7d C |
99 | if (!await checkVideoChannelIsNotTheLastOne(res)) return |
100 | ||
101 | return next() | |
72c7248b C |
102 | } |
103 | ] | |
104 | ||
8a19bee1 C |
105 | const videoChannelsNameWithHostValidator = [ |
106 | param('nameWithHost').exists().withMessage('Should have an video channel name with host'), | |
72c7248b | 107 | |
a2431b7d | 108 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
8a19bee1 | 109 | logger.debug('Checking videoChannelsNameWithHostValidator parameters', { parameters: req.params }) |
72c7248b | 110 | |
a2431b7d | 111 | if (areValidationErrors(req, res)) return |
6b738c7a | 112 | |
0f6acda1 | 113 | if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return |
a2431b7d C |
114 | |
115 | return next() | |
72c7248b C |
116 | } |
117 | ] | |
118 | ||
06a05d5f C |
119 | const localVideoChannelValidator = [ |
120 | param('name').custom(isVideoChannelNameValid).withMessage('Should have a valid video channel name'), | |
121 | ||
122 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
123 | logger.debug('Checking localVideoChannelValidator parameters', { parameters: req.params }) | |
124 | ||
125 | if (areValidationErrors(req, res)) return | |
0f6acda1 | 126 | if (!await doesLocalVideoChannelNameExist(req.params.name, res)) return |
06a05d5f C |
127 | |
128 | return next() | |
129 | } | |
130 | ] | |
131 | ||
747c5628 | 132 | const videoChannelStatsValidator = [ |
5a61ffbb C |
133 | query('withStats') |
134 | .optional() | |
135 | .customSanitizer(toBooleanOrNull) | |
136 | .custom(isBooleanValid).withMessage('Should have a valid stats flag'), | |
747c5628 RK |
137 | |
138 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
139 | if (areValidationErrors(req, res)) return | |
140 | return next() | |
141 | } | |
142 | ] | |
143 | ||
37a44fc9 C |
144 | const videoChannelsListValidator = [ |
145 | query('search').optional().not().isEmpty().withMessage('Should have a valid search'), | |
146 | ||
147 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
148 | logger.debug('Checking video channels search query', { parameters: req.query }) | |
149 | ||
150 | if (areValidationErrors(req, res)) return | |
151 | ||
152 | return next() | |
153 | } | |
154 | ] | |
155 | ||
72c7248b C |
156 | // --------------------------------------------------------------------------- |
157 | ||
158 | export { | |
72c7248b C |
159 | videoChannelsAddValidator, |
160 | videoChannelsUpdateValidator, | |
161 | videoChannelsRemoveValidator, | |
8a19bee1 | 162 | videoChannelsNameWithHostValidator, |
37a44fc9 | 163 | videoChannelsListValidator, |
747c5628 RK |
164 | localVideoChannelValidator, |
165 | videoChannelStatsValidator | |
72c7248b C |
166 | } |
167 | ||
168 | // --------------------------------------------------------------------------- | |
169 | ||
0283eaac | 170 | function checkUserCanDeleteVideoChannel (user: MUser, videoChannel: MChannelAccountDefault, res: express.Response) { |
50d6de9c | 171 | if (videoChannel.Actor.isOwned() === false) { |
76148b27 RK |
172 | res.fail({ |
173 | status: HttpStatusCode.FORBIDDEN_403, | |
174 | message: 'Cannot remove video channel of another server.' | |
175 | }) | |
a2431b7d | 176 | return false |
72c7248b C |
177 | } |
178 | ||
179 | // Check if the user can delete the video channel | |
180 | // The user can delete it if s/he is an admin | |
38fa2065 | 181 | // Or if s/he is the video channel's account |
a2431b7d | 182 | if (user.hasRight(UserRight.REMOVE_ANY_VIDEO_CHANNEL) === false && videoChannel.Account.userId !== user.id) { |
76148b27 RK |
183 | res.fail({ |
184 | status: HttpStatusCode.FORBIDDEN_403, | |
185 | message: 'Cannot remove video channel of another user' | |
186 | }) | |
a2431b7d | 187 | return false |
72c7248b C |
188 | } |
189 | ||
a2431b7d | 190 | return true |
72c7248b C |
191 | } |
192 | ||
a2431b7d | 193 | async function checkVideoChannelIsNotTheLastOne (res: express.Response) { |
3fd3ab2d | 194 | const count = await VideoChannelModel.countByAccount(res.locals.oauth.token.User.Account.id) |
a2431b7d C |
195 | |
196 | if (count <= 1) { | |
76148b27 RK |
197 | res.fail({ |
198 | status: HttpStatusCode.CONFLICT_409, | |
199 | message: 'Cannot remove the last channel of this user' | |
200 | }) | |
a2431b7d C |
201 | return false |
202 | } | |
203 | ||
204 | return true | |
72c7248b | 205 | } |