]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/controllers/api/videos/abuse.ts
Fix lint
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / abuse.ts
... / ...
CommitLineData
1import * as express from 'express'
2
3import { database as db } from '../../../initializers/database'
4import * as friends from '../../../lib/friends'
5import {
6 logger,
7 getFormattedObjects,
8 retryTransactionWrapper
9} from '../../../helpers'
10import {
11 authenticate,
12 ensureIsAdmin,
13 paginationValidator,
14 videoAbuseReportValidator,
15 videoAbusesSortValidator,
16 setVideoAbusesSort,
17 setPagination,
18 asyncMiddleware
19} from '../../../middlewares'
20import { VideoInstance } from '../../../models'
21import { VideoAbuseCreate } from '../../../../shared'
22
23const abuseVideoRouter = express.Router()
24
25abuseVideoRouter.get('/abuse',
26 authenticate,
27 ensureIsAdmin,
28 paginationValidator,
29 videoAbusesSortValidator,
30 setVideoAbusesSort,
31 setPagination,
32 asyncMiddleware(listVideoAbuses)
33)
34abuseVideoRouter.post('/:id/abuse',
35 authenticate,
36 videoAbuseReportValidator,
37 asyncMiddleware(reportVideoAbuseRetryWrapper)
38)
39
40// ---------------------------------------------------------------------------
41
42export {
43 abuseVideoRouter
44}
45
46// ---------------------------------------------------------------------------
47
48async 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
54async 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
65async 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}