eachSeries(requests, function (request, callbackEach) {
const videoData = request.data
- if (request.type === 'add') {
- addRemoteVideo(videoData, fromPod, callbackEach)
- } else if (request.type === 'remove') {
- removeRemoteVideo(videoData, fromPod, 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 })
})
}
-function removeRemoteVideo (videoToRemoveData, fromPod, 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(fromPod.host, 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('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: fromPod.host })
+ ], 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)
})
}
if (err) throw err
const video = res.body.data[0]
- toRemove.push(res.body.data[2].id)
- toRemove.push(res.body.data[3].id)
+ toRemove.push(res.body.data[2])
+ toRemove.push(res.body.data[3])
webtorrent.add(video.magnetUri, function (torrent) {
expect(torrent.files).to.exist
})
})
})
+ })
+
+ describe('Should manipulate these videos', function () {
+ it('Should update the video 3 by asking pod 3', function (done) {
+ this.timeout(15000)
+
+ const name = 'my super video updated'
+ const description = 'my super description updated'
+ const tags = [ 'tagup1', 'tagup2' ]
+
+ videosUtils.updateVideo(servers[2].url, servers[2].accessToken, toRemove[0].id, name, description, tags, function (err) {
+ if (err) throw err
+
+ setTimeout(done, 11000)
+ })
+ })
+
+ it('Should have the video 3 updated on each pod', function (done) {
+ each(servers, function (server, callback) {
+ videosUtils.getVideosList(server.url, function (err, res) {
+ if (err) throw err
+
+ const videos = res.body.data
+ const videoUpdated = videos.find(function (video) {
+ return video.name === 'my super video updated'
+ })
+
+ expect(!!videoUpdated).to.be.true
+ expect(videoUpdated.description).to.equal('my super description updated')
+ expect(videoUpdated.tags).to.deep.equal([ 'tagup1', 'tagup2' ])
+
+ callback()
+ })
+ }, done)
+ })
- it('Should remove the file 3 and 3-2 by asking pod 3', function (done) {
+ it('Should remove the videos 3 and 3-2 by asking pod 3', function (done) {
this.timeout(15000)
series([
function (next) {
- videosUtils.removeVideo(servers[2].url, servers[2].accessToken, toRemove[0], next)
+ videosUtils.removeVideo(servers[2].url, servers[2].accessToken, toRemove[0].id, next)
},
function (next) {
- videosUtils.removeVideo(servers[2].url, servers[2].accessToken, toRemove[1], next)
+ videosUtils.removeVideo(servers[2].url, servers[2].accessToken, toRemove[1].id, next)
}],
function (err) {
if (err) throw err
const videos = res.body.data
expect(videos).to.be.an('array')
expect(videos.length).to.equal(2)
- expect(videos[0].id).not.to.equal(videos[1].id)
- expect(videos[0].id).not.to.equal(toRemove[0])
- expect(videos[1].id).not.to.equal(toRemove[0])
- expect(videos[0].id).not.to.equal(toRemove[1])
- expect(videos[1].id).not.to.equal(toRemove[1])
+ expect(videos[0].name).not.to.equal(videos[1].name)
+ expect(videos[0].name).not.to.equal(toRemove[0].name)
+ expect(videos[1].name).not.to.equal(toRemove[0].name)
+ expect(videos[0].name).not.to.equal(toRemove[1].name)
+ expect(videos[1].name).not.to.equal(toRemove[1].name)
callback()
})