]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/videos/comment.ts
Begin to add avatar to actors
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / comment.ts
CommitLineData
bf1f6508
C
1import * as express from 'express'
2import { VideoCommentCreate } from '../../../../shared/models/videos/video-comment.model'
da854ddd
C
3import { retryTransactionWrapper } from '../../../helpers/database-utils'
4import { getFormattedObjects } from '../../../helpers/utils'
bf1f6508
C
5import { sequelizeTypescript } from '../../../initializers'
6import { buildFormattedCommentTree, createVideoComment } from '../../../lib/video-comment'
7import { asyncMiddleware, authenticate, paginationValidator, setPagination, setVideoCommentThreadsSort } from '../../../middlewares'
8import { videoCommentThreadsSortValidator } from '../../../middlewares/validators'
9import {
10 addVideoCommentReplyValidator, addVideoCommentThreadValidator, listVideoCommentThreadsValidator,
11 listVideoThreadCommentsValidator
12} from '../../../middlewares/validators/video-comments'
13import { VideoCommentModel } from '../../../models/video/video-comment'
14
15const videoCommentRouter = express.Router()
16
17videoCommentRouter.get('/:videoId/comment-threads',
18 paginationValidator,
19 videoCommentThreadsSortValidator,
20 setVideoCommentThreadsSort,
21 setPagination,
22 asyncMiddleware(listVideoCommentThreadsValidator),
23 asyncMiddleware(listVideoThreads)
24)
25videoCommentRouter.get('/:videoId/comment-threads/:threadId',
26 asyncMiddleware(listVideoThreadCommentsValidator),
27 asyncMiddleware(listVideoThreadComments)
28)
29
30videoCommentRouter.post('/:videoId/comment-threads',
31 authenticate,
32 asyncMiddleware(addVideoCommentThreadValidator),
33 asyncMiddleware(addVideoCommentThreadRetryWrapper)
34)
35videoCommentRouter.post('/:videoId/comments/:commentId',
36 authenticate,
37 asyncMiddleware(addVideoCommentReplyValidator),
38 asyncMiddleware(addVideoCommentReplyRetryWrapper)
39)
40
41// ---------------------------------------------------------------------------
42
43export {
44 videoCommentRouter
45}
46
47// ---------------------------------------------------------------------------
48
49async 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
55async 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
61async 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
74function 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
87async 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
100function 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}