]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/videos/comment.ts
Add registered setting CLI plugin install test
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / comment.ts
CommitLineData
bf1f6508 1import * as express from 'express'
0f8d00e3 2import { ResultList, UserRight } from '../../../../shared/models'
bf1f6508 3import { VideoCommentCreate } from '../../../../shared/models/videos/video-comment.model'
444c0a0e 4import { auditLoggerFactory, CommentAuditView, getAuditIdFromRes } from '../../../helpers/audit-logger'
da854ddd 5import { getFormattedObjects } from '../../../helpers/utils'
80fdaf06 6import { sequelizeTypescript } from '../../../initializers/database'
444c0a0e
C
7import { Notifier } from '../../../lib/notifier'
8import { Hooks } from '../../../lib/plugins/hooks'
9import { buildFormattedCommentTree, createVideoComment, removeComment } from '../../../lib/video-comment'
90d4bb81
C
10import {
11 asyncMiddleware,
12 asyncRetryTransactionMiddleware,
dae86118 13 authenticate,
0f8d00e3 14 ensureUserHasRight,
dae86118 15 optionalAuthenticate,
90d4bb81
C
16 paginationValidator,
17 setDefaultPagination,
18 setDefaultSort
19} from '../../../middlewares'
bf1f6508 20import {
90d4bb81
C
21 addVideoCommentReplyValidator,
22 addVideoCommentThreadValidator,
0f8d00e3 23 listVideoCommentsValidator,
90d4bb81
C
24 listVideoCommentThreadsValidator,
25 listVideoThreadCommentsValidator,
6e46de09 26 removeVideoCommentValidator,
0f8d00e3 27 videoCommentsValidator,
6e46de09
C
28 videoCommentThreadsSortValidator
29} from '../../../middlewares/validators'
91411dba 30import { AccountModel } from '../../../models/account/account'
444c0a0e 31import { VideoCommentModel } from '../../../models/video/video-comment'
bf1f6508 32
80e36cd9 33const auditLogger = auditLoggerFactory('comments')
bf1f6508
C
34const videoCommentRouter = express.Router()
35
36videoCommentRouter.get('/:videoId/comment-threads',
37 paginationValidator,
38 videoCommentThreadsSortValidator,
1174a847 39 setDefaultSort,
f05a1c30 40 setDefaultPagination,
bf1f6508 41 asyncMiddleware(listVideoCommentThreadsValidator),
7ad9b984 42 optionalAuthenticate,
bf1f6508
C
43 asyncMiddleware(listVideoThreads)
44)
45videoCommentRouter.get('/:videoId/comment-threads/:threadId',
46 asyncMiddleware(listVideoThreadCommentsValidator),
7ad9b984 47 optionalAuthenticate,
bf1f6508
C
48 asyncMiddleware(listVideoThreadComments)
49)
50
51videoCommentRouter.post('/:videoId/comment-threads',
52 authenticate,
53 asyncMiddleware(addVideoCommentThreadValidator),
90d4bb81 54 asyncRetryTransactionMiddleware(addVideoCommentThread)
bf1f6508
C
55)
56videoCommentRouter.post('/:videoId/comments/:commentId',
57 authenticate,
58 asyncMiddleware(addVideoCommentReplyValidator),
90d4bb81 59 asyncRetryTransactionMiddleware(addVideoCommentReply)
bf1f6508 60)
4cb6d457
C
61videoCommentRouter.delete('/:videoId/comments/:commentId',
62 authenticate,
63 asyncMiddleware(removeVideoCommentValidator),
90d4bb81 64 asyncRetryTransactionMiddleware(removeVideoComment)
4cb6d457 65)
bf1f6508 66
0f8d00e3
C
67videoCommentRouter.get('/comments',
68 authenticate,
69 ensureUserHasRight(UserRight.SEE_ALL_COMMENTS),
70 paginationValidator,
71 videoCommentsValidator,
72 setDefaultSort,
73 setDefaultPagination,
74 listVideoCommentsValidator,
75 asyncMiddleware(listComments)
76)
77
bf1f6508
C
78// ---------------------------------------------------------------------------
79
80export {
81 videoCommentRouter
82}
83
84// ---------------------------------------------------------------------------
85
0f8d00e3
C
86async function listComments (req: express.Request, res: express.Response) {
87 const options = {
88 start: req.query.start,
89 count: req.query.count,
90 sort: req.query.sort,
91
92 isLocal: req.query.isLocal,
93 search: req.query.search,
94 searchAccount: req.query.searchAccount,
95 searchVideo: req.query.searchVideo
96 }
97
98 const resultList = await VideoCommentModel.listCommentsForApi(options)
99
100 return res.json({
101 total: resultList.total,
102 data: resultList.data.map(c => c.toFormattedAdminJSON())
103 })
104}
105
dae86118 106async function listVideoThreads (req: express.Request, res: express.Response) {
453e83ea 107 const video = res.locals.onlyVideo
dae86118 108 const user = res.locals.oauth ? res.locals.oauth.token.User : undefined
7ad9b984 109
47564bbe
C
110 let resultList: ResultList<VideoCommentModel>
111
112 if (video.commentsEnabled === true) {
b4055e1c
C
113 const apiOptions = await Hooks.wrapObject({
114 videoId: video.id,
696d83fd 115 isVideoOwned: video.isOwned(),
b4055e1c
C
116 start: req.query.start,
117 count: req.query.count,
118 sort: req.query.sort,
453e83ea 119 user
b4055e1c
C
120 }, 'filter:api.video-threads.list.params')
121
89cd1275
C
122 resultList = await Hooks.wrapPromiseFun(
123 VideoCommentModel.listThreadsForApi,
124 apiOptions,
b4055e1c
C
125 'filter:api.video-threads.list.result'
126 )
47564bbe
C
127 } else {
128 resultList = {
129 total: 0,
130 data: []
131 }
132 }
bf1f6508
C
133
134 return res.json(getFormattedObjects(resultList.data, resultList.total))
135}
136
dae86118 137async function listVideoThreadComments (req: express.Request, res: express.Response) {
453e83ea 138 const video = res.locals.onlyVideo
dae86118 139 const user = res.locals.oauth ? res.locals.oauth.token.User : undefined
7ad9b984 140
47564bbe
C
141 let resultList: ResultList<VideoCommentModel>
142
143 if (video.commentsEnabled === true) {
b4055e1c
C
144 const apiOptions = await Hooks.wrapObject({
145 videoId: video.id,
696d83fd 146 isVideoOwned: video.isOwned(),
b4055e1c 147 threadId: res.locals.videoCommentThread.id,
6691c522 148 user
b4055e1c
C
149 }, 'filter:api.video-thread-comments.list.params')
150
89cd1275
C
151 resultList = await Hooks.wrapPromiseFun(
152 VideoCommentModel.listThreadCommentsForApi,
153 apiOptions,
b4055e1c
C
154 'filter:api.video-thread-comments.list.result'
155 )
47564bbe
C
156 } else {
157 resultList = {
158 total: 0,
159 data: []
160 }
161 }
bf1f6508
C
162
163 return res.json(buildFormattedCommentTree(resultList))
164}
165
90d4bb81 166async function addVideoCommentThread (req: express.Request, res: express.Response) {
bf1f6508
C
167 const videoCommentInfo: VideoCommentCreate = req.body
168
90d4bb81 169 const comment = await sequelizeTypescript.transaction(async t => {
dae86118 170 const account = await AccountModel.load(res.locals.oauth.token.User.Account.id, t)
91411dba 171
bf1f6508
C
172 return createVideoComment({
173 text: videoCommentInfo.text,
ea44f375 174 inReplyToComment: null,
453e83ea 175 video: res.locals.videoAll,
91411dba 176 account
bf1f6508
C
177 }, t)
178 })
bf1f6508 179
cef534ed 180 Notifier.Instance.notifyOnNewComment(comment)
993cef4b 181 auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
80e36cd9 182
b4055e1c
C
183 Hooks.runAction('action:api.video-thread.created', { comment })
184
444c0a0e 185 return res.json({ comment: comment.toFormattedJSON() })
bf1f6508
C
186}
187
90d4bb81 188async function addVideoCommentReply (req: express.Request, res: express.Response) {
bf1f6508
C
189 const videoCommentInfo: VideoCommentCreate = req.body
190
90d4bb81 191 const comment = await sequelizeTypescript.transaction(async t => {
dae86118 192 const account = await AccountModel.load(res.locals.oauth.token.User.Account.id, t)
91411dba 193
bf1f6508
C
194 return createVideoComment({
195 text: videoCommentInfo.text,
453e83ea
C
196 inReplyToComment: res.locals.videoCommentFull,
197 video: res.locals.videoAll,
91411dba 198 account
bf1f6508
C
199 }, t)
200 })
4cb6d457 201
cef534ed 202 Notifier.Instance.notifyOnNewComment(comment)
993cef4b 203 auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
80e36cd9 204
b4055e1c
C
205 Hooks.runAction('action:api.video-comment-reply.created', { comment })
206
444c0a0e 207 return res.json({ comment: comment.toFormattedJSON() })
4cb6d457
C
208}
209
210async function removeVideoComment (req: express.Request, res: express.Response) {
453e83ea 211 const videoCommentInstance = res.locals.videoCommentFull
69222afa 212
444c0a0e 213 await removeComment(videoCommentInstance)
4cb6d457 214
b4055e1c 215 auditLogger.delete(getAuditIdFromRes(res), new CommentAuditView(videoCommentInstance.toFormattedJSON()))
b4055e1c 216
8adf0a76 217 return res.type('json').status(204).end()
4cb6d457 218}