]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/api/videos.js
Server: add ability to update a video
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos.js
index 992f03db0c3b864d15b58346314368fec68c2b15..1b306d1cf038644f09f77adcacb37c35e14f9f13 100644 (file)
@@ -1,6 +1,5 @@
 'use strict'
 
-const each = require('async/each')
 const express = require('express')
 const fs = require('fs')
 const multer = require('multer')
@@ -51,6 +50,12 @@ router.get('/',
   pagination.setPagination,
   listVideos
 )
+router.put('/:id',
+  oAuth.authenticate,
+  reqFiles,
+  validatorsVideos.videosUpdate,
+  updateVideo
+)
 router.post('/',
   oAuth.authenticate,
   reqFiles,
@@ -95,50 +100,22 @@ function addVideo (req, res, next) {
     },
 
     function findOrCreateAuthor (t, callback) {
-      const username = res.locals.oauth.token.user.username
-
-      const query = {
-        where: {
-          name: username,
-          podId: null
-        },
-        defaults: {
-          name: username,
-          podId: null // null because it is OUR pod
-        },
-        transaction: t
-      }
+      const user = res.locals.oauth.token.User
+
+      const name = user.username
+      // null because it is OUR pod
+      const podId = null
+      const userId = user.id
 
-      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 = videoInfos.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)
       })
     },
@@ -194,7 +171,7 @@ function addVideo (req, res, next) {
     },
 
     function sendToFriends (t, video, callback) {
-      video.toRemoteJSON(function (err, remoteVideo) {
+      video.toAddRemoteJSON(function (err, remoteVideo) {
         if (err) return callback(err)
 
         // Now we'll add the video's meta data to our friends
@@ -222,6 +199,83 @@ function addVideo (req, res, next) {
   })
 }
 
+function updateVideo (req, res, next) {
+  let videoInstance = res.locals.video
+  const videoInfosToUpdate = req.body
+
+  waterfall([
+
+    function startTransaction (callback) {
+      db.sequelize.transaction().asCallback(function (err, t) {
+        return callback(err, t)
+      })
+    },
+
+    function findOrCreateTags (t, callback) {
+      if (videoInfosToUpdate.tags) {
+        db.Tag.findOrCreateTags(videoInfosToUpdate.tags, t, function (err, tagInstances) {
+          return callback(err, t, tagInstances)
+        })
+      } else {
+        return callback(null, t, null)
+      }
+    },
+
+    function updateVideoIntoDB (t, tagInstances, callback) {
+      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) {
+        if (err) return callback(err)
+
+        return callback(err, t, tagInstances)
+      })
+    },
+
+    function associateTagsToVideo (t, tagInstances, callback) {
+      if (tagInstances) {
+        const options = { transaction: t }
+
+        videoInstance.setTags(tagInstances, options).asCallback(function (err) {
+          videoInstance.Tags = tagInstances
+
+          return callback(err, t)
+        })
+      } else {
+        return callback(null, t)
+      }
+    },
+
+    function sendToFriends (t, callback) {
+      const json = videoInstance.toUpdateRemoteJSON()
+
+      // Now we'll update the video's meta data to our friends
+      friends.updateVideoToFriends(json)
+
+      return callback(null, t)
+    }
+
+  ], function andFinally (err, t) {
+    if (err) {
+      logger.error('Cannot insert the video.')
+
+      // Abort transaction?
+      if (t) t.rollback()
+
+      return next(err)
+    }
+
+    // Commit transaction
+    t.commit()
+
+    // TODO : include Location of the new video -> 201
+    return res.type('json').status(204).end()
+  })
+}
+
 function getVideo (req, res, next) {
   db.Video.loadAndPopulateAuthorAndPodAndTags(req.params.id, function (err, video) {
     if (err) return next(err)
@@ -246,27 +300,15 @@ function removeVideo (req, res, next) {
   const videoId = req.params.id
 
   waterfall([
-    function getVideo (callback) {
-      db.Video.load(videoId, callback)
-    },
-
-    function removeFromDB (video, callback) {
-      video.destroy().asCallback(function (err) {
-        if (err) return callback(err)
-
-        return callback(null, video)
+    function loadVideo (callback) {
+      db.Video.load(videoId, function (err, video) {
+        return callback(err, video)
       })
     },
 
-    function sendInformationToFriends (video, callback) {
-      const params = {
-        name: video.name,
-        remoteId: video.id
-      }
-
-      friends.removeVideoToFriends(params)
-
-      return callback(null)
+    function deleteVideo (video, callback) {
+      // Informations to other pods will be sent by the afterDestroy video hook
+      video.destroy().asCallback(callback)
     }
   ], function andFinally (err) {
     if (err) {