]>
Commit | Line | Data |
---|---|---|
41fb13c3 | 1 | import express from 'express' |
747c5628 | 2 | import { body, param, query } from 'express-validator' |
26d6bf65 | 3 | import { MChannelAccountDefault, MUser } from '@server/types/models' |
6e46de09 | 4 | import { UserRight } from '../../../../shared' |
c0e8b12e | 5 | import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes' |
5a61ffbb | 6 | import { isBooleanValid, toBooleanOrNull } from '../../../helpers/custom-validators/misc' |
4e50b6a1 | 7 | import { |
7d8e778a | 8 | isVideoChannelDescriptionValid, |
27db7840 C |
9 | isVideoChannelDisplayNameValid, |
10 | isVideoChannelSupportValid, | |
11 | isVideoChannelUsernameValid | |
6e46de09 C |
12 | } from '../../../helpers/custom-validators/video-channels' |
13 | import { logger } from '../../../helpers/logger' | |
7d9ba5c0 | 14 | import { ActorModel } from '../../../models/actor/actor' |
6e46de09 | 15 | import { VideoChannelModel } from '../../../models/video/video-channel' |
10363c74 | 16 | import { areValidationErrors, doesLocalVideoChannelNameExist, doesVideoChannelNameWithHostExist } from '../shared' |
754b6f5f | 17 | import { CONFIG } from '@server/initializers/config' |
72c7248b | 18 | |
72c7248b | 19 | const videoChannelsAddValidator = [ |
27db7840 C |
20 | body('name').custom(isVideoChannelUsernameValid).withMessage('Should have a valid channel name'), |
21 | body('displayName').custom(isVideoChannelDisplayNameValid).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) |
754b6f5f FC |
40 | if (count >= CONFIG.VIDEO_CHANNELS.MAX_PER_USER) { |
41 | res.fail({ message: `You cannot create more than ${CONFIG.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() | |
27db7840 | 53 | .custom(isVideoChannelDisplayNameValid).withMessage('Should have a valid display name'), |
7d14d4d2 C |
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 | |
906f46d0 | 64 | (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 |
a2431b7d C |
68 | |
69 | return next() | |
72c7248b C |
70 | } |
71 | ] | |
72 | ||
73 | const videoChannelsRemoveValidator = [ | |
8a19bee1 | 74 | param('nameWithHost').exists().withMessage('Should have an video channel name with host'), |
72c7248b | 75 | |
a2431b7d | 76 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
72c7248b C |
77 | logger.debug('Checking videoChannelsRemove parameters', { parameters: req.params }) |
78 | ||
a2431b7d | 79 | if (areValidationErrors(req, res)) return |
0f6acda1 | 80 | if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return |
a2431b7d | 81 | |
d48ff09d | 82 | if (!checkUserCanDeleteVideoChannel(res.locals.oauth.token.User, res.locals.videoChannel, res)) return |
a2431b7d C |
83 | if (!await checkVideoChannelIsNotTheLastOne(res)) return |
84 | ||
85 | return next() | |
72c7248b C |
86 | } |
87 | ] | |
88 | ||
8a19bee1 C |
89 | const videoChannelsNameWithHostValidator = [ |
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) => { |
8a19bee1 | 93 | logger.debug('Checking videoChannelsNameWithHostValidator parameters', { parameters: req.params }) |
72c7248b | 94 | |
a2431b7d | 95 | if (areValidationErrors(req, res)) return |
6b738c7a | 96 | |
0f6acda1 | 97 | if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return |
a2431b7d C |
98 | |
99 | return next() | |
72c7248b C |
100 | } |
101 | ] | |
102 | ||
06a05d5f | 103 | const localVideoChannelValidator = [ |
27db7840 | 104 | param('name').custom(isVideoChannelDisplayNameValid).withMessage('Should have a valid video channel name'), |
06a05d5f C |
105 | |
106 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
107 | logger.debug('Checking localVideoChannelValidator parameters', { parameters: req.params }) | |
108 | ||
109 | if (areValidationErrors(req, res)) return | |
0f6acda1 | 110 | if (!await doesLocalVideoChannelNameExist(req.params.name, res)) return |
06a05d5f C |
111 | |
112 | return next() | |
113 | } | |
114 | ] | |
115 | ||
747c5628 | 116 | const videoChannelStatsValidator = [ |
5a61ffbb C |
117 | query('withStats') |
118 | .optional() | |
119 | .customSanitizer(toBooleanOrNull) | |
120 | .custom(isBooleanValid).withMessage('Should have a valid stats flag'), | |
747c5628 RK |
121 | |
122 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
123 | if (areValidationErrors(req, res)) return | |
124 | return next() | |
125 | } | |
126 | ] | |
127 | ||
37a44fc9 C |
128 | const videoChannelsListValidator = [ |
129 | query('search').optional().not().isEmpty().withMessage('Should have a valid search'), | |
130 | ||
131 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
132 | logger.debug('Checking video channels search query', { parameters: req.query }) | |
133 | ||
134 | if (areValidationErrors(req, res)) return | |
135 | ||
136 | return next() | |
137 | } | |
138 | ] | |
139 | ||
72c7248b C |
140 | // --------------------------------------------------------------------------- |
141 | ||
142 | export { | |
72c7248b C |
143 | videoChannelsAddValidator, |
144 | videoChannelsUpdateValidator, | |
145 | videoChannelsRemoveValidator, | |
8a19bee1 | 146 | videoChannelsNameWithHostValidator, |
37a44fc9 | 147 | videoChannelsListValidator, |
747c5628 RK |
148 | localVideoChannelValidator, |
149 | videoChannelStatsValidator | |
72c7248b C |
150 | } |
151 | ||
152 | // --------------------------------------------------------------------------- | |
153 | ||
0283eaac | 154 | function checkUserCanDeleteVideoChannel (user: MUser, videoChannel: MChannelAccountDefault, res: express.Response) { |
50d6de9c | 155 | if (videoChannel.Actor.isOwned() === false) { |
76148b27 RK |
156 | res.fail({ |
157 | status: HttpStatusCode.FORBIDDEN_403, | |
158 | message: 'Cannot remove video channel of another server.' | |
159 | }) | |
a2431b7d | 160 | return false |
72c7248b C |
161 | } |
162 | ||
163 | // Check if the user can delete the video channel | |
164 | // The user can delete it if s/he is an admin | |
38fa2065 | 165 | // Or if s/he is the video channel's account |
a2431b7d | 166 | if (user.hasRight(UserRight.REMOVE_ANY_VIDEO_CHANNEL) === false && videoChannel.Account.userId !== user.id) { |
76148b27 RK |
167 | res.fail({ |
168 | status: HttpStatusCode.FORBIDDEN_403, | |
169 | message: 'Cannot remove video channel of another user' | |
170 | }) | |
a2431b7d | 171 | return false |
72c7248b C |
172 | } |
173 | ||
a2431b7d | 174 | return true |
72c7248b C |
175 | } |
176 | ||
a2431b7d | 177 | async function checkVideoChannelIsNotTheLastOne (res: express.Response) { |
3fd3ab2d | 178 | const count = await VideoChannelModel.countByAccount(res.locals.oauth.token.User.Account.id) |
a2431b7d C |
179 | |
180 | if (count <= 1) { | |
76148b27 RK |
181 | res.fail({ |
182 | status: HttpStatusCode.CONFLICT_409, | |
183 | message: 'Cannot remove the last channel of this user' | |
184 | }) | |
a2431b7d C |
185 | return false |
186 | } | |
187 | ||
188 | return true | |
72c7248b | 189 | } |