Begin moving video channel to actor
[oweals/peertube.git] / server / controllers / api / videos / abuse.ts
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'
6 import {
7   asyncMiddleware,
8   authenticate,
9   ensureUserHasRight,
10   paginationValidator,
11   setPagination,
12   setVideoAbusesSort,
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'
19
20 const abuseVideoRouter = express.Router()
21
22 abuseVideoRouter.get('/abuse',
23   authenticate,
24   ensureUserHasRight(UserRight.MANAGE_VIDEO_ABUSES),
25   paginationValidator,
26   videoAbusesSortValidator,
27   setVideoAbusesSort,
28   setPagination,
29   asyncMiddleware(listVideoAbuses)
30 )
31 abuseVideoRouter.post('/:id/abuse',
32   authenticate,
33   asyncMiddleware(videoAbuseReportValidator),
34   asyncMiddleware(reportVideoAbuseRetryWrapper)
35 )
36
37 // ---------------------------------------------------------------------------
38
39 export {
40   abuseVideoRouter
41 }
42
43 // ---------------------------------------------------------------------------
44
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)
47
48   return res.json(getFormattedObjects(resultList.data, resultList.total))
49 }
50
51 async function reportVideoAbuseRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
52   const options = {
53     arguments: [ req, res ],
54     errorMessage: 'Cannot report abuse to the video with many retries.'
55   }
56
57   await retryTransactionWrapper(reportVideoAbuse, options)
58
59   return res.type('json').status(204).end()
60 }
61
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
66
67   const abuseToCreate = {
68     reporterAccountId: reporterAccount.id,
69     reason: body.reason,
70     videoId: videoInstance.id
71   }
72
73   await sequelizeTypescript.transaction(async t => {
74     const videoAbuseInstance = await VideoAbuseModel.create(abuseToCreate, { transaction: t })
75     videoAbuseInstance.Video = videoInstance
76
77     // We send the video abuse to the origin server
78     if (videoInstance.isOwned() === false) {
79       await sendVideoAbuse(reporterAccount.Actor, videoAbuseInstance, videoInstance, t)
80     }
81   })
82
83   logger.info('Abuse report for video %s created.', videoInstance.name)
84 }