diff options
author | Chocobozzz <me@florianbigard.com> | 2017-12-22 10:50:07 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2017-12-22 11:29:12 +0100 |
commit | bf1f650817dadfd5eeee9e5e0b6b6938c136e25d (patch) | |
tree | 0f1dc95d87089be177ebe60740a55dd0c96b2414 /server/controllers/api/videos/comment.ts | |
parent | 6d8524702874120a4667269a81a61e3c7c5e300d (diff) | |
download | PeerTube-bf1f650817dadfd5eeee9e5e0b6b6938c136e25d.tar.gz PeerTube-bf1f650817dadfd5eeee9e5e0b6b6938c136e25d.tar.zst PeerTube-bf1f650817dadfd5eeee9e5e0b6b6938c136e25d.zip |
Add comments controller
Diffstat (limited to 'server/controllers/api/videos/comment.ts')
-rw-r--r-- | server/controllers/api/videos/comment.ts | 202 |
1 files changed, 114 insertions, 88 deletions
diff --git a/server/controllers/api/videos/comment.ts b/server/controllers/api/videos/comment.ts index b69aa5d40..81c9e7d16 100644 --- a/server/controllers/api/videos/comment.ts +++ b/server/controllers/api/videos/comment.ts | |||
@@ -1,88 +1,114 @@ | |||
1 | // import * as express from 'express' | 1 | import * as express from 'express' |
2 | // import { logger, getFormattedObjects } from '../../../helpers' | 2 | import { VideoCommentCreate } from '../../../../shared/models/videos/video-comment.model' |
3 | // import { | 3 | import { getFormattedObjects, retryTransactionWrapper } from '../../../helpers' |
4 | // authenticate, | 4 | import { sequelizeTypescript } from '../../../initializers' |
5 | // ensureUserHasRight, | 5 | import { buildFormattedCommentTree, createVideoComment } from '../../../lib/video-comment' |
6 | // videosBlacklistAddValidator, | 6 | import { asyncMiddleware, authenticate, paginationValidator, setPagination, setVideoCommentThreadsSort } from '../../../middlewares' |
7 | // videosBlacklistRemoveValidator, | 7 | import { videoCommentThreadsSortValidator } from '../../../middlewares/validators' |
8 | // paginationValidator, | 8 | import { |
9 | // blacklistSortValidator, | 9 | addVideoCommentReplyValidator, addVideoCommentThreadValidator, listVideoCommentThreadsValidator, |
10 | // setBlacklistSort, | 10 | listVideoThreadCommentsValidator |
11 | // setPagination, | 11 | } from '../../../middlewares/validators/video-comments' |
12 | // asyncMiddleware | 12 | import { VideoCommentModel } from '../../../models/video/video-comment' |
13 | // } from '../../../middlewares' | 13 | |
14 | // import { BlacklistedVideo, UserRight } from '../../../../shared' | 14 | const videoCommentRouter = express.Router() |
15 | // import { VideoBlacklistModel } from '../../../models/video/video-blacklist' | 15 | |
16 | // | 16 | videoCommentRouter.get('/:videoId/comment-threads', |
17 | // const videoCommentRouter = express.Router() | 17 | paginationValidator, |
18 | // | 18 | videoCommentThreadsSortValidator, |
19 | // videoCommentRouter.get('/:videoId/comment', | 19 | setVideoCommentThreadsSort, |
20 | // authenticate, | 20 | setPagination, |
21 | // ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST), | 21 | asyncMiddleware(listVideoCommentThreadsValidator), |
22 | // asyncMiddleware(listVideoCommentsThreadsValidator), | 22 | asyncMiddleware(listVideoThreads) |
23 | // asyncMiddleware(listVideoCommentsThreads) | 23 | ) |
24 | // ) | 24 | videoCommentRouter.get('/:videoId/comment-threads/:threadId', |
25 | // | 25 | asyncMiddleware(listVideoThreadCommentsValidator), |
26 | // videoCommentRouter.post('/:videoId/comment', | 26 | asyncMiddleware(listVideoThreadComments) |
27 | // authenticate, | 27 | ) |
28 | // ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST), | 28 | |
29 | // asyncMiddleware(videosBlacklistAddValidator), | 29 | videoCommentRouter.post('/:videoId/comment-threads', |
30 | // asyncMiddleware(addVideoToBlacklist) | 30 | authenticate, |
31 | // ) | 31 | asyncMiddleware(addVideoCommentThreadValidator), |
32 | // | 32 | asyncMiddleware(addVideoCommentThreadRetryWrapper) |
33 | // videoCommentRouter.get('/blacklist', | 33 | ) |
34 | // authenticate, | 34 | videoCommentRouter.post('/:videoId/comments/:commentId', |
35 | // ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST), | 35 | authenticate, |
36 | // paginationValidator, | 36 | asyncMiddleware(addVideoCommentReplyValidator), |
37 | // blacklistSortValidator, | 37 | asyncMiddleware(addVideoCommentReplyRetryWrapper) |
38 | // setBlacklistSort, | 38 | ) |
39 | // setPagination, | 39 | |
40 | // asyncMiddleware(listBlacklist) | 40 | // --------------------------------------------------------------------------- |
41 | // ) | 41 | |
42 | // | 42 | export { |
43 | // videoCommentRouter.delete('/:videoId/blacklist', | 43 | videoCommentRouter |
44 | // authenticate, | 44 | } |
45 | // ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST), | 45 | |
46 | // asyncMiddleware(videosBlacklistRemoveValidator), | 46 | // --------------------------------------------------------------------------- |
47 | // asyncMiddleware(removeVideoFromBlacklistController) | 47 | |
48 | // ) | 48 | async function listVideoThreads (req: express.Request, res: express.Response, next: express.NextFunction) { |
49 | // | 49 | const resultList = await VideoCommentModel.listThreadsForApi(res.locals.video.id, req.query.start, req.query.count, req.query.sort) |
50 | // // --------------------------------------------------------------------------- | 50 | |
51 | // | 51 | return res.json(getFormattedObjects(resultList.data, resultList.total)) |
52 | // export { | 52 | } |
53 | // videoCommentRouter | 53 | |
54 | // } | 54 | async function listVideoThreadComments (req: express.Request, res: express.Response, next: express.NextFunction) { |
55 | // | 55 | const resultList = await VideoCommentModel.listThreadCommentsForApi(res.locals.video.id, res.locals.videoCommentThread.id) |
56 | // // --------------------------------------------------------------------------- | 56 | |
57 | // | 57 | return res.json(buildFormattedCommentTree(resultList)) |
58 | // async function addVideoToBlacklist (req: express.Request, res: express.Response, next: express.NextFunction) { | 58 | } |
59 | // const videoInstance = res.locals.video | 59 | |
60 | // | 60 | async function addVideoCommentThreadRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) { |
61 | // const toCreate = { | 61 | const options = { |
62 | // videoId: videoInstance.id | 62 | arguments: [ req, res ], |
63 | // } | 63 | errorMessage: 'Cannot insert the video comment thread with many retries.' |
64 | // | 64 | } |
65 | // await VideoBlacklistModel.create(toCreate) | 65 | |
66 | // return res.type('json').status(204).end() | 66 | const comment = await retryTransactionWrapper(addVideoCommentThread, options) |
67 | // } | 67 | |
68 | // | 68 | res.json({ |
69 | // async function listBlacklist (req: express.Request, res: express.Response, next: express.NextFunction) { | 69 | comment: { |
70 | // const resultList = await VideoBlacklistModel.listForApi(req.query.start, req.query.count, req.query.sort) | 70 | id: comment.id |
71 | // | 71 | } |
72 | // return res.json(getFormattedObjects<BlacklistedVideo, VideoBlacklistModel>(resultList.data, resultList.total)) | 72 | }).end() |
73 | // } | 73 | } |
74 | // | 74 | |
75 | // async function removeVideoFromBlacklistController (req: express.Request, res: express.Response, next: express.NextFunction) { | 75 | function addVideoCommentThread (req: express.Request, res: express.Response) { |
76 | // const blacklistedVideo = res.locals.blacklistedVideo as VideoBlacklistModel | 76 | const videoCommentInfo: VideoCommentCreate = req.body |
77 | // | 77 | |
78 | // try { | 78 | return sequelizeTypescript.transaction(async t => { |
79 | // await blacklistedVideo.destroy() | 79 | return createVideoComment({ |
80 | // | 80 | text: videoCommentInfo.text, |
81 | // logger.info('Video %s removed from blacklist.', res.locals.video.uuid) | 81 | inReplyToComment: null, |
82 | // | 82 | video: res.locals.video, |
83 | // return res.sendStatus(204) | 83 | actorId: res.locals.oauth.token.User.Account.Actor.id |
84 | // } catch (err) { | 84 | }, t) |
85 | // logger.error('Some error while removing video %s from blacklist.', res.locals.video.uuid, err) | 85 | }) |
86 | // throw err | 86 | } |
87 | // } | 87 | |
88 | // } | 88 | async function addVideoCommentReplyRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) { |
89 | const options = { | ||
90 | arguments: [ req, res ], | ||
91 | errorMessage: 'Cannot insert the video comment reply with many retries.' | ||
92 | } | ||
93 | |||
94 | const comment = await retryTransactionWrapper(addVideoCommentReply, options) | ||
95 | |||
96 | res.json({ | ||
97 | comment: { | ||
98 | id: comment.id | ||
99 | } | ||
100 | }).end() | ||
101 | } | ||
102 | |||
103 | function addVideoCommentReply (req: express.Request, res: express.Response, next: express.NextFunction) { | ||
104 | const videoCommentInfo: VideoCommentCreate = req.body | ||
105 | |||
106 | return sequelizeTypescript.transaction(async t => { | ||
107 | return createVideoComment({ | ||
108 | text: videoCommentInfo.text, | ||
109 | inReplyToComment: res.locals.videoComment.id, | ||
110 | video: res.locals.video, | ||
111 | actorId: res.locals.oauth.token.User.Account.Actor.id | ||
112 | }, t) | ||
113 | }) | ||
114 | } | ||