Server: retry video abuse requests too

pull/30/head
Chocobozzz 2017-01-10 22:33:00 +01:00
parent bd14d16a29
commit bf4ff8fe0b
1 changed files with 56 additions and 12 deletions

View File

@ -56,7 +56,7 @@ router.get('/abuse',
router.post('/:id/abuse', router.post('/:id/abuse',
oAuth.authenticate, oAuth.authenticate,
validatorsVideos.videoAbuseReport, validatorsVideos.videoAbuseReport,
reportVideoAbuse reportVideoAbuseRetryWrapper
) )
router.get('/', router.get('/',
@ -375,7 +375,23 @@ function listVideoAbuses (req, res, next) {
}) })
} }
function reportVideoAbuse (req, res, next) { function reportVideoAbuseRetryWrapper (req, res, next) {
utils.transactionRetryer(
function (callback) {
return reportVideoAbuse(req, res, callback)
},
function (err) {
if (err) {
logger.error('Cannot report abuse to the video with many retries.', { error: err })
return next(err)
}
return res.type('json').status(204).end()
}
)
}
function reportVideoAbuse (req, res, finalCallback) {
const videoInstance = res.locals.video const videoInstance = res.locals.video
const reporterUsername = res.locals.oauth.token.User.username const reporterUsername = res.locals.oauth.token.User.username
@ -386,9 +402,21 @@ function reportVideoAbuse (req, res, next) {
reporterPodId: null // This is our pod that reported this abuse reporterPodId: null // This is our pod that reported this abuse
} }
db.VideoAbuse.create(abuse).asCallback(function (err) { waterfall([
if (err) return next(err)
function startTransaction (callback) {
db.sequelize.transaction().asCallback(function (err, t) {
return callback(err, t)
})
},
function createAbuse (t, callback) {
db.VideoAbuse.create(abuse).asCallback(function (err, abuse) {
return callback(err, t, abuse)
})
},
function sendToFriendsIfNeeded (t, abuse, callback) {
// We send the information to the destination pod // We send the information to the destination pod
if (videoInstance.isOwned() === false) { if (videoInstance.isOwned() === false) {
const reportData = { const reportData = {
@ -400,7 +428,23 @@ function reportVideoAbuse (req, res, next) {
friends.reportAbuseVideoToFriend(reportData, videoInstance) friends.reportAbuseVideoToFriend(reportData, videoInstance)
} }
return res.type('json').status(204).end() return callback(null, t)
}
], function andFinally (err, t) {
if (err) {
logger.debug('Cannot update the video.', { error: err })
// Abort transaction?
if (t) t.rollback()
return finalCallback(err)
}
// Commit transaction
t.commit()
return finalCallback(null)
}) })
} }