aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/videos/abuse.js
diff options
context:
space:
mode:
Diffstat (limited to 'server/controllers/api/videos/abuse.js')
-rw-r--r--server/controllers/api/videos/abuse.js112
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
3const express = require('express')
4const waterfall = require('async/waterfall')
5
6const db = require('../../../initializers/database')
7const logger = require('../../../helpers/logger')
8const friends = require('../../../lib/friends')
9const middlewares = require('../../../middlewares')
10const admin = middlewares.admin
11const oAuth = middlewares.oauth
12const pagination = middlewares.pagination
13const validators = middlewares.validators
14const validatorsPagination = validators.pagination
15const validatorsSort = validators.sort
16const validatorsVideos = validators.videos
17const sort = middlewares.sort
18const databaseUtils = require('../../../helpers/database-utils')
19const utils = require('../../../helpers/utils')
20
21const router = express.Router()
22
23router.get('/abuse',
24 oAuth.authenticate,
25 admin.ensureIsAdmin,
26 validatorsPagination.pagination,
27 validatorsSort.videoAbusesSort,
28 sort.setVideoAbusesSort,
29 pagination.setPagination,
30 listVideoAbuses
31)
32router.post('/:id/abuse',
33 oAuth.authenticate,
34 validatorsVideos.videoAbuseReport,
35 reportVideoAbuseRetryWrapper
36)
37
38// ---------------------------------------------------------------------------
39
40module.exports = router
41
42// ---------------------------------------------------------------------------
43
44function 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
52function 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
65function 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}