]>
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' |
5a61ffbb C |
6 | import { isActorPreferredUsernameValid } from '../../../helpers/custom-validators/activitypub/actor' |
7 | import { isBooleanValid, toBooleanOrNull } from '../../../helpers/custom-validators/misc' | |
4e50b6a1 | 8 | import { |
7d8e778a | 9 | isVideoChannelDescriptionValid, |
7d8e778a C |
10 | isVideoChannelNameValid, |
11 | isVideoChannelSupportValid | |
6e46de09 C |
12 | } from '../../../helpers/custom-validators/video-channels' |
13 | import { logger } from '../../../helpers/logger' | |
5a61ffbb C |
14 | import { doesLocalVideoChannelNameExist, doesVideoChannelNameWithHostExist } from '../../../helpers/middlewares' |
15 | import { ActorModel } from '../../../models/activitypub/actor' | |
6e46de09 C |
16 | import { VideoChannelModel } from '../../../models/video/video-channel' |
17 | import { areValidationErrors } from '../utils' | |
2d53be02 | 18 | import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes' |
72c7248b | 19 | |
72c7248b | 20 | const videoChannelsAddValidator = [ |
8a19bee1 | 21 | body('name').custom(isActorPreferredUsernameValid).withMessage('Should have a valid channel name'), |
08c1efbe | 22 | body('displayName').custom(isVideoChannelNameValid).withMessage('Should have a valid display name'), |
2422c46b C |
23 | body('description').optional().custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'), |
24 | body('support').optional().custom(isVideoChannelSupportValid).withMessage('Should have a valid support text'), | |
72c7248b | 25 | |
601527d7 | 26 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
72c7248b C |
27 | logger.debug('Checking videoChannelsAdd parameters', { parameters: req.body }) |
28 | ||
a2431b7d C |
29 | if (areValidationErrors(req, res)) return |
30 | ||
601527d7 C |
31 | const actor = await ActorModel.loadLocalByName(req.body.name) |
32 | if (actor) { | |
2d53be02 | 33 | res.status(HttpStatusCode.CONFLICT_409) |
601527d7 C |
34 | .send({ error: 'Another actor (account/channel) with this name on this instance already exists or has already existed.' }) |
35 | .end() | |
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) { |
2d53be02 | 41 | res.status(HttpStatusCode.BAD_REQUEST_400) |
a3ce4ae8 C |
42 | .send({ error: `You cannot create more than ${VIDEO_CHANNELS.MAX_PER_USER} channels` }) |
43 | .end() | |
44 | return false | |
45 | } | |
46 | ||
a2431b7d | 47 | return next() |
72c7248b C |
48 | } |
49 | ] | |
50 | ||
51 | const videoChannelsUpdateValidator = [ | |
8a19bee1 | 52 | param('nameWithHost').exists().withMessage('Should have an video channel name with host'), |
7d14d4d2 C |
53 | body('displayName') |
54 | .optional() | |
55 | .custom(isVideoChannelNameValid).withMessage('Should have a valid display name'), | |
56 | body('description') | |
57 | .optional() | |
58 | .custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'), | |
59 | body('support') | |
60 | .optional() | |
61 | .custom(isVideoChannelSupportValid).withMessage('Should have a valid support text'), | |
62 | body('bulkVideosSupportUpdate') | |
63 | .optional() | |
64 | .custom(isBooleanValid).withMessage('Should have a valid bulkVideosSupportUpdate boolean field'), | |
72c7248b | 65 | |
a2431b7d | 66 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
72c7248b C |
67 | logger.debug('Checking videoChannelsUpdate parameters', { parameters: req.body }) |
68 | ||
a2431b7d | 69 | if (areValidationErrors(req, res)) return |
0f6acda1 | 70 | if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return |
a2431b7d C |
71 | |
72 | // We need to make additional checks | |
d50acfab | 73 | if (res.locals.videoChannel.Actor.isOwned() === false) { |
2d53be02 | 74 | return res.status(HttpStatusCode.FORBIDDEN_403) |
a2431b7d C |
75 | .json({ error: 'Cannot update video channel of another server' }) |
76 | .end() | |
77 | } | |
78 | ||
79 | if (res.locals.videoChannel.Account.userId !== res.locals.oauth.token.User.id) { | |
2d53be02 | 80 | return res.status(HttpStatusCode.FORBIDDEN_403) |
a2431b7d C |
81 | .json({ error: 'Cannot update video channel of another user' }) |
82 | .end() | |
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 | ||
72c7248b C |
144 | // --------------------------------------------------------------------------- |
145 | ||
146 | export { | |
72c7248b C |
147 | videoChannelsAddValidator, |
148 | videoChannelsUpdateValidator, | |
149 | videoChannelsRemoveValidator, | |
8a19bee1 | 150 | videoChannelsNameWithHostValidator, |
747c5628 RK |
151 | localVideoChannelValidator, |
152 | videoChannelStatsValidator | |
72c7248b C |
153 | } |
154 | ||
155 | // --------------------------------------------------------------------------- | |
156 | ||
0283eaac | 157 | function checkUserCanDeleteVideoChannel (user: MUser, videoChannel: MChannelAccountDefault, res: express.Response) { |
50d6de9c | 158 | if (videoChannel.Actor.isOwned() === false) { |
2d53be02 | 159 | res.status(HttpStatusCode.FORBIDDEN_403) |
60862425 | 160 | .json({ error: 'Cannot remove video channel of another server.' }) |
72c7248b | 161 | .end() |
a2431b7d C |
162 | |
163 | return false | |
72c7248b C |
164 | } |
165 | ||
166 | // Check if the user can delete the video channel | |
167 | // The user can delete it if s/he is an admin | |
38fa2065 | 168 | // Or if s/he is the video channel's account |
a2431b7d | 169 | if (user.hasRight(UserRight.REMOVE_ANY_VIDEO_CHANNEL) === false && videoChannel.Account.userId !== user.id) { |
2d53be02 | 170 | res.status(HttpStatusCode.FORBIDDEN_403) |
72c7248b C |
171 | .json({ error: 'Cannot remove video channel of another user' }) |
172 | .end() | |
a2431b7d C |
173 | |
174 | return false | |
72c7248b C |
175 | } |
176 | ||
a2431b7d | 177 | return true |
72c7248b C |
178 | } |
179 | ||
a2431b7d | 180 | async function checkVideoChannelIsNotTheLastOne (res: express.Response) { |
3fd3ab2d | 181 | const count = await VideoChannelModel.countByAccount(res.locals.oauth.token.User.Account.id) |
a2431b7d C |
182 | |
183 | if (count <= 1) { | |
2d53be02 RK |
184 | res.status(HttpStatusCode.CONFLICT_409) |
185 | .json({ error: 'Cannot remove the last channel of this user' }) | |
186 | .end() | |
a2431b7d C |
187 | |
188 | return false | |
189 | } | |
190 | ||
191 | return true | |
72c7248b | 192 | } |