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