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