]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/videos/comment.ts
Merge branch 'release/v1.2.0'
[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,
7ad9b984 11 authenticate, optionalAuthenticate,
90d4bb81
C
12 paginationValidator,
13 setDefaultPagination,
14 setDefaultSort
15} from '../../../middlewares'
bf1f6508 16import {
90d4bb81
C
17 addVideoCommentReplyValidator,
18 addVideoCommentThreadValidator,
19 listVideoCommentThreadsValidator,
20 listVideoThreadCommentsValidator,
6e46de09
C
21 removeVideoCommentValidator,
22 videoCommentThreadsSortValidator
23} from '../../../middlewares/validators'
47564bbe 24import { VideoModel } from '../../../models/video/video'
bf1f6508 25import { VideoCommentModel } from '../../../models/video/video-comment'
993cef4b 26import { auditLoggerFactory, CommentAuditView, getAuditIdFromRes } from '../../../helpers/audit-logger'
91411dba
C
27import { AccountModel } from '../../../models/account/account'
28import { UserModel } from '../../../models/account/user'
cef534ed 29import { Notifier } from '../../../lib/notifier'
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
73async function listVideoThreads (req: express.Request, res: express.Response, next: express.NextFunction) {
47564bbe 74 const video = res.locals.video as VideoModel
7ad9b984
C
75 const user: UserModel = res.locals.oauth ? res.locals.oauth.token.User : undefined
76
47564bbe
C
77 let resultList: ResultList<VideoCommentModel>
78
79 if (video.commentsEnabled === true) {
7ad9b984 80 resultList = await VideoCommentModel.listThreadsForApi(video.id, req.query.start, req.query.count, req.query.sort, user)
47564bbe
C
81 } else {
82 resultList = {
83 total: 0,
84 data: []
85 }
86 }
bf1f6508
C
87
88 return res.json(getFormattedObjects(resultList.data, resultList.total))
89}
90
91async function listVideoThreadComments (req: express.Request, res: express.Response, next: express.NextFunction) {
47564bbe 92 const video = res.locals.video as VideoModel
7ad9b984
C
93 const user: UserModel = res.locals.oauth ? res.locals.oauth.token.User : undefined
94
47564bbe
C
95 let resultList: ResultList<VideoCommentModel>
96
97 if (video.commentsEnabled === true) {
7ad9b984 98 resultList = await VideoCommentModel.listThreadCommentsForApi(video.id, res.locals.videoCommentThread.id, user)
47564bbe
C
99 } else {
100 resultList = {
101 total: 0,
102 data: []
103 }
104 }
bf1f6508
C
105
106 return res.json(buildFormattedCommentTree(resultList))
107}
108
90d4bb81 109async function addVideoCommentThread (req: express.Request, res: express.Response) {
bf1f6508
C
110 const videoCommentInfo: VideoCommentCreate = req.body
111
90d4bb81 112 const comment = await sequelizeTypescript.transaction(async t => {
91411dba
C
113 const account = await AccountModel.load((res.locals.oauth.token.User as UserModel).Account.id, t)
114
bf1f6508
C
115 return createVideoComment({
116 text: videoCommentInfo.text,
ea44f375 117 inReplyToComment: null,
bf1f6508 118 video: res.locals.video,
91411dba 119 account
bf1f6508
C
120 }, t)
121 })
bf1f6508 122
cef534ed 123 Notifier.Instance.notifyOnNewComment(comment)
993cef4b 124 auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
80e36cd9 125
90d4bb81 126 return res.json({
4635f59d 127 comment: comment.toFormattedJSON()
bf1f6508
C
128 }).end()
129}
130
90d4bb81 131async function addVideoCommentReply (req: express.Request, res: express.Response) {
bf1f6508
C
132 const videoCommentInfo: VideoCommentCreate = req.body
133
90d4bb81 134 const comment = await sequelizeTypescript.transaction(async t => {
91411dba
C
135 const account = await AccountModel.load((res.locals.oauth.token.User as UserModel).Account.id, t)
136
bf1f6508
C
137 return createVideoComment({
138 text: videoCommentInfo.text,
ea44f375 139 inReplyToComment: res.locals.videoComment,
bf1f6508 140 video: res.locals.video,
91411dba 141 account
bf1f6508
C
142 }, t)
143 })
4cb6d457 144
cef534ed 145 Notifier.Instance.notifyOnNewComment(comment)
993cef4b 146 auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
80e36cd9 147
91411dba 148 return res.json({ comment: comment.toFormattedJSON() }).end()
4cb6d457
C
149}
150
151async function removeVideoComment (req: express.Request, res: express.Response) {
152 const videoCommentInstance: VideoCommentModel = res.locals.videoComment
153
154 await sequelizeTypescript.transaction(async t => {
155 await videoCommentInstance.destroy({ transaction: t })
156 })
157
80e36cd9 158 auditLogger.delete(
993cef4b 159 getAuditIdFromRes(res),
80e36cd9
AB
160 new CommentAuditView(videoCommentInstance.toFormattedJSON())
161 )
4cb6d457 162 logger.info('Video comment %d deleted.', videoCommentInstance.id)
90d4bb81
C
163
164 return res.type('json').status(204).end()
4cb6d457 165}