aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/videos/abuse.ts
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-07-05 13:26:25 +0200
committerChocobozzz <florian.bigard@gmail.com>2017-07-05 14:14:16 +0200
commit6fcd19ba737f1f5614a56c6925adb882dea43b8d (patch)
tree3365a96d82bc7f00ae504a568725c8e914150cf8 /server/controllers/api/videos/abuse.ts
parent5fe7e898316e18369c3e1aba307b55077adc7bfb (diff)
downloadPeerTube-6fcd19ba737f1f5614a56c6925adb882dea43b8d.tar.gz
PeerTube-6fcd19ba737f1f5614a56c6925adb882dea43b8d.tar.zst
PeerTube-6fcd19ba737f1f5614a56c6925adb882dea43b8d.zip
Move to promises
Closes https://github.com/Chocobozzz/PeerTube/issues/74
Diffstat (limited to 'server/controllers/api/videos/abuse.ts')
-rw-r--r--server/controllers/api/videos/abuse.ts78
1 files changed, 28 insertions, 50 deletions
diff --git a/server/controllers/api/videos/abuse.ts b/server/controllers/api/videos/abuse.ts
index 78e8e8b3d..fcbd5465f 100644
--- a/server/controllers/api/videos/abuse.ts
+++ b/server/controllers/api/videos/abuse.ts
@@ -1,16 +1,11 @@
1import * as express from 'express' 1import * as express from 'express'
2import * as Sequelize from 'sequelize'
3import { waterfall } from 'async'
4 2
5import { database as db } from '../../../initializers/database' 3import { database as db } from '../../../initializers/database'
6import * as friends from '../../../lib/friends' 4import * as friends from '../../../lib/friends'
7import { 5import {
8 logger, 6 logger,
9 getFormatedObjects, 7 getFormatedObjects,
10 retryTransactionWrapper, 8 retryTransactionWrapper
11 startSerializableTransaction,
12 commitTransaction,
13 rollbackTransaction
14} from '../../../helpers' 9} from '../../../helpers'
15import { 10import {
16 authenticate, 11 authenticate,
@@ -21,6 +16,7 @@ import {
21 setVideoAbusesSort, 16 setVideoAbusesSort,
22 setPagination 17 setPagination
23} from '../../../middlewares' 18} from '../../../middlewares'
19import { VideoInstance } from '../../../models'
24 20
25const abuseVideoRouter = express.Router() 21const abuseVideoRouter = express.Router()
26 22
@@ -48,11 +44,9 @@ export {
48// --------------------------------------------------------------------------- 44// ---------------------------------------------------------------------------
49 45
50function listVideoAbuses (req: express.Request, res: express.Response, next: express.NextFunction) { 46function listVideoAbuses (req: express.Request, res: express.Response, next: express.NextFunction) {
51 db.VideoAbuse.listForApi(req.query.start, req.query.count, req.query.sort, function (err, abusesList, abusesTotal) { 47 db.VideoAbuse.listForApi(req.query.start, req.query.count, req.query.sort)
52 if (err) return next(err) 48 .then(result => res.json(getFormatedObjects(result.data, result.total)))
53 49 .catch(err => next(err))
54 res.json(getFormatedObjects(abusesList, abusesTotal))
55 })
56} 50}
57 51
58function reportVideoAbuseRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) { 52function reportVideoAbuseRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
@@ -61,14 +55,12 @@ function reportVideoAbuseRetryWrapper (req: express.Request, res: express.Respon
61 errorMessage: 'Cannot report abuse to the video with many retries.' 55 errorMessage: 'Cannot report abuse to the video with many retries.'
62 } 56 }
63 57
64 retryTransactionWrapper(reportVideoAbuse, options, function (err) { 58 retryTransactionWrapper(reportVideoAbuse, options)
65 if (err) return next(err) 59 .then(() => res.type('json').status(204).end())
66 60 .catch(err => next(err))
67 return res.type('json').status(204).end()
68 })
69} 61}
70 62
71function reportVideoAbuse (req: express.Request, res: express.Response, finalCallback: (err: Error) => void) { 63function reportVideoAbuse (req: express.Request, res: express.Response) {
72 const videoInstance = res.locals.video 64 const videoInstance = res.locals.video
73 const reporterUsername = res.locals.oauth.token.User.username 65 const reporterUsername = res.locals.oauth.token.User.username
74 66
@@ -79,40 +71,26 @@ function reportVideoAbuse (req: express.Request, res: express.Response, finalCal
79 reporterPodId: null // This is our pod that reported this abuse 71 reporterPodId: null // This is our pod that reported this abuse
80 } 72 }
81 73
82 waterfall([ 74 return db.sequelize.transaction(t => {
83 75 return db.VideoAbuse.create(abuse, { transaction: t })
84 startSerializableTransaction, 76 .then(abuse => {
85 77 // We send the information to the destination pod
86 function createAbuse (t, callback) { 78 if (videoInstance.isOwned() === false) {
87 db.VideoAbuse.create(abuse).asCallback(function (err, abuse) { 79 const reportData = {
88 return callback(err, t, abuse) 80 reporterUsername,
89 }) 81 reportReason: abuse.reason,
90 }, 82 videoRemoteId: videoInstance.remoteId
91 83 }
92 function sendToFriendsIfNeeded (t, abuse, callback) { 84
93 // We send the information to the destination pod 85 return friends.reportAbuseVideoToFriend(reportData, videoInstance, t).then(() => videoInstance)
94 if (videoInstance.isOwned() === false) {
95 const reportData = {
96 reporterUsername,
97 reportReason: abuse.reason,
98 videoRemoteId: videoInstance.remoteId
99 } 86 }
100 87
101 friends.reportAbuseVideoToFriend(reportData, videoInstance) 88 return videoInstance
102 } 89 })
103 90 })
104 return callback(null, t) 91 .then((videoInstance: VideoInstance) => logger.info('Abuse report for video %s created.', videoInstance.name))
105 }, 92 .catch(err => {
106 93 logger.debug('Cannot update the video.', { error: err })
107 commitTransaction 94 throw err
108
109 ], function andFinally (err: Error, t: Sequelize.Transaction) {
110 if (err) {
111 logger.debug('Cannot update the video.', { error: err })
112 return rollbackTransaction(err, t, finalCallback)
113 }
114
115 logger.info('Abuse report for video %s created.', videoInstance.name)
116 return finalCallback(null)
117 }) 95 })
118} 96}