2017-06-05 21:53:49 +02:00
|
|
|
import * as express from 'express'
|
2017-05-05 16:53:35 +02:00
|
|
|
|
2017-05-22 20:58:25 +02:00
|
|
|
import { database as db } from '../../../initializers/database'
|
2017-06-05 21:53:49 +02:00
|
|
|
import * as friends from '../../../lib/friends'
|
2017-05-15 22:22:03 +02:00
|
|
|
import {
|
|
|
|
logger,
|
2017-08-25 11:45:31 +02:00
|
|
|
getFormattedObjects,
|
2017-07-05 13:26:25 +02:00
|
|
|
retryTransactionWrapper
|
2017-05-15 22:22:03 +02:00
|
|
|
} from '../../../helpers'
|
|
|
|
import {
|
|
|
|
authenticate,
|
|
|
|
ensureIsAdmin,
|
|
|
|
paginationValidator,
|
|
|
|
videoAbuseReportValidator,
|
|
|
|
videoAbusesSortValidator,
|
|
|
|
setVideoAbusesSort,
|
|
|
|
setPagination
|
|
|
|
} from '../../../middlewares'
|
2017-07-05 13:26:25 +02:00
|
|
|
import { VideoInstance } from '../../../models'
|
2017-07-10 19:43:21 +02:00
|
|
|
import { VideoAbuseCreate } from '../../../../shared'
|
2017-05-15 22:22:03 +02:00
|
|
|
|
|
|
|
const abuseVideoRouter = express.Router()
|
|
|
|
|
|
|
|
abuseVideoRouter.get('/abuse',
|
|
|
|
authenticate,
|
|
|
|
ensureIsAdmin,
|
|
|
|
paginationValidator,
|
|
|
|
videoAbusesSortValidator,
|
|
|
|
setVideoAbusesSort,
|
|
|
|
setPagination,
|
2017-05-05 16:53:35 +02:00
|
|
|
listVideoAbuses
|
|
|
|
)
|
2017-05-15 22:22:03 +02:00
|
|
|
abuseVideoRouter.post('/:id/abuse',
|
|
|
|
authenticate,
|
|
|
|
videoAbuseReportValidator,
|
2017-05-05 16:53:35 +02:00
|
|
|
reportVideoAbuseRetryWrapper
|
|
|
|
)
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-05-15 22:22:03 +02:00
|
|
|
export {
|
|
|
|
abuseVideoRouter
|
|
|
|
}
|
2017-05-05 16:53:35 +02:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-06-10 22:15:25 +02:00
|
|
|
function listVideoAbuses (req: express.Request, res: express.Response, next: express.NextFunction) {
|
2017-07-05 13:26:25 +02:00
|
|
|
db.VideoAbuse.listForApi(req.query.start, req.query.count, req.query.sort)
|
2017-08-25 11:45:31 +02:00
|
|
|
.then(result => res.json(getFormattedObjects(result.data, result.total)))
|
2017-07-05 13:26:25 +02:00
|
|
|
.catch(err => next(err))
|
2017-05-05 16:53:35 +02:00
|
|
|
}
|
|
|
|
|
2017-06-10 22:15:25 +02:00
|
|
|
function reportVideoAbuseRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
|
2017-05-05 16:53:35 +02:00
|
|
|
const options = {
|
|
|
|
arguments: [ req, res ],
|
|
|
|
errorMessage: 'Cannot report abuse to the video with many retries.'
|
|
|
|
}
|
|
|
|
|
2017-07-05 13:26:25 +02:00
|
|
|
retryTransactionWrapper(reportVideoAbuse, options)
|
|
|
|
.then(() => res.type('json').status(204).end())
|
|
|
|
.catch(err => next(err))
|
2017-05-05 16:53:35 +02:00
|
|
|
}
|
|
|
|
|
2017-07-05 13:26:25 +02:00
|
|
|
function reportVideoAbuse (req: express.Request, res: express.Response) {
|
2017-07-11 16:01:56 +02:00
|
|
|
const videoInstance = res.locals.video as VideoInstance
|
2017-05-05 16:53:35 +02:00
|
|
|
const reporterUsername = res.locals.oauth.token.User.username
|
2017-07-10 19:43:21 +02:00
|
|
|
const body: VideoAbuseCreate = req.body
|
2017-05-05 16:53:35 +02:00
|
|
|
|
|
|
|
const abuse = {
|
|
|
|
reporterUsername,
|
2017-07-10 19:43:21 +02:00
|
|
|
reason: body.reason,
|
2017-05-05 16:53:35 +02:00
|
|
|
videoId: videoInstance.id,
|
|
|
|
reporterPodId: null // This is our pod that reported this abuse
|
|
|
|
}
|
|
|
|
|
2017-07-05 13:26:25 +02:00
|
|
|
return db.sequelize.transaction(t => {
|
|
|
|
return db.VideoAbuse.create(abuse, { transaction: t })
|
|
|
|
.then(abuse => {
|
|
|
|
// We send the information to the destination pod
|
|
|
|
if (videoInstance.isOwned() === false) {
|
|
|
|
const reportData = {
|
|
|
|
reporterUsername,
|
|
|
|
reportReason: abuse.reason,
|
2017-07-11 16:01:56 +02:00
|
|
|
videoUUID: videoInstance.uuid
|
2017-07-05 13:26:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return friends.reportAbuseVideoToFriend(reportData, videoInstance, t).then(() => videoInstance)
|
2017-05-05 16:53:35 +02:00
|
|
|
}
|
|
|
|
|
2017-07-05 13:26:25 +02:00
|
|
|
return videoInstance
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.then((videoInstance: VideoInstance) => logger.info('Abuse report for video %s created.', videoInstance.name))
|
|
|
|
.catch(err => {
|
2017-07-07 18:26:12 +02:00
|
|
|
logger.debug('Cannot update the video.', err)
|
2017-07-05 13:26:25 +02:00
|
|
|
throw err
|
2017-05-05 16:53:35 +02:00
|
|
|
})
|
|
|
|
}
|