]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/videos/abuse.ts
68db025b7f3bf67a0ec079b61039bdd5fdd01171
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / abuse.ts
1 import * as express from 'express'
2 import { waterfall } from 'async'
3
4 import { database as db } from '../../../initializers/database'
5 import * as friends from '../../../lib/friends'
6 import {
7 logger,
8 getFormatedObjects,
9 retryTransactionWrapper,
10 startSerializableTransaction,
11 commitTransaction,
12 rollbackTransaction
13 } from '../../../helpers'
14 import {
15 authenticate,
16 ensureIsAdmin,
17 paginationValidator,
18 videoAbuseReportValidator,
19 videoAbusesSortValidator,
20 setVideoAbusesSort,
21 setPagination
22 } from '../../../middlewares'
23
24 const abuseVideoRouter = express.Router()
25
26 abuseVideoRouter.get('/abuse',
27 authenticate,
28 ensureIsAdmin,
29 paginationValidator,
30 videoAbusesSortValidator,
31 setVideoAbusesSort,
32 setPagination,
33 listVideoAbuses
34 )
35 abuseVideoRouter.post('/:id/abuse',
36 authenticate,
37 videoAbuseReportValidator,
38 reportVideoAbuseRetryWrapper
39 )
40
41 // ---------------------------------------------------------------------------
42
43 export {
44 abuseVideoRouter
45 }
46
47 // ---------------------------------------------------------------------------
48
49 function 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
57 function 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
70 function 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 }