1 import * as express from 'express'
2 import { UserRight, VideoAbuseCreate } from '../../../../shared'
3 import { getFormattedObjects, logger, retryTransactionWrapper } from '../../../helpers'
4 import { sequelizeTypescript } from '../../../initializers'
5 import { sendVideoAbuse } from '../../../lib/activitypub/send'
13 videoAbuseReportValidator,
14 videoAbusesSortValidator
15 } from '../../../middlewares'
16 import { AccountModel } from '../../../models/account/account'
17 import { VideoModel } from '../../../models/video/video'
18 import { VideoAbuseModel } from '../../../models/video/video-abuse'
20 const abuseVideoRouter = express.Router()
22 abuseVideoRouter.get('/abuse',
24 ensureUserHasRight(UserRight.MANAGE_VIDEO_ABUSES),
26 videoAbusesSortValidator,
29 asyncMiddleware(listVideoAbuses)
31 abuseVideoRouter.post('/:id/abuse',
33 asyncMiddleware(videoAbuseReportValidator),
34 asyncMiddleware(reportVideoAbuseRetryWrapper)
37 // ---------------------------------------------------------------------------
43 // ---------------------------------------------------------------------------
45 async function listVideoAbuses (req: express.Request, res: express.Response, next: express.NextFunction) {
46 const resultList = await VideoAbuseModel.listForApi(req.query.start, req.query.count, req.query.sort)
48 return res.json(getFormattedObjects(resultList.data, resultList.total))
51 async function reportVideoAbuseRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
53 arguments: [ req, res ],
54 errorMessage: 'Cannot report abuse to the video with many retries.'
57 await retryTransactionWrapper(reportVideoAbuse, options)
59 return res.type('json').status(204).end()
62 async function reportVideoAbuse (req: express.Request, res: express.Response) {
63 const videoInstance = res.locals.video as VideoModel
64 const reporterAccount = res.locals.oauth.token.User.Account as AccountModel
65 const body: VideoAbuseCreate = req.body
67 const abuseToCreate = {
68 reporterAccountId: reporterAccount.id,
70 videoId: videoInstance.id
73 await sequelizeTypescript.transaction(async t => {
74 const videoAbuseInstance = await VideoAbuseModel.create(abuseToCreate, { transaction: t })
75 videoAbuseInstance.Video = videoInstance
77 // We send the video abuse to the origin server
78 if (videoInstance.isOwned() === false) {
79 await sendVideoAbuse(reporterAccount.Actor, videoAbuseInstance, videoInstance, t)
83 logger.info('Abuse report for video %s created.', videoInstance.name)