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'
20 import { VideoAbuseCreate } from '../../../../shared'
22 const abuseVideoRouter = express.Router()
24 abuseVideoRouter.get('/abuse',
28 videoAbusesSortValidator,
33 abuseVideoRouter.post('/:id/abuse',
35 videoAbuseReportValidator,
36 reportVideoAbuseRetryWrapper
39 // ---------------------------------------------------------------------------
45 // ---------------------------------------------------------------------------
47 function listVideoAbuses (req: express.Request, res: express.Response, next: express.NextFunction) {
48 db.VideoAbuse.listForApi(req.query.start, req.query.count, req.query.sort)
49 .then(result => res.json(getFormatedObjects(result.data, result.total)))
50 .catch(err => next(err))
53 function reportVideoAbuseRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
55 arguments: [ req, res ],
56 errorMessage: 'Cannot report abuse to the video with many retries.'
59 retryTransactionWrapper(reportVideoAbuse, options)
60 .then(() => res.type('json').status(204).end())
61 .catch(err => next(err))
64 function reportVideoAbuse (req: express.Request, res: express.Response) {
65 const videoInstance = res.locals.video as VideoInstance
66 const reporterUsername = res.locals.oauth.token.User.username
67 const body: VideoAbuseCreate = req.body
72 videoId: videoInstance.id,
73 reporterPodId: null // This is our pod that reported this abuse
76 return db.sequelize.transaction(t => {
77 return db.VideoAbuse.create(abuse, { transaction: t })
79 // We send the information to the destination pod
80 if (videoInstance.isOwned() === false) {
83 reportReason: abuse.reason,
84 videoUUID: videoInstance.uuid
87 return friends.reportAbuseVideoToFriend(reportData, videoInstance, t).then(() => videoInstance)
93 .then((videoInstance: VideoInstance) => logger.info('Abuse report for video %s created.', videoInstance.name))
95 logger.debug('Cannot update the video.', err)