]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/videos/comment.ts
bdd3cf9e27334a1689d2b4ea543375b613e4bcb0
[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 { 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 optionalAuthenticate,
15 paginationValidator,
16 setDefaultPagination,
17 setDefaultSort
18 } from '../../../middlewares'
19 import {
20 addVideoCommentReplyValidator,
21 addVideoCommentThreadValidator,
22 listVideoCommentThreadsValidator,
23 listVideoThreadCommentsValidator,
24 removeVideoCommentValidator,
25 videoCommentThreadsSortValidator
26 } from '../../../middlewares/validators'
27 import { AccountModel } from '../../../models/account/account'
28 import { VideoCommentModel } from '../../../models/video/video-comment'
29
30 const auditLogger = auditLoggerFactory('comments')
31 const videoCommentRouter = express.Router()
32
33 videoCommentRouter.get('/:videoId/comment-threads',
34 paginationValidator,
35 videoCommentThreadsSortValidator,
36 setDefaultSort,
37 setDefaultPagination,
38 asyncMiddleware(listVideoCommentThreadsValidator),
39 optionalAuthenticate,
40 asyncMiddleware(listVideoThreads)
41 )
42 videoCommentRouter.get('/:videoId/comment-threads/:threadId',
43 asyncMiddleware(listVideoThreadCommentsValidator),
44 optionalAuthenticate,
45 asyncMiddleware(listVideoThreadComments)
46 )
47
48 videoCommentRouter.post('/:videoId/comment-threads',
49 authenticate,
50 asyncMiddleware(addVideoCommentThreadValidator),
51 asyncRetryTransactionMiddleware(addVideoCommentThread)
52 )
53 videoCommentRouter.post('/:videoId/comments/:commentId',
54 authenticate,
55 asyncMiddleware(addVideoCommentReplyValidator),
56 asyncRetryTransactionMiddleware(addVideoCommentReply)
57 )
58 videoCommentRouter.delete('/:videoId/comments/:commentId',
59 authenticate,
60 asyncMiddleware(removeVideoCommentValidator),
61 asyncRetryTransactionMiddleware(removeVideoComment)
62 )
63
64 // ---------------------------------------------------------------------------
65
66 export {
67 videoCommentRouter
68 }
69
70 // ---------------------------------------------------------------------------
71
72 async function listVideoThreads (req: express.Request, res: express.Response) {
73 const video = res.locals.onlyVideo
74 const user = res.locals.oauth ? res.locals.oauth.token.User : undefined
75
76 let resultList: ResultList<VideoCommentModel>
77
78 if (video.commentsEnabled === true) {
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,
84 user
85 }, 'filter:api.video-threads.list.params')
86
87 resultList = await Hooks.wrapPromiseFun(
88 VideoCommentModel.listThreadsForApi,
89 apiOptions,
90 'filter:api.video-threads.list.result'
91 )
92 } else {
93 resultList = {
94 total: 0,
95 data: []
96 }
97 }
98
99 return res.json(getFormattedObjects(resultList.data, resultList.total))
100 }
101
102 async function listVideoThreadComments (req: express.Request, res: express.Response) {
103 const video = res.locals.onlyVideo
104 const user = res.locals.oauth ? res.locals.oauth.token.User : undefined
105
106 let resultList: ResultList<VideoCommentModel>
107
108 if (video.commentsEnabled === true) {
109 const apiOptions = await Hooks.wrapObject({
110 videoId: video.id,
111 threadId: res.locals.videoCommentThread.id,
112 user
113 }, 'filter:api.video-thread-comments.list.params')
114
115 resultList = await Hooks.wrapPromiseFun(
116 VideoCommentModel.listThreadCommentsForApi,
117 apiOptions,
118 'filter:api.video-thread-comments.list.result'
119 )
120 } else {
121 resultList = {
122 total: 0,
123 data: []
124 }
125 }
126
127 return res.json(buildFormattedCommentTree(resultList))
128 }
129
130 async function addVideoCommentThread (req: express.Request, res: express.Response) {
131 const videoCommentInfo: VideoCommentCreate = req.body
132
133 const comment = await sequelizeTypescript.transaction(async t => {
134 const account = await AccountModel.load(res.locals.oauth.token.User.Account.id, t)
135
136 return createVideoComment({
137 text: videoCommentInfo.text,
138 inReplyToComment: null,
139 video: res.locals.videoAll,
140 account
141 }, t)
142 })
143
144 Notifier.Instance.notifyOnNewComment(comment)
145 auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
146
147 Hooks.runAction('action:api.video-thread.created', { comment })
148
149 return res.json({ comment: comment.toFormattedJSON() })
150 }
151
152 async function addVideoCommentReply (req: express.Request, res: express.Response) {
153 const videoCommentInfo: VideoCommentCreate = req.body
154
155 const comment = await sequelizeTypescript.transaction(async t => {
156 const account = await AccountModel.load(res.locals.oauth.token.User.Account.id, t)
157
158 return createVideoComment({
159 text: videoCommentInfo.text,
160 inReplyToComment: res.locals.videoCommentFull,
161 video: res.locals.videoAll,
162 account
163 }, t)
164 })
165
166 Notifier.Instance.notifyOnNewComment(comment)
167 auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
168
169 Hooks.runAction('action:api.video-comment-reply.created', { comment })
170
171 return res.json({ comment: comment.toFormattedJSON() })
172 }
173
174 async function removeVideoComment (req: express.Request, res: express.Response) {
175 const videoCommentInstance = res.locals.videoCommentFull
176
177 await removeComment(videoCommentInstance)
178
179 auditLogger.delete(getAuditIdFromRes(res), new CommentAuditView(videoCommentInstance.toFormattedJSON()))
180
181 return res.type('json').status(204)
182 }