diff options
Diffstat (limited to 'server/controllers/api/videos/comment.ts')
-rw-r--r-- | server/controllers/api/videos/comment.ts | 68 |
1 files changed, 25 insertions, 43 deletions
diff --git a/server/controllers/api/videos/comment.ts b/server/controllers/api/videos/comment.ts index f8a669e35..bbeb0d557 100644 --- a/server/controllers/api/videos/comment.ts +++ b/server/controllers/api/videos/comment.ts | |||
@@ -1,15 +1,24 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | import { ResultList } from '../../../../shared/models' | 2 | import { ResultList } from '../../../../shared/models' |
3 | import { VideoCommentCreate } from '../../../../shared/models/videos/video-comment.model' | 3 | import { VideoCommentCreate } from '../../../../shared/models/videos/video-comment.model' |
4 | import { retryTransactionWrapper } from '../../../helpers/database-utils' | ||
5 | import { logger } from '../../../helpers/logger' | 4 | import { logger } from '../../../helpers/logger' |
6 | import { getFormattedObjects } from '../../../helpers/utils' | 5 | import { getFormattedObjects } from '../../../helpers/utils' |
7 | import { sequelizeTypescript } from '../../../initializers' | 6 | import { sequelizeTypescript } from '../../../initializers' |
8 | import { buildFormattedCommentTree, createVideoComment } from '../../../lib/video-comment' | 7 | import { buildFormattedCommentTree, createVideoComment } from '../../../lib/video-comment' |
9 | import { asyncMiddleware, authenticate, paginationValidator, setDefaultSort, setDefaultPagination } from '../../../middlewares' | 8 | import { |
9 | asyncMiddleware, | ||
10 | asyncRetryTransactionMiddleware, | ||
11 | authenticate, | ||
12 | paginationValidator, | ||
13 | setDefaultPagination, | ||
14 | setDefaultSort | ||
15 | } from '../../../middlewares' | ||
10 | import { videoCommentThreadsSortValidator } from '../../../middlewares/validators' | 16 | import { videoCommentThreadsSortValidator } from '../../../middlewares/validators' |
11 | import { | 17 | import { |
12 | addVideoCommentReplyValidator, addVideoCommentThreadValidator, listVideoCommentThreadsValidator, listVideoThreadCommentsValidator, | 18 | addVideoCommentReplyValidator, |
19 | addVideoCommentThreadValidator, | ||
20 | listVideoCommentThreadsValidator, | ||
21 | listVideoThreadCommentsValidator, | ||
13 | removeVideoCommentValidator | 22 | removeVideoCommentValidator |
14 | } from '../../../middlewares/validators/video-comments' | 23 | } from '../../../middlewares/validators/video-comments' |
15 | import { VideoModel } from '../../../models/video/video' | 24 | import { VideoModel } from '../../../models/video/video' |
@@ -33,17 +42,17 @@ videoCommentRouter.get('/:videoId/comment-threads/:threadId', | |||
33 | videoCommentRouter.post('/:videoId/comment-threads', | 42 | videoCommentRouter.post('/:videoId/comment-threads', |
34 | authenticate, | 43 | authenticate, |
35 | asyncMiddleware(addVideoCommentThreadValidator), | 44 | asyncMiddleware(addVideoCommentThreadValidator), |
36 | asyncMiddleware(addVideoCommentThreadRetryWrapper) | 45 | asyncRetryTransactionMiddleware(addVideoCommentThread) |
37 | ) | 46 | ) |
38 | videoCommentRouter.post('/:videoId/comments/:commentId', | 47 | videoCommentRouter.post('/:videoId/comments/:commentId', |
39 | authenticate, | 48 | authenticate, |
40 | asyncMiddleware(addVideoCommentReplyValidator), | 49 | asyncMiddleware(addVideoCommentReplyValidator), |
41 | asyncMiddleware(addVideoCommentReplyRetryWrapper) | 50 | asyncRetryTransactionMiddleware(addVideoCommentReply) |
42 | ) | 51 | ) |
43 | videoCommentRouter.delete('/:videoId/comments/:commentId', | 52 | videoCommentRouter.delete('/:videoId/comments/:commentId', |
44 | authenticate, | 53 | authenticate, |
45 | asyncMiddleware(removeVideoCommentValidator), | 54 | asyncMiddleware(removeVideoCommentValidator), |
46 | asyncMiddleware(removeVideoCommentRetryWrapper) | 55 | asyncRetryTransactionMiddleware(removeVideoComment) |
47 | ) | 56 | ) |
48 | 57 | ||
49 | // --------------------------------------------------------------------------- | 58 | // --------------------------------------------------------------------------- |
@@ -86,23 +95,10 @@ async function listVideoThreadComments (req: express.Request, res: express.Respo | |||
86 | return res.json(buildFormattedCommentTree(resultList)) | 95 | return res.json(buildFormattedCommentTree(resultList)) |
87 | } | 96 | } |
88 | 97 | ||
89 | async function addVideoCommentThreadRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) { | 98 | async function addVideoCommentThread (req: express.Request, res: express.Response) { |
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({ | ||
98 | comment: comment.toFormattedJSON() | ||
99 | }).end() | ||
100 | } | ||
101 | |||
102 | function addVideoCommentThread (req: express.Request, res: express.Response) { | ||
103 | const videoCommentInfo: VideoCommentCreate = req.body | 99 | const videoCommentInfo: VideoCommentCreate = req.body |
104 | 100 | ||
105 | return sequelizeTypescript.transaction(async t => { | 101 | const comment = await sequelizeTypescript.transaction(async t => { |
106 | return createVideoComment({ | 102 | return createVideoComment({ |
107 | text: videoCommentInfo.text, | 103 | text: videoCommentInfo.text, |
108 | inReplyToComment: null, | 104 | inReplyToComment: null, |
@@ -110,25 +106,16 @@ function addVideoCommentThread (req: express.Request, res: express.Response) { | |||
110 | account: res.locals.oauth.token.User.Account | 106 | account: res.locals.oauth.token.User.Account |
111 | }, t) | 107 | }, t) |
112 | }) | 108 | }) |
113 | } | ||
114 | 109 | ||
115 | async function addVideoCommentReplyRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) { | 110 | return res.json({ |
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({ | ||
124 | comment: comment.toFormattedJSON() | 111 | comment: comment.toFormattedJSON() |
125 | }).end() | 112 | }).end() |
126 | } | 113 | } |
127 | 114 | ||
128 | function addVideoCommentReply (req: express.Request, res: express.Response, next: express.NextFunction) { | 115 | async function addVideoCommentReply (req: express.Request, res: express.Response) { |
129 | const videoCommentInfo: VideoCommentCreate = req.body | 116 | const videoCommentInfo: VideoCommentCreate = req.body |
130 | 117 | ||
131 | return sequelizeTypescript.transaction(async t => { | 118 | const comment = await sequelizeTypescript.transaction(async t => { |
132 | return createVideoComment({ | 119 | return createVideoComment({ |
133 | text: videoCommentInfo.text, | 120 | text: videoCommentInfo.text, |
134 | inReplyToComment: res.locals.videoComment, | 121 | inReplyToComment: res.locals.videoComment, |
@@ -136,17 +123,10 @@ function addVideoCommentReply (req: express.Request, res: express.Response, next | |||
136 | account: res.locals.oauth.token.User.Account | 123 | account: res.locals.oauth.token.User.Account |
137 | }, t) | 124 | }, t) |
138 | }) | 125 | }) |
139 | } | ||
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 | 126 | ||
147 | await retryTransactionWrapper(removeVideoComment, options) | 127 | return res.json({ |
148 | 128 | comment: comment.toFormattedJSON() | |
149 | return res.type('json').status(204).end() | 129 | }).end() |
150 | } | 130 | } |
151 | 131 | ||
152 | async function removeVideoComment (req: express.Request, res: express.Response) { | 132 | async function removeVideoComment (req: express.Request, res: express.Response) { |
@@ -157,4 +137,6 @@ async function removeVideoComment (req: express.Request, res: express.Response) | |||
157 | }) | 137 | }) |
158 | 138 | ||
159 | logger.info('Video comment %d deleted.', videoCommentInstance.id) | 139 | logger.info('Video comment %d deleted.', videoCommentInstance.id) |
140 | |||
141 | return res.type('json').status(204).end() | ||
160 | } | 142 | } |