aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/videos/comment.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-06-13 14:27:40 +0200
committerChocobozzz <me@florianbigard.com>2018-06-13 14:27:40 +0200
commit90d4bb8125e80c8060416d4d135ddeaf0a622ede (patch)
treeb3b7181329a08ecc930b54fe7b48095c4155393c /server/controllers/api/videos/comment.ts
parent3cd0734fd9b0ff21aaef02317a874e8f1c06e027 (diff)
downloadPeerTube-90d4bb8125e80c8060416d4d135ddeaf0a622ede.tar.gz
PeerTube-90d4bb8125e80c8060416d4d135ddeaf0a622ede.tar.zst
PeerTube-90d4bb8125e80c8060416d4d135ddeaf0a622ede.zip
Refractor retry transaction function
Diffstat (limited to 'server/controllers/api/videos/comment.ts')
-rw-r--r--server/controllers/api/videos/comment.ts68
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 @@
1import * as express from 'express' 1import * as express from 'express'
2import { ResultList } from '../../../../shared/models' 2import { ResultList } from '../../../../shared/models'
3import { VideoCommentCreate } from '../../../../shared/models/videos/video-comment.model' 3import { VideoCommentCreate } from '../../../../shared/models/videos/video-comment.model'
4import { retryTransactionWrapper } from '../../../helpers/database-utils'
5import { logger } from '../../../helpers/logger' 4import { logger } from '../../../helpers/logger'
6import { getFormattedObjects } from '../../../helpers/utils' 5import { getFormattedObjects } from '../../../helpers/utils'
7import { sequelizeTypescript } from '../../../initializers' 6import { sequelizeTypescript } from '../../../initializers'
8import { buildFormattedCommentTree, createVideoComment } from '../../../lib/video-comment' 7import { buildFormattedCommentTree, createVideoComment } from '../../../lib/video-comment'
9import { asyncMiddleware, authenticate, paginationValidator, setDefaultSort, setDefaultPagination } from '../../../middlewares' 8import {
9 asyncMiddleware,
10 asyncRetryTransactionMiddleware,
11 authenticate,
12 paginationValidator,
13 setDefaultPagination,
14 setDefaultSort
15} from '../../../middlewares'
10import { videoCommentThreadsSortValidator } from '../../../middlewares/validators' 16import { videoCommentThreadsSortValidator } from '../../../middlewares/validators'
11import { 17import {
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'
15import { VideoModel } from '../../../models/video/video' 24import { VideoModel } from '../../../models/video/video'
@@ -33,17 +42,17 @@ videoCommentRouter.get('/:videoId/comment-threads/:threadId',
33videoCommentRouter.post('/:videoId/comment-threads', 42videoCommentRouter.post('/:videoId/comment-threads',
34 authenticate, 43 authenticate,
35 asyncMiddleware(addVideoCommentThreadValidator), 44 asyncMiddleware(addVideoCommentThreadValidator),
36 asyncMiddleware(addVideoCommentThreadRetryWrapper) 45 asyncRetryTransactionMiddleware(addVideoCommentThread)
37) 46)
38videoCommentRouter.post('/:videoId/comments/:commentId', 47videoCommentRouter.post('/:videoId/comments/:commentId',
39 authenticate, 48 authenticate,
40 asyncMiddleware(addVideoCommentReplyValidator), 49 asyncMiddleware(addVideoCommentReplyValidator),
41 asyncMiddleware(addVideoCommentReplyRetryWrapper) 50 asyncRetryTransactionMiddleware(addVideoCommentReply)
42) 51)
43videoCommentRouter.delete('/:videoId/comments/:commentId', 52videoCommentRouter.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
89async function addVideoCommentThreadRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) { 98async 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
102function 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
115async 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
128function addVideoCommentReply (req: express.Request, res: express.Response, next: express.NextFunction) { 115async 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
141async 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
152async function removeVideoComment (req: express.Request, res: express.Response) { 132async 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}