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