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