]>
Commit | Line | Data |
---|---|---|
41fb13c3 | 1 | import express from 'express' |
0f8d00e3 | 2 | import { body, param, query } from 'express-validator' |
26d6bf65 | 3 | import { MUserAccountUrl } from '@server/types/models' |
d17c7b4e | 4 | import { HttpStatusCode, UserRight } from '@shared/models' |
d4a8e7a6 | 5 | import { exists, isBooleanValid, isIdValid, toBooleanOrNull } from '../../../helpers/custom-validators/misc' |
10363c74 | 6 | import { isValidVideoCommentText } from '../../../helpers/custom-validators/video-comments' |
6e46de09 | 7 | import { logger } from '../../../helpers/logger' |
ceba0e65 C |
8 | import { AcceptResult, isLocalVideoCommentReplyAccepted, isLocalVideoThreadAccepted } from '../../../lib/moderation' |
9 | import { Hooks } from '../../../lib/plugins/hooks' | |
57f6896f | 10 | import { MCommentOwnerVideoReply, MVideo, MVideoFullLight } from '../../../types/models/video' |
84c8d986 C |
11 | import { |
12 | areValidationErrors, | |
ff9d43f6 | 13 | checkCanSeeVideo, |
84c8d986 C |
14 | doesVideoCommentExist, |
15 | doesVideoCommentThreadExist, | |
16 | doesVideoExist, | |
17 | isValidVideoIdParam | |
18 | } from '../shared' | |
bf1f6508 | 19 | |
0f8d00e3 C |
20 | const listVideoCommentsValidator = [ |
21 | query('isLocal') | |
22 | .optional() | |
f1273314 | 23 | .customSanitizer(toBooleanOrNull) |
0f8d00e3 C |
24 | .custom(isBooleanValid) |
25 | .withMessage('Should have a valid is local boolean'), | |
26 | ||
0e6cd1c0 C |
27 | query('onLocalVideo') |
28 | .optional() | |
29 | .customSanitizer(toBooleanOrNull) | |
30 | .custom(isBooleanValid) | |
31 | .withMessage('Should have a valid is on local video boolean'), | |
32 | ||
0f8d00e3 C |
33 | query('search') |
34 | .optional() | |
35 | .custom(exists).withMessage('Should have a valid search'), | |
36 | ||
37 | query('searchAccount') | |
38 | .optional() | |
39 | .custom(exists).withMessage('Should have a valid account search'), | |
40 | ||
41 | query('searchVideo') | |
42 | .optional() | |
43 | .custom(exists).withMessage('Should have a valid video search'), | |
44 | ||
45 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
46 | logger.debug('Checking listVideoCommentsValidator parameters.', { parameters: req.query }) | |
47 | ||
48 | if (areValidationErrors(req, res)) return | |
49 | ||
50 | return next() | |
51 | } | |
52 | ] | |
53 | ||
bf1f6508 | 54 | const listVideoCommentThreadsValidator = [ |
d4a8e7a6 | 55 | isValidVideoIdParam('videoId'), |
bf1f6508 C |
56 | |
57 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
d3ea8975 | 58 | logger.debug('Checking listVideoCommentThreads parameters.', { parameters: req.params }) |
bf1f6508 C |
59 | |
60 | if (areValidationErrors(req, res)) return | |
0f6acda1 | 61 | if (!await doesVideoExist(req.params.videoId, res, 'only-video')) return |
bf1f6508 | 62 | |
ff9d43f6 | 63 | if (!await checkCanSeeVideo({ req, res, paramId: req.params.videoId, video: res.locals.onlyVideo })) return |
84c8d986 | 64 | |
bf1f6508 C |
65 | return next() |
66 | } | |
67 | ] | |
68 | ||
69 | const listVideoThreadCommentsValidator = [ | |
d4a8e7a6 C |
70 | isValidVideoIdParam('videoId'), |
71 | ||
72 | param('threadId') | |
73 | .custom(isIdValid).not().isEmpty().withMessage('Should have a valid threadId'), | |
bf1f6508 C |
74 | |
75 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
d3ea8975 | 76 | logger.debug('Checking listVideoThreadComments parameters.', { parameters: req.params }) |
bf1f6508 C |
77 | |
78 | if (areValidationErrors(req, res)) return | |
0f6acda1 | 79 | if (!await doesVideoExist(req.params.videoId, res, 'only-video')) return |
453e83ea | 80 | if (!await doesVideoCommentThreadExist(req.params.threadId, res.locals.onlyVideo, res)) return |
bf1f6508 | 81 | |
ff9d43f6 | 82 | if (!await checkCanSeeVideo({ req, res, paramId: req.params.videoId, video: res.locals.onlyVideo })) return |
84c8d986 | 83 | |
bf1f6508 C |
84 | return next() |
85 | } | |
86 | ] | |
87 | ||
88 | const addVideoCommentThreadValidator = [ | |
d4a8e7a6 C |
89 | isValidVideoIdParam('videoId'), |
90 | ||
91 | body('text') | |
92 | .custom(isValidVideoCommentText).not().isEmpty().withMessage('Should have a valid comment text'), | |
bf1f6508 C |
93 | |
94 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
5de8a55a | 95 | logger.debug('Checking addVideoCommentThread parameters.', { parameters: req.params, body: req.body }) |
bf1f6508 C |
96 | |
97 | if (areValidationErrors(req, res)) return | |
0f6acda1 | 98 | if (!await doesVideoExist(req.params.videoId, res)) return |
6ea9295b | 99 | |
ff9d43f6 | 100 | if (!await checkCanSeeVideo({ req, res, paramId: req.params.videoId, video: res.locals.videoAll })) return |
6ea9295b | 101 | |
453e83ea | 102 | if (!isVideoCommentsEnabled(res.locals.videoAll, res)) return |
a1587156 | 103 | if (!await isVideoCommentAccepted(req, res, res.locals.videoAll, false)) return |
bf1f6508 C |
104 | |
105 | return next() | |
106 | } | |
107 | ] | |
108 | ||
109 | const addVideoCommentReplyValidator = [ | |
d4a8e7a6 C |
110 | isValidVideoIdParam('videoId'), |
111 | ||
bf1f6508 | 112 | param('commentId').custom(isIdValid).not().isEmpty().withMessage('Should have a valid commentId'), |
d4a8e7a6 | 113 | |
bf1f6508 C |
114 | body('text').custom(isValidVideoCommentText).not().isEmpty().withMessage('Should have a valid comment text'), |
115 | ||
116 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
5de8a55a | 117 | logger.debug('Checking addVideoCommentReply parameters.', { parameters: req.params, body: req.body }) |
bf1f6508 C |
118 | |
119 | if (areValidationErrors(req, res)) return | |
0f6acda1 | 120 | if (!await doesVideoExist(req.params.videoId, res)) return |
6ea9295b | 121 | |
ff9d43f6 | 122 | if (!await checkCanSeeVideo({ req, res, paramId: req.params.videoId, video: res.locals.videoAll })) return |
6ea9295b | 123 | |
453e83ea C |
124 | if (!isVideoCommentsEnabled(res.locals.videoAll, res)) return |
125 | if (!await doesVideoCommentExist(req.params.commentId, res.locals.videoAll, res)) return | |
126 | if (!await isVideoCommentAccepted(req, res, res.locals.videoAll, true)) return | |
bf1f6508 C |
127 | |
128 | return next() | |
129 | } | |
130 | ] | |
131 | ||
da854ddd | 132 | const videoCommentGetValidator = [ |
d4a8e7a6 C |
133 | isValidVideoIdParam('videoId'), |
134 | ||
135 | param('commentId') | |
136 | .custom(isIdValid).not().isEmpty().withMessage('Should have a valid commentId'), | |
da854ddd C |
137 | |
138 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
139 | logger.debug('Checking videoCommentGetValidator parameters.', { parameters: req.params }) | |
140 | ||
141 | if (areValidationErrors(req, res)) return | |
0f6acda1 | 142 | if (!await doesVideoExist(req.params.videoId, res, 'id')) return |
453e83ea | 143 | if (!await doesVideoCommentExist(req.params.commentId, res.locals.videoId, res)) return |
da854ddd C |
144 | |
145 | return next() | |
146 | } | |
147 | ] | |
148 | ||
4cb6d457 | 149 | const removeVideoCommentValidator = [ |
d4a8e7a6 C |
150 | isValidVideoIdParam('videoId'), |
151 | ||
4cb6d457 C |
152 | param('commentId').custom(isIdValid).not().isEmpty().withMessage('Should have a valid commentId'), |
153 | ||
154 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
155 | logger.debug('Checking removeVideoCommentValidator parameters.', { parameters: req.params }) | |
156 | ||
157 | if (areValidationErrors(req, res)) return | |
0f6acda1 | 158 | if (!await doesVideoExist(req.params.videoId, res)) return |
453e83ea | 159 | if (!await doesVideoCommentExist(req.params.commentId, res.locals.videoAll, res)) return |
4cb6d457 C |
160 | |
161 | // Check if the user who did the request is able to delete the video | |
453e83ea | 162 | if (!checkUserCanDeleteVideoComment(res.locals.oauth.token.User, res.locals.videoCommentFull, res)) return |
4cb6d457 C |
163 | |
164 | return next() | |
165 | } | |
166 | ] | |
167 | ||
bf1f6508 C |
168 | // --------------------------------------------------------------------------- |
169 | ||
170 | export { | |
171 | listVideoCommentThreadsValidator, | |
172 | listVideoThreadCommentsValidator, | |
173 | addVideoCommentThreadValidator, | |
0f8d00e3 | 174 | listVideoCommentsValidator, |
da854ddd | 175 | addVideoCommentReplyValidator, |
4cb6d457 C |
176 | videoCommentGetValidator, |
177 | removeVideoCommentValidator | |
bf1f6508 C |
178 | } |
179 | ||
180 | // --------------------------------------------------------------------------- | |
181 | ||
453e83ea | 182 | function isVideoCommentsEnabled (video: MVideo, res: express.Response) { |
47564bbe | 183 | if (video.commentsEnabled !== true) { |
76148b27 RK |
184 | res.fail({ |
185 | status: HttpStatusCode.CONFLICT_409, | |
186 | message: 'Video comments are disabled for this video.' | |
187 | }) | |
47564bbe C |
188 | return false |
189 | } | |
190 | ||
191 | return true | |
192 | } | |
4cb6d457 | 193 | |
fde37dc9 | 194 | function checkUserCanDeleteVideoComment (user: MUserAccountUrl, videoComment: MCommentOwnerVideoReply, res: express.Response) { |
c883db6d | 195 | if (videoComment.isDeleted()) { |
76148b27 RK |
196 | res.fail({ |
197 | status: HttpStatusCode.CONFLICT_409, | |
198 | message: 'This comment is already deleted' | |
199 | }) | |
c883db6d C |
200 | return false |
201 | } | |
202 | ||
fde37dc9 C |
203 | const userAccount = user.Account |
204 | ||
205 | if ( | |
206 | user.hasRight(UserRight.REMOVE_ANY_VIDEO_COMMENT) === false && // Not a moderator | |
207 | videoComment.accountId !== userAccount.id && // Not the comment owner | |
208 | videoComment.Video.VideoChannel.accountId !== userAccount.id // Not the video owner | |
209 | ) { | |
76148b27 RK |
210 | res.fail({ |
211 | status: HttpStatusCode.FORBIDDEN_403, | |
212 | message: 'Cannot remove video comment of another user' | |
213 | }) | |
4cb6d457 C |
214 | return false |
215 | } | |
216 | ||
217 | return true | |
218 | } | |
b4055e1c | 219 | |
453e83ea | 220 | async function isVideoCommentAccepted (req: express.Request, res: express.Response, video: MVideoFullLight, isReply: boolean) { |
b4055e1c | 221 | const acceptParameters = { |
453e83ea | 222 | video, |
b4055e1c C |
223 | commentBody: req.body, |
224 | user: res.locals.oauth.token.User | |
225 | } | |
226 | ||
227 | let acceptedResult: AcceptResult | |
228 | ||
229 | if (isReply) { | |
453e83ea | 230 | const acceptReplyParameters = Object.assign(acceptParameters, { parentComment: res.locals.videoCommentFull }) |
b4055e1c | 231 | |
6691c522 C |
232 | acceptedResult = await Hooks.wrapFun( |
233 | isLocalVideoCommentReplyAccepted, | |
234 | acceptReplyParameters, | |
b4055e1c C |
235 | 'filter:api.video-comment-reply.create.accept.result' |
236 | ) | |
237 | } else { | |
6691c522 C |
238 | acceptedResult = await Hooks.wrapFun( |
239 | isLocalVideoThreadAccepted, | |
240 | acceptParameters, | |
b4055e1c C |
241 | 'filter:api.video-thread.create.accept.result' |
242 | ) | |
243 | } | |
244 | ||
245 | if (!acceptedResult || acceptedResult.accepted !== true) { | |
246 | logger.info('Refused local comment.', { acceptedResult, acceptParameters }) | |
b4055e1c | 247 | |
76148b27 RK |
248 | res.fail({ |
249 | status: HttpStatusCode.FORBIDDEN_403, | |
250 | message: acceptedResult?.errorMessage || 'Refused local comment' | |
251 | }) | |
b4055e1c C |
252 | return false |
253 | } | |
254 | ||
255 | return true | |
256 | } |