]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/videos/abuse.ts
78e8e8b3d7ed9bf188731640c016e7e44d1d5238
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / abuse.ts
1 import * as express from 'express'
2 import * as Sequelize from 'sequelize'
3 import { waterfall } from 'async'
4
5 import { database as db } from '../../../initializers/database'
6 import * as friends from '../../../lib/friends'
7 import {
8 logger,
9 getFormatedObjects,
10 retryTransactionWrapper,
11 startSerializableTransaction,
12 commitTransaction,
13 rollbackTransaction
14 } from '../../../helpers'
15 import {
16 authenticate,
17 ensureIsAdmin,
18 paginationValidator,
19 videoAbuseReportValidator,
20 videoAbusesSortValidator,
21 setVideoAbusesSort,
22 setPagination
23 } from '../../../middlewares'
24
25 const abuseVideoRouter = express.Router()
26
27 abuseVideoRouter.get('/abuse',
28 authenticate,
29 ensureIsAdmin,
30 paginationValidator,
31 videoAbusesSortValidator,
32 setVideoAbusesSort,
33 setPagination,
34 listVideoAbuses
35 )
36 abuseVideoRouter.post('/:id/abuse',
37 authenticate,
38 videoAbuseReportValidator,
39 reportVideoAbuseRetryWrapper
40 )
41
42 // ---------------------------------------------------------------------------
43
44 export {
45 abuseVideoRouter
46 }
47
48 // ---------------------------------------------------------------------------
49
50 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) {
52 if (err) return next(err)
53
54 res.json(getFormatedObjects(abusesList, abusesTotal))
55 })
56 }
57
58 function reportVideoAbuseRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
59 const options = {
60 arguments: [ req, res ],
61 errorMessage: 'Cannot report abuse to the video with many retries.'
62 }
63
64 retryTransactionWrapper(reportVideoAbuse, options, function (err) {
65 if (err) return next(err)
66
67 return res.type('json').status(204).end()
68 })
69 }
70
71 function reportVideoAbuse (req: express.Request, res: express.Response, finalCallback: (err: Error) => void) {
72 const videoInstance = res.locals.video
73 const reporterUsername = res.locals.oauth.token.User.username
74
75 const abuse = {
76 reporterUsername,
77 reason: req.body.reason,
78 videoId: videoInstance.id,
79 reporterPodId: null // This is our pod that reported this abuse
80 }
81
82 waterfall([
83
84 startSerializableTransaction,
85
86 function createAbuse (t, callback) {
87 db.VideoAbuse.create(abuse).asCallback(function (err, abuse) {
88 return callback(err, t, abuse)
89 })
90 },
91
92 function sendToFriendsIfNeeded (t, abuse, callback) {
93 // We send the information to the destination pod
94 if (videoInstance.isOwned() === false) {
95 const reportData = {
96 reporterUsername,
97 reportReason: abuse.reason,
98 videoRemoteId: videoInstance.remoteId
99 }
100
101 friends.reportAbuseVideoToFriend(reportData, videoInstance)
102 }
103
104 return callback(null, t)
105 },
106
107 commitTransaction
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 })
118 }