]>
Commit | Line | Data |
---|---|---|
bf1f6508 | 1 | import * as express from 'express' |
0f8d00e3 | 2 | import { ResultList, UserRight } from '../../../../shared/models' |
bf1f6508 | 3 | import { VideoCommentCreate } from '../../../../shared/models/videos/video-comment.model' |
444c0a0e | 4 | import { auditLoggerFactory, CommentAuditView, getAuditIdFromRes } from '../../../helpers/audit-logger' |
da854ddd | 5 | import { getFormattedObjects } from '../../../helpers/utils' |
80fdaf06 | 6 | import { sequelizeTypescript } from '../../../initializers/database' |
444c0a0e C |
7 | import { Notifier } from '../../../lib/notifier' |
8 | import { Hooks } from '../../../lib/plugins/hooks' | |
9 | import { buildFormattedCommentTree, createVideoComment, removeComment } from '../../../lib/video-comment' | |
90d4bb81 C |
10 | import { |
11 | asyncMiddleware, | |
12 | asyncRetryTransactionMiddleware, | |
dae86118 | 13 | authenticate, |
0f8d00e3 | 14 | ensureUserHasRight, |
dae86118 | 15 | optionalAuthenticate, |
90d4bb81 C |
16 | paginationValidator, |
17 | setDefaultPagination, | |
18 | setDefaultSort | |
19 | } from '../../../middlewares' | |
bf1f6508 | 20 | import { |
90d4bb81 C |
21 | addVideoCommentReplyValidator, |
22 | addVideoCommentThreadValidator, | |
0f8d00e3 | 23 | listVideoCommentsValidator, |
90d4bb81 C |
24 | listVideoCommentThreadsValidator, |
25 | listVideoThreadCommentsValidator, | |
6e46de09 | 26 | removeVideoCommentValidator, |
0f8d00e3 | 27 | videoCommentsValidator, |
6e46de09 C |
28 | videoCommentThreadsSortValidator |
29 | } from '../../../middlewares/validators' | |
91411dba | 30 | import { AccountModel } from '../../../models/account/account' |
444c0a0e | 31 | import { VideoCommentModel } from '../../../models/video/video-comment' |
2d53be02 | 32 | import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes' |
bf1f6508 | 33 | |
80e36cd9 | 34 | const auditLogger = auditLoggerFactory('comments') |
bf1f6508 C |
35 | const videoCommentRouter = express.Router() |
36 | ||
37 | videoCommentRouter.get('/:videoId/comment-threads', | |
38 | paginationValidator, | |
39 | videoCommentThreadsSortValidator, | |
1174a847 | 40 | setDefaultSort, |
f05a1c30 | 41 | setDefaultPagination, |
bf1f6508 | 42 | asyncMiddleware(listVideoCommentThreadsValidator), |
7ad9b984 | 43 | optionalAuthenticate, |
bf1f6508 C |
44 | asyncMiddleware(listVideoThreads) |
45 | ) | |
46 | videoCommentRouter.get('/:videoId/comment-threads/:threadId', | |
47 | asyncMiddleware(listVideoThreadCommentsValidator), | |
7ad9b984 | 48 | optionalAuthenticate, |
bf1f6508 C |
49 | asyncMiddleware(listVideoThreadComments) |
50 | ) | |
51 | ||
52 | videoCommentRouter.post('/:videoId/comment-threads', | |
53 | authenticate, | |
54 | asyncMiddleware(addVideoCommentThreadValidator), | |
90d4bb81 | 55 | asyncRetryTransactionMiddleware(addVideoCommentThread) |
bf1f6508 C |
56 | ) |
57 | videoCommentRouter.post('/:videoId/comments/:commentId', | |
58 | authenticate, | |
59 | asyncMiddleware(addVideoCommentReplyValidator), | |
90d4bb81 | 60 | asyncRetryTransactionMiddleware(addVideoCommentReply) |
bf1f6508 | 61 | ) |
4cb6d457 C |
62 | videoCommentRouter.delete('/:videoId/comments/:commentId', |
63 | authenticate, | |
64 | asyncMiddleware(removeVideoCommentValidator), | |
90d4bb81 | 65 | asyncRetryTransactionMiddleware(removeVideoComment) |
4cb6d457 | 66 | ) |
bf1f6508 | 67 | |
0f8d00e3 C |
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 | ||
bf1f6508 C |
79 | // --------------------------------------------------------------------------- |
80 | ||
81 | export { | |
82 | videoCommentRouter | |
83 | } | |
84 | ||
85 | // --------------------------------------------------------------------------- | |
86 | ||
0f8d00e3 C |
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 | ||
dae86118 | 107 | async function listVideoThreads (req: express.Request, res: express.Response) { |
453e83ea | 108 | const video = res.locals.onlyVideo |
dae86118 | 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, | |
696d83fd | 116 | isVideoOwned: video.isOwned(), |
b4055e1c C |
117 | start: req.query.start, |
118 | count: req.query.count, | |
119 | sort: req.query.sort, | |
453e83ea | 120 | user |
b4055e1c C |
121 | }, 'filter:api.video-threads.list.params') |
122 | ||
89cd1275 C |
123 | resultList = await Hooks.wrapPromiseFun( |
124 | VideoCommentModel.listThreadsForApi, | |
125 | apiOptions, | |
b4055e1c C |
126 | 'filter:api.video-threads.list.result' |
127 | ) | |
47564bbe C |
128 | } else { |
129 | resultList = { | |
130 | total: 0, | |
131 | data: [] | |
132 | } | |
133 | } | |
bf1f6508 C |
134 | |
135 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
136 | } | |
137 | ||
dae86118 | 138 | async function listVideoThreadComments (req: express.Request, res: express.Response) { |
453e83ea | 139 | const video = res.locals.onlyVideo |
dae86118 | 140 | const user = res.locals.oauth ? res.locals.oauth.token.User : undefined |
7ad9b984 | 141 | |
47564bbe C |
142 | let resultList: ResultList<VideoCommentModel> |
143 | ||
144 | if (video.commentsEnabled === true) { | |
b4055e1c C |
145 | const apiOptions = await Hooks.wrapObject({ |
146 | videoId: video.id, | |
696d83fd | 147 | isVideoOwned: video.isOwned(), |
b4055e1c | 148 | threadId: res.locals.videoCommentThread.id, |
6691c522 | 149 | user |
b4055e1c C |
150 | }, 'filter:api.video-thread-comments.list.params') |
151 | ||
89cd1275 C |
152 | resultList = await Hooks.wrapPromiseFun( |
153 | VideoCommentModel.listThreadCommentsForApi, | |
154 | apiOptions, | |
b4055e1c C |
155 | 'filter:api.video-thread-comments.list.result' |
156 | ) | |
47564bbe C |
157 | } else { |
158 | resultList = { | |
159 | total: 0, | |
160 | data: [] | |
161 | } | |
162 | } | |
bf1f6508 | 163 | |
9b337d8c | 164 | if (resultList.data.length === 0) { |
2d53be02 | 165 | return res.sendStatus(HttpStatusCode.NOT_FOUND_404) |
9b337d8c C |
166 | } |
167 | ||
bf1f6508 C |
168 | return res.json(buildFormattedCommentTree(resultList)) |
169 | } | |
170 | ||
90d4bb81 | 171 | async function addVideoCommentThread (req: express.Request, res: express.Response) { |
bf1f6508 C |
172 | const videoCommentInfo: VideoCommentCreate = req.body |
173 | ||
90d4bb81 | 174 | const comment = await sequelizeTypescript.transaction(async t => { |
dae86118 | 175 | const account = await AccountModel.load(res.locals.oauth.token.User.Account.id, t) |
91411dba | 176 | |
bf1f6508 C |
177 | return createVideoComment({ |
178 | text: videoCommentInfo.text, | |
ea44f375 | 179 | inReplyToComment: null, |
453e83ea | 180 | video: res.locals.videoAll, |
91411dba | 181 | account |
bf1f6508 C |
182 | }, t) |
183 | }) | |
bf1f6508 | 184 | |
cef534ed | 185 | Notifier.Instance.notifyOnNewComment(comment) |
993cef4b | 186 | auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON())) |
80e36cd9 | 187 | |
b4055e1c C |
188 | Hooks.runAction('action:api.video-thread.created', { comment }) |
189 | ||
444c0a0e | 190 | return res.json({ comment: comment.toFormattedJSON() }) |
bf1f6508 C |
191 | } |
192 | ||
90d4bb81 | 193 | async function addVideoCommentReply (req: express.Request, res: express.Response) { |
bf1f6508 C |
194 | const videoCommentInfo: VideoCommentCreate = req.body |
195 | ||
90d4bb81 | 196 | const comment = await sequelizeTypescript.transaction(async t => { |
dae86118 | 197 | const account = await AccountModel.load(res.locals.oauth.token.User.Account.id, t) |
91411dba | 198 | |
bf1f6508 C |
199 | return createVideoComment({ |
200 | text: videoCommentInfo.text, | |
453e83ea C |
201 | inReplyToComment: res.locals.videoCommentFull, |
202 | video: res.locals.videoAll, | |
91411dba | 203 | account |
bf1f6508 C |
204 | }, t) |
205 | }) | |
4cb6d457 | 206 | |
cef534ed | 207 | Notifier.Instance.notifyOnNewComment(comment) |
993cef4b | 208 | auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON())) |
80e36cd9 | 209 | |
b4055e1c C |
210 | Hooks.runAction('action:api.video-comment-reply.created', { comment }) |
211 | ||
444c0a0e | 212 | return res.json({ comment: comment.toFormattedJSON() }) |
4cb6d457 C |
213 | } |
214 | ||
215 | async function removeVideoComment (req: express.Request, res: express.Response) { | |
453e83ea | 216 | const videoCommentInstance = res.locals.videoCommentFull |
69222afa | 217 | |
444c0a0e | 218 | await removeComment(videoCommentInstance) |
4cb6d457 | 219 | |
b4055e1c | 220 | auditLogger.delete(getAuditIdFromRes(res), new CommentAuditView(videoCommentInstance.toFormattedJSON())) |
b4055e1c | 221 | |
2d53be02 RK |
222 | return res.type('json') |
223 | .status(HttpStatusCode.NO_CONTENT_204) | |
224 | .end() | |
4cb6d457 | 225 | } |