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