router.post('/:id/abuse',
oAuth.authenticate,
validatorsVideos.videoAbuseReport,
- reportVideoAbuse
+ reportVideoAbuseRetryWrapper
)
router.get('/',
}
// Commit transaction
- t.commit()
+ t.commit().asCallback(function (err) {
+ if (err) return callback(err)
- logger.info('Video with name %s created.', videoInfos.name)
-
- return callback(null)
+ logger.info('Video with name %s created.', videoInfos.name)
+ return callback(null)
+ })
})
}
function updateVideo (req, res, finalCallback) {
const videoInstance = res.locals.video
+ const videoFieldsSave = videoInstance.toJSON()
const videoInfosToUpdate = req.body
waterfall([
function startTransaction (callback) {
- db.sequelize.transaction().asCallback(function (err, t) {
+ db.sequelize.transaction({ isolationLevel: 'SERIALIZABLE' }).asCallback(function (err, t) {
return callback(err, t)
})
},
},
function updateVideoIntoDB (t, tagInstances, callback) {
- const options = { transaction: t }
+ const options = {
+ transaction: t
+ }
if (videoInfosToUpdate.name) videoInstance.set('name', videoInfosToUpdate.name)
if (videoInfosToUpdate.description) videoInstance.set('description', videoInfosToUpdate.description)
- // Add tags association
videoInstance.save(options).asCallback(function (err) {
return callback(err, t, tagInstances)
})
// Abort transaction?
if (t) t.rollback()
+ // Force fields we want to update
+ // If the transaction is retried, sequelize will think the object has not changed
+ // So it will skip the SQL request, even if the last one was ROLLBACKed!
+ Object.keys(videoFieldsSave).forEach(function (key) {
+ const value = videoFieldsSave[key]
+ videoInstance.set(key, value)
+ })
+
return finalCallback(err)
}
// Commit transaction
- t.commit()
+ t.commit().asCallback(function (err) {
+ if (err) return finalCallback(err)
- return finalCallback(null)
+ logger.info('Video with name %s updated.', videoInfosToUpdate.name)
+ return finalCallback(null)
+ })
})
}
})
}
-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 reporterUsername = res.locals.oauth.token.User.username
reporterPodId: null // This is our pod that reported this abuse
}
- db.VideoAbuse.create(abuse).asCallback(function (err) {
- if (err) return next(err)
+ waterfall([
- // We send the information to the destination pod
- if (videoInstance.isOwned() === false) {
- const reportData = {
- reporterUsername,
- reportReason: abuse.reason,
- videoRemoteId: videoInstance.remoteId
+ 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
+ if (videoInstance.isOwned() === false) {
+ const reportData = {
+ reporterUsername,
+ reportReason: abuse.reason,
+ videoRemoteId: videoInstance.remoteId
+ }
+
+ friends.reportAbuseVideoToFriend(reportData, videoInstance)
}
- friends.reportAbuseVideoToFriend(reportData, videoInstance)
+ return callback(null, t)
}
- return res.type('json').status(204).end()
+ ], 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().asCallback(function (err) {
+ if (err) return finalCallback(err)
+
+ logger.info('Abuse report for video %s created.', videoInstance.name)
+ return finalCallback(null)
+ })
})
}