]>
Commit | Line | Data |
---|---|---|
1 | import * as express from 'express' | |
2 | import { body, param } from 'express-validator/check' | |
3 | import { UserRight } from '../../../../shared' | |
4 | import { doesAccountNameWithHostExist } from '../../../helpers/custom-validators/accounts' | |
5 | import { | |
6 | doesLocalVideoChannelNameExist, | |
7 | isVideoChannelDescriptionValid, | |
8 | isVideoChannelNameValid, | |
9 | doesVideoChannelNameWithHostExist, | |
10 | isVideoChannelSupportValid | |
11 | } from '../../../helpers/custom-validators/video-channels' | |
12 | import { logger } from '../../../helpers/logger' | |
13 | import { UserModel } from '../../../models/account/user' | |
14 | import { VideoChannelModel } from '../../../models/video/video-channel' | |
15 | import { areValidationErrors } from '../utils' | |
16 | import { isActorPreferredUsernameValid } from '../../../helpers/custom-validators/activitypub/actor' | |
17 | import { ActorModel } from '../../../models/activitypub/actor' | |
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 | return next() | |
39 | } | |
40 | ] | |
41 | ||
42 | const videoChannelsUpdateValidator = [ | |
43 | param('nameWithHost').exists().withMessage('Should have an video channel name with host'), | |
44 | body('displayName').optional().custom(isVideoChannelNameValid).withMessage('Should have a valid display name'), | |
45 | body('description').optional().custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'), | |
46 | body('support').optional().custom(isVideoChannelSupportValid).withMessage('Should have a valid support text'), | |
47 | ||
48 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
49 | logger.debug('Checking videoChannelsUpdate parameters', { parameters: req.body }) | |
50 | ||
51 | if (areValidationErrors(req, res)) return | |
52 | if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return | |
53 | ||
54 | // We need to make additional checks | |
55 | if (res.locals.videoChannel.Actor.isOwned() === false) { | |
56 | return res.status(403) | |
57 | .json({ error: 'Cannot update video channel of another server' }) | |
58 | .end() | |
59 | } | |
60 | ||
61 | if (res.locals.videoChannel.Account.userId !== res.locals.oauth.token.User.id) { | |
62 | return res.status(403) | |
63 | .json({ error: 'Cannot update video channel of another user' }) | |
64 | .end() | |
65 | } | |
66 | ||
67 | return next() | |
68 | } | |
69 | ] | |
70 | ||
71 | const videoChannelsRemoveValidator = [ | |
72 | param('nameWithHost').exists().withMessage('Should have an video channel name with host'), | |
73 | ||
74 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
75 | logger.debug('Checking videoChannelsRemove parameters', { parameters: req.params }) | |
76 | ||
77 | if (areValidationErrors(req, res)) return | |
78 | if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return | |
79 | ||
80 | if (!checkUserCanDeleteVideoChannel(res.locals.oauth.token.User, res.locals.videoChannel, res)) return | |
81 | if (!await checkVideoChannelIsNotTheLastOne(res)) return | |
82 | ||
83 | return next() | |
84 | } | |
85 | ] | |
86 | ||
87 | const videoChannelsNameWithHostValidator = [ | |
88 | param('nameWithHost').exists().withMessage('Should have an video channel name with host'), | |
89 | ||
90 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
91 | logger.debug('Checking videoChannelsNameWithHostValidator parameters', { parameters: req.params }) | |
92 | ||
93 | if (areValidationErrors(req, res)) return | |
94 | ||
95 | if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return | |
96 | ||
97 | return next() | |
98 | } | |
99 | ] | |
100 | ||
101 | const localVideoChannelValidator = [ | |
102 | param('name').custom(isVideoChannelNameValid).withMessage('Should have a valid video channel name'), | |
103 | ||
104 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
105 | logger.debug('Checking localVideoChannelValidator parameters', { parameters: req.params }) | |
106 | ||
107 | if (areValidationErrors(req, res)) return | |
108 | if (!await doesLocalVideoChannelNameExist(req.params.name, res)) return | |
109 | ||
110 | return next() | |
111 | } | |
112 | ] | |
113 | ||
114 | // --------------------------------------------------------------------------- | |
115 | ||
116 | export { | |
117 | videoChannelsAddValidator, | |
118 | videoChannelsUpdateValidator, | |
119 | videoChannelsRemoveValidator, | |
120 | videoChannelsNameWithHostValidator, | |
121 | localVideoChannelValidator | |
122 | } | |
123 | ||
124 | // --------------------------------------------------------------------------- | |
125 | ||
126 | function checkUserCanDeleteVideoChannel (user: UserModel, videoChannel: VideoChannelModel, res: express.Response) { | |
127 | if (videoChannel.Actor.isOwned() === false) { | |
128 | res.status(403) | |
129 | .json({ error: 'Cannot remove video channel of another server.' }) | |
130 | .end() | |
131 | ||
132 | return false | |
133 | } | |
134 | ||
135 | // Check if the user can delete the video channel | |
136 | // The user can delete it if s/he is an admin | |
137 | // Or if s/he is the video channel's account | |
138 | if (user.hasRight(UserRight.REMOVE_ANY_VIDEO_CHANNEL) === false && videoChannel.Account.userId !== user.id) { | |
139 | res.status(403) | |
140 | .json({ error: 'Cannot remove video channel of another user' }) | |
141 | .end() | |
142 | ||
143 | return false | |
144 | } | |
145 | ||
146 | return true | |
147 | } | |
148 | ||
149 | async function checkVideoChannelIsNotTheLastOne (res: express.Response) { | |
150 | const count = await VideoChannelModel.countByAccount(res.locals.oauth.token.User.Account.id) | |
151 | ||
152 | if (count <= 1) { | |
153 | res.status(409) | |
154 | .json({ error: 'Cannot remove the last channel of this user' }) | |
155 | .end() | |
156 | ||
157 | return false | |
158 | } | |
159 | ||
160 | return true | |
161 | } |