]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/videos/comment.ts
70c1148ba3839945a4a45a5d9556f66d5bb486b0
[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 { logger } from '../../../helpers/logger'
5 import { getFormattedObjects } from '../../../helpers/utils'
6 import { sequelizeTypescript } from '../../../initializers'
7 import { buildFormattedCommentTree, createVideoComment } from '../../../lib/video-comment'
8 import {
9 asyncMiddleware,
10 asyncRetryTransactionMiddleware,
11 authenticate, optionalAuthenticate,
12 paginationValidator,
13 setDefaultPagination,
14 setDefaultSort
15 } from '../../../middlewares'
16 import {
17 addVideoCommentReplyValidator,
18 addVideoCommentThreadValidator,
19 listVideoCommentThreadsValidator,
20 listVideoThreadCommentsValidator,
21 removeVideoCommentValidator,
22 videoCommentThreadsSortValidator
23 } from '../../../middlewares/validators'
24 import { VideoModel } from '../../../models/video/video'
25 import { VideoCommentModel } from '../../../models/video/video-comment'
26 import { auditLoggerFactory, CommentAuditView, getAuditIdFromRes } from '../../../helpers/audit-logger'
27 import { AccountModel } from '../../../models/account/account'
28 import { UserModel } from '../../../models/account/user'
29 import { Notifier } from '../../../lib/notifier'
30
31 const auditLogger = auditLoggerFactory('comments')
32 const videoCommentRouter = express.Router()
33
34 videoCommentRouter.get('/:videoId/comment-threads',
35 paginationValidator,
36 videoCommentThreadsSortValidator,
37 setDefaultSort,
38 setDefaultPagination,
39 asyncMiddleware(listVideoCommentThreadsValidator),
40 optionalAuthenticate,
41 asyncMiddleware(listVideoThreads)
42 )
43 videoCommentRouter.get('/:videoId/comment-threads/:threadId',
44 asyncMiddleware(listVideoThreadCommentsValidator),
45 optionalAuthenticate,
46 asyncMiddleware(listVideoThreadComments)
47 )
48
49 videoCommentRouter.post('/:videoId/comment-threads',
50 authenticate,
51 asyncMiddleware(addVideoCommentThreadValidator),
52 asyncRetryTransactionMiddleware(addVideoCommentThread)
53 )
54 videoCommentRouter.post('/:videoId/comments/:commentId',
55 authenticate,
56 asyncMiddleware(addVideoCommentReplyValidator),
57 asyncRetryTransactionMiddleware(addVideoCommentReply)
58 )
59 videoCommentRouter.delete('/:videoId/comments/:commentId',
60 authenticate,
61 asyncMiddleware(removeVideoCommentValidator),
62 asyncRetryTransactionMiddleware(removeVideoComment)
63 )
64
65 // ---------------------------------------------------------------------------
66
67 export {
68 videoCommentRouter
69 }
70
71 // ---------------------------------------------------------------------------
72
73 async function listVideoThreads (req: express.Request, res: express.Response, next: express.NextFunction) {
74 const video = res.locals.video as VideoModel
75 const user: UserModel = res.locals.oauth ? res.locals.oauth.token.User : undefined
76
77 let resultList: ResultList<VideoCommentModel>
78
79 if (video.commentsEnabled === true) {
80 resultList = await VideoCommentModel.listThreadsForApi(video.id, req.query.start, req.query.count, req.query.sort, user)
81 } else {
82 resultList = {
83 total: 0,
84 data: []
85 }
86 }
87
88 return res.json(getFormattedObjects(resultList.data, resultList.total))
89 }
90
91 async function listVideoThreadComments (req: express.Request, res: express.Response, next: express.NextFunction) {
92 const video = res.locals.video as VideoModel
93 const user: UserModel = res.locals.oauth ? res.locals.oauth.token.User : undefined
94
95 let resultList: ResultList<VideoCommentModel>
96
97 if (video.commentsEnabled === true) {
98 resultList = await VideoCommentModel.listThreadCommentsForApi(video.id, res.locals.videoCommentThread.id, user)
99 } else {
100 resultList = {
101 total: 0,
102 data: []
103 }
104 }
105
106 return res.json(buildFormattedCommentTree(resultList))
107 }
108
109 async function addVideoCommentThread (req: express.Request, res: express.Response) {
110 const videoCommentInfo: VideoCommentCreate = req.body
111
112 const comment = await sequelizeTypescript.transaction(async t => {
113 const account = await AccountModel.load((res.locals.oauth.token.User as UserModel).Account.id, t)
114
115 return createVideoComment({
116 text: videoCommentInfo.text,
117 inReplyToComment: null,
118 video: res.locals.video,
119 account
120 }, t)
121 })
122
123 Notifier.Instance.notifyOnNewComment(comment)
124 auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
125
126 return res.json({
127 comment: comment.toFormattedJSON()
128 }).end()
129 }
130
131 async function addVideoCommentReply (req: express.Request, res: express.Response) {
132 const videoCommentInfo: VideoCommentCreate = req.body
133
134 const comment = await sequelizeTypescript.transaction(async t => {
135 const account = await AccountModel.load((res.locals.oauth.token.User as UserModel).Account.id, t)
136
137 return createVideoComment({
138 text: videoCommentInfo.text,
139 inReplyToComment: res.locals.videoComment,
140 video: res.locals.video,
141 account
142 }, t)
143 })
144
145 Notifier.Instance.notifyOnNewComment(comment)
146 auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
147
148 return res.json({ comment: comment.toFormattedJSON() }).end()
149 }
150
151 async 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
158 auditLogger.delete(
159 getAuditIdFromRes(res),
160 new CommentAuditView(videoCommentInstance.toFormattedJSON())
161 )
162 logger.info('Video comment %d deleted.', videoCommentInstance.id)
163
164 return res.type('json').status(204).end()
165 }