]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/videos/abuse.ts
Fix lint
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / abuse.ts
CommitLineData
4d4e5cd4 1import * as express from 'express'
d33242b0 2
e02643f3 3import { database as db } from '../../../initializers/database'
4d4e5cd4 4import * as friends from '../../../lib/friends'
65fcc311
C
5import {
6 logger,
0aef76c4 7 getFormattedObjects,
6fcd19ba 8 retryTransactionWrapper
65fcc311
C
9} from '../../../helpers'
10import {
11 authenticate,
12 ensureIsAdmin,
13 paginationValidator,
14 videoAbuseReportValidator,
15 videoAbusesSortValidator,
16 setVideoAbusesSort,
eb080476
C
17 setPagination,
18 asyncMiddleware
65fcc311 19} from '../../../middlewares'
6fcd19ba 20import { VideoInstance } from '../../../models'
4771e000 21import { VideoAbuseCreate } from '../../../../shared'
65fcc311
C
22
23const abuseVideoRouter = express.Router()
24
25abuseVideoRouter.get('/abuse',
26 authenticate,
27 ensureIsAdmin,
28 paginationValidator,
29 videoAbusesSortValidator,
30 setVideoAbusesSort,
31 setPagination,
eb080476 32 asyncMiddleware(listVideoAbuses)
d33242b0 33)
65fcc311
C
34abuseVideoRouter.post('/:id/abuse',
35 authenticate,
36 videoAbuseReportValidator,
eb080476 37 asyncMiddleware(reportVideoAbuseRetryWrapper)
d33242b0
C
38)
39
40// ---------------------------------------------------------------------------
41
65fcc311
C
42export {
43 abuseVideoRouter
44}
d33242b0
C
45
46// ---------------------------------------------------------------------------
47
eb080476
C
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))
d33242b0
C
52}
53
eb080476 54async function reportVideoAbuseRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
d33242b0
C
55 const options = {
56 arguments: [ req, res ],
57 errorMessage: 'Cannot report abuse to the video with many retries.'
58 }
59
eb080476
C
60 await retryTransactionWrapper(reportVideoAbuse, options)
61
62 return res.type('json').status(204).end()
d33242b0
C
63}
64
eb080476 65async function reportVideoAbuse (req: express.Request, res: express.Response) {
0a6658fd 66 const videoInstance = res.locals.video as VideoInstance
d33242b0 67 const reporterUsername = res.locals.oauth.token.User.username
4771e000 68 const body: VideoAbuseCreate = req.body
d33242b0 69
eb080476 70 const abuseToCreate = {
d33242b0 71 reporterUsername,
4771e000 72 reason: body.reason,
d33242b0
C
73 videoId: videoInstance.id,
74 reporterPodId: null // This is our pod that reported this abuse
75 }
76
eb080476
C
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 }
d33242b0 89 })
eb080476
C
90
91 logger.info('Abuse report for video %s created.', videoInstance.name)
d33242b0 92}