]>
Commit | Line | Data |
---|---|---|
4d4e5cd4 | 1 | import * as express from 'express' |
d33242b0 | 2 | |
e02643f3 | 3 | import { database as db } from '../../../initializers/database' |
65fcc311 C |
4 | import { |
5 | logger, | |
0aef76c4 | 6 | getFormattedObjects, |
6fcd19ba | 7 | retryTransactionWrapper |
65fcc311 C |
8 | } from '../../../helpers' |
9 | import { | |
10 | authenticate, | |
954605a8 | 11 | ensureUserHasRight, |
65fcc311 C |
12 | paginationValidator, |
13 | videoAbuseReportValidator, | |
14 | videoAbusesSortValidator, | |
15 | setVideoAbusesSort, | |
eb080476 C |
16 | setPagination, |
17 | asyncMiddleware | |
65fcc311 | 18 | } from '../../../middlewares' |
6fcd19ba | 19 | import { VideoInstance } from '../../../models' |
954605a8 | 20 | import { VideoAbuseCreate, UserRight } from '../../../../shared' |
65fcc311 C |
21 | |
22 | const abuseVideoRouter = express.Router() | |
23 | ||
24 | abuseVideoRouter.get('/abuse', | |
25 | authenticate, | |
954605a8 | 26 | ensureUserHasRight(UserRight.MANAGE_VIDEO_ABUSES), |
65fcc311 C |
27 | paginationValidator, |
28 | videoAbusesSortValidator, | |
29 | setVideoAbusesSort, | |
30 | setPagination, | |
eb080476 | 31 | asyncMiddleware(listVideoAbuses) |
d33242b0 | 32 | ) |
65fcc311 C |
33 | abuseVideoRouter.post('/:id/abuse', |
34 | authenticate, | |
35 | videoAbuseReportValidator, | |
eb080476 | 36 | asyncMiddleware(reportVideoAbuseRetryWrapper) |
d33242b0 C |
37 | ) |
38 | ||
39 | // --------------------------------------------------------------------------- | |
40 | ||
65fcc311 C |
41 | export { |
42 | abuseVideoRouter | |
43 | } | |
d33242b0 C |
44 | |
45 | // --------------------------------------------------------------------------- | |
46 | ||
eb080476 C |
47 | async function listVideoAbuses (req: express.Request, res: express.Response, next: express.NextFunction) { |
48 | const resultList = await db.VideoAbuse.listForApi(req.query.start, req.query.count, req.query.sort) | |
49 | ||
50 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
d33242b0 C |
51 | } |
52 | ||
eb080476 | 53 | async 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 | ||
eb080476 C |
59 | await retryTransactionWrapper(reportVideoAbuse, options) |
60 | ||
61 | return res.type('json').status(204).end() | |
d33242b0 C |
62 | } |
63 | ||
eb080476 | 64 | async 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 | 68 | |
eb080476 | 69 | const abuseToCreate = { |
d33242b0 | 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 | ||
eb080476 C |
76 | await db.sequelize.transaction(async t => { |
77 | const abuse = await db.VideoAbuse.create(abuseToCreate, { transaction: t }) | |
78 | // We send the information to the destination pod | |
79 | if (videoInstance.isOwned() === false) { | |
80 | const reportData = { | |
81 | reporterUsername, | |
82 | reportReason: abuse.reason, | |
83 | videoUUID: videoInstance.uuid | |
84 | } | |
85 | ||
571389d4 C |
86 | // await friends.reportAbuseVideoToFriend(reportData, videoInstance, t) |
87 | // TODO: send abuse to origin pod | |
eb080476 | 88 | } |
d33242b0 | 89 | }) |
eb080476 C |
90 | |
91 | logger.info('Abuse report for video %s created.', videoInstance.name) | |
d33242b0 | 92 | } |