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