]>
Commit | Line | Data |
---|---|---|
bf1f6508 C |
1 | import * as express from 'express' |
2 | import { VideoCommentCreate } from '../../../../shared/models/videos/video-comment.model' | |
da854ddd C |
3 | import { retryTransactionWrapper } from '../../../helpers/database-utils' |
4 | import { getFormattedObjects } from '../../../helpers/utils' | |
bf1f6508 C |
5 | import { sequelizeTypescript } from '../../../initializers' |
6 | import { buildFormattedCommentTree, createVideoComment } from '../../../lib/video-comment' | |
7 | import { asyncMiddleware, authenticate, paginationValidator, setPagination, setVideoCommentThreadsSort } from '../../../middlewares' | |
8 | import { videoCommentThreadsSortValidator } from '../../../middlewares/validators' | |
9 | import { | |
10 | addVideoCommentReplyValidator, addVideoCommentThreadValidator, listVideoCommentThreadsValidator, | |
11 | listVideoThreadCommentsValidator | |
12 | } from '../../../middlewares/validators/video-comments' | |
13 | import { VideoCommentModel } from '../../../models/video/video-comment' | |
14 | ||
15 | const videoCommentRouter = express.Router() | |
16 | ||
17 | videoCommentRouter.get('/:videoId/comment-threads', | |
18 | paginationValidator, | |
19 | videoCommentThreadsSortValidator, | |
20 | setVideoCommentThreadsSort, | |
21 | setPagination, | |
22 | asyncMiddleware(listVideoCommentThreadsValidator), | |
23 | asyncMiddleware(listVideoThreads) | |
24 | ) | |
25 | videoCommentRouter.get('/:videoId/comment-threads/:threadId', | |
26 | asyncMiddleware(listVideoThreadCommentsValidator), | |
27 | asyncMiddleware(listVideoThreadComments) | |
28 | ) | |
29 | ||
30 | videoCommentRouter.post('/:videoId/comment-threads', | |
31 | authenticate, | |
32 | asyncMiddleware(addVideoCommentThreadValidator), | |
33 | asyncMiddleware(addVideoCommentThreadRetryWrapper) | |
34 | ) | |
35 | videoCommentRouter.post('/:videoId/comments/:commentId', | |
36 | authenticate, | |
37 | asyncMiddleware(addVideoCommentReplyValidator), | |
38 | asyncMiddleware(addVideoCommentReplyRetryWrapper) | |
39 | ) | |
40 | ||
41 | // --------------------------------------------------------------------------- | |
42 | ||
43 | export { | |
44 | videoCommentRouter | |
45 | } | |
46 | ||
47 | // --------------------------------------------------------------------------- | |
48 | ||
49 | async function listVideoThreads (req: express.Request, res: express.Response, next: express.NextFunction) { | |
50 | const resultList = await VideoCommentModel.listThreadsForApi(res.locals.video.id, req.query.start, req.query.count, req.query.sort) | |
51 | ||
52 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
53 | } | |
54 | ||
55 | async function listVideoThreadComments (req: express.Request, res: express.Response, next: express.NextFunction) { | |
56 | const resultList = await VideoCommentModel.listThreadCommentsForApi(res.locals.video.id, res.locals.videoCommentThread.id) | |
57 | ||
58 | return res.json(buildFormattedCommentTree(resultList)) | |
59 | } | |
60 | ||
61 | async function addVideoCommentThreadRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) { | |
62 | const options = { | |
63 | arguments: [ req, res ], | |
64 | errorMessage: 'Cannot insert the video comment thread with many retries.' | |
65 | } | |
66 | ||
67 | const comment = await retryTransactionWrapper(addVideoCommentThread, options) | |
68 | ||
69 | res.json({ | |
4635f59d | 70 | comment: comment.toFormattedJSON() |
bf1f6508 C |
71 | }).end() |
72 | } | |
73 | ||
74 | function addVideoCommentThread (req: express.Request, res: express.Response) { | |
75 | const videoCommentInfo: VideoCommentCreate = req.body | |
76 | ||
77 | return sequelizeTypescript.transaction(async t => { | |
78 | return createVideoComment({ | |
79 | text: videoCommentInfo.text, | |
ea44f375 | 80 | inReplyToComment: null, |
bf1f6508 | 81 | video: res.locals.video, |
4635f59d | 82 | account: res.locals.oauth.token.User.Account |
bf1f6508 C |
83 | }, t) |
84 | }) | |
85 | } | |
86 | ||
87 | async function addVideoCommentReplyRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) { | |
88 | const options = { | |
89 | arguments: [ req, res ], | |
90 | errorMessage: 'Cannot insert the video comment reply with many retries.' | |
91 | } | |
92 | ||
93 | const comment = await retryTransactionWrapper(addVideoCommentReply, options) | |
94 | ||
95 | res.json({ | |
4635f59d | 96 | comment: comment.toFormattedJSON() |
bf1f6508 C |
97 | }).end() |
98 | } | |
99 | ||
100 | function addVideoCommentReply (req: express.Request, res: express.Response, next: express.NextFunction) { | |
101 | const videoCommentInfo: VideoCommentCreate = req.body | |
102 | ||
103 | return sequelizeTypescript.transaction(async t => { | |
104 | return createVideoComment({ | |
105 | text: videoCommentInfo.text, | |
ea44f375 | 106 | inReplyToComment: res.locals.videoComment, |
bf1f6508 | 107 | video: res.locals.video, |
4635f59d | 108 | account: res.locals.oauth.token.User.Account |
bf1f6508 C |
109 | }, t) |
110 | }) | |
111 | } |