diff options
Diffstat (limited to 'server/controllers/api/videos/abuse.js')
-rw-r--r-- | server/controllers/api/videos/abuse.js | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/server/controllers/api/videos/abuse.js b/server/controllers/api/videos/abuse.js new file mode 100644 index 000000000..0fb44bb14 --- /dev/null +++ b/server/controllers/api/videos/abuse.js | |||
@@ -0,0 +1,112 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const express = require('express') | ||
4 | const waterfall = require('async/waterfall') | ||
5 | |||
6 | const db = require('../../../initializers/database') | ||
7 | const logger = require('../../../helpers/logger') | ||
8 | const friends = require('../../../lib/friends') | ||
9 | const middlewares = require('../../../middlewares') | ||
10 | const admin = middlewares.admin | ||
11 | const oAuth = middlewares.oauth | ||
12 | const pagination = middlewares.pagination | ||
13 | const validators = middlewares.validators | ||
14 | const validatorsPagination = validators.pagination | ||
15 | const validatorsSort = validators.sort | ||
16 | const validatorsVideos = validators.videos | ||
17 | const sort = middlewares.sort | ||
18 | const databaseUtils = require('../../../helpers/database-utils') | ||
19 | const utils = require('../../../helpers/utils') | ||
20 | |||
21 | const router = express.Router() | ||
22 | |||
23 | router.get('/abuse', | ||
24 | oAuth.authenticate, | ||
25 | admin.ensureIsAdmin, | ||
26 | validatorsPagination.pagination, | ||
27 | validatorsSort.videoAbusesSort, | ||
28 | sort.setVideoAbusesSort, | ||
29 | pagination.setPagination, | ||
30 | listVideoAbuses | ||
31 | ) | ||
32 | router.post('/:id/abuse', | ||
33 | oAuth.authenticate, | ||
34 | validatorsVideos.videoAbuseReport, | ||
35 | reportVideoAbuseRetryWrapper | ||
36 | ) | ||
37 | |||
38 | // --------------------------------------------------------------------------- | ||
39 | |||
40 | module.exports = router | ||
41 | |||
42 | // --------------------------------------------------------------------------- | ||
43 | |||
44 | function listVideoAbuses (req, res, next) { | ||
45 | db.VideoAbuse.listForApi(req.query.start, req.query.count, req.query.sort, function (err, abusesList, abusesTotal) { | ||
46 | if (err) return next(err) | ||
47 | |||
48 | res.json(utils.getFormatedObjects(abusesList, abusesTotal)) | ||
49 | }) | ||
50 | } | ||
51 | |||
52 | function reportVideoAbuseRetryWrapper (req, res, next) { | ||
53 | const options = { | ||
54 | arguments: [ req, res ], | ||
55 | errorMessage: 'Cannot report abuse to the video with many retries.' | ||
56 | } | ||
57 | |||
58 | databaseUtils.retryTransactionWrapper(reportVideoAbuse, options, function (err) { | ||
59 | if (err) return next(err) | ||
60 | |||
61 | return res.type('json').status(204).end() | ||
62 | }) | ||
63 | } | ||
64 | |||
65 | function reportVideoAbuse (req, res, finalCallback) { | ||
66 | const videoInstance = res.locals.video | ||
67 | const reporterUsername = res.locals.oauth.token.User.username | ||
68 | |||
69 | const abuse = { | ||
70 | reporterUsername, | ||
71 | reason: req.body.reason, | ||
72 | videoId: videoInstance.id, | ||
73 | reporterPodId: null // This is our pod that reported this abuse | ||
74 | } | ||
75 | |||
76 | waterfall([ | ||
77 | |||
78 | databaseUtils.startSerializableTransaction, | ||
79 | |||
80 | function createAbuse (t, callback) { | ||
81 | db.VideoAbuse.create(abuse).asCallback(function (err, abuse) { | ||
82 | return callback(err, t, abuse) | ||
83 | }) | ||
84 | }, | ||
85 | |||
86 | function sendToFriendsIfNeeded (t, abuse, callback) { | ||
87 | // We send the information to the destination pod | ||
88 | if (videoInstance.isOwned() === false) { | ||
89 | const reportData = { | ||
90 | reporterUsername, | ||
91 | reportReason: abuse.reason, | ||
92 | videoRemoteId: videoInstance.remoteId | ||
93 | } | ||
94 | |||
95 | friends.reportAbuseVideoToFriend(reportData, videoInstance) | ||
96 | } | ||
97 | |||
98 | return callback(null, t) | ||
99 | }, | ||
100 | |||
101 | databaseUtils.commitTransaction | ||
102 | |||
103 | ], function andFinally (err, t) { | ||
104 | if (err) { | ||
105 | logger.debug('Cannot update the video.', { error: err }) | ||
106 | return databaseUtils.rollbackTransaction(err, t, finalCallback) | ||
107 | } | ||
108 | |||
109 | logger.info('Abuse report for video %s created.', videoInstance.name) | ||
110 | return finalCallback(null) | ||
111 | }) | ||
112 | } | ||