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,
19 } from '../../../middlewares'
20 import { VideoInstance } from '../../../models'
21 import { VideoAbuseCreate } from '../../../../shared'
23 const abuseVideoRouter = express.Router()
25 abuseVideoRouter.get('/abuse',
29 videoAbusesSortValidator,
32 asyncMiddleware(listVideoAbuses)
34 abuseVideoRouter.post('/:id/abuse',
36 videoAbuseReportValidator,
37 asyncMiddleware(reportVideoAbuseRetryWrapper)
40 // ---------------------------------------------------------------------------
46 // ---------------------------------------------------------------------------
48 async function listVideoAbuses (req: express.Request, res: express.Response, next: express.NextFunction) {
49 const resultList = await db.VideoAbuse.listForApi(req.query.start, req.query.count, req.query.sort)
51 return res.json(getFormattedObjects(resultList.data, resultList.total))
54 async function reportVideoAbuseRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
56 arguments: [ req, res ],
57 errorMessage: 'Cannot report abuse to the video with many retries.'
60 await retryTransactionWrapper(reportVideoAbuse, options)
62 return res.type('json').status(204).end()
65 async function reportVideoAbuse (req: express.Request, res: express.Response) {
66 const videoInstance = res.locals.video as VideoInstance
67 const reporterUsername = res.locals.oauth.token.User.username
68 const body: VideoAbuseCreate = req.body
70 const abuseToCreate = {
73 videoId: videoInstance.id,
74 reporterPodId: null // This is our pod that reported this abuse
77 await db.sequelize.transaction(async t => {
78 const abuse = await db.VideoAbuse.create(abuseToCreate, { 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 await friends.reportAbuseVideoToFriend(reportData, videoInstance, t)
91 logger.info('Abuse report for video %s created.', videoInstance.name)