1 import * as express from 'express'
3 import { database as db } from '../../../initializers/database'
7 retryTransactionWrapper
8 } from '../../../helpers'
13 videoAbuseReportValidator,
14 videoAbusesSortValidator,
18 } from '../../../middlewares'
19 import { VideoInstance } from '../../../models'
20 import { VideoAbuseCreate, UserRight } from '../../../../shared'
22 const abuseVideoRouter = express.Router()
24 abuseVideoRouter.get('/abuse',
26 ensureUserHasRight(UserRight.MANAGE_VIDEO_ABUSES),
28 videoAbusesSortValidator,
31 asyncMiddleware(listVideoAbuses)
33 abuseVideoRouter.post('/:id/abuse',
35 videoAbuseReportValidator,
36 asyncMiddleware(reportVideoAbuseRetryWrapper)
39 // ---------------------------------------------------------------------------
45 // ---------------------------------------------------------------------------
47 async function listVideoAbuses (req: express.Request, res: express.Response, next: express.NextFunction) {
48 const resultList = await db.VideoAbuse.listForApi(req.query.start, req.query.count, req.query.sort)
50 return res.json(getFormattedObjects(resultList.data, resultList.total))
53 async 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 await retryTransactionWrapper(reportVideoAbuse, options)
61 return res.type('json').status(204).end()
64 async 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
69 const abuseToCreate = {
72 videoId: videoInstance.id,
73 reporterServerId: null // This is our server that reported this abuse
76 await db.sequelize.transaction(async t => {
77 const abuse = await db.VideoAbuse.create(abuseToCreate, { transaction: t })
78 // We send the information to the destination server
79 if (videoInstance.isOwned() === false) {
82 reportReason: abuse.reason,
83 videoUUID: videoInstance.uuid
86 // await friends.reportAbuseVideoToFriend(reportData, videoInstance, t)
87 // TODO: send abuse to origin server
91 logger.info('Abuse report for video %s created.', videoInstance.name)