]>
Commit | Line | Data |
---|---|---|
bf1f6508 | 1 | import * as express from 'express' |
47564bbe | 2 | import { ResultList } from '../../../../shared/models' |
bf1f6508 | 3 | import { VideoCommentCreate } from '../../../../shared/models/videos/video-comment.model' |
da854ddd | 4 | import { retryTransactionWrapper } from '../../../helpers/database-utils' |
4cb6d457 | 5 | import { logger } from '../../../helpers/logger' |
da854ddd | 6 | import { getFormattedObjects } from '../../../helpers/utils' |
bf1f6508 C |
7 | import { sequelizeTypescript } from '../../../initializers' |
8 | import { buildFormattedCommentTree, createVideoComment } from '../../../lib/video-comment' | |
f05a1c30 | 9 | import { asyncMiddleware, authenticate, paginationValidator, setDefaultSort, setDefaultPagination } from '../../../middlewares' |
bf1f6508 C |
10 | import { videoCommentThreadsSortValidator } from '../../../middlewares/validators' |
11 | import { | |
4cb6d457 C |
12 | addVideoCommentReplyValidator, addVideoCommentThreadValidator, listVideoCommentThreadsValidator, listVideoThreadCommentsValidator, |
13 | removeVideoCommentValidator | |
bf1f6508 | 14 | } from '../../../middlewares/validators/video-comments' |
47564bbe | 15 | import { VideoModel } from '../../../models/video/video' |
bf1f6508 C |
16 | import { VideoCommentModel } from '../../../models/video/video-comment' |
17 | ||
18 | const videoCommentRouter = express.Router() | |
19 | ||
20 | videoCommentRouter.get('/:videoId/comment-threads', | |
21 | paginationValidator, | |
22 | videoCommentThreadsSortValidator, | |
1174a847 | 23 | setDefaultSort, |
f05a1c30 | 24 | setDefaultPagination, |
bf1f6508 C |
25 | asyncMiddleware(listVideoCommentThreadsValidator), |
26 | asyncMiddleware(listVideoThreads) | |
27 | ) | |
28 | videoCommentRouter.get('/:videoId/comment-threads/:threadId', | |
29 | asyncMiddleware(listVideoThreadCommentsValidator), | |
30 | asyncMiddleware(listVideoThreadComments) | |
31 | ) | |
32 | ||
33 | videoCommentRouter.post('/:videoId/comment-threads', | |
34 | authenticate, | |
35 | asyncMiddleware(addVideoCommentThreadValidator), | |
36 | asyncMiddleware(addVideoCommentThreadRetryWrapper) | |
37 | ) | |
38 | videoCommentRouter.post('/:videoId/comments/:commentId', | |
39 | authenticate, | |
40 | asyncMiddleware(addVideoCommentReplyValidator), | |
41 | asyncMiddleware(addVideoCommentReplyRetryWrapper) | |
42 | ) | |
4cb6d457 C |
43 | videoCommentRouter.delete('/:videoId/comments/:commentId', |
44 | authenticate, | |
45 | asyncMiddleware(removeVideoCommentValidator), | |
46 | asyncMiddleware(removeVideoCommentRetryWrapper) | |
47 | ) | |
bf1f6508 C |
48 | |
49 | // --------------------------------------------------------------------------- | |
50 | ||
51 | export { | |
52 | videoCommentRouter | |
53 | } | |
54 | ||
55 | // --------------------------------------------------------------------------- | |
56 | ||
57 | async function listVideoThreads (req: express.Request, res: express.Response, next: express.NextFunction) { | |
47564bbe C |
58 | const video = res.locals.video as VideoModel |
59 | let resultList: ResultList<VideoCommentModel> | |
60 | ||
61 | if (video.commentsEnabled === true) { | |
62 | resultList = await VideoCommentModel.listThreadsForApi(video.id, req.query.start, req.query.count, req.query.sort) | |
63 | } else { | |
64 | resultList = { | |
65 | total: 0, | |
66 | data: [] | |
67 | } | |
68 | } | |
bf1f6508 C |
69 | |
70 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
71 | } | |
72 | ||
73 | async function listVideoThreadComments (req: express.Request, res: express.Response, next: express.NextFunction) { | |
47564bbe C |
74 | const video = res.locals.video as VideoModel |
75 | let resultList: ResultList<VideoCommentModel> | |
76 | ||
77 | if (video.commentsEnabled === true) { | |
78 | resultList = await VideoCommentModel.listThreadCommentsForApi(res.locals.video.id, res.locals.videoCommentThread.id) | |
79 | } else { | |
80 | resultList = { | |
81 | total: 0, | |
82 | data: [] | |
83 | } | |
84 | } | |
bf1f6508 C |
85 | |
86 | return res.json(buildFormattedCommentTree(resultList)) | |
87 | } | |
88 | ||
89 | async function addVideoCommentThreadRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) { | |
90 | const options = { | |
91 | arguments: [ req, res ], | |
92 | errorMessage: 'Cannot insert the video comment thread with many retries.' | |
93 | } | |
94 | ||
95 | const comment = await retryTransactionWrapper(addVideoCommentThread, options) | |
96 | ||
97 | res.json({ | |
4635f59d | 98 | comment: comment.toFormattedJSON() |
bf1f6508 C |
99 | }).end() |
100 | } | |
101 | ||
102 | function addVideoCommentThread (req: express.Request, res: express.Response) { | |
103 | const videoCommentInfo: VideoCommentCreate = req.body | |
104 | ||
105 | return sequelizeTypescript.transaction(async t => { | |
106 | return createVideoComment({ | |
107 | text: videoCommentInfo.text, | |
ea44f375 | 108 | inReplyToComment: null, |
bf1f6508 | 109 | video: res.locals.video, |
4635f59d | 110 | account: res.locals.oauth.token.User.Account |
bf1f6508 C |
111 | }, t) |
112 | }) | |
113 | } | |
114 | ||
115 | async function addVideoCommentReplyRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) { | |
116 | const options = { | |
117 | arguments: [ req, res ], | |
118 | errorMessage: 'Cannot insert the video comment reply with many retries.' | |
119 | } | |
120 | ||
121 | const comment = await retryTransactionWrapper(addVideoCommentReply, options) | |
122 | ||
123 | res.json({ | |
4635f59d | 124 | comment: comment.toFormattedJSON() |
bf1f6508 C |
125 | }).end() |
126 | } | |
127 | ||
128 | function addVideoCommentReply (req: express.Request, res: express.Response, next: express.NextFunction) { | |
129 | const videoCommentInfo: VideoCommentCreate = req.body | |
130 | ||
131 | return sequelizeTypescript.transaction(async t => { | |
132 | return createVideoComment({ | |
133 | text: videoCommentInfo.text, | |
ea44f375 | 134 | inReplyToComment: res.locals.videoComment, |
bf1f6508 | 135 | video: res.locals.video, |
4635f59d | 136 | account: res.locals.oauth.token.User.Account |
bf1f6508 C |
137 | }, t) |
138 | }) | |
139 | } | |
4cb6d457 C |
140 | |
141 | async function removeVideoCommentRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) { | |
142 | const options = { | |
143 | arguments: [ req, res ], | |
144 | errorMessage: 'Cannot remove the video comment with many retries.' | |
145 | } | |
146 | ||
147 | await retryTransactionWrapper(removeVideoComment, options) | |
148 | ||
149 | return res.type('json').status(204).end() | |
150 | } | |
151 | ||
152 | async function removeVideoComment (req: express.Request, res: express.Response) { | |
153 | const videoCommentInstance: VideoCommentModel = res.locals.videoComment | |
154 | ||
155 | await sequelizeTypescript.transaction(async t => { | |
156 | await videoCommentInstance.destroy({ transaction: t }) | |
157 | }) | |
158 | ||
159 | logger.info('Video comment %d deleted.', videoCommentInstance.id) | |
160 | } |