]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/api/remote.js
Server: split check params tests
[github/Chocobozzz/PeerTube.git] / server / controllers / api / remote.js
index 94d6e740e8f221d2285ce3be3d4d695d7d79c743..be5e6dc98e99e16dd82459e7eddc5387bb42e57f 100644 (file)
@@ -1,6 +1,5 @@
 'use strict'
 
-const each = require('async/each')
 const eachSeries = require('async/eachSeries')
 const express = require('express')
 const waterfall = require('async/waterfall')
@@ -28,19 +27,28 @@ module.exports = router
 
 function remoteVideos (req, res, next) {
   const requests = req.body.data
-  const fromHost = req.body.signature.host
+  const fromPod = res.locals.secure.pod
 
   // We need to process in the same order to keep consistency
   // TODO: optimization
   eachSeries(requests, function (request, callbackEach) {
     const videoData = request.data
 
-    if (request.type === 'add') {
-      addRemoteVideo(videoData, fromHost, callbackEach)
-    } else if (request.type === 'remove') {
-      removeRemoteVideo(videoData, fromHost, callbackEach)
-    } else {
-      logger.error('Unkown remote request type %s.', request.type)
+    switch (request.type) {
+      case 'add':
+        addRemoteVideo(videoData, fromPod, callbackEach)
+        break
+
+      case 'update':
+        updateRemoteVideo(videoData, fromPod, callbackEach)
+        break
+
+      case 'remove':
+        removeRemoteVideo(videoData, fromPod, callbackEach)
+        break
+
+      default:
+        logger.error('Unkown remote request type %s.', request.type)
     }
   }, function (err) {
     if (err) logger.error('Error managing remote videos.', { error: err })
@@ -50,7 +58,7 @@ function remoteVideos (req, res, next) {
   return res.type('json').status(204).end()
 }
 
-function addRemoteVideo (videoToCreateData, fromHost, finalCallback) {
+function addRemoteVideo (videoToCreateData, fromPod, finalCallback) {
   logger.debug('Adding remote video "%s".', videoToCreateData.name)
 
   waterfall([
@@ -61,70 +69,21 @@ function addRemoteVideo (videoToCreateData, fromHost, finalCallback) {
       })
     },
 
-    function findOrCreatePod (t, callback) {
-      const query = {
-        where: {
-          host: fromHost
-        },
-        defaults: {
-          host: fromHost
-        },
-        transaction: t
-      }
+    function findOrCreateAuthor (t, callback) {
+      const name = videoToCreateData.author
+      const podId = fromPod.id
+      // This author is from another pod so we do not associate a user
+      const userId = null
 
-      db.Pod.findOrCreate(query).asCallback(function (err, result) {
-        // [ instance, wasCreated ]
-        return callback(err, t, result[0])
-      })
-    },
-
-    function findOrCreateAuthor (t, pod, callback) {
-      const username = videoToCreateData.author
-
-      const query = {
-        where: {
-          name: username,
-          podId: pod.id,
-          userId: null
-        },
-        defaults: {
-          name: username,
-          podId: pod.id,
-          userId: null
-        },
-        transaction: t
-      }
-
-      db.Author.findOrCreate(query).asCallback(function (err, result) {
-        // [ instance, wasCreated ]
-        return callback(err, t, result[0])
+      db.Author.findOrCreateAuthor(name, podId, userId, t, function (err, authorInstance) {
+        return callback(err, t, authorInstance)
       })
     },
 
     function findOrCreateTags (t, author, callback) {
       const tags = videoToCreateData.tags
-      const tagInstances = []
-
-      each(tags, function (tag, callbackEach) {
-        const query = {
-          where: {
-            name: tag
-          },
-          defaults: {
-            name: tag
-          },
-          transaction: t
-        }
 
-        db.Tag.findOrCreate(query).asCallback(function (err, res) {
-          if (err) return callbackEach(err)
-
-          // res = [ tag, isCreated ]
-          const tag = res[0]
-          tagInstances.push(tag)
-          return callbackEach()
-        })
-      }, function (err) {
+      db.Tag.findOrCreateTags(tags, t, function (err, tagInstances) {
         return callback(err, t, author, tagInstances)
       })
     },
@@ -138,7 +97,8 @@ function addRemoteVideo (videoToCreateData, fromHost, finalCallback) {
         description: videoToCreateData.description,
         authorId: author.id,
         duration: videoToCreateData.duration,
-        createdAt: videoToCreateData.createdAt
+        createdAt: videoToCreateData.createdAt,
+        updatedAt: videoToCreateData.updatedAt
       }
 
       const video = db.Video.build(videoData)
@@ -147,9 +107,9 @@ function addRemoteVideo (videoToCreateData, fromHost, finalCallback) {
     },
 
     function generateThumbnail (t, tagInstances, video, callback) {
-      db.Video.generateThumbnailFromBase64(video, videoToCreateData.thumbnailBase64, function (err) {
+      db.Video.generateThumbnailFromData(video, videoToCreateData.thumbnailData, function (err) {
         if (err) {
-          logger.error('Cannot generate thumbnail from base 64 data.', { error: err })
+          logger.error('Cannot generate thumbnail from data.', { error: err })
           return callback(err)
         }
 
@@ -192,24 +152,86 @@ function addRemoteVideo (videoToCreateData, fromHost, finalCallback) {
   })
 }
 
-function removeRemoteVideo (videoToRemoveData, fromHost, callback) {
-  // TODO: use bulkDestroy?
+function updateRemoteVideo (videoAttributesToUpdate, fromPod, finalCallback) {
+  logger.debug('Updating remote video "%s".', videoAttributesToUpdate.name)
 
-  // We need the list because we have to remove some other stuffs (thumbnail etc)
-  db.Video.listByHostAndRemoteId(fromHost, videoToRemoveData.remoteId, function (err, videosList) {
-    if (err) {
-      logger.error('Cannot list videos from host and remote id.', { error: err.message })
-      return callback(err)
+  waterfall([
+
+    function startTransaction (callback) {
+      db.sequelize.transaction().asCallback(function (err, t) {
+        return callback(err, t)
+      })
+    },
+
+    function findVideo (t, callback) {
+      db.Video.loadByHostAndRemoteId(fromPod.host, videoAttributesToUpdate.remoteId, function (err, videoInstance) {
+        if (err || !videoInstance) {
+          logger.error('Cannot load video from host and remote id.', { error: err.message })
+          return callback(err)
+        }
+
+        return callback(null, t, videoInstance)
+      })
+    },
+
+    function findOrCreateTags (t, videoInstance, callback) {
+      const tags = videoAttributesToUpdate.tags
+
+      db.Tag.findOrCreateTags(tags, t, function (err, tagInstances) {
+        return callback(err, t, videoInstance, tagInstances)
+      })
+    },
+
+    function updateVideoIntoDB (t, videoInstance, tagInstances, callback) {
+      const options = { transaction: t }
+
+      videoInstance.set('name', videoAttributesToUpdate.name)
+      videoInstance.set('description', videoAttributesToUpdate.description)
+      videoInstance.set('infoHash', videoAttributesToUpdate.infoHash)
+      videoInstance.set('duration', videoAttributesToUpdate.duration)
+      videoInstance.set('createdAt', videoAttributesToUpdate.createdAt)
+      videoInstance.set('updatedAt', videoAttributesToUpdate.updatedAt)
+      videoInstance.set('extname', videoAttributesToUpdate.extname)
+
+      videoInstance.save(options).asCallback(function (err) {
+        return callback(err, t, videoInstance, tagInstances)
+      })
+    },
+
+    function associateTagsToVideo (t, videoInstance, tagInstances, callback) {
+      const options = { transaction: t }
+
+      videoInstance.setTags(tagInstances, options).asCallback(function (err) {
+        return callback(err, t)
+      })
     }
 
-    if (videosList.length === 0) {
-      logger.error('No remote video was found for this pod.', { remoteId: videoToRemoveData.remoteId, podHost: fromHost })
+  ], function (err, t) {
+    if (err) {
+      logger.error('Cannot update the remote video.')
+
+      // Abort transaction?
+      if (t) t.rollback()
+
+      return finalCallback(err)
     }
 
-    each(videosList, function (video, callbackEach) {
-      logger.debug('Removing remote video %s.', video.remoteId)
+    // Commit transaction
+    t.commit()
+
+    return finalCallback()
+  })
+}
+
+function removeRemoteVideo (videoToRemoveData, fromPod, callback) {
+  // We need the instance because we have to remove some other stuffs (thumbnail etc)
+  db.Video.loadByHostAndRemoteId(fromPod.host, videoToRemoveData.remoteId, function (err, video) {
+    if (err || !video) {
+      logger.error('Cannot load video from host and remote id.', { error: err.message })
+      return callback(err)
+    }
 
-      video.destroy().asCallback(callbackEach)
-    }, callback)
+    logger.debug('Removing remote video %s.', video.remoteId)
+    video.destroy().asCallback(callback)
   })
 }