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
const videoData = request.data
if (request.type === 'add') {
- addRemoteVideo(videoData, fromHost, callbackEach)
+ addRemoteVideo(videoData, fromPod, callbackEach)
} else if (request.type === 'remove') {
- removeRemoteVideo(videoData, fromHost, callbackEach)
+ removeRemoteVideo(videoData, fromPod, callbackEach)
} else {
logger.error('Unkown remote request type %s.', request.type)
}
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([
})
},
- function findOrCreatePod (t, callback) {
- const query = {
- where: {
- host: fromHost
- },
- defaults: {
- host: fromHost
- },
- transaction: t
- }
-
- db.Pod.findOrCreate(query).asCallback(function (err, result) {
- // [ instance, wasCreated ]
- return callback(err, t, result[0])
- })
- },
+ 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
- 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)
})
},
})
}
-function removeRemoteVideo (videoToRemoveData, fromHost, callback) {
+function removeRemoteVideo (videoToRemoveData, fromPod, callback) {
// TODO: use bulkDestroy?
// We need the list because we have to remove some other stuffs (thumbnail etc)
- db.Video.listByHostAndRemoteId(fromHost, videoToRemoveData.remoteId, function (err, videosList) {
+ 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)
}
if (videosList.length === 0) {
- logger.error('No remote video was found for this pod.', { remoteId: videoToRemoveData.remoteId, podHost: fromHost })
+ logger.error('No remote video was found for this pod.', { remoteId: videoToRemoveData.remoteId, podHost: fromPod.host })
}
each(videosList, function (video, callbackEach) {
'use strict'
-const each = require('async/each')
const express = require('express')
const fs = require('fs')
const multer = require('multer')
function findOrCreateAuthor (t, callback) {
const user = res.locals.oauth.token.User
- const query = {
- where: {
- name: user.username,
- podId: null,
- userId: user.id
- },
- defaults: {
- name: user.username,
- podId: null, // null because it is OUR pod
- userId: user.id
- },
- transaction: t
- }
-
- db.Author.findOrCreate(query).asCallback(function (err, result) {
- const authorInstance = result[0]
+ const name = user.username
+ // null because it is OUR pod
+ const podId = null
+ const userId = user.id
+ 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)
})
},