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