]>
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 | ||
27 | const videoCommentRouter = express.Router() | |
28 | ||
29 | videoCommentRouter.get('/:videoId/comment-threads', | |
30 | paginationValidator, | |
31 | videoCommentThreadsSortValidator, | |
32 | setDefaultSort, | |
33 | setDefaultPagination, | |
34 | asyncMiddleware(listVideoCommentThreadsValidator), | |
35 | asyncMiddleware(listVideoThreads) | |
36 | ) | |
37 | videoCommentRouter.get('/:videoId/comment-threads/:threadId', | |
38 | asyncMiddleware(listVideoThreadCommentsValidator), | |
39 | asyncMiddleware(listVideoThreadComments) | |
40 | ) | |
41 | ||
42 | videoCommentRouter.post('/:videoId/comment-threads', | |
43 | authenticate, | |
44 | asyncMiddleware(addVideoCommentThreadValidator), | |
45 | asyncRetryTransactionMiddleware(addVideoCommentThread) | |
46 | ) | |
47 | videoCommentRouter.post('/:videoId/comments/:commentId', | |
48 | authenticate, | |
49 | asyncMiddleware(addVideoCommentReplyValidator), | |
50 | asyncRetryTransactionMiddleware(addVideoCommentReply) | |
51 | ) | |
52 | videoCommentRouter.delete('/:videoId/comments/:commentId', | |
53 | authenticate, | |
54 | asyncMiddleware(removeVideoCommentValidator), | |
55 | asyncRetryTransactionMiddleware(removeVideoComment) | |
56 | ) | |
57 | ||
58 | // --------------------------------------------------------------------------- | |
59 | ||
60 | export { | |
61 | videoCommentRouter | |
62 | } | |
63 | ||
64 | // --------------------------------------------------------------------------- | |
65 | ||
66 | async function listVideoThreads (req: express.Request, res: express.Response, next: express.NextFunction) { | |
67 | const video = res.locals.video as VideoModel | |
68 | let resultList: ResultList<VideoCommentModel> | |
69 | ||
70 | if (video.commentsEnabled === true) { | |
71 | resultList = await VideoCommentModel.listThreadsForApi(video.id, req.query.start, req.query.count, req.query.sort) | |
72 | } else { | |
73 | resultList = { | |
74 | total: 0, | |
75 | data: [] | |
76 | } | |
77 | } | |
78 | ||
79 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
80 | } | |
81 | ||
82 | async function listVideoThreadComments (req: express.Request, res: express.Response, next: express.NextFunction) { | |
83 | const video = res.locals.video as VideoModel | |
84 | let resultList: ResultList<VideoCommentModel> | |
85 | ||
86 | if (video.commentsEnabled === true) { | |
87 | resultList = await VideoCommentModel.listThreadCommentsForApi(res.locals.video.id, res.locals.videoCommentThread.id) | |
88 | } else { | |
89 | resultList = { | |
90 | total: 0, | |
91 | data: [] | |
92 | } | |
93 | } | |
94 | ||
95 | return res.json(buildFormattedCommentTree(resultList)) | |
96 | } | |
97 | ||
98 | async function addVideoCommentThread (req: express.Request, res: express.Response) { | |
99 | const videoCommentInfo: VideoCommentCreate = req.body | |
100 | ||
101 | const comment = await sequelizeTypescript.transaction(async t => { | |
102 | return createVideoComment({ | |
103 | text: videoCommentInfo.text, | |
104 | inReplyToComment: null, | |
105 | video: res.locals.video, | |
106 | account: res.locals.oauth.token.User.Account | |
107 | }, t) | |
108 | }) | |
109 | ||
110 | return res.json({ | |
111 | comment: comment.toFormattedJSON() | |
112 | }).end() | |
113 | } | |
114 | ||
115 | async function addVideoCommentReply (req: express.Request, res: express.Response) { | |
116 | const videoCommentInfo: VideoCommentCreate = req.body | |
117 | ||
118 | const comment = await sequelizeTypescript.transaction(async t => { | |
119 | return createVideoComment({ | |
120 | text: videoCommentInfo.text, | |
121 | inReplyToComment: res.locals.videoComment, | |
122 | video: res.locals.video, | |
123 | account: res.locals.oauth.token.User.Account | |
124 | }, t) | |
125 | }) | |
126 | ||
127 | return res.json({ | |
128 | comment: comment.toFormattedJSON() | |
129 | }).end() | |
130 | } | |
131 | ||
132 | async function removeVideoComment (req: express.Request, res: express.Response) { | |
133 | const videoCommentInstance: VideoCommentModel = res.locals.videoComment | |
134 | ||
135 | await sequelizeTypescript.transaction(async t => { | |
136 | await videoCommentInstance.destroy({ transaction: t }) | |
137 | }) | |
138 | ||
139 | logger.info('Video comment %d deleted.', videoCommentInstance.id) | |
140 | ||
141 | return res.type('json').status(204).end() | |
142 | } |