]>
Commit | Line | Data |
---|---|---|
1 | import * as express from 'express' | |
2 | import { body, param } from 'express-validator' | |
3 | import { UserRight } from '../../../../shared' | |
4 | import { | |
5 | isVideoChannelDescriptionValid, | |
6 | isVideoChannelNameValid, | |
7 | isVideoChannelSupportValid | |
8 | } from '../../../helpers/custom-validators/video-channels' | |
9 | import { logger } from '../../../helpers/logger' | |
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' | |
14 | import { isBooleanValid } from '../../../helpers/custom-validators/misc' | |
15 | import { doesLocalVideoChannelNameExist, doesVideoChannelNameWithHostExist } from '../../../helpers/middlewares' | |
16 | import { MChannelAccountDefault, MUser } from '@server/typings/models' | |
17 | import { VIDEO_CHANNELS } from '@server/initializers/constants' | |
18 | ||
19 | const videoChannelsAddValidator = [ | |
20 | body('name').custom(isActorPreferredUsernameValid).withMessage('Should have a valid channel name'), | |
21 | body('displayName').custom(isVideoChannelNameValid).withMessage('Should have a valid display name'), | |
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'), | |
24 | ||
25 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
26 | logger.debug('Checking videoChannelsAdd parameters', { parameters: req.body }) | |
27 | ||
28 | if (areValidationErrors(req, res)) return | |
29 | ||
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 | ||
38 | const count = await VideoChannelModel.countByAccount(res.locals.oauth.token.User.Account.id) | |
39 | if (count >= VIDEO_CHANNELS.MAX_PER_USER) { | |
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 | ||
46 | return next() | |
47 | } | |
48 | ] | |
49 | ||
50 | const videoChannelsUpdateValidator = [ | |
51 | param('nameWithHost').exists().withMessage('Should have an video channel name with host'), | |
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'), | |
64 | ||
65 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
66 | logger.debug('Checking videoChannelsUpdate parameters', { parameters: req.body }) | |
67 | ||
68 | if (areValidationErrors(req, res)) return | |
69 | if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return | |
70 | ||
71 | // We need to make additional checks | |
72 | if (res.locals.videoChannel.Actor.isOwned() === false) { | |
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() | |
85 | } | |
86 | ] | |
87 | ||
88 | const videoChannelsRemoveValidator = [ | |
89 | param('nameWithHost').exists().withMessage('Should have an video channel name with host'), | |
90 | ||
91 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
92 | logger.debug('Checking videoChannelsRemove parameters', { parameters: req.params }) | |
93 | ||
94 | if (areValidationErrors(req, res)) return | |
95 | if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return | |
96 | ||
97 | if (!checkUserCanDeleteVideoChannel(res.locals.oauth.token.User, res.locals.videoChannel, res)) return | |
98 | if (!await checkVideoChannelIsNotTheLastOne(res)) return | |
99 | ||
100 | return next() | |
101 | } | |
102 | ] | |
103 | ||
104 | const videoChannelsNameWithHostValidator = [ | |
105 | param('nameWithHost').exists().withMessage('Should have an video channel name with host'), | |
106 | ||
107 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
108 | logger.debug('Checking videoChannelsNameWithHostValidator parameters', { parameters: req.params }) | |
109 | ||
110 | if (areValidationErrors(req, res)) return | |
111 | ||
112 | if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return | |
113 | ||
114 | return next() | |
115 | } | |
116 | ] | |
117 | ||
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 | |
125 | if (!await doesLocalVideoChannelNameExist(req.params.name, res)) return | |
126 | ||
127 | return next() | |
128 | } | |
129 | ] | |
130 | ||
131 | // --------------------------------------------------------------------------- | |
132 | ||
133 | export { | |
134 | videoChannelsAddValidator, | |
135 | videoChannelsUpdateValidator, | |
136 | videoChannelsRemoveValidator, | |
137 | videoChannelsNameWithHostValidator, | |
138 | localVideoChannelValidator | |
139 | } | |
140 | ||
141 | // --------------------------------------------------------------------------- | |
142 | ||
143 | function checkUserCanDeleteVideoChannel (user: MUser, videoChannel: MChannelAccountDefault, res: express.Response) { | |
144 | if (videoChannel.Actor.isOwned() === false) { | |
145 | res.status(403) | |
146 | .json({ error: 'Cannot remove video channel of another server.' }) | |
147 | .end() | |
148 | ||
149 | return false | |
150 | } | |
151 | ||
152 | // Check if the user can delete the video channel | |
153 | // The user can delete it if s/he is an admin | |
154 | // Or if s/he is the video channel's account | |
155 | if (user.hasRight(UserRight.REMOVE_ANY_VIDEO_CHANNEL) === false && videoChannel.Account.userId !== user.id) { | |
156 | res.status(403) | |
157 | .json({ error: 'Cannot remove video channel of another user' }) | |
158 | .end() | |
159 | ||
160 | return false | |
161 | } | |
162 | ||
163 | return true | |
164 | } | |
165 | ||
166 | async function checkVideoChannelIsNotTheLastOne (res: express.Response) { | |
167 | const count = await VideoChannelModel.countByAccount(res.locals.oauth.token.User.Account.id) | |
168 | ||
169 | if (count <= 1) { | |
170 | res.status(409) | |
171 | .json({ error: 'Cannot remove the last channel of this user' }) | |
172 | .end() | |
173 | ||
174 | return false | |
175 | } | |
176 | ||
177 | return true | |
178 | } |