]>
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, | |
7ad9b984 | 11 | authenticate, optionalAuthenticate, |
90d4bb81 C |
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 | 38 | asyncMiddleware(listVideoCommentThreadsValidator), |
7ad9b984 | 39 | optionalAuthenticate, |
bf1f6508 C |
40 | asyncMiddleware(listVideoThreads) |
41 | ) | |
42 | videoCommentRouter.get('/:videoId/comment-threads/:threadId', | |
43 | asyncMiddleware(listVideoThreadCommentsValidator), | |
7ad9b984 | 44 | optionalAuthenticate, |
bf1f6508 C |
45 | asyncMiddleware(listVideoThreadComments) |
46 | ) | |
47 | ||
48 | videoCommentRouter.post('/:videoId/comment-threads', | |
49 | authenticate, | |
50 | asyncMiddleware(addVideoCommentThreadValidator), | |
90d4bb81 | 51 | asyncRetryTransactionMiddleware(addVideoCommentThread) |
bf1f6508 C |
52 | ) |
53 | videoCommentRouter.post('/:videoId/comments/:commentId', | |
54 | authenticate, | |
55 | asyncMiddleware(addVideoCommentReplyValidator), | |
90d4bb81 | 56 | asyncRetryTransactionMiddleware(addVideoCommentReply) |
bf1f6508 | 57 | ) |
4cb6d457 C |
58 | videoCommentRouter.delete('/:videoId/comments/:commentId', |
59 | authenticate, | |
60 | asyncMiddleware(removeVideoCommentValidator), | |
90d4bb81 | 61 | asyncRetryTransactionMiddleware(removeVideoComment) |
4cb6d457 | 62 | ) |
bf1f6508 C |
63 | |
64 | // --------------------------------------------------------------------------- | |
65 | ||
66 | export { | |
67 | videoCommentRouter | |
68 | } | |
69 | ||
70 | // --------------------------------------------------------------------------- | |
71 | ||
72 | async function listVideoThreads (req: express.Request, res: express.Response, next: express.NextFunction) { | |
47564bbe | 73 | const video = res.locals.video as VideoModel |
7ad9b984 C |
74 | const user: UserModel = res.locals.oauth ? res.locals.oauth.token.User : undefined |
75 | ||
47564bbe C |
76 | let resultList: ResultList<VideoCommentModel> |
77 | ||
78 | if (video.commentsEnabled === true) { | |
7ad9b984 | 79 | resultList = await VideoCommentModel.listThreadsForApi(video.id, req.query.start, req.query.count, req.query.sort, user) |
47564bbe C |
80 | } else { |
81 | resultList = { | |
82 | total: 0, | |
83 | data: [] | |
84 | } | |
85 | } | |
bf1f6508 C |
86 | |
87 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
88 | } | |
89 | ||
90 | async function listVideoThreadComments (req: express.Request, res: express.Response, next: express.NextFunction) { | |
47564bbe | 91 | const video = res.locals.video as VideoModel |
7ad9b984 C |
92 | const user: UserModel = res.locals.oauth ? res.locals.oauth.token.User : undefined |
93 | ||
47564bbe C |
94 | let resultList: ResultList<VideoCommentModel> |
95 | ||
96 | if (video.commentsEnabled === true) { | |
7ad9b984 | 97 | resultList = await VideoCommentModel.listThreadCommentsForApi(video.id, res.locals.videoCommentThread.id, user) |
47564bbe C |
98 | } else { |
99 | resultList = { | |
100 | total: 0, | |
101 | data: [] | |
102 | } | |
103 | } | |
bf1f6508 C |
104 | |
105 | return res.json(buildFormattedCommentTree(resultList)) | |
106 | } | |
107 | ||
90d4bb81 | 108 | async function addVideoCommentThread (req: express.Request, res: express.Response) { |
bf1f6508 C |
109 | const videoCommentInfo: VideoCommentCreate = req.body |
110 | ||
90d4bb81 | 111 | const comment = await sequelizeTypescript.transaction(async t => { |
91411dba C |
112 | const account = await AccountModel.load((res.locals.oauth.token.User as UserModel).Account.id, t) |
113 | ||
bf1f6508 C |
114 | return createVideoComment({ |
115 | text: videoCommentInfo.text, | |
ea44f375 | 116 | inReplyToComment: null, |
bf1f6508 | 117 | video: res.locals.video, |
91411dba | 118 | account |
bf1f6508 C |
119 | }, t) |
120 | }) | |
bf1f6508 | 121 | |
993cef4b | 122 | auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON())) |
80e36cd9 | 123 | |
90d4bb81 | 124 | return res.json({ |
4635f59d | 125 | comment: comment.toFormattedJSON() |
bf1f6508 C |
126 | }).end() |
127 | } | |
128 | ||
90d4bb81 | 129 | async function addVideoCommentReply (req: express.Request, res: express.Response) { |
bf1f6508 C |
130 | const videoCommentInfo: VideoCommentCreate = req.body |
131 | ||
90d4bb81 | 132 | const comment = await sequelizeTypescript.transaction(async t => { |
91411dba C |
133 | const account = await AccountModel.load((res.locals.oauth.token.User as UserModel).Account.id, t) |
134 | ||
bf1f6508 C |
135 | return createVideoComment({ |
136 | text: videoCommentInfo.text, | |
ea44f375 | 137 | inReplyToComment: res.locals.videoComment, |
bf1f6508 | 138 | video: res.locals.video, |
91411dba | 139 | account |
bf1f6508 C |
140 | }, t) |
141 | }) | |
4cb6d457 | 142 | |
993cef4b | 143 | auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON())) |
80e36cd9 | 144 | |
91411dba | 145 | return res.json({ comment: comment.toFormattedJSON() }).end() |
4cb6d457 C |
146 | } |
147 | ||
148 | async function removeVideoComment (req: express.Request, res: express.Response) { | |
149 | const videoCommentInstance: VideoCommentModel = res.locals.videoComment | |
150 | ||
151 | await sequelizeTypescript.transaction(async t => { | |
152 | await videoCommentInstance.destroy({ transaction: t }) | |
153 | }) | |
154 | ||
80e36cd9 | 155 | auditLogger.delete( |
993cef4b | 156 | getAuditIdFromRes(res), |
80e36cd9 AB |
157 | new CommentAuditView(videoCommentInstance.toFormattedJSON()) |
158 | ) | |
4cb6d457 | 159 | logger.info('Video comment %d deleted.', videoCommentInstance.id) |
90d4bb81 C |
160 | |
161 | return res.type('json').status(204).end() | |
4cb6d457 | 162 | } |