diff options
Diffstat (limited to 'server/controllers/api/videos/abuse.ts')
-rw-r--r-- | server/controllers/api/videos/abuse.ts | 62 |
1 files changed, 28 insertions, 34 deletions
diff --git a/server/controllers/api/videos/abuse.ts b/server/controllers/api/videos/abuse.ts index c9313d5f5..4c7abf395 100644 --- a/server/controllers/api/videos/abuse.ts +++ b/server/controllers/api/videos/abuse.ts | |||
@@ -14,7 +14,8 @@ import { | |||
14 | videoAbuseReportValidator, | 14 | videoAbuseReportValidator, |
15 | videoAbusesSortValidator, | 15 | videoAbusesSortValidator, |
16 | setVideoAbusesSort, | 16 | setVideoAbusesSort, |
17 | setPagination | 17 | setPagination, |
18 | asyncMiddleware | ||
18 | } from '../../../middlewares' | 19 | } from '../../../middlewares' |
19 | import { VideoInstance } from '../../../models' | 20 | import { VideoInstance } from '../../../models' |
20 | import { VideoAbuseCreate } from '../../../../shared' | 21 | import { VideoAbuseCreate } from '../../../../shared' |
@@ -28,12 +29,12 @@ abuseVideoRouter.get('/abuse', | |||
28 | videoAbusesSortValidator, | 29 | videoAbusesSortValidator, |
29 | setVideoAbusesSort, | 30 | setVideoAbusesSort, |
30 | setPagination, | 31 | setPagination, |
31 | listVideoAbuses | 32 | asyncMiddleware(listVideoAbuses) |
32 | ) | 33 | ) |
33 | abuseVideoRouter.post('/:id/abuse', | 34 | abuseVideoRouter.post('/:id/abuse', |
34 | authenticate, | 35 | authenticate, |
35 | videoAbuseReportValidator, | 36 | videoAbuseReportValidator, |
36 | reportVideoAbuseRetryWrapper | 37 | asyncMiddleware(reportVideoAbuseRetryWrapper) |
37 | ) | 38 | ) |
38 | 39 | ||
39 | // --------------------------------------------------------------------------- | 40 | // --------------------------------------------------------------------------- |
@@ -44,55 +45,48 @@ export { | |||
44 | 45 | ||
45 | // --------------------------------------------------------------------------- | 46 | // --------------------------------------------------------------------------- |
46 | 47 | ||
47 | function listVideoAbuses (req: express.Request, res: express.Response, next: express.NextFunction) { | 48 | async function listVideoAbuses (req: express.Request, res: express.Response, next: express.NextFunction) { |
48 | db.VideoAbuse.listForApi(req.query.start, req.query.count, req.query.sort) | 49 | const resultList = await db.VideoAbuse.listForApi(req.query.start, req.query.count, req.query.sort) |
49 | .then(result => res.json(getFormattedObjects(result.data, result.total))) | 50 | |
50 | .catch(err => next(err)) | 51 | return res.json(getFormattedObjects(resultList.data, resultList.total)) |
51 | } | 52 | } |
52 | 53 | ||
53 | function reportVideoAbuseRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) { | 54 | async function reportVideoAbuseRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) { |
54 | const options = { | 55 | const options = { |
55 | arguments: [ req, res ], | 56 | arguments: [ req, res ], |
56 | errorMessage: 'Cannot report abuse to the video with many retries.' | 57 | errorMessage: 'Cannot report abuse to the video with many retries.' |
57 | } | 58 | } |
58 | 59 | ||
59 | retryTransactionWrapper(reportVideoAbuse, options) | 60 | await retryTransactionWrapper(reportVideoAbuse, options) |
60 | .then(() => res.type('json').status(204).end()) | 61 | |
61 | .catch(err => next(err)) | 62 | return res.type('json').status(204).end() |
62 | } | 63 | } |
63 | 64 | ||
64 | function reportVideoAbuse (req: express.Request, res: express.Response) { | 65 | async function reportVideoAbuse (req: express.Request, res: express.Response) { |
65 | const videoInstance = res.locals.video as VideoInstance | 66 | const videoInstance = res.locals.video as VideoInstance |
66 | const reporterUsername = res.locals.oauth.token.User.username | 67 | const reporterUsername = res.locals.oauth.token.User.username |
67 | const body: VideoAbuseCreate = req.body | 68 | const body: VideoAbuseCreate = req.body |
68 | 69 | ||
69 | const abuse = { | 70 | const abuseToCreate = { |
70 | reporterUsername, | 71 | reporterUsername, |
71 | reason: body.reason, | 72 | reason: body.reason, |
72 | videoId: videoInstance.id, | 73 | videoId: videoInstance.id, |
73 | reporterPodId: null // This is our pod that reported this abuse | 74 | reporterPodId: null // This is our pod that reported this abuse |
74 | } | 75 | } |
75 | 76 | ||
76 | return db.sequelize.transaction(t => { | 77 | await db.sequelize.transaction(async t => { |
77 | return db.VideoAbuse.create(abuse, { transaction: t }) | 78 | const abuse = await db.VideoAbuse.create(abuseToCreate, { transaction: t }) |
78 | .then(abuse => { | 79 | // We send the information to the destination pod |
79 | // We send the information to the destination pod | 80 | if (videoInstance.isOwned() === false) { |
80 | if (videoInstance.isOwned() === false) { | 81 | const reportData = { |
81 | const reportData = { | 82 | reporterUsername, |
82 | reporterUsername, | 83 | reportReason: abuse.reason, |
83 | reportReason: abuse.reason, | 84 | videoUUID: videoInstance.uuid |
84 | videoUUID: videoInstance.uuid | 85 | } |
85 | } | 86 | |
86 | 87 | await friends.reportAbuseVideoToFriend(reportData, videoInstance, t) | |
87 | return friends.reportAbuseVideoToFriend(reportData, videoInstance, t).then(() => videoInstance) | 88 | } |
88 | } | ||
89 | |||
90 | return videoInstance | ||
91 | }) | ||
92 | }) | ||
93 | .then((videoInstance: VideoInstance) => logger.info('Abuse report for video %s created.', videoInstance.name)) | ||
94 | .catch(err => { | ||
95 | logger.debug('Cannot update the video.', err) | ||
96 | throw err | ||
97 | }) | 89 | }) |
90 | |||
91 | logger.info('Abuse report for video %s created.', videoInstance.name) | ||
98 | } | 92 | } |