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