]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/api/videos.js
Server: always check commit result
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos.js
index 4d45c11c0334fe585c31ad4ff5756dc8d03c0844..df068f961797d36897b582ed14abdf0338915658 100644 (file)
@@ -56,7 +56,7 @@ router.get('/abuse',
 router.post('/:id/abuse',
   oAuth.authenticate,
   validatorsVideos.videoAbuseReport,
-  reportVideoAbuse
+  reportVideoAbuseRetryWrapper
 )
 
 router.get('/',
@@ -231,11 +231,12 @@ function addVideo (req, res, videoFile, callback) {
     }
 
     // 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)
+    })
   })
 }
 
@@ -324,9 +325,12 @@ function updateVideo (req, res, finalCallback) {
     }
 
     // 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)
+    })
   })
 }
 
@@ -375,7 +379,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 reporterUsername = res.locals.oauth.token.User.username
 
@@ -386,21 +406,52 @@ function reportVideoAbuse (req, res, next) {
     reporterPodId: null // This is our pod that reported this abuse
   }
 
-  db.VideoAbuse.create(abuse).asCallback(function (err) {
-    if (err) return next(err)
+  waterfall([
+
+    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)
+      })
+    },
 
-    // We send the information to the destination pod
-    if (videoInstance.isOwned() === false) {
-      const reportData = {
-        reporterUsername,
-        reportReason: abuse.reason,
-        videoRemoteId: videoInstance.remoteId
+    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)
+    })
   })
 }