]>
Commit | Line | Data |
---|---|---|
1 | import * as express from 'express' | |
2 | import { ResultList } from '../../../../shared/models' | |
3 | import { VideoCommentCreate } from '../../../../shared/models/videos/video-comment.model' | |
4 | import { logger } from '../../../helpers/logger' | |
5 | import { getFormattedObjects } from '../../../helpers/utils' | |
6 | import { sequelizeTypescript } from '../../../initializers' | |
7 | import { buildFormattedCommentTree, createVideoComment } from '../../../lib/video-comment' | |
8 | import { | |
9 | asyncMiddleware, | |
10 | asyncRetryTransactionMiddleware, | |
11 | authenticate, | |
12 | paginationValidator, | |
13 | setDefaultPagination, | |
14 | setDefaultSort | |
15 | } from '../../../middlewares' | |
16 | import { videoCommentThreadsSortValidator } from '../../../middlewares/validators' | |
17 | import { | |
18 | addVideoCommentReplyValidator, | |
19 | addVideoCommentThreadValidator, | |
20 | listVideoCommentThreadsValidator, | |
21 | listVideoThreadCommentsValidator, | |
22 | removeVideoCommentValidator | |
23 | } from '../../../middlewares/validators/video-comments' | |
24 | import { VideoModel } from '../../../models/video/video' | |
25 | import { VideoCommentModel } from '../../../models/video/video-comment' | |
26 | import { auditLoggerFactory, CommentAuditView } from '../../../helpers/audit-logger' | |
27 | ||
28 | const auditLogger = auditLoggerFactory('comments') | |
29 | const videoCommentRouter = express.Router() | |
30 | ||
31 | videoCommentRouter.get('/:videoId/comment-threads', | |
32 | paginationValidator, | |
33 | videoCommentThreadsSortValidator, | |
34 | setDefaultSort, | |
35 | setDefaultPagination, | |
36 | asyncMiddleware(listVideoCommentThreadsValidator), | |
37 | asyncMiddleware(listVideoThreads) | |
38 | ) | |
39 | videoCommentRouter.get('/:videoId/comment-threads/:threadId', | |
40 | asyncMiddleware(listVideoThreadCommentsValidator), | |
41 | asyncMiddleware(listVideoThreadComments) | |
42 | ) | |
43 | ||
44 | videoCommentRouter.post('/:videoId/comment-threads', | |
45 | authenticate, | |
46 | asyncMiddleware(addVideoCommentThreadValidator), | |
47 | asyncRetryTransactionMiddleware(addVideoCommentThread) | |
48 | ) | |
49 | videoCommentRouter.post('/:videoId/comments/:commentId', | |
50 | authenticate, | |
51 | asyncMiddleware(addVideoCommentReplyValidator), | |
52 | asyncRetryTransactionMiddleware(addVideoCommentReply) | |
53 | ) | |
54 | videoCommentRouter.delete('/:videoId/comments/:commentId', | |
55 | authenticate, | |
56 | asyncMiddleware(removeVideoCommentValidator), | |
57 | asyncRetryTransactionMiddleware(removeVideoComment) | |
58 | ) | |
59 | ||
60 | // --------------------------------------------------------------------------- | |
61 | ||
62 | export { | |
63 | videoCommentRouter | |
64 | } | |
65 | ||
66 | // --------------------------------------------------------------------------- | |
67 | ||
68 | async function listVideoThreads (req: express.Request, res: express.Response, next: express.NextFunction) { | |
69 | const video = res.locals.video as VideoModel | |
70 | let resultList: ResultList<VideoCommentModel> | |
71 | ||
72 | if (video.commentsEnabled === true) { | |
73 | resultList = await VideoCommentModel.listThreadsForApi(video.id, req.query.start, req.query.count, req.query.sort) | |
74 | } else { | |
75 | resultList = { | |
76 | total: 0, | |
77 | data: [] | |
78 | } | |
79 | } | |
80 | ||
81 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
82 | } | |
83 | ||
84 | async function listVideoThreadComments (req: express.Request, res: express.Response, next: express.NextFunction) { | |
85 | const video = res.locals.video as VideoModel | |
86 | let resultList: ResultList<VideoCommentModel> | |
87 | ||
88 | if (video.commentsEnabled === true) { | |
89 | resultList = await VideoCommentModel.listThreadCommentsForApi(video.id, res.locals.videoCommentThread.id) | |
90 | } else { | |
91 | resultList = { | |
92 | total: 0, | |
93 | data: [] | |
94 | } | |
95 | } | |
96 | ||
97 | return res.json(buildFormattedCommentTree(resultList)) | |
98 | } | |
99 | ||
100 | async function addVideoCommentThread (req: express.Request, res: express.Response) { | |
101 | const videoCommentInfo: VideoCommentCreate = req.body | |
102 | ||
103 | const comment = await sequelizeTypescript.transaction(async t => { | |
104 | return createVideoComment({ | |
105 | text: videoCommentInfo.text, | |
106 | inReplyToComment: null, | |
107 | video: res.locals.video, | |
108 | account: res.locals.oauth.token.User.Account | |
109 | }, t) | |
110 | }) | |
111 | ||
112 | auditLogger.create(res.locals.oauth.token.User.Account.Actor.getIdentifier(), new CommentAuditView(comment.toFormattedJSON())) | |
113 | ||
114 | return res.json({ | |
115 | comment: comment.toFormattedJSON() | |
116 | }).end() | |
117 | } | |
118 | ||
119 | async function addVideoCommentReply (req: express.Request, res: express.Response) { | |
120 | const videoCommentInfo: VideoCommentCreate = req.body | |
121 | ||
122 | const comment = await sequelizeTypescript.transaction(async t => { | |
123 | return createVideoComment({ | |
124 | text: videoCommentInfo.text, | |
125 | inReplyToComment: res.locals.videoComment, | |
126 | video: res.locals.video, | |
127 | account: res.locals.oauth.token.User.Account | |
128 | }, t) | |
129 | }) | |
130 | ||
131 | auditLogger.create(res.locals.oauth.token.User.Account.Actor.getIdentifier(), new CommentAuditView(comment.toFormattedJSON())) | |
132 | ||
133 | return res.json({ | |
134 | comment: comment.toFormattedJSON() | |
135 | }).end() | |
136 | } | |
137 | ||
138 | async function removeVideoComment (req: express.Request, res: express.Response) { | |
139 | const videoCommentInstance: VideoCommentModel = res.locals.videoComment | |
140 | ||
141 | await sequelizeTypescript.transaction(async t => { | |
142 | await videoCommentInstance.destroy({ transaction: t }) | |
143 | }) | |
144 | ||
145 | auditLogger.delete( | |
146 | res.locals.oauth.token.User.Account.Actor.getIdentifier(), | |
147 | new CommentAuditView(videoCommentInstance.toFormattedJSON()) | |
148 | ) | |
149 | logger.info('Video comment %d deleted.', videoCommentInstance.id) | |
150 | ||
151 | return res.type('json').status(204).end() | |
152 | } |