]>
Commit | Line | Data |
---|---|---|
1 | import express from 'express' | |
2 | import { ResultList, ThreadsResultList, UserRight, VideoCommentCreate } from '../../../../shared/models' | |
3 | import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes' | |
4 | import { VideoCommentThreads } from '../../../../shared/models/videos/comment/video-comment.model' | |
5 | import { auditLoggerFactory, CommentAuditView, getAuditIdFromRes } from '../../../helpers/audit-logger' | |
6 | import { getFormattedObjects } from '../../../helpers/utils' | |
7 | import { sequelizeTypescript } from '../../../initializers/database' | |
8 | import { Notifier } from '../../../lib/notifier' | |
9 | import { Hooks } from '../../../lib/plugins/hooks' | |
10 | import { buildFormattedCommentTree, createVideoComment, removeComment } from '../../../lib/video-comment' | |
11 | import { | |
12 | asyncMiddleware, | |
13 | asyncRetryTransactionMiddleware, | |
14 | authenticate, | |
15 | ensureUserHasRight, | |
16 | optionalAuthenticate, | |
17 | paginationValidator, | |
18 | setDefaultPagination, | |
19 | setDefaultSort | |
20 | } from '../../../middlewares' | |
21 | import { | |
22 | addVideoCommentReplyValidator, | |
23 | addVideoCommentThreadValidator, | |
24 | listVideoCommentsValidator, | |
25 | listVideoCommentThreadsValidator, | |
26 | listVideoThreadCommentsValidator, | |
27 | removeVideoCommentValidator, | |
28 | videoCommentsValidator, | |
29 | videoCommentThreadsSortValidator | |
30 | } from '../../../middlewares/validators' | |
31 | import { AccountModel } from '../../../models/account/account' | |
32 | import { VideoCommentModel } from '../../../models/video/video-comment' | |
33 | ||
34 | const auditLogger = auditLoggerFactory('comments') | |
35 | const videoCommentRouter = express.Router() | |
36 | ||
37 | videoCommentRouter.get('/:videoId/comment-threads', | |
38 | paginationValidator, | |
39 | videoCommentThreadsSortValidator, | |
40 | setDefaultSort, | |
41 | setDefaultPagination, | |
42 | asyncMiddleware(listVideoCommentThreadsValidator), | |
43 | optionalAuthenticate, | |
44 | asyncMiddleware(listVideoThreads) | |
45 | ) | |
46 | videoCommentRouter.get('/:videoId/comment-threads/:threadId', | |
47 | asyncMiddleware(listVideoThreadCommentsValidator), | |
48 | optionalAuthenticate, | |
49 | asyncMiddleware(listVideoThreadComments) | |
50 | ) | |
51 | ||
52 | videoCommentRouter.post('/:videoId/comment-threads', | |
53 | authenticate, | |
54 | asyncMiddleware(addVideoCommentThreadValidator), | |
55 | asyncRetryTransactionMiddleware(addVideoCommentThread) | |
56 | ) | |
57 | videoCommentRouter.post('/:videoId/comments/:commentId', | |
58 | authenticate, | |
59 | asyncMiddleware(addVideoCommentReplyValidator), | |
60 | asyncRetryTransactionMiddleware(addVideoCommentReply) | |
61 | ) | |
62 | videoCommentRouter.delete('/:videoId/comments/:commentId', | |
63 | authenticate, | |
64 | asyncMiddleware(removeVideoCommentValidator), | |
65 | asyncRetryTransactionMiddleware(removeVideoComment) | |
66 | ) | |
67 | ||
68 | videoCommentRouter.get('/comments', | |
69 | authenticate, | |
70 | ensureUserHasRight(UserRight.SEE_ALL_COMMENTS), | |
71 | paginationValidator, | |
72 | videoCommentsValidator, | |
73 | setDefaultSort, | |
74 | setDefaultPagination, | |
75 | listVideoCommentsValidator, | |
76 | asyncMiddleware(listComments) | |
77 | ) | |
78 | ||
79 | // --------------------------------------------------------------------------- | |
80 | ||
81 | export { | |
82 | videoCommentRouter | |
83 | } | |
84 | ||
85 | // --------------------------------------------------------------------------- | |
86 | ||
87 | async function listComments (req: express.Request, res: express.Response) { | |
88 | const options = { | |
89 | start: req.query.start, | |
90 | count: req.query.count, | |
91 | sort: req.query.sort, | |
92 | ||
93 | isLocal: req.query.isLocal, | |
94 | search: req.query.search, | |
95 | searchAccount: req.query.searchAccount, | |
96 | searchVideo: req.query.searchVideo | |
97 | } | |
98 | ||
99 | const resultList = await VideoCommentModel.listCommentsForApi(options) | |
100 | ||
101 | return res.json({ | |
102 | total: resultList.total, | |
103 | data: resultList.data.map(c => c.toFormattedAdminJSON()) | |
104 | }) | |
105 | } | |
106 | ||
107 | async function listVideoThreads (req: express.Request, res: express.Response) { | |
108 | const video = res.locals.onlyVideo | |
109 | const user = res.locals.oauth ? res.locals.oauth.token.User : undefined | |
110 | ||
111 | let resultList: ThreadsResultList<VideoCommentModel> | |
112 | ||
113 | if (video.commentsEnabled === true) { | |
114 | const apiOptions = await Hooks.wrapObject({ | |
115 | videoId: video.id, | |
116 | isVideoOwned: video.isOwned(), | |
117 | start: req.query.start, | |
118 | count: req.query.count, | |
119 | sort: req.query.sort, | |
120 | user | |
121 | }, 'filter:api.video-threads.list.params') | |
122 | ||
123 | resultList = await Hooks.wrapPromiseFun( | |
124 | VideoCommentModel.listThreadsForApi, | |
125 | apiOptions, | |
126 | 'filter:api.video-threads.list.result' | |
127 | ) | |
128 | } else { | |
129 | resultList = { | |
130 | total: 0, | |
131 | totalNotDeletedComments: 0, | |
132 | data: [] | |
133 | } | |
134 | } | |
135 | ||
136 | return res.json({ | |
137 | ...getFormattedObjects(resultList.data, resultList.total), | |
138 | totalNotDeletedComments: resultList.totalNotDeletedComments | |
139 | } as VideoCommentThreads) | |
140 | } | |
141 | ||
142 | async function listVideoThreadComments (req: express.Request, res: express.Response) { | |
143 | const video = res.locals.onlyVideo | |
144 | const user = res.locals.oauth ? res.locals.oauth.token.User : undefined | |
145 | ||
146 | let resultList: ResultList<VideoCommentModel> | |
147 | ||
148 | if (video.commentsEnabled === true) { | |
149 | const apiOptions = await Hooks.wrapObject({ | |
150 | videoId: video.id, | |
151 | isVideoOwned: video.isOwned(), | |
152 | threadId: res.locals.videoCommentThread.id, | |
153 | user | |
154 | }, 'filter:api.video-thread-comments.list.params') | |
155 | ||
156 | resultList = await Hooks.wrapPromiseFun( | |
157 | VideoCommentModel.listThreadCommentsForApi, | |
158 | apiOptions, | |
159 | 'filter:api.video-thread-comments.list.result' | |
160 | ) | |
161 | } else { | |
162 | resultList = { | |
163 | total: 0, | |
164 | data: [] | |
165 | } | |
166 | } | |
167 | ||
168 | if (resultList.data.length === 0) { | |
169 | return res.fail({ | |
170 | status: HttpStatusCode.NOT_FOUND_404, | |
171 | message: 'No comments were found' | |
172 | }) | |
173 | } | |
174 | ||
175 | return res.json(buildFormattedCommentTree(resultList)) | |
176 | } | |
177 | ||
178 | async function addVideoCommentThread (req: express.Request, res: express.Response) { | |
179 | const videoCommentInfo: VideoCommentCreate = req.body | |
180 | ||
181 | const comment = await sequelizeTypescript.transaction(async t => { | |
182 | const account = await AccountModel.load(res.locals.oauth.token.User.Account.id, t) | |
183 | ||
184 | return createVideoComment({ | |
185 | text: videoCommentInfo.text, | |
186 | inReplyToComment: null, | |
187 | video: res.locals.videoAll, | |
188 | account | |
189 | }, t) | |
190 | }) | |
191 | ||
192 | Notifier.Instance.notifyOnNewComment(comment) | |
193 | auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON())) | |
194 | ||
195 | Hooks.runAction('action:api.video-thread.created', { comment, req, res }) | |
196 | ||
197 | return res.json({ comment: comment.toFormattedJSON() }) | |
198 | } | |
199 | ||
200 | async function addVideoCommentReply (req: express.Request, res: express.Response) { | |
201 | const videoCommentInfo: VideoCommentCreate = req.body | |
202 | ||
203 | const comment = await sequelizeTypescript.transaction(async t => { | |
204 | const account = await AccountModel.load(res.locals.oauth.token.User.Account.id, t) | |
205 | ||
206 | return createVideoComment({ | |
207 | text: videoCommentInfo.text, | |
208 | inReplyToComment: res.locals.videoCommentFull, | |
209 | video: res.locals.videoAll, | |
210 | account | |
211 | }, t) | |
212 | }) | |
213 | ||
214 | Notifier.Instance.notifyOnNewComment(comment) | |
215 | auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON())) | |
216 | ||
217 | Hooks.runAction('action:api.video-comment-reply.created', { comment, req, res }) | |
218 | ||
219 | return res.json({ comment: comment.toFormattedJSON() }) | |
220 | } | |
221 | ||
222 | async function removeVideoComment (req: express.Request, res: express.Response) { | |
223 | const videoCommentInstance = res.locals.videoCommentFull | |
224 | ||
225 | await removeComment(videoCommentInstance, req, res) | |
226 | ||
227 | auditLogger.delete(getAuditIdFromRes(res), new CommentAuditView(videoCommentInstance.toFormattedJSON())) | |
228 | ||
229 | return res.type('json') | |
230 | .status(HttpStatusCode.NO_CONTENT_204) | |
231 | .end() | |
232 | } |