]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/videos/comment.ts
Fix video comments display with deleted comments
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / comment.ts
1 import * as express from 'express'
2 import { ResultList, ThreadsResultList, UserRight } from '../../../../shared/models'
3 import { VideoCommentCreate } from '../../../../shared/models/videos/video-comment.model'
4 import { auditLoggerFactory, CommentAuditView, getAuditIdFromRes } from '../../../helpers/audit-logger'
5 import { getFormattedObjects } from '../../../helpers/utils'
6 import { sequelizeTypescript } from '../../../initializers/database'
7 import { Notifier } from '../../../lib/notifier'
8 import { Hooks } from '../../../lib/plugins/hooks'
9 import { buildFormattedCommentTree, createVideoComment, removeComment } from '../../../lib/video-comment'
10 import {
11 asyncMiddleware,
12 asyncRetryTransactionMiddleware,
13 authenticate,
14 ensureUserHasRight,
15 optionalAuthenticate,
16 paginationValidator,
17 setDefaultPagination,
18 setDefaultSort
19 } from '../../../middlewares'
20 import {
21 addVideoCommentReplyValidator,
22 addVideoCommentThreadValidator,
23 listVideoCommentsValidator,
24 listVideoCommentThreadsValidator,
25 listVideoThreadCommentsValidator,
26 removeVideoCommentValidator,
27 videoCommentsValidator,
28 videoCommentThreadsSortValidator
29 } from '../../../middlewares/validators'
30 import { AccountModel } from '../../../models/account/account'
31 import { VideoCommentModel } from '../../../models/video/video-comment'
32 import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
33 import { logger } from '@server/helpers/logger'
34
35 const auditLogger = auditLoggerFactory('comments')
36 const videoCommentRouter = express.Router()
37
38 videoCommentRouter.get('/:videoId/comment-threads',
39 paginationValidator,
40 videoCommentThreadsSortValidator,
41 setDefaultSort,
42 setDefaultPagination,
43 asyncMiddleware(listVideoCommentThreadsValidator),
44 optionalAuthenticate,
45 asyncMiddleware(listVideoThreads)
46 )
47 videoCommentRouter.get('/:videoId/comment-threads/:threadId',
48 asyncMiddleware(listVideoThreadCommentsValidator),
49 optionalAuthenticate,
50 asyncMiddleware(listVideoThreadComments)
51 )
52
53 videoCommentRouter.post('/:videoId/comment-threads',
54 authenticate,
55 asyncMiddleware(addVideoCommentThreadValidator),
56 asyncRetryTransactionMiddleware(addVideoCommentThread)
57 )
58 videoCommentRouter.post('/:videoId/comments/:commentId',
59 authenticate,
60 asyncMiddleware(addVideoCommentReplyValidator),
61 asyncRetryTransactionMiddleware(addVideoCommentReply)
62 )
63 videoCommentRouter.delete('/:videoId/comments/:commentId',
64 authenticate,
65 asyncMiddleware(removeVideoCommentValidator),
66 asyncRetryTransactionMiddleware(removeVideoComment)
67 )
68
69 videoCommentRouter.get('/comments',
70 authenticate,
71 ensureUserHasRight(UserRight.SEE_ALL_COMMENTS),
72 paginationValidator,
73 videoCommentsValidator,
74 setDefaultSort,
75 setDefaultPagination,
76 listVideoCommentsValidator,
77 asyncMiddleware(listComments)
78 )
79
80 // ---------------------------------------------------------------------------
81
82 export {
83 videoCommentRouter
84 }
85
86 // ---------------------------------------------------------------------------
87
88 async function listComments (req: express.Request, res: express.Response) {
89 const options = {
90 start: req.query.start,
91 count: req.query.count,
92 sort: req.query.sort,
93
94 isLocal: req.query.isLocal,
95 search: req.query.search,
96 searchAccount: req.query.searchAccount,
97 searchVideo: req.query.searchVideo
98 }
99
100 const resultList = await VideoCommentModel.listCommentsForApi(options)
101
102 return res.json({
103 total: resultList.total,
104 data: resultList.data.map(c => c.toFormattedAdminJSON())
105 })
106 }
107
108 async function listVideoThreads (req: express.Request, res: express.Response) {
109 const video = res.locals.onlyVideo
110 const user = res.locals.oauth ? res.locals.oauth.token.User : undefined
111
112 let resultList: ThreadsResultList<VideoCommentModel>
113
114 if (video.commentsEnabled === true) {
115 const apiOptions = await Hooks.wrapObject({
116 videoId: video.id,
117 isVideoOwned: video.isOwned(),
118 start: req.query.start,
119 count: req.query.count,
120 sort: req.query.sort,
121 user
122 }, 'filter:api.video-threads.list.params')
123
124 resultList = await Hooks.wrapPromiseFun(
125 VideoCommentModel.listThreadsForApi,
126 apiOptions,
127 'filter:api.video-threads.list.result'
128 )
129 } else {
130 resultList = {
131 total: 0,
132 totalNotDeletedComments: 0,
133 data: []
134 }
135 }
136
137 return res.json({
138 ...getFormattedObjects(resultList.data, resultList.total),
139 totalNotDeletedComments: resultList.totalNotDeletedComments
140 })
141 }
142
143 async function listVideoThreadComments (req: express.Request, res: express.Response) {
144 const video = res.locals.onlyVideo
145 const user = res.locals.oauth ? res.locals.oauth.token.User : undefined
146
147 let resultList: ResultList<VideoCommentModel>
148
149 if (video.commentsEnabled === true) {
150 const apiOptions = await Hooks.wrapObject({
151 videoId: video.id,
152 isVideoOwned: video.isOwned(),
153 threadId: res.locals.videoCommentThread.id,
154 user
155 }, 'filter:api.video-thread-comments.list.params')
156
157 resultList = await Hooks.wrapPromiseFun(
158 VideoCommentModel.listThreadCommentsForApi,
159 apiOptions,
160 'filter:api.video-thread-comments.list.result'
161 )
162 } else {
163 resultList = {
164 total: 0,
165 data: []
166 }
167 }
168
169 logger.info('coucou', { resultList })
170
171 if (resultList.data.length === 0) {
172 return res.sendStatus(HttpStatusCode.NOT_FOUND_404)
173 }
174
175 return res.json(buildFormattedCommentTree(resultList))
176 }
177
178 async function addVideoCommentThread (req: express.Request, res: express.Response) {
179 const videoCommentInfo: VideoCommentCreate = req.body
180
181 const comment = await sequelizeTypescript.transaction(async t => {
182 const account = await AccountModel.load(res.locals.oauth.token.User.Account.id, t)
183
184 return createVideoComment({
185 text: videoCommentInfo.text,
186 inReplyToComment: null,
187 video: res.locals.videoAll,
188 account
189 }, t)
190 })
191
192 Notifier.Instance.notifyOnNewComment(comment)
193 auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
194
195 Hooks.runAction('action:api.video-thread.created', { comment })
196
197 return res.json({ comment: comment.toFormattedJSON() })
198 }
199
200 async function addVideoCommentReply (req: express.Request, res: express.Response) {
201 const videoCommentInfo: VideoCommentCreate = req.body
202
203 const comment = await sequelizeTypescript.transaction(async t => {
204 const account = await AccountModel.load(res.locals.oauth.token.User.Account.id, t)
205
206 return createVideoComment({
207 text: videoCommentInfo.text,
208 inReplyToComment: res.locals.videoCommentFull,
209 video: res.locals.videoAll,
210 account
211 }, t)
212 })
213
214 Notifier.Instance.notifyOnNewComment(comment)
215 auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
216
217 Hooks.runAction('action:api.video-comment-reply.created', { comment })
218
219 return res.json({ comment: comment.toFormattedJSON() })
220 }
221
222 async function removeVideoComment (req: express.Request, res: express.Response) {
223 const videoCommentInstance = res.locals.videoCommentFull
224
225 await removeComment(videoCommentInstance)
226
227 auditLogger.delete(getAuditIdFromRes(res), new CommentAuditView(videoCommentInstance.toFormattedJSON()))
228
229 return res.type('json')
230 .status(HttpStatusCode.NO_CONTENT_204)
231 .end()
232 }