1 import * as express from 'express'
2 import * as Sequelize from 'sequelize'
3 import { waterfall } from 'async'
5 import { database as db } from '../../../initializers/database'
6 import * as friends from '../../../lib/friends'
10 retryTransactionWrapper,
11 startSerializableTransaction,
14 } from '../../../helpers'
19 videoAbuseReportValidator,
20 videoAbusesSortValidator,
23 } from '../../../middlewares'
25 const abuseVideoRouter = express.Router()
27 abuseVideoRouter.get('/abuse',
31 videoAbusesSortValidator,
36 abuseVideoRouter.post('/:id/abuse',
38 videoAbuseReportValidator,
39 reportVideoAbuseRetryWrapper
42 // ---------------------------------------------------------------------------
48 // ---------------------------------------------------------------------------
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)
54 res.json(getFormatedObjects(abusesList, abusesTotal))
58 function reportVideoAbuseRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
60 arguments: [ req, res ],
61 errorMessage: 'Cannot report abuse to the video with many retries.'
64 retryTransactionWrapper(reportVideoAbuse, options, function (err) {
65 if (err) return next(err)
67 return res.type('json').status(204).end()
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
77 reason: req.body.reason,
78 videoId: videoInstance.id,
79 reporterPodId: null // This is our pod that reported this abuse
84 startSerializableTransaction,
86 function createAbuse (t, callback) {
87 db.VideoAbuse.create(abuse).asCallback(function (err, abuse) {
88 return callback(err, t, abuse)
92 function sendToFriendsIfNeeded (t, abuse, callback) {
93 // We send the information to the destination pod
94 if (videoInstance.isOwned() === false) {
97 reportReason: abuse.reason,
98 videoRemoteId: videoInstance.remoteId
101 friends.reportAbuseVideoToFriend(reportData, videoInstance)
104 return callback(null, t)
109 ], function andFinally (err: Error, t: Sequelize.Transaction) {
111 logger.debug('Cannot update the video.', { error: err })
112 return rollbackTransaction(err, t, finalCallback)
115 logger.info('Abuse report for video %s created.', videoInstance.name)
116 return finalCallback(null)