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