Server: little refractoring
This commit is contained in:
parent
d396a937b6
commit
4ff0d86208
5 changed files with 92 additions and 104 deletions
|
@ -28,7 +28,7 @@ module.exports = router
|
||||||
|
|
||||||
function remoteVideos (req, res, next) {
|
function remoteVideos (req, res, next) {
|
||||||
const requests = req.body.data
|
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
|
// We need to process in the same order to keep consistency
|
||||||
// TODO: optimization
|
// TODO: optimization
|
||||||
|
@ -36,9 +36,9 @@ function remoteVideos (req, res, next) {
|
||||||
const videoData = request.data
|
const videoData = request.data
|
||||||
|
|
||||||
if (request.type === 'add') {
|
if (request.type === 'add') {
|
||||||
addRemoteVideo(videoData, fromHost, callbackEach)
|
addRemoteVideo(videoData, fromPod, callbackEach)
|
||||||
} else if (request.type === 'remove') {
|
} else if (request.type === 'remove') {
|
||||||
removeRemoteVideo(videoData, fromHost, callbackEach)
|
removeRemoteVideo(videoData, fromPod, callbackEach)
|
||||||
} else {
|
} else {
|
||||||
logger.error('Unkown remote request type %s.', request.type)
|
logger.error('Unkown remote request type %s.', request.type)
|
||||||
}
|
}
|
||||||
|
@ -50,7 +50,7 @@ function remoteVideos (req, res, next) {
|
||||||
return res.type('json').status(204).end()
|
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)
|
logger.debug('Adding remote video "%s".', videoToCreateData.name)
|
||||||
|
|
||||||
waterfall([
|
waterfall([
|
||||||
|
@ -61,70 +61,21 @@ function addRemoteVideo (videoToCreateData, fromHost, finalCallback) {
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
|
||||||
function findOrCreatePod (t, callback) {
|
function findOrCreateAuthor (t, callback) {
|
||||||
const query = {
|
const name = videoToCreateData.author
|
||||||
where: {
|
const podId = fromPod.id
|
||||||
host: fromHost
|
// This author is from another pod so we do not associate a user
|
||||||
},
|
const userId = null
|
||||||
defaults: {
|
|
||||||
host: fromHost
|
|
||||||
},
|
|
||||||
transaction: t
|
|
||||||
}
|
|
||||||
|
|
||||||
db.Pod.findOrCreate(query).asCallback(function (err, result) {
|
db.Author.findOrCreateAuthor(name, podId, userId, t, function (err, authorInstance) {
|
||||||
// [ instance, wasCreated ]
|
return callback(err, t, authorInstance)
|
||||||
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])
|
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
|
||||||
function findOrCreateTags (t, author, callback) {
|
function findOrCreateTags (t, author, callback) {
|
||||||
const tags = videoToCreateData.tags
|
const tags = videoToCreateData.tags
|
||||||
const tagInstances = []
|
|
||||||
|
|
||||||
each(tags, function (tag, callbackEach) {
|
db.Tag.findOrCreateTags(tags, t, function (err, tagInstances) {
|
||||||
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) {
|
|
||||||
return callback(err, t, author, tagInstances)
|
return callback(err, t, author, tagInstances)
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
@ -192,18 +143,18 @@ function addRemoteVideo (videoToCreateData, fromHost, finalCallback) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function removeRemoteVideo (videoToRemoveData, fromHost, callback) {
|
function removeRemoteVideo (videoToRemoveData, fromPod, callback) {
|
||||||
// TODO: use bulkDestroy?
|
// TODO: use bulkDestroy?
|
||||||
|
|
||||||
// We need the list because we have to remove some other stuffs (thumbnail etc)
|
// 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) {
|
if (err) {
|
||||||
logger.error('Cannot list videos from host and remote id.', { error: err.message })
|
logger.error('Cannot list videos from host and remote id.', { error: err.message })
|
||||||
return callback(err)
|
return callback(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (videosList.length === 0) {
|
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) {
|
each(videosList, function (video, callbackEach) {
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const each = require('async/each')
|
|
||||||
const express = require('express')
|
const express = require('express')
|
||||||
const fs = require('fs')
|
const fs = require('fs')
|
||||||
const multer = require('multer')
|
const multer = require('multer')
|
||||||
|
@ -97,51 +96,20 @@ function addVideo (req, res, next) {
|
||||||
function findOrCreateAuthor (t, callback) {
|
function findOrCreateAuthor (t, callback) {
|
||||||
const user = res.locals.oauth.token.User
|
const user = res.locals.oauth.token.User
|
||||||
|
|
||||||
const query = {
|
const name = user.username
|
||||||
where: {
|
// null because it is OUR pod
|
||||||
name: user.username,
|
const podId = null
|
||||||
podId: null,
|
const userId = user.id
|
||||||
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]
|
|
||||||
|
|
||||||
|
db.Author.findOrCreateAuthor(name, podId, userId, t, function (err, authorInstance) {
|
||||||
return callback(err, t, authorInstance)
|
return callback(err, t, authorInstance)
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
|
||||||
function findOrCreateTags (t, author, callback) {
|
function findOrCreateTags (t, author, callback) {
|
||||||
const tags = videoInfos.tags
|
const tags = videoInfos.tags
|
||||||
const tagInstances = []
|
|
||||||
|
|
||||||
each(tags, function (tag, callbackEach) {
|
db.Tag.findOrCreateTags(tags, t, function (err, tagInstances) {
|
||||||
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) {
|
|
||||||
return callback(err, t, author, tagInstances)
|
return callback(err, t, author, tagInstances)
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
|
|
@ -26,6 +26,10 @@ function checkSignature (req, res, next) {
|
||||||
const signatureOk = peertubeCrypto.checkSignature(pod.publicKey, host, req.body.signature.signature)
|
const signatureOk = peertubeCrypto.checkSignature(pod.publicKey, host, req.body.signature.signature)
|
||||||
|
|
||||||
if (signatureOk === true) {
|
if (signatureOk === true) {
|
||||||
|
res.locals.secure = {
|
||||||
|
pod
|
||||||
|
}
|
||||||
|
|
||||||
return next()
|
return next()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -29,7 +29,9 @@ module.exports = function (sequelize, DataTypes) {
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
classMethods: {
|
classMethods: {
|
||||||
associate
|
associate,
|
||||||
|
|
||||||
|
findOrCreateAuthor
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
@ -56,3 +58,28 @@ function associate (models) {
|
||||||
onDelete: 'cascade'
|
onDelete: 'cascade'
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function findOrCreateAuthor (name, podId, userId, transaction, callback) {
|
||||||
|
if (!callback) {
|
||||||
|
callback = transaction
|
||||||
|
transaction = null
|
||||||
|
}
|
||||||
|
|
||||||
|
const author = {
|
||||||
|
name,
|
||||||
|
podId,
|
||||||
|
userId
|
||||||
|
}
|
||||||
|
|
||||||
|
const query = {
|
||||||
|
where: author,
|
||||||
|
defaults: author
|
||||||
|
}
|
||||||
|
|
||||||
|
if (transaction) query.transaction = transaction
|
||||||
|
|
||||||
|
this.findOrCreate(query).asCallback(function (err, result) {
|
||||||
|
// [ instance, wasCreated ]
|
||||||
|
return callback(err, result[0])
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
|
const each = require('async/each')
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
module.exports = function (sequelize, DataTypes) {
|
module.exports = function (sequelize, DataTypes) {
|
||||||
|
@ -19,7 +21,9 @@ module.exports = function (sequelize, DataTypes) {
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
classMethods: {
|
classMethods: {
|
||||||
associate
|
associate,
|
||||||
|
|
||||||
|
findOrCreateTags
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
@ -36,3 +40,37 @@ function associate (models) {
|
||||||
onDelete: 'cascade'
|
onDelete: 'cascade'
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function findOrCreateTags (tags, transaction, callback) {
|
||||||
|
if (!callback) {
|
||||||
|
callback = transaction
|
||||||
|
transaction = null
|
||||||
|
}
|
||||||
|
|
||||||
|
const self = this
|
||||||
|
const tagInstances = []
|
||||||
|
|
||||||
|
each(tags, function (tag, callbackEach) {
|
||||||
|
const query = {
|
||||||
|
where: {
|
||||||
|
name: tag
|
||||||
|
},
|
||||||
|
defaults: {
|
||||||
|
name: tag
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (transaction) query.transaction = transaction
|
||||||
|
|
||||||
|
self.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) {
|
||||||
|
return callback(err, tagInstances)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue