]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/videos/comment.ts
Introduce comments command
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / comment.ts
CommitLineData
bf1f6508 1import * as express from 'express'
300cb723 2import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
12edc149
C
3import { ResultList, ThreadsResultList, UserRight, VideoCommentCreate } from '../../../../shared/models'
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,
94 search: req.query.search,
95 searchAccount: req.query.searchAccount,
96 searchVideo: req.query.searchVideo
97 }
98
99 const resultList = await VideoCommentModel.listCommentsForApi(options)
100
101 return res.json({
102 total: resultList.total,
103 data: resultList.data.map(c => c.toFormattedAdminJSON())
104 })
105}
106
dae86118 107async function listVideoThreads (req: express.Request, res: express.Response) {
453e83ea 108 const video = res.locals.onlyVideo
dae86118 109 const user = res.locals.oauth ? res.locals.oauth.token.User : undefined
7ad9b984 110
9d6b9d10 111 let resultList: ThreadsResultList<VideoCommentModel>
47564bbe
C
112
113 if (video.commentsEnabled === true) {
b4055e1c
C
114 const apiOptions = await Hooks.wrapObject({
115 videoId: video.id,
696d83fd 116 isVideoOwned: video.isOwned(),
b4055e1c
C
117 start: req.query.start,
118 count: req.query.count,
119 sort: req.query.sort,
453e83ea 120 user
b4055e1c
C
121 }, 'filter:api.video-threads.list.params')
122
89cd1275
C
123 resultList = await Hooks.wrapPromiseFun(
124 VideoCommentModel.listThreadsForApi,
125 apiOptions,
b4055e1c
C
126 'filter:api.video-threads.list.result'
127 )
47564bbe
C
128 } else {
129 resultList = {
130 total: 0,
9d6b9d10 131 totalNotDeletedComments: 0,
47564bbe
C
132 data: []
133 }
134 }
bf1f6508 135
9d6b9d10
C
136 return res.json({
137 ...getFormattedObjects(resultList.data, resultList.total),
138 totalNotDeletedComments: resultList.totalNotDeletedComments
12edc149 139 } as VideoCommentThreads)
bf1f6508
C
140}
141
dae86118 142async function listVideoThreadComments (req: express.Request, res: express.Response) {
453e83ea 143 const video = res.locals.onlyVideo
dae86118 144 const user = res.locals.oauth ? res.locals.oauth.token.User : undefined
7ad9b984 145
47564bbe
C
146 let resultList: ResultList<VideoCommentModel>
147
148 if (video.commentsEnabled === true) {
b4055e1c
C
149 const apiOptions = await Hooks.wrapObject({
150 videoId: video.id,
696d83fd 151 isVideoOwned: video.isOwned(),
b4055e1c 152 threadId: res.locals.videoCommentThread.id,
6691c522 153 user
b4055e1c
C
154 }, 'filter:api.video-thread-comments.list.params')
155
89cd1275
C
156 resultList = await Hooks.wrapPromiseFun(
157 VideoCommentModel.listThreadCommentsForApi,
158 apiOptions,
b4055e1c
C
159 'filter:api.video-thread-comments.list.result'
160 )
47564bbe
C
161 } else {
162 resultList = {
163 total: 0,
164 data: []
165 }
166 }
bf1f6508 167
9b337d8c 168 if (resultList.data.length === 0) {
76148b27
RK
169 return res.fail({
170 status: HttpStatusCode.NOT_FOUND_404,
171 message: 'No comments were found'
172 })
9b337d8c
C
173 }
174
bf1f6508
C
175 return res.json(buildFormattedCommentTree(resultList))
176}
177
90d4bb81 178async function addVideoCommentThread (req: express.Request, res: express.Response) {
bf1f6508
C
179 const videoCommentInfo: VideoCommentCreate = req.body
180
90d4bb81 181 const comment = await sequelizeTypescript.transaction(async t => {
dae86118 182 const account = await AccountModel.load(res.locals.oauth.token.User.Account.id, t)
91411dba 183
bf1f6508
C
184 return createVideoComment({
185 text: videoCommentInfo.text,
ea44f375 186 inReplyToComment: null,
453e83ea 187 video: res.locals.videoAll,
91411dba 188 account
bf1f6508
C
189 }, t)
190 })
bf1f6508 191
cef534ed 192 Notifier.Instance.notifyOnNewComment(comment)
993cef4b 193 auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
80e36cd9 194
b4055e1c
C
195 Hooks.runAction('action:api.video-thread.created', { comment })
196
444c0a0e 197 return res.json({ comment: comment.toFormattedJSON() })
bf1f6508
C
198}
199
90d4bb81 200async function addVideoCommentReply (req: express.Request, res: express.Response) {
bf1f6508
C
201 const videoCommentInfo: VideoCommentCreate = req.body
202
90d4bb81 203 const comment = await sequelizeTypescript.transaction(async t => {
dae86118 204 const account = await AccountModel.load(res.locals.oauth.token.User.Account.id, t)
91411dba 205
bf1f6508
C
206 return createVideoComment({
207 text: videoCommentInfo.text,
453e83ea
C
208 inReplyToComment: res.locals.videoCommentFull,
209 video: res.locals.videoAll,
91411dba 210 account
bf1f6508
C
211 }, t)
212 })
4cb6d457 213
cef534ed 214 Notifier.Instance.notifyOnNewComment(comment)
993cef4b 215 auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
80e36cd9 216
b4055e1c
C
217 Hooks.runAction('action:api.video-comment-reply.created', { comment })
218
444c0a0e 219 return res.json({ comment: comment.toFormattedJSON() })
4cb6d457
C
220}
221
222async function removeVideoComment (req: express.Request, res: express.Response) {
453e83ea 223 const videoCommentInstance = res.locals.videoCommentFull
69222afa 224
444c0a0e 225 await removeComment(videoCommentInstance)
4cb6d457 226
b4055e1c 227 auditLogger.delete(getAuditIdFromRes(res), new CommentAuditView(videoCommentInstance.toFormattedJSON()))
b4055e1c 228
2d53be02
RK
229 return res.type('json')
230 .status(HttpStatusCode.NO_CONTENT_204)
231 .end()
4cb6d457 232}