aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/videos/abuse.ts
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-10-25 11:55:06 +0200
committerChocobozzz <florian.bigard@gmail.com>2017-10-26 09:11:38 +0200
commiteb08047657e739bcd9e592d76307befa3998482b (patch)
treefc309f51ece792fd4307c4af510710a853e1d6b2 /server/controllers/api/videos/abuse.ts
parent5f04dd2f743961e0a06c29531cc3ccc9e4928d56 (diff)
downloadPeerTube-eb08047657e739bcd9e592d76307befa3998482b.tar.gz
PeerTube-eb08047657e739bcd9e592d76307befa3998482b.tar.zst
PeerTube-eb08047657e739bcd9e592d76307befa3998482b.zip
Use async/await in controllers
Diffstat (limited to 'server/controllers/api/videos/abuse.ts')
-rw-r--r--server/controllers/api/videos/abuse.ts62
1 files changed, 28 insertions, 34 deletions
diff --git a/server/controllers/api/videos/abuse.ts b/server/controllers/api/videos/abuse.ts
index c9313d5f5..4c7abf395 100644
--- a/server/controllers/api/videos/abuse.ts
+++ b/server/controllers/api/videos/abuse.ts
@@ -14,7 +14,8 @@ import {
14 videoAbuseReportValidator, 14 videoAbuseReportValidator,
15 videoAbusesSortValidator, 15 videoAbusesSortValidator,
16 setVideoAbusesSort, 16 setVideoAbusesSort,
17 setPagination 17 setPagination,
18 asyncMiddleware
18} from '../../../middlewares' 19} from '../../../middlewares'
19import { VideoInstance } from '../../../models' 20import { VideoInstance } from '../../../models'
20import { VideoAbuseCreate } from '../../../../shared' 21import { VideoAbuseCreate } from '../../../../shared'
@@ -28,12 +29,12 @@ abuseVideoRouter.get('/abuse',
28 videoAbusesSortValidator, 29 videoAbusesSortValidator,
29 setVideoAbusesSort, 30 setVideoAbusesSort,
30 setPagination, 31 setPagination,
31 listVideoAbuses 32 asyncMiddleware(listVideoAbuses)
32) 33)
33abuseVideoRouter.post('/:id/abuse', 34abuseVideoRouter.post('/:id/abuse',
34 authenticate, 35 authenticate,
35 videoAbuseReportValidator, 36 videoAbuseReportValidator,
36 reportVideoAbuseRetryWrapper 37 asyncMiddleware(reportVideoAbuseRetryWrapper)
37) 38)
38 39
39// --------------------------------------------------------------------------- 40// ---------------------------------------------------------------------------
@@ -44,55 +45,48 @@ export {
44 45
45// --------------------------------------------------------------------------- 46// ---------------------------------------------------------------------------
46 47
47function listVideoAbuses (req: express.Request, res: express.Response, next: express.NextFunction) { 48async function listVideoAbuses (req: express.Request, res: express.Response, next: express.NextFunction) {
48 db.VideoAbuse.listForApi(req.query.start, req.query.count, req.query.sort) 49 const resultList = await db.VideoAbuse.listForApi(req.query.start, req.query.count, req.query.sort)
49 .then(result => res.json(getFormattedObjects(result.data, result.total))) 50
50 .catch(err => next(err)) 51 return res.json(getFormattedObjects(resultList.data, resultList.total))
51} 52}
52 53
53function reportVideoAbuseRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) { 54async function reportVideoAbuseRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
54 const options = { 55 const options = {
55 arguments: [ req, res ], 56 arguments: [ req, res ],
56 errorMessage: 'Cannot report abuse to the video with many retries.' 57 errorMessage: 'Cannot report abuse to the video with many retries.'
57 } 58 }
58 59
59 retryTransactionWrapper(reportVideoAbuse, options) 60 await retryTransactionWrapper(reportVideoAbuse, options)
60 .then(() => res.type('json').status(204).end()) 61
61 .catch(err => next(err)) 62 return res.type('json').status(204).end()
62} 63}
63 64
64function reportVideoAbuse (req: express.Request, res: express.Response) { 65async function reportVideoAbuse (req: express.Request, res: express.Response) {
65 const videoInstance = res.locals.video as VideoInstance 66 const videoInstance = res.locals.video as VideoInstance
66 const reporterUsername = res.locals.oauth.token.User.username 67 const reporterUsername = res.locals.oauth.token.User.username
67 const body: VideoAbuseCreate = req.body 68 const body: VideoAbuseCreate = req.body
68 69
69 const abuse = { 70 const abuseToCreate = {
70 reporterUsername, 71 reporterUsername,
71 reason: body.reason, 72 reason: body.reason,
72 videoId: videoInstance.id, 73 videoId: videoInstance.id,
73 reporterPodId: null // This is our pod that reported this abuse 74 reporterPodId: null // This is our pod that reported this abuse
74 } 75 }
75 76
76 return db.sequelize.transaction(t => { 77 await db.sequelize.transaction(async t => {
77 return db.VideoAbuse.create(abuse, { transaction: t }) 78 const abuse = await db.VideoAbuse.create(abuseToCreate, { transaction: t })
78 .then(abuse => { 79 // We send the information to the destination pod
79 // We send the information to the destination pod 80 if (videoInstance.isOwned() === false) {
80 if (videoInstance.isOwned() === false) { 81 const reportData = {
81 const reportData = { 82 reporterUsername,
82 reporterUsername, 83 reportReason: abuse.reason,
83 reportReason: abuse.reason, 84 videoUUID: videoInstance.uuid
84 videoUUID: videoInstance.uuid 85 }
85 } 86
86 87 await friends.reportAbuseVideoToFriend(reportData, videoInstance, t)
87 return friends.reportAbuseVideoToFriend(reportData, videoInstance, t).then(() => videoInstance) 88 }
88 }
89
90 return videoInstance
91 })
92 })
93 .then((videoInstance: VideoInstance) => logger.info('Abuse report for video %s created.', videoInstance.name))
94 .catch(err => {
95 logger.debug('Cannot update the video.', err)
96 throw err
97 }) 89 })
90
91 logger.info('Abuse report for video %s created.', videoInstance.name)
98} 92}