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