]>
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' |
bf1f6508 | 32 | |
80e36cd9 | 33 | const auditLogger = auditLoggerFactory('comments') |
bf1f6508 C |
34 | const videoCommentRouter = express.Router() |
35 | ||
36 | videoCommentRouter.get('/:videoId/comment-threads', | |
37 | paginationValidator, | |
38 | videoCommentThreadsSortValidator, | |
1174a847 | 39 | setDefaultSort, |
f05a1c30 | 40 | setDefaultPagination, |
bf1f6508 | 41 | asyncMiddleware(listVideoCommentThreadsValidator), |
7ad9b984 | 42 | optionalAuthenticate, |
bf1f6508 C |
43 | asyncMiddleware(listVideoThreads) |
44 | ) | |
45 | videoCommentRouter.get('/:videoId/comment-threads/:threadId', | |
46 | asyncMiddleware(listVideoThreadCommentsValidator), | |
7ad9b984 | 47 | optionalAuthenticate, |
bf1f6508 C |
48 | asyncMiddleware(listVideoThreadComments) |
49 | ) | |
50 | ||
51 | videoCommentRouter.post('/:videoId/comment-threads', | |
52 | authenticate, | |
53 | asyncMiddleware(addVideoCommentThreadValidator), | |
90d4bb81 | 54 | asyncRetryTransactionMiddleware(addVideoCommentThread) |
bf1f6508 C |
55 | ) |
56 | videoCommentRouter.post('/:videoId/comments/:commentId', | |
57 | authenticate, | |
58 | asyncMiddleware(addVideoCommentReplyValidator), | |
90d4bb81 | 59 | asyncRetryTransactionMiddleware(addVideoCommentReply) |
bf1f6508 | 60 | ) |
4cb6d457 C |
61 | videoCommentRouter.delete('/:videoId/comments/:commentId', |
62 | authenticate, | |
63 | asyncMiddleware(removeVideoCommentValidator), | |
90d4bb81 | 64 | asyncRetryTransactionMiddleware(removeVideoComment) |
4cb6d457 | 65 | ) |
bf1f6508 | 66 | |
0f8d00e3 C |
67 | videoCommentRouter.get('/comments', |
68 | authenticate, | |
69 | ensureUserHasRight(UserRight.SEE_ALL_COMMENTS), | |
70 | paginationValidator, | |
71 | videoCommentsValidator, | |
72 | setDefaultSort, | |
73 | setDefaultPagination, | |
74 | listVideoCommentsValidator, | |
75 | asyncMiddleware(listComments) | |
76 | ) | |
77 | ||
bf1f6508 C |
78 | // --------------------------------------------------------------------------- |
79 | ||
80 | export { | |
81 | videoCommentRouter | |
82 | } | |
83 | ||
84 | // --------------------------------------------------------------------------- | |
85 | ||
0f8d00e3 C |
86 | async function listComments (req: express.Request, res: express.Response) { |
87 | const options = { | |
88 | start: req.query.start, | |
89 | count: req.query.count, | |
90 | sort: req.query.sort, | |
91 | ||
92 | isLocal: req.query.isLocal, | |
93 | search: req.query.search, | |
94 | searchAccount: req.query.searchAccount, | |
95 | searchVideo: req.query.searchVideo | |
96 | } | |
97 | ||
98 | const resultList = await VideoCommentModel.listCommentsForApi(options) | |
99 | ||
100 | return res.json({ | |
101 | total: resultList.total, | |
102 | data: resultList.data.map(c => c.toFormattedAdminJSON()) | |
103 | }) | |
104 | } | |
105 | ||
dae86118 | 106 | async function listVideoThreads (req: express.Request, res: express.Response) { |
453e83ea | 107 | const video = res.locals.onlyVideo |
dae86118 | 108 | const user = res.locals.oauth ? res.locals.oauth.token.User : undefined |
7ad9b984 | 109 | |
47564bbe C |
110 | let resultList: ResultList<VideoCommentModel> |
111 | ||
112 | if (video.commentsEnabled === true) { | |
b4055e1c C |
113 | const apiOptions = await Hooks.wrapObject({ |
114 | videoId: video.id, | |
696d83fd | 115 | isVideoOwned: video.isOwned(), |
b4055e1c C |
116 | start: req.query.start, |
117 | count: req.query.count, | |
118 | sort: req.query.sort, | |
453e83ea | 119 | user |
b4055e1c C |
120 | }, 'filter:api.video-threads.list.params') |
121 | ||
89cd1275 C |
122 | resultList = await Hooks.wrapPromiseFun( |
123 | VideoCommentModel.listThreadsForApi, | |
124 | apiOptions, | |
b4055e1c C |
125 | 'filter:api.video-threads.list.result' |
126 | ) | |
47564bbe C |
127 | } else { |
128 | resultList = { | |
129 | total: 0, | |
130 | data: [] | |
131 | } | |
132 | } | |
bf1f6508 C |
133 | |
134 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
135 | } | |
136 | ||
dae86118 | 137 | async function listVideoThreadComments (req: express.Request, res: express.Response) { |
453e83ea | 138 | const video = res.locals.onlyVideo |
dae86118 | 139 | const user = res.locals.oauth ? res.locals.oauth.token.User : undefined |
7ad9b984 | 140 | |
47564bbe C |
141 | let resultList: ResultList<VideoCommentModel> |
142 | ||
143 | if (video.commentsEnabled === true) { | |
b4055e1c C |
144 | const apiOptions = await Hooks.wrapObject({ |
145 | videoId: video.id, | |
696d83fd | 146 | isVideoOwned: video.isOwned(), |
b4055e1c | 147 | threadId: res.locals.videoCommentThread.id, |
6691c522 | 148 | user |
b4055e1c C |
149 | }, 'filter:api.video-thread-comments.list.params') |
150 | ||
89cd1275 C |
151 | resultList = await Hooks.wrapPromiseFun( |
152 | VideoCommentModel.listThreadCommentsForApi, | |
153 | apiOptions, | |
b4055e1c C |
154 | 'filter:api.video-thread-comments.list.result' |
155 | ) | |
47564bbe C |
156 | } else { |
157 | resultList = { | |
158 | total: 0, | |
159 | data: [] | |
160 | } | |
161 | } | |
bf1f6508 | 162 | |
9b337d8c C |
163 | if (resultList.data.length === 0) { |
164 | return res.sendStatus(404) | |
165 | } | |
166 | ||
bf1f6508 C |
167 | return res.json(buildFormattedCommentTree(resultList)) |
168 | } | |
169 | ||
90d4bb81 | 170 | async function addVideoCommentThread (req: express.Request, res: express.Response) { |
bf1f6508 C |
171 | const videoCommentInfo: VideoCommentCreate = req.body |
172 | ||
90d4bb81 | 173 | const comment = await sequelizeTypescript.transaction(async t => { |
dae86118 | 174 | const account = await AccountModel.load(res.locals.oauth.token.User.Account.id, t) |
91411dba | 175 | |
bf1f6508 C |
176 | return createVideoComment({ |
177 | text: videoCommentInfo.text, | |
ea44f375 | 178 | inReplyToComment: null, |
453e83ea | 179 | video: res.locals.videoAll, |
91411dba | 180 | account |
bf1f6508 C |
181 | }, t) |
182 | }) | |
bf1f6508 | 183 | |
cef534ed | 184 | Notifier.Instance.notifyOnNewComment(comment) |
993cef4b | 185 | auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON())) |
80e36cd9 | 186 | |
b4055e1c C |
187 | Hooks.runAction('action:api.video-thread.created', { comment }) |
188 | ||
444c0a0e | 189 | return res.json({ comment: comment.toFormattedJSON() }) |
bf1f6508 C |
190 | } |
191 | ||
90d4bb81 | 192 | async function addVideoCommentReply (req: express.Request, res: express.Response) { |
bf1f6508 C |
193 | const videoCommentInfo: VideoCommentCreate = req.body |
194 | ||
90d4bb81 | 195 | const comment = await sequelizeTypescript.transaction(async t => { |
dae86118 | 196 | const account = await AccountModel.load(res.locals.oauth.token.User.Account.id, t) |
91411dba | 197 | |
bf1f6508 C |
198 | return createVideoComment({ |
199 | text: videoCommentInfo.text, | |
453e83ea C |
200 | inReplyToComment: res.locals.videoCommentFull, |
201 | video: res.locals.videoAll, | |
91411dba | 202 | account |
bf1f6508 C |
203 | }, t) |
204 | }) | |
4cb6d457 | 205 | |
cef534ed | 206 | Notifier.Instance.notifyOnNewComment(comment) |
993cef4b | 207 | auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON())) |
80e36cd9 | 208 | |
b4055e1c C |
209 | Hooks.runAction('action:api.video-comment-reply.created', { comment }) |
210 | ||
444c0a0e | 211 | return res.json({ comment: comment.toFormattedJSON() }) |
4cb6d457 C |
212 | } |
213 | ||
214 | async function removeVideoComment (req: express.Request, res: express.Response) { | |
453e83ea | 215 | const videoCommentInstance = res.locals.videoCommentFull |
69222afa | 216 | |
444c0a0e | 217 | await removeComment(videoCommentInstance) |
4cb6d457 | 218 | |
b4055e1c | 219 | auditLogger.delete(getAuditIdFromRes(res), new CommentAuditView(videoCommentInstance.toFormattedJSON())) |
b4055e1c | 220 | |
8adf0a76 | 221 | return res.type('json').status(204).end() |
4cb6d457 | 222 | } |