]>
Commit | Line | Data |
---|---|---|
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 | getFormatedObjects, | |
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 | ||
21 | const abuseVideoRouter = express.Router() | |
22 | ||
23 | abuseVideoRouter.get('/abuse', | |
24 | authenticate, | |
25 | ensureIsAdmin, | |
26 | paginationValidator, | |
27 | videoAbusesSortValidator, | |
28 | setVideoAbusesSort, | |
29 | setPagination, | |
30 | listVideoAbuses | |
31 | ) | |
32 | abuseVideoRouter.post('/:id/abuse', | |
33 | authenticate, | |
34 | videoAbuseReportValidator, | |
35 | reportVideoAbuseRetryWrapper | |
36 | ) | |
37 | ||
38 | // --------------------------------------------------------------------------- | |
39 | ||
40 | export { | |
41 | abuseVideoRouter | |
42 | } | |
43 | ||
44 | // --------------------------------------------------------------------------- | |
45 | ||
46 | function listVideoAbuses (req: express.Request, res: express.Response, next: express.NextFunction) { | |
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)) | |
50 | } | |
51 | ||
52 | function reportVideoAbuseRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) { | |
53 | const options = { | |
54 | arguments: [ req, res ], | |
55 | errorMessage: 'Cannot report abuse to the video with many retries.' | |
56 | } | |
57 | ||
58 | retryTransactionWrapper(reportVideoAbuse, options) | |
59 | .then(() => res.type('json').status(204).end()) | |
60 | .catch(err => next(err)) | |
61 | } | |
62 | ||
63 | function reportVideoAbuse (req: express.Request, res: express.Response) { | |
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 | ||
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) | |
86 | } | |
87 | ||
88 | return videoInstance | |
89 | }) | |
90 | }) | |
91 | .then((videoInstance: VideoInstance) => logger.info('Abuse report for video %s created.', videoInstance.name)) | |
92 | .catch(err => { | |
93 | logger.debug('Cannot update the video.', err) | |
94 | throw err | |
95 | }) | |
96 | } |