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