]>
Commit | Line | Data |
---|---|---|
4d4e5cd4 | 1 | import * as express from 'express' |
65fcc311 C |
2 | import { |
3 | logger, | |
0aef76c4 | 4 | getFormattedObjects, |
6fcd19ba | 5 | retryTransactionWrapper |
65fcc311 | 6 | } from '../../../helpers' |
3fd3ab2d | 7 | import { sequelizeTypescript } from '../../../initializers' |
65fcc311 C |
8 | import { |
9 | authenticate, | |
954605a8 | 10 | ensureUserHasRight, |
65fcc311 C |
11 | paginationValidator, |
12 | videoAbuseReportValidator, | |
13 | videoAbusesSortValidator, | |
14 | setVideoAbusesSort, | |
eb080476 C |
15 | setPagination, |
16 | asyncMiddleware | |
65fcc311 | 17 | } from '../../../middlewares' |
954605a8 | 18 | import { VideoAbuseCreate, UserRight } from '../../../../shared' |
8e13fa7d | 19 | import { sendVideoAbuse } from '../../../lib/index' |
3fd3ab2d C |
20 | import { AccountModel } from '../../../models/account/account' |
21 | import { VideoModel } from '../../../models/video/video' | |
22 | import { VideoAbuseModel } from '../../../models/video/video-abuse' | |
65fcc311 C |
23 | |
24 | const abuseVideoRouter = express.Router() | |
25 | ||
26 | abuseVideoRouter.get('/abuse', | |
27 | authenticate, | |
954605a8 | 28 | ensureUserHasRight(UserRight.MANAGE_VIDEO_ABUSES), |
65fcc311 C |
29 | paginationValidator, |
30 | videoAbusesSortValidator, | |
31 | setVideoAbusesSort, | |
32 | setPagination, | |
eb080476 | 33 | asyncMiddleware(listVideoAbuses) |
d33242b0 | 34 | ) |
65fcc311 C |
35 | abuseVideoRouter.post('/:id/abuse', |
36 | authenticate, | |
a2431b7d | 37 | asyncMiddleware(videoAbuseReportValidator), |
eb080476 | 38 | asyncMiddleware(reportVideoAbuseRetryWrapper) |
d33242b0 C |
39 | ) |
40 | ||
41 | // --------------------------------------------------------------------------- | |
42 | ||
65fcc311 C |
43 | export { |
44 | abuseVideoRouter | |
45 | } | |
d33242b0 C |
46 | |
47 | // --------------------------------------------------------------------------- | |
48 | ||
eb080476 | 49 | async function listVideoAbuses (req: express.Request, res: express.Response, next: express.NextFunction) { |
3fd3ab2d | 50 | const resultList = await VideoAbuseModel.listForApi(req.query.start, req.query.count, req.query.sort) |
eb080476 C |
51 | |
52 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
d33242b0 C |
53 | } |
54 | ||
eb080476 | 55 | async function reportVideoAbuseRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) { |
d33242b0 C |
56 | const options = { |
57 | arguments: [ req, res ], | |
58 | errorMessage: 'Cannot report abuse to the video with many retries.' | |
59 | } | |
60 | ||
eb080476 C |
61 | await retryTransactionWrapper(reportVideoAbuse, options) |
62 | ||
63 | return res.type('json').status(204).end() | |
d33242b0 C |
64 | } |
65 | ||
eb080476 | 66 | async function reportVideoAbuse (req: express.Request, res: express.Response) { |
3fd3ab2d C |
67 | const videoInstance = res.locals.video as VideoModel |
68 | const reporterAccount = res.locals.oauth.token.User.Account as AccountModel | |
4771e000 | 69 | const body: VideoAbuseCreate = req.body |
d33242b0 | 70 | |
eb080476 | 71 | const abuseToCreate = { |
8e13fa7d | 72 | reporterAccountId: reporterAccount.id, |
4771e000 | 73 | reason: body.reason, |
8e13fa7d | 74 | videoId: videoInstance.id |
d33242b0 C |
75 | } |
76 | ||
3fd3ab2d C |
77 | await sequelizeTypescript.transaction(async t => { |
78 | const videoAbuseInstance = await VideoAbuseModel.create(abuseToCreate, { transaction: t }) | |
21e0727a | 79 | videoAbuseInstance.Video = videoInstance |
8e13fa7d C |
80 | |
81 | // We send the video abuse to the origin server | |
eb080476 | 82 | if (videoInstance.isOwned() === false) { |
8e13fa7d | 83 | await sendVideoAbuse(reporterAccount, videoAbuseInstance, videoInstance, t) |
eb080476 | 84 | } |
d33242b0 | 85 | }) |
eb080476 C |
86 | |
87 | logger.info('Abuse report for video %s created.', videoInstance.name) | |
d33242b0 | 88 | } |