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