]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/videos/comment.ts
Send video comment comments to followers/origin
[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'
3import { getFormattedObjects, retryTransactionWrapper } from '../../../helpers'
4import { sequelizeTypescript } from '../../../initializers'
5import { buildFormattedCommentTree, createVideoComment } from '../../../lib/video-comment'
6import { asyncMiddleware, authenticate, paginationValidator, setPagination, setVideoCommentThreadsSort } from '../../../middlewares'
7import { videoCommentThreadsSortValidator } from '../../../middlewares/validators'
8import {
9 addVideoCommentReplyValidator, addVideoCommentThreadValidator, listVideoCommentThreadsValidator,
10 listVideoThreadCommentsValidator
11} from '../../../middlewares/validators/video-comments'
12import { VideoCommentModel } from '../../../models/video/video-comment'
13
14const videoCommentRouter = express.Router()
15
16videoCommentRouter.get('/:videoId/comment-threads',
17 paginationValidator,
18 videoCommentThreadsSortValidator,
19 setVideoCommentThreadsSort,
20 setPagination,
21 asyncMiddleware(listVideoCommentThreadsValidator),
22 asyncMiddleware(listVideoThreads)
23)
24videoCommentRouter.get('/:videoId/comment-threads/:threadId',
25 asyncMiddleware(listVideoThreadCommentsValidator),
26 asyncMiddleware(listVideoThreadComments)
27)
28
29videoCommentRouter.post('/:videoId/comment-threads',
30 authenticate,
31 asyncMiddleware(addVideoCommentThreadValidator),
32 asyncMiddleware(addVideoCommentThreadRetryWrapper)
33)
34videoCommentRouter.post('/:videoId/comments/:commentId',
35 authenticate,
36 asyncMiddleware(addVideoCommentReplyValidator),
37 asyncMiddleware(addVideoCommentReplyRetryWrapper)
38)
39
40// ---------------------------------------------------------------------------
41
42export {
43 videoCommentRouter
44}
45
46// ---------------------------------------------------------------------------
47
48async 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
54async 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
60async 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
75function 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,
ea44f375 81 inReplyToComment: null,
bf1f6508 82 video: res.locals.video,
d3ea8975 83 accountId: res.locals.oauth.token.User.Account.id
bf1f6508
C
84 }, t)
85 })
86}
87
88async 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
103function 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,
ea44f375 109 inReplyToComment: res.locals.videoComment,
bf1f6508 110 video: res.locals.video,
d3ea8975 111 accountId: res.locals.oauth.token.User.Account.id
bf1f6508
C
112 }, t)
113 })
114}