]>
Commit | Line | Data |
---|---|---|
bf1f6508 | 1 | import * as express from 'express' |
47564bbe | 2 | import { ResultList } from '../../../../shared/models' |
bf1f6508 | 3 | import { VideoCommentCreate } from '../../../../shared/models/videos/video-comment.model' |
4cb6d457 | 4 | import { logger } from '../../../helpers/logger' |
da854ddd | 5 | import { getFormattedObjects } from '../../../helpers/utils' |
bf1f6508 C |
6 | import { sequelizeTypescript } from '../../../initializers' |
7 | import { buildFormattedCommentTree, createVideoComment } from '../../../lib/video-comment' | |
90d4bb81 C |
8 | import { |
9 | asyncMiddleware, | |
10 | asyncRetryTransactionMiddleware, | |
11 | authenticate, | |
12 | paginationValidator, | |
13 | setDefaultPagination, | |
14 | setDefaultSort | |
15 | } from '../../../middlewares' | |
bf1f6508 | 16 | import { |
90d4bb81 C |
17 | addVideoCommentReplyValidator, |
18 | addVideoCommentThreadValidator, | |
19 | listVideoCommentThreadsValidator, | |
20 | listVideoThreadCommentsValidator, | |
6e46de09 C |
21 | removeVideoCommentValidator, |
22 | videoCommentThreadsSortValidator | |
23 | } from '../../../middlewares/validators' | |
47564bbe | 24 | import { VideoModel } from '../../../models/video/video' |
bf1f6508 | 25 | import { VideoCommentModel } from '../../../models/video/video-comment' |
993cef4b | 26 | import { auditLoggerFactory, CommentAuditView, getAuditIdFromRes } from '../../../helpers/audit-logger' |
91411dba C |
27 | import { AccountModel } from '../../../models/account/account' |
28 | import { UserModel } from '../../../models/account/user' | |
bf1f6508 | 29 | |
80e36cd9 | 30 | const auditLogger = auditLoggerFactory('comments') |
bf1f6508 C |
31 | const videoCommentRouter = express.Router() |
32 | ||
33 | videoCommentRouter.get('/:videoId/comment-threads', | |
34 | paginationValidator, | |
35 | videoCommentThreadsSortValidator, | |
1174a847 | 36 | setDefaultSort, |
f05a1c30 | 37 | setDefaultPagination, |
bf1f6508 C |
38 | asyncMiddleware(listVideoCommentThreadsValidator), |
39 | asyncMiddleware(listVideoThreads) | |
40 | ) | |
41 | videoCommentRouter.get('/:videoId/comment-threads/:threadId', | |
42 | asyncMiddleware(listVideoThreadCommentsValidator), | |
43 | asyncMiddleware(listVideoThreadComments) | |
44 | ) | |
45 | ||
46 | videoCommentRouter.post('/:videoId/comment-threads', | |
47 | authenticate, | |
48 | asyncMiddleware(addVideoCommentThreadValidator), | |
90d4bb81 | 49 | asyncRetryTransactionMiddleware(addVideoCommentThread) |
bf1f6508 C |
50 | ) |
51 | videoCommentRouter.post('/:videoId/comments/:commentId', | |
52 | authenticate, | |
53 | asyncMiddleware(addVideoCommentReplyValidator), | |
90d4bb81 | 54 | asyncRetryTransactionMiddleware(addVideoCommentReply) |
bf1f6508 | 55 | ) |
4cb6d457 C |
56 | videoCommentRouter.delete('/:videoId/comments/:commentId', |
57 | authenticate, | |
58 | asyncMiddleware(removeVideoCommentValidator), | |
90d4bb81 | 59 | asyncRetryTransactionMiddleware(removeVideoComment) |
4cb6d457 | 60 | ) |
bf1f6508 C |
61 | |
62 | // --------------------------------------------------------------------------- | |
63 | ||
64 | export { | |
65 | videoCommentRouter | |
66 | } | |
67 | ||
68 | // --------------------------------------------------------------------------- | |
69 | ||
70 | async function listVideoThreads (req: express.Request, res: express.Response, next: express.NextFunction) { | |
47564bbe C |
71 | const video = res.locals.video as VideoModel |
72 | let resultList: ResultList<VideoCommentModel> | |
73 | ||
74 | if (video.commentsEnabled === true) { | |
75 | resultList = await VideoCommentModel.listThreadsForApi(video.id, req.query.start, req.query.count, req.query.sort) | |
76 | } else { | |
77 | resultList = { | |
78 | total: 0, | |
79 | data: [] | |
80 | } | |
81 | } | |
bf1f6508 C |
82 | |
83 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
84 | } | |
85 | ||
86 | async function listVideoThreadComments (req: express.Request, res: express.Response, next: express.NextFunction) { | |
47564bbe C |
87 | const video = res.locals.video as VideoModel |
88 | let resultList: ResultList<VideoCommentModel> | |
89 | ||
90 | if (video.commentsEnabled === true) { | |
627621c1 | 91 | resultList = await VideoCommentModel.listThreadCommentsForApi(video.id, res.locals.videoCommentThread.id) |
47564bbe C |
92 | } else { |
93 | resultList = { | |
94 | total: 0, | |
95 | data: [] | |
96 | } | |
97 | } | |
bf1f6508 C |
98 | |
99 | return res.json(buildFormattedCommentTree(resultList)) | |
100 | } | |
101 | ||
90d4bb81 | 102 | async function addVideoCommentThread (req: express.Request, res: express.Response) { |
bf1f6508 C |
103 | const videoCommentInfo: VideoCommentCreate = req.body |
104 | ||
90d4bb81 | 105 | const comment = await sequelizeTypescript.transaction(async t => { |
91411dba C |
106 | const account = await AccountModel.load((res.locals.oauth.token.User as UserModel).Account.id, t) |
107 | ||
bf1f6508 C |
108 | return createVideoComment({ |
109 | text: videoCommentInfo.text, | |
ea44f375 | 110 | inReplyToComment: null, |
bf1f6508 | 111 | video: res.locals.video, |
91411dba | 112 | account |
bf1f6508 C |
113 | }, t) |
114 | }) | |
bf1f6508 | 115 | |
993cef4b | 116 | auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON())) |
80e36cd9 | 117 | |
90d4bb81 | 118 | return res.json({ |
4635f59d | 119 | comment: comment.toFormattedJSON() |
bf1f6508 C |
120 | }).end() |
121 | } | |
122 | ||
90d4bb81 | 123 | async function addVideoCommentReply (req: express.Request, res: express.Response) { |
bf1f6508 C |
124 | const videoCommentInfo: VideoCommentCreate = req.body |
125 | ||
90d4bb81 | 126 | const comment = await sequelizeTypescript.transaction(async t => { |
91411dba C |
127 | const account = await AccountModel.load((res.locals.oauth.token.User as UserModel).Account.id, t) |
128 | ||
bf1f6508 C |
129 | return createVideoComment({ |
130 | text: videoCommentInfo.text, | |
ea44f375 | 131 | inReplyToComment: res.locals.videoComment, |
bf1f6508 | 132 | video: res.locals.video, |
91411dba | 133 | account |
bf1f6508 C |
134 | }, t) |
135 | }) | |
4cb6d457 | 136 | |
993cef4b | 137 | auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON())) |
80e36cd9 | 138 | |
91411dba | 139 | return res.json({ comment: comment.toFormattedJSON() }).end() |
4cb6d457 C |
140 | } |
141 | ||
142 | async function removeVideoComment (req: express.Request, res: express.Response) { | |
143 | const videoCommentInstance: VideoCommentModel = res.locals.videoComment | |
144 | ||
145 | await sequelizeTypescript.transaction(async t => { | |
146 | await videoCommentInstance.destroy({ transaction: t }) | |
147 | }) | |
148 | ||
80e36cd9 | 149 | auditLogger.delete( |
993cef4b | 150 | getAuditIdFromRes(res), |
80e36cd9 AB |
151 | new CommentAuditView(videoCommentInstance.toFormattedJSON()) |
152 | ) | |
4cb6d457 | 153 | logger.info('Video comment %d deleted.', videoCommentInstance.id) |
90d4bb81 C |
154 | |
155 | return res.type('json').status(204).end() | |
4cb6d457 | 156 | } |