PeerTube/server/controllers/api/videos/abuse.ts

85 lines
2.8 KiB
TypeScript
Raw Normal View History

2017-06-05 21:53:49 +02:00
import * as express from 'express'
2017-12-14 17:38:41 +01:00
import { UserRight, VideoAbuseCreate } from '../../../../shared'
import { getFormattedObjects, logger, retryTransactionWrapper } from '../../../helpers'
2017-12-12 17:53:50 +01:00
import { sequelizeTypescript } from '../../../initializers'
2017-12-14 17:38:41 +01:00
import { sendVideoAbuse } from '../../../lib/activitypub/send'
2017-05-15 22:22:03 +02:00
import {
2017-12-14 17:38:41 +01:00
asyncMiddleware,
2017-05-15 22:22:03 +02:00
authenticate,
ensureUserHasRight,
2017-05-15 22:22:03 +02:00
paginationValidator,
2017-10-25 11:55:06 +02:00
setPagination,
2017-12-14 17:38:41 +01:00
setVideoAbusesSort,
videoAbuseReportValidator,
videoAbusesSortValidator
2017-05-15 22:22:03 +02:00
} from '../../../middlewares'
2017-12-12 17:53:50 +01:00
import { AccountModel } from '../../../models/account/account'
import { VideoModel } from '../../../models/video/video'
import { VideoAbuseModel } from '../../../models/video/video-abuse'
2017-05-15 22:22:03 +02:00
const abuseVideoRouter = express.Router()
abuseVideoRouter.get('/abuse',
authenticate,
ensureUserHasRight(UserRight.MANAGE_VIDEO_ABUSES),
2017-05-15 22:22:03 +02:00
paginationValidator,
videoAbusesSortValidator,
setVideoAbusesSort,
setPagination,
2017-10-25 11:55:06 +02:00
asyncMiddleware(listVideoAbuses)
2017-05-05 16:53:35 +02:00
)
2017-05-15 22:22:03 +02:00
abuseVideoRouter.post('/:id/abuse',
authenticate,
2017-11-27 17:30:46 +01:00
asyncMiddleware(videoAbuseReportValidator),
2017-10-25 11:55:06 +02:00
asyncMiddleware(reportVideoAbuseRetryWrapper)
2017-05-05 16:53:35 +02:00
)
// ---------------------------------------------------------------------------
2017-05-15 22:22:03 +02:00
export {
abuseVideoRouter
}
2017-05-05 16:53:35 +02:00
// ---------------------------------------------------------------------------
2017-10-25 11:55:06 +02:00
async function listVideoAbuses (req: express.Request, res: express.Response, next: express.NextFunction) {
2017-12-12 17:53:50 +01:00
const resultList = await VideoAbuseModel.listForApi(req.query.start, req.query.count, req.query.sort)
2017-10-25 11:55:06 +02:00
return res.json(getFormattedObjects(resultList.data, resultList.total))
2017-05-05 16:53:35 +02:00
}
2017-10-25 11:55:06 +02:00
async 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-10-25 11:55:06 +02:00
await retryTransactionWrapper(reportVideoAbuse, options)
return res.type('json').status(204).end()
2017-05-05 16:53:35 +02:00
}
2017-10-25 11:55:06 +02:00
async function reportVideoAbuse (req: express.Request, res: express.Response) {
2017-12-12 17:53:50 +01:00
const videoInstance = res.locals.video as VideoModel
const reporterAccount = res.locals.oauth.token.User.Account as AccountModel
const body: VideoAbuseCreate = req.body
2017-05-05 16:53:35 +02:00
2017-10-25 11:55:06 +02:00
const abuseToCreate = {
2017-11-15 15:12:23 +01:00
reporterAccountId: reporterAccount.id,
reason: body.reason,
2017-11-15 15:12:23 +01:00
videoId: videoInstance.id
2017-05-05 16:53:35 +02:00
}
2017-12-12 17:53:50 +01:00
await sequelizeTypescript.transaction(async t => {
const videoAbuseInstance = await VideoAbuseModel.create(abuseToCreate, { transaction: t })
2017-11-16 17:04:19 +01:00
videoAbuseInstance.Video = videoInstance
2017-11-15 15:12:23 +01:00
// We send the video abuse to the origin server
2017-10-25 11:55:06 +02:00
if (videoInstance.isOwned() === false) {
2017-12-14 17:38:41 +01:00
await sendVideoAbuse(reporterAccount.Actor, videoAbuseInstance, videoInstance, t)
2017-10-25 11:55:06 +02:00
}
2017-05-05 16:53:35 +02:00
})
2017-10-25 11:55:06 +02:00
logger.info('Abuse report for video %s created.', videoInstance.name)
2017-05-05 16:53:35 +02:00
}