2017-06-05 14:53:49 -05:00
|
|
|
import * as express from 'express'
|
2017-05-05 09:53:35 -05:00
|
|
|
|
2017-05-22 13:58:25 -05:00
|
|
|
import { database as db } from '../../../initializers/database'
|
2017-05-15 15:22:03 -05:00
|
|
|
import {
|
|
|
|
logger,
|
2017-08-25 04:45:31 -05:00
|
|
|
getFormattedObjects,
|
2017-07-05 06:26:25 -05:00
|
|
|
retryTransactionWrapper
|
2017-05-15 15:22:03 -05:00
|
|
|
} from '../../../helpers'
|
|
|
|
import {
|
|
|
|
authenticate,
|
2017-10-27 09:55:03 -05:00
|
|
|
ensureUserHasRight,
|
2017-05-15 15:22:03 -05:00
|
|
|
paginationValidator,
|
|
|
|
videoAbuseReportValidator,
|
|
|
|
videoAbusesSortValidator,
|
|
|
|
setVideoAbusesSort,
|
2017-10-25 04:55:06 -05:00
|
|
|
setPagination,
|
|
|
|
asyncMiddleware
|
2017-05-15 15:22:03 -05:00
|
|
|
} from '../../../middlewares'
|
2017-07-05 06:26:25 -05:00
|
|
|
import { VideoInstance } from '../../../models'
|
2017-10-27 09:55:03 -05:00
|
|
|
import { VideoAbuseCreate, UserRight } from '../../../../shared'
|
2017-11-15 08:12:23 -06:00
|
|
|
import { sendVideoAbuse } from '../../../lib/index'
|
2017-05-15 15:22:03 -05:00
|
|
|
|
|
|
|
const abuseVideoRouter = express.Router()
|
|
|
|
|
|
|
|
abuseVideoRouter.get('/abuse',
|
|
|
|
authenticate,
|
2017-10-27 09:55:03 -05:00
|
|
|
ensureUserHasRight(UserRight.MANAGE_VIDEO_ABUSES),
|
2017-05-15 15:22:03 -05:00
|
|
|
paginationValidator,
|
|
|
|
videoAbusesSortValidator,
|
|
|
|
setVideoAbusesSort,
|
|
|
|
setPagination,
|
2017-10-25 04:55:06 -05:00
|
|
|
asyncMiddleware(listVideoAbuses)
|
2017-05-05 09:53:35 -05:00
|
|
|
)
|
2017-05-15 15:22:03 -05:00
|
|
|
abuseVideoRouter.post('/:id/abuse',
|
|
|
|
authenticate,
|
2017-11-27 10:30:46 -06:00
|
|
|
asyncMiddleware(videoAbuseReportValidator),
|
2017-10-25 04:55:06 -05:00
|
|
|
asyncMiddleware(reportVideoAbuseRetryWrapper)
|
2017-05-05 09:53:35 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-05-15 15:22:03 -05:00
|
|
|
export {
|
|
|
|
abuseVideoRouter
|
|
|
|
}
|
2017-05-05 09:53:35 -05:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
async function listVideoAbuses (req: express.Request, res: express.Response, next: express.NextFunction) {
|
|
|
|
const resultList = await db.VideoAbuse.listForApi(req.query.start, req.query.count, req.query.sort)
|
|
|
|
|
|
|
|
return res.json(getFormattedObjects(resultList.data, resultList.total))
|
2017-05-05 09:53:35 -05:00
|
|
|
}
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
async function reportVideoAbuseRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
|
2017-05-05 09:53:35 -05:00
|
|
|
const options = {
|
|
|
|
arguments: [ req, res ],
|
|
|
|
errorMessage: 'Cannot report abuse to the video with many retries.'
|
|
|
|
}
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
await retryTransactionWrapper(reportVideoAbuse, options)
|
|
|
|
|
|
|
|
return res.type('json').status(204).end()
|
2017-05-05 09:53:35 -05:00
|
|
|
}
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
async function reportVideoAbuse (req: express.Request, res: express.Response) {
|
2017-07-11 09:01:56 -05:00
|
|
|
const videoInstance = res.locals.video as VideoInstance
|
2017-11-15 08:12:23 -06:00
|
|
|
const reporterAccount = res.locals.oauth.token.User.Account
|
2017-07-10 12:43:21 -05:00
|
|
|
const body: VideoAbuseCreate = req.body
|
2017-05-05 09:53:35 -05:00
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
const abuseToCreate = {
|
2017-11-15 08:12:23 -06:00
|
|
|
reporterAccountId: reporterAccount.id,
|
2017-07-10 12:43:21 -05:00
|
|
|
reason: body.reason,
|
2017-11-15 08:12:23 -06:00
|
|
|
videoId: videoInstance.id
|
2017-05-05 09:53:35 -05:00
|
|
|
}
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
await db.sequelize.transaction(async t => {
|
2017-11-15 08:12:23 -06:00
|
|
|
const videoAbuseInstance = await db.VideoAbuse.create(abuseToCreate, { transaction: t })
|
2017-11-16 10:04:19 -06:00
|
|
|
videoAbuseInstance.Video = videoInstance
|
2017-11-15 08:12:23 -06:00
|
|
|
|
|
|
|
// We send the video abuse to the origin server
|
2017-10-25 04:55:06 -05:00
|
|
|
if (videoInstance.isOwned() === false) {
|
2017-11-15 08:12:23 -06:00
|
|
|
await sendVideoAbuse(reporterAccount, videoAbuseInstance, videoInstance, t)
|
2017-10-25 04:55:06 -05:00
|
|
|
}
|
2017-05-05 09:53:35 -05:00
|
|
|
})
|
2017-10-25 04:55:06 -05:00
|
|
|
|
|
|
|
logger.info('Abuse report for video %s created.', videoInstance.name)
|
2017-05-05 09:53:35 -05:00
|
|
|
}
|