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