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