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