1 import * as express from 'express'
2 import { waterfall } from 'async'
4 import { database as db } from '../../../initializers/database'
5 import * as friends from '../../../lib/friends'
9 retryTransactionWrapper,
10 startSerializableTransaction,
13 } from '../../../helpers'
18 videoAbuseReportValidator,
19 videoAbusesSortValidator,
22 } from '../../../middlewares'
24 const abuseVideoRouter = express.Router()
26 abuseVideoRouter.get('/abuse',
30 videoAbusesSortValidator,
35 abuseVideoRouter.post('/:id/abuse',
37 videoAbuseReportValidator,
38 reportVideoAbuseRetryWrapper
41 // ---------------------------------------------------------------------------
47 // ---------------------------------------------------------------------------
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)
53 res.json(getFormatedObjects(abusesList, abusesTotal))
57 function reportVideoAbuseRetryWrapper (req, res, next) {
59 arguments: [ req, res ],
60 errorMessage: 'Cannot report abuse to the video with many retries.'
63 retryTransactionWrapper(reportVideoAbuse, options, function (err) {
64 if (err) return next(err)
66 return res.type('json').status(204).end()
70 function reportVideoAbuse (req, res, finalCallback) {
71 const videoInstance = res.locals.video
72 const reporterUsername = res.locals.oauth.token.User.username
76 reason: req.body.reason,
77 videoId: videoInstance.id,
78 reporterPodId: null // This is our pod that reported this abuse
83 startSerializableTransaction,
85 function createAbuse (t, callback) {
86 db.VideoAbuse.create(abuse).asCallback(function (err, abuse) {
87 return callback(err, t, abuse)
91 function sendToFriendsIfNeeded (t, abuse, callback) {
92 // We send the information to the destination pod
93 if (videoInstance.isOwned() === false) {
96 reportReason: abuse.reason,
97 videoRemoteId: videoInstance.remoteId
100 friends.reportAbuseVideoToFriend(reportData, videoInstance)
103 return callback(null, t)
108 ], function andFinally (err, t) {
110 logger.debug('Cannot update the video.', { error: err })
111 return rollbackTransaction(err, t, finalCallback)
114 logger.info('Abuse report for video %s created.', videoInstance.name)
115 return finalCallback(null)