]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/videos/abuse.ts
c9313d5f5b680fb2b42326a266342930cc63c32b
[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 ensureIsAdmin,
13 paginationValidator,
14 videoAbuseReportValidator,
15 videoAbusesSortValidator,
16 setVideoAbusesSort,
17 setPagination
18 } from '../../../middlewares'
19 import { VideoInstance } from '../../../models'
20 import { VideoAbuseCreate } from '../../../../shared'
21
22 const abuseVideoRouter = express.Router()
23
24 abuseVideoRouter.get('/abuse',
25 authenticate,
26 ensureIsAdmin,
27 paginationValidator,
28 videoAbusesSortValidator,
29 setVideoAbusesSort,
30 setPagination,
31 listVideoAbuses
32 )
33 abuseVideoRouter.post('/:id/abuse',
34 authenticate,
35 videoAbuseReportValidator,
36 reportVideoAbuseRetryWrapper
37 )
38
39 // ---------------------------------------------------------------------------
40
41 export {
42 abuseVideoRouter
43 }
44
45 // ---------------------------------------------------------------------------
46
47 function listVideoAbuses (req: express.Request, res: express.Response, next: express.NextFunction) {
48 db.VideoAbuse.listForApi(req.query.start, req.query.count, req.query.sort)
49 .then(result => res.json(getFormattedObjects(result.data, result.total)))
50 .catch(err => next(err))
51 }
52
53 function reportVideoAbuseRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
54 const options = {
55 arguments: [ req, res ],
56 errorMessage: 'Cannot report abuse to the video with many retries.'
57 }
58
59 retryTransactionWrapper(reportVideoAbuse, options)
60 .then(() => res.type('json').status(204).end())
61 .catch(err => next(err))
62 }
63
64 function reportVideoAbuse (req: express.Request, res: express.Response) {
65 const videoInstance = res.locals.video as VideoInstance
66 const reporterUsername = res.locals.oauth.token.User.username
67 const body: VideoAbuseCreate = req.body
68
69 const abuse = {
70 reporterUsername,
71 reason: body.reason,
72 videoId: videoInstance.id,
73 reporterPodId: null // This is our pod that reported this abuse
74 }
75
76 return db.sequelize.transaction(t => {
77 return db.VideoAbuse.create(abuse, { transaction: t })
78 .then(abuse => {
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 return friends.reportAbuseVideoToFriend(reportData, videoInstance, t).then(() => videoInstance)
88 }
89
90 return videoInstance
91 })
92 })
93 .then((videoInstance: VideoInstance) => logger.info('Abuse report for video %s created.', videoInstance.name))
94 .catch(err => {
95 logger.debug('Cannot update the video.', err)
96 throw err
97 })
98 }