]>
Commit | Line | Data |
---|---|---|
4d4e5cd4 | 1 | import * as express from 'express' |
d33242b0 | 2 | |
e02643f3 | 3 | import { database as db } from '../../../initializers/database' |
4d4e5cd4 | 4 | import * as friends from '../../../lib/friends' |
65fcc311 C |
5 | import { |
6 | logger, | |
0aef76c4 | 7 | getFormattedObjects, |
6fcd19ba | 8 | retryTransactionWrapper |
65fcc311 C |
9 | } from '../../../helpers' |
10 | import { | |
11 | authenticate, | |
12 | ensureIsAdmin, | |
13 | paginationValidator, | |
14 | videoAbuseReportValidator, | |
15 | videoAbusesSortValidator, | |
16 | setVideoAbusesSort, | |
17 | setPagination | |
18 | } from '../../../middlewares' | |
6fcd19ba | 19 | import { VideoInstance } from '../../../models' |
4771e000 | 20 | import { VideoAbuseCreate } from '../../../../shared' |
65fcc311 C |
21 | |
22 | const abuseVideoRouter = express.Router() | |
23 | ||
24 | abuseVideoRouter.get('/abuse', | |
25 | authenticate, | |
26 | ensureIsAdmin, | |
27 | paginationValidator, | |
28 | videoAbusesSortValidator, | |
29 | setVideoAbusesSort, | |
30 | setPagination, | |
d33242b0 C |
31 | listVideoAbuses |
32 | ) | |
65fcc311 C |
33 | abuseVideoRouter.post('/:id/abuse', |
34 | authenticate, | |
35 | videoAbuseReportValidator, | |
d33242b0 C |
36 | reportVideoAbuseRetryWrapper |
37 | ) | |
38 | ||
39 | // --------------------------------------------------------------------------- | |
40 | ||
65fcc311 C |
41 | export { |
42 | abuseVideoRouter | |
43 | } | |
d33242b0 C |
44 | |
45 | // --------------------------------------------------------------------------- | |
46 | ||
69818c93 | 47 | function listVideoAbuses (req: express.Request, res: express.Response, next: express.NextFunction) { |
6fcd19ba | 48 | db.VideoAbuse.listForApi(req.query.start, req.query.count, req.query.sort) |
0aef76c4 | 49 | .then(result => res.json(getFormattedObjects(result.data, result.total))) |
6fcd19ba | 50 | .catch(err => next(err)) |
d33242b0 C |
51 | } |
52 | ||
69818c93 | 53 | function reportVideoAbuseRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) { |
d33242b0 C |
54 | const options = { |
55 | arguments: [ req, res ], | |
56 | errorMessage: 'Cannot report abuse to the video with many retries.' | |
57 | } | |
58 | ||
6fcd19ba C |
59 | retryTransactionWrapper(reportVideoAbuse, options) |
60 | .then(() => res.type('json').status(204).end()) | |
61 | .catch(err => next(err)) | |
d33242b0 C |
62 | } |
63 | ||
6fcd19ba | 64 | function reportVideoAbuse (req: express.Request, res: express.Response) { |
0a6658fd | 65 | const videoInstance = res.locals.video as VideoInstance |
d33242b0 | 66 | const reporterUsername = res.locals.oauth.token.User.username |
4771e000 | 67 | const body: VideoAbuseCreate = req.body |
d33242b0 C |
68 | |
69 | const abuse = { | |
70 | reporterUsername, | |
4771e000 | 71 | reason: body.reason, |
d33242b0 C |
72 | videoId: videoInstance.id, |
73 | reporterPodId: null // This is our pod that reported this abuse | |
74 | } | |
75 | ||
6fcd19ba C |
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, | |
0a6658fd | 84 | videoUUID: videoInstance.uuid |
6fcd19ba C |
85 | } |
86 | ||
87 | return friends.reportAbuseVideoToFriend(reportData, videoInstance, t).then(() => videoInstance) | |
d33242b0 C |
88 | } |
89 | ||
6fcd19ba C |
90 | return videoInstance |
91 | }) | |
92 | }) | |
93 | .then((videoInstance: VideoInstance) => logger.info('Abuse report for video %s created.', videoInstance.name)) | |
94 | .catch(err => { | |
ad0997ad | 95 | logger.debug('Cannot update the video.', err) |
6fcd19ba | 96 | throw err |
d33242b0 C |
97 | }) |
98 | } |