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