diff options
Diffstat (limited to 'server/controllers/api/videos/abuse.ts')
-rw-r--r-- | server/controllers/api/videos/abuse.ts | 78 |
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 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | import * as Sequelize from 'sequelize' | ||
3 | import { waterfall } from 'async' | ||
4 | 2 | ||
5 | import { database as db } from '../../../initializers/database' | 3 | import { database as db } from '../../../initializers/database' |
6 | import * as friends from '../../../lib/friends' | 4 | import * as friends from '../../../lib/friends' |
7 | import { | 5 | import { |
8 | logger, | 6 | logger, |
9 | getFormatedObjects, | 7 | getFormatedObjects, |
10 | retryTransactionWrapper, | 8 | retryTransactionWrapper |
11 | startSerializableTransaction, | ||
12 | commitTransaction, | ||
13 | rollbackTransaction | ||
14 | } from '../../../helpers' | 9 | } from '../../../helpers' |
15 | import { | 10 | import { |
16 | authenticate, | 11 | authenticate, |
@@ -21,6 +16,7 @@ import { | |||
21 | setVideoAbusesSort, | 16 | setVideoAbusesSort, |
22 | setPagination | 17 | setPagination |
23 | } from '../../../middlewares' | 18 | } from '../../../middlewares' |
19 | import { VideoInstance } from '../../../models' | ||
24 | 20 | ||
25 | const abuseVideoRouter = express.Router() | 21 | const abuseVideoRouter = express.Router() |
26 | 22 | ||
@@ -48,11 +44,9 @@ export { | |||
48 | // --------------------------------------------------------------------------- | 44 | // --------------------------------------------------------------------------- |
49 | 45 | ||
50 | function listVideoAbuses (req: express.Request, res: express.Response, next: express.NextFunction) { | 46 | function 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 | ||
58 | function reportVideoAbuseRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) { | 52 | function 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 | ||
71 | function reportVideoAbuse (req: express.Request, res: express.Response, finalCallback: (err: Error) => void) { | 63 | function 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 | } |