]>
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, | |
dae86118 C |
11 | authenticate, |
12 | optionalAuthenticate, | |
90d4bb81 C |
13 | paginationValidator, |
14 | setDefaultPagination, | |
15 | setDefaultSort | |
16 | } from '../../../middlewares' | |
bf1f6508 | 17 | import { |
90d4bb81 C |
18 | addVideoCommentReplyValidator, |
19 | addVideoCommentThreadValidator, | |
20 | listVideoCommentThreadsValidator, | |
21 | listVideoThreadCommentsValidator, | |
6e46de09 C |
22 | removeVideoCommentValidator, |
23 | videoCommentThreadsSortValidator | |
24 | } from '../../../middlewares/validators' | |
bf1f6508 | 25 | import { VideoCommentModel } from '../../../models/video/video-comment' |
993cef4b | 26 | import { auditLoggerFactory, CommentAuditView, getAuditIdFromRes } from '../../../helpers/audit-logger' |
91411dba | 27 | import { AccountModel } from '../../../models/account/account' |
cef534ed | 28 | import { Notifier } from '../../../lib/notifier' |
b4055e1c | 29 | import { Hooks } from '../../../lib/plugins/hooks' |
511765c9 | 30 | import { sendDeleteVideoComment } from '../../../lib/activitypub/send' |
bf1f6508 | 31 | |
80e36cd9 | 32 | const auditLogger = auditLoggerFactory('comments') |
bf1f6508 C |
33 | const videoCommentRouter = express.Router() |
34 | ||
35 | videoCommentRouter.get('/:videoId/comment-threads', | |
36 | paginationValidator, | |
37 | videoCommentThreadsSortValidator, | |
1174a847 | 38 | setDefaultSort, |
f05a1c30 | 39 | setDefaultPagination, |
bf1f6508 | 40 | asyncMiddleware(listVideoCommentThreadsValidator), |
7ad9b984 | 41 | optionalAuthenticate, |
bf1f6508 C |
42 | asyncMiddleware(listVideoThreads) |
43 | ) | |
44 | videoCommentRouter.get('/:videoId/comment-threads/:threadId', | |
45 | asyncMiddleware(listVideoThreadCommentsValidator), | |
7ad9b984 | 46 | optionalAuthenticate, |
bf1f6508 C |
47 | asyncMiddleware(listVideoThreadComments) |
48 | ) | |
49 | ||
50 | videoCommentRouter.post('/:videoId/comment-threads', | |
51 | authenticate, | |
52 | asyncMiddleware(addVideoCommentThreadValidator), | |
90d4bb81 | 53 | asyncRetryTransactionMiddleware(addVideoCommentThread) |
bf1f6508 C |
54 | ) |
55 | videoCommentRouter.post('/:videoId/comments/:commentId', | |
56 | authenticate, | |
57 | asyncMiddleware(addVideoCommentReplyValidator), | |
90d4bb81 | 58 | asyncRetryTransactionMiddleware(addVideoCommentReply) |
bf1f6508 | 59 | ) |
4cb6d457 C |
60 | videoCommentRouter.delete('/:videoId/comments/:commentId', |
61 | authenticate, | |
62 | asyncMiddleware(removeVideoCommentValidator), | |
90d4bb81 | 63 | asyncRetryTransactionMiddleware(removeVideoComment) |
4cb6d457 | 64 | ) |
bf1f6508 C |
65 | |
66 | // --------------------------------------------------------------------------- | |
67 | ||
68 | export { | |
69 | videoCommentRouter | |
70 | } | |
71 | ||
72 | // --------------------------------------------------------------------------- | |
73 | ||
dae86118 | 74 | async function listVideoThreads (req: express.Request, res: express.Response) { |
453e83ea | 75 | const video = res.locals.onlyVideo |
dae86118 | 76 | const user = res.locals.oauth ? res.locals.oauth.token.User : undefined |
7ad9b984 | 77 | |
47564bbe C |
78 | let resultList: ResultList<VideoCommentModel> |
79 | ||
80 | if (video.commentsEnabled === true) { | |
b4055e1c C |
81 | const apiOptions = await Hooks.wrapObject({ |
82 | videoId: video.id, | |
83 | start: req.query.start, | |
84 | count: req.query.count, | |
85 | sort: req.query.sort, | |
453e83ea | 86 | user |
b4055e1c C |
87 | }, 'filter:api.video-threads.list.params') |
88 | ||
89cd1275 C |
89 | resultList = await Hooks.wrapPromiseFun( |
90 | VideoCommentModel.listThreadsForApi, | |
91 | apiOptions, | |
b4055e1c C |
92 | 'filter:api.video-threads.list.result' |
93 | ) | |
47564bbe C |
94 | } else { |
95 | resultList = { | |
96 | total: 0, | |
97 | data: [] | |
98 | } | |
99 | } | |
bf1f6508 C |
100 | |
101 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
102 | } | |
103 | ||
dae86118 | 104 | async function listVideoThreadComments (req: express.Request, res: express.Response) { |
453e83ea | 105 | const video = res.locals.onlyVideo |
dae86118 | 106 | const user = res.locals.oauth ? res.locals.oauth.token.User : undefined |
7ad9b984 | 107 | |
47564bbe C |
108 | let resultList: ResultList<VideoCommentModel> |
109 | ||
110 | if (video.commentsEnabled === true) { | |
b4055e1c C |
111 | const apiOptions = await Hooks.wrapObject({ |
112 | videoId: video.id, | |
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 | 132 | async 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 | ||
90d4bb81 | 151 | return res.json({ |
4635f59d | 152 | comment: comment.toFormattedJSON() |
bf1f6508 C |
153 | }).end() |
154 | } | |
155 | ||
90d4bb81 | 156 | async function addVideoCommentReply (req: express.Request, res: express.Response) { |
bf1f6508 C |
157 | const videoCommentInfo: VideoCommentCreate = req.body |
158 | ||
90d4bb81 | 159 | const comment = await sequelizeTypescript.transaction(async t => { |
dae86118 | 160 | const account = await AccountModel.load(res.locals.oauth.token.User.Account.id, t) |
91411dba | 161 | |
bf1f6508 C |
162 | return createVideoComment({ |
163 | text: videoCommentInfo.text, | |
453e83ea C |
164 | inReplyToComment: res.locals.videoCommentFull, |
165 | video: res.locals.videoAll, | |
91411dba | 166 | account |
bf1f6508 C |
167 | }, t) |
168 | }) | |
4cb6d457 | 169 | |
cef534ed | 170 | Notifier.Instance.notifyOnNewComment(comment) |
993cef4b | 171 | auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON())) |
80e36cd9 | 172 | |
b4055e1c C |
173 | Hooks.runAction('action:api.video-comment-reply.created', { comment }) |
174 | ||
91411dba | 175 | return res.json({ comment: comment.toFormattedJSON() }).end() |
4cb6d457 C |
176 | } |
177 | ||
178 | async function removeVideoComment (req: express.Request, res: express.Response) { | |
453e83ea | 179 | const videoCommentInstance = res.locals.videoCommentFull |
4cb6d457 C |
180 | |
181 | await sequelizeTypescript.transaction(async t => { | |
182 | await videoCommentInstance.destroy({ transaction: t }) | |
511765c9 C |
183 | |
184 | if (videoCommentInstance.isOwned() || videoCommentInstance.Video.isOwned()) { | |
185 | await sendDeleteVideoComment(videoCommentInstance, t) | |
186 | } | |
4cb6d457 C |
187 | }) |
188 | ||
b4055e1c | 189 | auditLogger.delete(getAuditIdFromRes(res), new CommentAuditView(videoCommentInstance.toFormattedJSON())) |
4cb6d457 | 190 | logger.info('Video comment %d deleted.', videoCommentInstance.id) |
90d4bb81 | 191 | |
b4055e1c C |
192 | Hooks.runAction('action:api.video-comment.deleted', { comment: videoCommentInstance }) |
193 | ||
90d4bb81 | 194 | return res.type('json').status(204).end() |
4cb6d457 | 195 | } |