1 import * as express from 'express'
3 import { database as db } from '../../../initializers/database'
4 import * as friends from '../../../lib/friends'
8 retryTransactionWrapper
9 } from '../../../helpers'
14 videoAbuseReportValidator,
15 videoAbusesSortValidator,
18 } from '../../../middlewares'
19 import { VideoInstance } from '../../../models'
21 const abuseVideoRouter = express.Router()
23 abuseVideoRouter.get('/abuse',
27 videoAbusesSortValidator,
32 abuseVideoRouter.post('/:id/abuse',
34 videoAbuseReportValidator,
35 reportVideoAbuseRetryWrapper
38 // ---------------------------------------------------------------------------
44 // ---------------------------------------------------------------------------
46 function listVideoAbuses (req: express.Request, res: express.Response, next: express.NextFunction) {
47 db.VideoAbuse.listForApi(req.query.start, req.query.count, req.query.sort)
48 .then(result => res.json(getFormatedObjects(result.data, result.total)))
49 .catch(err => next(err))
52 function reportVideoAbuseRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
54 arguments: [ req, res ],
55 errorMessage: 'Cannot report abuse to the video with many retries.'
58 retryTransactionWrapper(reportVideoAbuse, options)
59 .then(() => res.type('json').status(204).end())
60 .catch(err => next(err))
63 function reportVideoAbuse (req: express.Request, res: express.Response) {
64 const videoInstance = res.locals.video
65 const reporterUsername = res.locals.oauth.token.User.username
69 reason: req.body.reason,
70 videoId: videoInstance.id,
71 reporterPodId: null // This is our pod that reported this abuse
74 return db.sequelize.transaction(t => {
75 return db.VideoAbuse.create(abuse, { transaction: t })
77 // We send the information to the destination pod
78 if (videoInstance.isOwned() === false) {
81 reportReason: abuse.reason,
82 videoRemoteId: videoInstance.remoteId
85 return friends.reportAbuseVideoToFriend(reportData, videoInstance, t).then(() => videoInstance)
91 .then((videoInstance: VideoInstance) => logger.info('Abuse report for video %s created.', videoInstance.name))
93 logger.debug('Cannot update the video.', err)