]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/videos/comment.ts
Add ability to bulk delete comments
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / comment.ts
CommitLineData
bf1f6508 1import * as express from 'express'
47564bbe 2import { ResultList } from '../../../../shared/models'
bf1f6508 3import { VideoCommentCreate } from '../../../../shared/models/videos/video-comment.model'
444c0a0e 4import { auditLoggerFactory, CommentAuditView, getAuditIdFromRes } from '../../../helpers/audit-logger'
da854ddd 5import { getFormattedObjects } from '../../../helpers/utils'
80fdaf06 6import { sequelizeTypescript } from '../../../initializers/database'
444c0a0e
C
7import { Notifier } from '../../../lib/notifier'
8import { Hooks } from '../../../lib/plugins/hooks'
9import { buildFormattedCommentTree, createVideoComment, removeComment } from '../../../lib/video-comment'
90d4bb81
C
10import {
11 asyncMiddleware,
12 asyncRetryTransactionMiddleware,
dae86118
C
13 authenticate,
14 optionalAuthenticate,
90d4bb81
C
15 paginationValidator,
16 setDefaultPagination,
17 setDefaultSort
18} from '../../../middlewares'
bf1f6508 19import {
90d4bb81
C
20 addVideoCommentReplyValidator,
21 addVideoCommentThreadValidator,
22 listVideoCommentThreadsValidator,
23 listVideoThreadCommentsValidator,
6e46de09
C
24 removeVideoCommentValidator,
25 videoCommentThreadsSortValidator
26} from '../../../middlewares/validators'
91411dba 27import { AccountModel } from '../../../models/account/account'
444c0a0e 28import { VideoCommentModel } from '../../../models/video/video-comment'
bf1f6508 29
80e36cd9 30const auditLogger = auditLoggerFactory('comments')
bf1f6508
C
31const videoCommentRouter = express.Router()
32
33videoCommentRouter.get('/:videoId/comment-threads',
34 paginationValidator,
35 videoCommentThreadsSortValidator,
1174a847 36 setDefaultSort,
f05a1c30 37 setDefaultPagination,
bf1f6508 38 asyncMiddleware(listVideoCommentThreadsValidator),
7ad9b984 39 optionalAuthenticate,
bf1f6508
C
40 asyncMiddleware(listVideoThreads)
41)
42videoCommentRouter.get('/:videoId/comment-threads/:threadId',
43 asyncMiddleware(listVideoThreadCommentsValidator),
7ad9b984 44 optionalAuthenticate,
bf1f6508
C
45 asyncMiddleware(listVideoThreadComments)
46)
47
48videoCommentRouter.post('/:videoId/comment-threads',
49 authenticate,
50 asyncMiddleware(addVideoCommentThreadValidator),
90d4bb81 51 asyncRetryTransactionMiddleware(addVideoCommentThread)
bf1f6508
C
52)
53videoCommentRouter.post('/:videoId/comments/:commentId',
54 authenticate,
55 asyncMiddleware(addVideoCommentReplyValidator),
90d4bb81 56 asyncRetryTransactionMiddleware(addVideoCommentReply)
bf1f6508 57)
4cb6d457
C
58videoCommentRouter.delete('/:videoId/comments/:commentId',
59 authenticate,
60 asyncMiddleware(removeVideoCommentValidator),
90d4bb81 61 asyncRetryTransactionMiddleware(removeVideoComment)
4cb6d457 62)
bf1f6508
C
63
64// ---------------------------------------------------------------------------
65
66export {
67 videoCommentRouter
68}
69
70// ---------------------------------------------------------------------------
71
dae86118 72async function listVideoThreads (req: express.Request, res: express.Response) {
453e83ea 73 const video = res.locals.onlyVideo
dae86118 74 const user = res.locals.oauth ? res.locals.oauth.token.User : undefined
7ad9b984 75
47564bbe
C
76 let resultList: ResultList<VideoCommentModel>
77
78 if (video.commentsEnabled === true) {
b4055e1c
C
79 const apiOptions = await Hooks.wrapObject({
80 videoId: video.id,
81 start: req.query.start,
82 count: req.query.count,
83 sort: req.query.sort,
453e83ea 84 user
b4055e1c
C
85 }, 'filter:api.video-threads.list.params')
86
89cd1275
C
87 resultList = await Hooks.wrapPromiseFun(
88 VideoCommentModel.listThreadsForApi,
89 apiOptions,
b4055e1c
C
90 'filter:api.video-threads.list.result'
91 )
47564bbe
C
92 } else {
93 resultList = {
94 total: 0,
95 data: []
96 }
97 }
bf1f6508
C
98
99 return res.json(getFormattedObjects(resultList.data, resultList.total))
100}
101
dae86118 102async function listVideoThreadComments (req: express.Request, res: express.Response) {
453e83ea 103 const video = res.locals.onlyVideo
dae86118 104 const user = res.locals.oauth ? res.locals.oauth.token.User : undefined
7ad9b984 105
47564bbe
C
106 let resultList: ResultList<VideoCommentModel>
107
108 if (video.commentsEnabled === true) {
b4055e1c
C
109 const apiOptions = await Hooks.wrapObject({
110 videoId: video.id,
111 threadId: res.locals.videoCommentThread.id,
6691c522 112 user
b4055e1c
C
113 }, 'filter:api.video-thread-comments.list.params')
114
89cd1275
C
115 resultList = await Hooks.wrapPromiseFun(
116 VideoCommentModel.listThreadCommentsForApi,
117 apiOptions,
b4055e1c
C
118 'filter:api.video-thread-comments.list.result'
119 )
47564bbe
C
120 } else {
121 resultList = {
122 total: 0,
123 data: []
124 }
125 }
bf1f6508
C
126
127 return res.json(buildFormattedCommentTree(resultList))
128}
129
90d4bb81 130async function addVideoCommentThread (req: express.Request, res: express.Response) {
bf1f6508
C
131 const videoCommentInfo: VideoCommentCreate = req.body
132
90d4bb81 133 const comment = await sequelizeTypescript.transaction(async t => {
dae86118 134 const account = await AccountModel.load(res.locals.oauth.token.User.Account.id, t)
91411dba 135
bf1f6508
C
136 return createVideoComment({
137 text: videoCommentInfo.text,
ea44f375 138 inReplyToComment: null,
453e83ea 139 video: res.locals.videoAll,
91411dba 140 account
bf1f6508
C
141 }, t)
142 })
bf1f6508 143
cef534ed 144 Notifier.Instance.notifyOnNewComment(comment)
993cef4b 145 auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
80e36cd9 146
b4055e1c
C
147 Hooks.runAction('action:api.video-thread.created', { comment })
148
444c0a0e 149 return res.json({ comment: comment.toFormattedJSON() })
bf1f6508
C
150}
151
90d4bb81 152async function addVideoCommentReply (req: express.Request, res: express.Response) {
bf1f6508
C
153 const videoCommentInfo: VideoCommentCreate = req.body
154
90d4bb81 155 const comment = await sequelizeTypescript.transaction(async t => {
dae86118 156 const account = await AccountModel.load(res.locals.oauth.token.User.Account.id, t)
91411dba 157
bf1f6508
C
158 return createVideoComment({
159 text: videoCommentInfo.text,
453e83ea
C
160 inReplyToComment: res.locals.videoCommentFull,
161 video: res.locals.videoAll,
91411dba 162 account
bf1f6508
C
163 }, t)
164 })
4cb6d457 165
cef534ed 166 Notifier.Instance.notifyOnNewComment(comment)
993cef4b 167 auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
80e36cd9 168
b4055e1c
C
169 Hooks.runAction('action:api.video-comment-reply.created', { comment })
170
444c0a0e 171 return res.json({ comment: comment.toFormattedJSON() })
4cb6d457
C
172}
173
174async function removeVideoComment (req: express.Request, res: express.Response) {
453e83ea 175 const videoCommentInstance = res.locals.videoCommentFull
69222afa 176
444c0a0e 177 await removeComment(videoCommentInstance)
4cb6d457 178
b4055e1c 179 auditLogger.delete(getAuditIdFromRes(res), new CommentAuditView(videoCommentInstance.toFormattedJSON()))
b4055e1c 180
444c0a0e 181 return res.type('json').status(204)
4cb6d457 182}