]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/videos/abuse.ts
Begin activitypub
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / abuse.ts
1 import * as express from 'express'
2
3 import { database as db } from '../../../initializers/database'
4 import * as friends from '../../../lib/friends'
5 import {
6 logger,
7 getFormattedObjects,
8 retryTransactionWrapper
9 } from '../../../helpers'
10 import {
11 authenticate,
12 ensureUserHasRight,
13 paginationValidator,
14 videoAbuseReportValidator,
15 videoAbusesSortValidator,
16 setVideoAbusesSort,
17 setPagination,
18 asyncMiddleware
19 } from '../../../middlewares'
20 import { VideoInstance } from '../../../models'
21 import { VideoAbuseCreate, UserRight } from '../../../../shared'
22
23 const abuseVideoRouter = express.Router()
24
25 abuseVideoRouter.get('/abuse',
26 authenticate,
27 ensureUserHasRight(UserRight.MANAGE_VIDEO_ABUSES),
28 paginationValidator,
29 videoAbusesSortValidator,
30 setVideoAbusesSort,
31 setPagination,
32 asyncMiddleware(listVideoAbuses)
33 )
34 abuseVideoRouter.post('/:id/abuse',
35 authenticate,
36 videoAbuseReportValidator,
37 asyncMiddleware(reportVideoAbuseRetryWrapper)
38 )
39
40 // ---------------------------------------------------------------------------
41
42 export {
43 abuseVideoRouter
44 }
45
46 // ---------------------------------------------------------------------------
47
48 async function listVideoAbuses (req: express.Request, res: express.Response, next: express.NextFunction) {
49 const resultList = await db.VideoAbuse.listForApi(req.query.start, req.query.count, req.query.sort)
50
51 return res.json(getFormattedObjects(resultList.data, resultList.total))
52 }
53
54 async function reportVideoAbuseRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
55 const options = {
56 arguments: [ req, res ],
57 errorMessage: 'Cannot report abuse to the video with many retries.'
58 }
59
60 await retryTransactionWrapper(reportVideoAbuse, options)
61
62 return res.type('json').status(204).end()
63 }
64
65 async function reportVideoAbuse (req: express.Request, res: express.Response) {
66 const videoInstance = res.locals.video as VideoInstance
67 const reporterUsername = res.locals.oauth.token.User.username
68 const body: VideoAbuseCreate = req.body
69
70 const abuseToCreate = {
71 reporterUsername,
72 reason: body.reason,
73 videoId: videoInstance.id,
74 reporterPodId: null // This is our pod that reported this abuse
75 }
76
77 await db.sequelize.transaction(async t => {
78 const abuse = await db.VideoAbuse.create(abuseToCreate, { transaction: t })
79 // We send the information to the destination pod
80 if (videoInstance.isOwned() === false) {
81 const reportData = {
82 reporterUsername,
83 reportReason: abuse.reason,
84 videoUUID: videoInstance.uuid
85 }
86
87 await friends.reportAbuseVideoToFriend(reportData, videoInstance, t)
88 }
89 })
90
91 logger.info('Abuse report for video %s created.', videoInstance.name)
92 }