]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/videos/abuse.ts
require -> import
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / abuse.ts
CommitLineData
4d4e5cd4 1import * as express from 'express'
65fcc311 2import { waterfall } from 'async'
d33242b0 3
e02643f3 4import { database as db } from '../../../initializers/database'
4d4e5cd4 5import * as friends from '../../../lib/friends'
65fcc311
C
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,
d33242b0
C
33 listVideoAbuses
34)
65fcc311
C
35abuseVideoRouter.post('/:id/abuse',
36 authenticate,
37 videoAbuseReportValidator,
d33242b0
C
38 reportVideoAbuseRetryWrapper
39)
40
41// ---------------------------------------------------------------------------
42
65fcc311
C
43export {
44 abuseVideoRouter
45}
d33242b0
C
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
65fcc311 53 res.json(getFormatedObjects(abusesList, abusesTotal))
d33242b0
C
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
65fcc311 63 retryTransactionWrapper(reportVideoAbuse, options, function (err) {
d33242b0
C
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
65fcc311 83 startSerializableTransaction,
d33242b0
C
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
65fcc311 106 commitTransaction
d33242b0
C
107
108 ], function andFinally (err, t) {
109 if (err) {
110 logger.debug('Cannot update the video.', { error: err })
65fcc311 111 return rollbackTransaction(err, t, finalCallback)
d33242b0
C
112 }
113
114 logger.info('Abuse report for video %s created.', videoInstance.name)
115 return finalCallback(null)
116 })
117}