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