]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/videos/comment.ts
Don't show videos of remote instance after unfollow
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / comment.ts
1 import * as express from 'express'
2 import { ResultList } from '../../../../shared/models'
3 import { VideoCommentCreate } from '../../../../shared/models/videos/video-comment.model'
4 import { retryTransactionWrapper } from '../../../helpers/database-utils'
5 import { logger } from '../../../helpers/logger'
6 import { getFormattedObjects } from '../../../helpers/utils'
7 import { sequelizeTypescript } from '../../../initializers'
8 import { buildFormattedCommentTree, createVideoComment } from '../../../lib/video-comment'
9 import { asyncMiddleware, authenticate, paginationValidator, setDefaultSort, setDefaultPagination } from '../../../middlewares'
10 import { videoCommentThreadsSortValidator } from '../../../middlewares/validators'
11 import {
12 addVideoCommentReplyValidator, addVideoCommentThreadValidator, listVideoCommentThreadsValidator, listVideoThreadCommentsValidator,
13 removeVideoCommentValidator
14 } from '../../../middlewares/validators/video-comments'
15 import { VideoModel } from '../../../models/video/video'
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,
23 setDefaultSort,
24 setDefaultPagination,
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 )
43 videoCommentRouter.delete('/:videoId/comments/:commentId',
44 authenticate,
45 asyncMiddleware(removeVideoCommentValidator),
46 asyncMiddleware(removeVideoCommentRetryWrapper)
47 )
48
49 // ---------------------------------------------------------------------------
50
51 export {
52 videoCommentRouter
53 }
54
55 // ---------------------------------------------------------------------------
56
57 async function listVideoThreads (req: express.Request, res: express.Response, next: express.NextFunction) {
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 }
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) {
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 }
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({
98 comment: comment.toFormattedJSON()
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,
108 inReplyToComment: null,
109 video: res.locals.video,
110 account: res.locals.oauth.token.User.Account
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({
124 comment: comment.toFormattedJSON()
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,
134 inReplyToComment: res.locals.videoComment,
135 video: res.locals.video,
136 account: res.locals.oauth.token.User.Account
137 }, t)
138 })
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
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 }