]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/videos/abuse.ts
Add dirty migration :/
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / abuse.ts
CommitLineData
4d4e5cd4 1import * as express from 'express'
50d6de9c
C
2import { UserRight, VideoAbuseCreate } from '../../../../shared'
3import { getFormattedObjects, logger, retryTransactionWrapper } from '../../../helpers'
3fd3ab2d 4import { sequelizeTypescript } from '../../../initializers'
50d6de9c 5import { sendVideoAbuse } from '../../../lib/activitypub/send'
65fcc311 6import {
50d6de9c 7 asyncMiddleware,
65fcc311 8 authenticate,
954605a8 9 ensureUserHasRight,
65fcc311 10 paginationValidator,
eb080476 11 setPagination,
50d6de9c
C
12 setVideoAbusesSort,
13 videoAbuseReportValidator,
14 videoAbusesSortValidator
65fcc311 15} from '../../../middlewares'
3fd3ab2d
C
16import { AccountModel } from '../../../models/account/account'
17import { VideoModel } from '../../../models/video/video'
18import { VideoAbuseModel } from '../../../models/video/video-abuse'
65fcc311
C
19
20const abuseVideoRouter = express.Router()
21
22abuseVideoRouter.get('/abuse',
23 authenticate,
954605a8 24 ensureUserHasRight(UserRight.MANAGE_VIDEO_ABUSES),
65fcc311
C
25 paginationValidator,
26 videoAbusesSortValidator,
27 setVideoAbusesSort,
28 setPagination,
eb080476 29 asyncMiddleware(listVideoAbuses)
d33242b0 30)
65fcc311
C
31abuseVideoRouter.post('/:id/abuse',
32 authenticate,
a2431b7d 33 asyncMiddleware(videoAbuseReportValidator),
eb080476 34 asyncMiddleware(reportVideoAbuseRetryWrapper)
d33242b0
C
35)
36
37// ---------------------------------------------------------------------------
38
65fcc311
C
39export {
40 abuseVideoRouter
41}
d33242b0
C
42
43// ---------------------------------------------------------------------------
44
eb080476 45async function listVideoAbuses (req: express.Request, res: express.Response, next: express.NextFunction) {
3fd3ab2d 46 const resultList = await VideoAbuseModel.listForApi(req.query.start, req.query.count, req.query.sort)
eb080476
C
47
48 return res.json(getFormattedObjects(resultList.data, resultList.total))
d33242b0
C
49}
50
eb080476 51async function reportVideoAbuseRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
d33242b0
C
52 const options = {
53 arguments: [ req, res ],
54 errorMessage: 'Cannot report abuse to the video with many retries.'
55 }
56
eb080476
C
57 await retryTransactionWrapper(reportVideoAbuse, options)
58
59 return res.type('json').status(204).end()
d33242b0
C
60}
61
eb080476 62async function reportVideoAbuse (req: express.Request, res: express.Response) {
3fd3ab2d
C
63 const videoInstance = res.locals.video as VideoModel
64 const reporterAccount = res.locals.oauth.token.User.Account as AccountModel
4771e000 65 const body: VideoAbuseCreate = req.body
d33242b0 66
eb080476 67 const abuseToCreate = {
8e13fa7d 68 reporterAccountId: reporterAccount.id,
4771e000 69 reason: body.reason,
8e13fa7d 70 videoId: videoInstance.id
d33242b0
C
71 }
72
3fd3ab2d
C
73 await sequelizeTypescript.transaction(async t => {
74 const videoAbuseInstance = await VideoAbuseModel.create(abuseToCreate, { transaction: t })
21e0727a 75 videoAbuseInstance.Video = videoInstance
8e13fa7d
C
76
77 // We send the video abuse to the origin server
eb080476 78 if (videoInstance.isOwned() === false) {
50d6de9c 79 await sendVideoAbuse(reporterAccount.Actor, videoAbuseInstance, videoInstance, t)
eb080476 80 }
d33242b0 81 })
eb080476
C
82
83 logger.info('Abuse report for video %s created.', videoInstance.name)
d33242b0 84}