]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/videos/abuse.ts
Move ensureRegistrationEnabled to middlewares
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / abuse.ts
CommitLineData
4d4e5cd4 1import * as express from 'express'
69818c93 2import * as Sequelize from 'sequelize'
65fcc311 3import { waterfall } from 'async'
d33242b0 4
e02643f3 5import { database as db } from '../../../initializers/database'
4d4e5cd4 6import * as friends from '../../../lib/friends'
65fcc311
C
7import {
8 logger,
9 getFormatedObjects,
10 retryTransactionWrapper,
11 startSerializableTransaction,
12 commitTransaction,
13 rollbackTransaction
14} from '../../../helpers'
15import {
16 authenticate,
17 ensureIsAdmin,
18 paginationValidator,
19 videoAbuseReportValidator,
20 videoAbusesSortValidator,
21 setVideoAbusesSort,
22 setPagination
23} from '../../../middlewares'
24
25const abuseVideoRouter = express.Router()
26
27abuseVideoRouter.get('/abuse',
28 authenticate,
29 ensureIsAdmin,
30 paginationValidator,
31 videoAbusesSortValidator,
32 setVideoAbusesSort,
33 setPagination,
d33242b0
C
34 listVideoAbuses
35)
65fcc311
C
36abuseVideoRouter.post('/:id/abuse',
37 authenticate,
38 videoAbuseReportValidator,
d33242b0
C
39 reportVideoAbuseRetryWrapper
40)
41
42// ---------------------------------------------------------------------------
43
65fcc311
C
44export {
45 abuseVideoRouter
46}
d33242b0
C
47
48// ---------------------------------------------------------------------------
49
69818c93 50function listVideoAbuses (req: express.Request, res: express.Response, next: express.NextFunction) {
d33242b0
C
51 db.VideoAbuse.listForApi(req.query.start, req.query.count, req.query.sort, function (err, abusesList, abusesTotal) {
52 if (err) return next(err)
53
65fcc311 54 res.json(getFormatedObjects(abusesList, abusesTotal))
d33242b0
C
55 })
56}
57
69818c93 58function reportVideoAbuseRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
d33242b0
C
59 const options = {
60 arguments: [ req, res ],
61 errorMessage: 'Cannot report abuse to the video with many retries.'
62 }
63
65fcc311 64 retryTransactionWrapper(reportVideoAbuse, options, function (err) {
d33242b0
C
65 if (err) return next(err)
66
67 return res.type('json').status(204).end()
68 })
69}
70
69818c93 71function reportVideoAbuse (req: express.Request, res: express.Response, finalCallback: (err: Error) => void) {
d33242b0
C
72 const videoInstance = res.locals.video
73 const reporterUsername = res.locals.oauth.token.User.username
74
75 const abuse = {
76 reporterUsername,
77 reason: req.body.reason,
78 videoId: videoInstance.id,
79 reporterPodId: null // This is our pod that reported this abuse
80 }
81
82 waterfall([
83
65fcc311 84 startSerializableTransaction,
d33242b0
C
85
86 function createAbuse (t, callback) {
87 db.VideoAbuse.create(abuse).asCallback(function (err, abuse) {
88 return callback(err, t, abuse)
89 })
90 },
91
92 function sendToFriendsIfNeeded (t, abuse, callback) {
93 // We send the information to the destination pod
94 if (videoInstance.isOwned() === false) {
95 const reportData = {
96 reporterUsername,
97 reportReason: abuse.reason,
98 videoRemoteId: videoInstance.remoteId
99 }
100
101 friends.reportAbuseVideoToFriend(reportData, videoInstance)
102 }
103
104 return callback(null, t)
105 },
106
65fcc311 107 commitTransaction
d33242b0 108
69818c93 109 ], function andFinally (err: Error, t: Sequelize.Transaction) {
d33242b0
C
110 if (err) {
111 logger.debug('Cannot update the video.', { error: err })
65fcc311 112 return rollbackTransaction(err, t, finalCallback)
d33242b0
C
113 }
114
115 logger.info('Abuse report for video %s created.', videoInstance.name)
116 return finalCallback(null)
117 })
118}