mirror of
https://github.com/Chocobozzz/PeerTube.git
synced 2024-11-22 08:46:54 -06:00
Server: make friends urls come from the request instead of the
configuration file
This commit is contained in:
parent
e861452fb2
commit
1e2564d392
@ -18,8 +18,5 @@ storage:
|
|||||||
logs: 'logs/'
|
logs: 'logs/'
|
||||||
thumbnails: 'thumbnails/'
|
thumbnails: 'thumbnails/'
|
||||||
|
|
||||||
network:
|
|
||||||
friends: []
|
|
||||||
|
|
||||||
electron:
|
electron:
|
||||||
debug: false
|
debug: false
|
||||||
|
@ -14,7 +14,3 @@ storage:
|
|||||||
uploads: 'test1/uploads/'
|
uploads: 'test1/uploads/'
|
||||||
logs: 'test1/logs/'
|
logs: 'test1/logs/'
|
||||||
thumbnails: 'test1/thumbnails/'
|
thumbnails: 'test1/thumbnails/'
|
||||||
|
|
||||||
network:
|
|
||||||
friends:
|
|
||||||
- 'http://localhost:9002'
|
|
||||||
|
@ -14,7 +14,3 @@ storage:
|
|||||||
uploads: 'test2/uploads/'
|
uploads: 'test2/uploads/'
|
||||||
logs: 'test2/logs/'
|
logs: 'test2/logs/'
|
||||||
thumbnails: 'test2/thumbnails/'
|
thumbnails: 'test2/thumbnails/'
|
||||||
|
|
||||||
network:
|
|
||||||
friends:
|
|
||||||
- 'http://localhost:9003'
|
|
||||||
|
@ -14,7 +14,3 @@ storage:
|
|||||||
uploads: 'test3/uploads/'
|
uploads: 'test3/uploads/'
|
||||||
logs: 'test3/logs/'
|
logs: 'test3/logs/'
|
||||||
thumbnails: 'test3/thumbnails/'
|
thumbnails: 'test3/thumbnails/'
|
||||||
|
|
||||||
network:
|
|
||||||
friends:
|
|
||||||
- 'http://localhost:9001'
|
|
||||||
|
@ -14,7 +14,3 @@ storage:
|
|||||||
uploads: 'test4/uploads/'
|
uploads: 'test4/uploads/'
|
||||||
logs: 'test4/logs/'
|
logs: 'test4/logs/'
|
||||||
thumbnails: 'test4/thumbnails/'
|
thumbnails: 'test4/thumbnails/'
|
||||||
|
|
||||||
network:
|
|
||||||
friends:
|
|
||||||
- 'http://localhost:9002'
|
|
||||||
|
@ -14,8 +14,3 @@ storage:
|
|||||||
uploads: 'test5/uploads/'
|
uploads: 'test5/uploads/'
|
||||||
logs: 'test5/logs/'
|
logs: 'test5/logs/'
|
||||||
thumbnails: 'test5/thumbnails/'
|
thumbnails: 'test5/thumbnails/'
|
||||||
|
|
||||||
network:
|
|
||||||
friends:
|
|
||||||
- 'http://localhost:9001'
|
|
||||||
- 'http://localhost:9004'
|
|
||||||
|
@ -14,9 +14,3 @@ storage:
|
|||||||
uploads: 'test6/uploads/'
|
uploads: 'test6/uploads/'
|
||||||
logs: 'test6/logs/'
|
logs: 'test6/logs/'
|
||||||
thumbnails: 'test6/thumbnails/'
|
thumbnails: 'test6/thumbnails/'
|
||||||
|
|
||||||
network:
|
|
||||||
friends:
|
|
||||||
- 'http://localhost:9001'
|
|
||||||
- 'http://localhost:9002'
|
|
||||||
- 'http://localhost:9003'
|
|
||||||
|
@ -19,7 +19,7 @@ const Video = mongoose.model('Video')
|
|||||||
|
|
||||||
router.get('/', listPodsUrl)
|
router.get('/', listPodsUrl)
|
||||||
router.post('/', validators.podsAdd, addPods)
|
router.post('/', validators.podsAdd, addPods)
|
||||||
router.get('/makefriends',
|
router.post('/makefriends',
|
||||||
oAuth.authenticate,
|
oAuth.authenticate,
|
||||||
admin.ensureIsAdmin,
|
admin.ensureIsAdmin,
|
||||||
validators.makeFriends,
|
validators.makeFriends,
|
||||||
@ -83,7 +83,9 @@ function listPodsUrl (req, res, next) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function makeFriends (req, res, next) {
|
function makeFriends (req, res, next) {
|
||||||
friends.makeFriends(function (err) {
|
const urls = req.body.urls
|
||||||
|
|
||||||
|
friends.makeFriends(urls, function (err) {
|
||||||
if (err) return next(err)
|
if (err) return next(err)
|
||||||
|
|
||||||
res.type('json').status(204).end()
|
res.type('json').status(204).end()
|
||||||
|
@ -1,8 +1,11 @@
|
|||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
|
const validator = require('express-validator').validator
|
||||||
|
|
||||||
const miscValidators = {
|
const miscValidators = {
|
||||||
exists: exists,
|
exists: exists,
|
||||||
isArray: isArray
|
isArray: isArray,
|
||||||
|
isEachUrl: isEachUrl
|
||||||
}
|
}
|
||||||
|
|
||||||
function exists (value) {
|
function exists (value) {
|
||||||
@ -13,6 +16,12 @@ function isArray (value) {
|
|||||||
return Array.isArray(value)
|
return Array.isArray(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isEachUrl (urls) {
|
||||||
|
return urls.every(function (url) {
|
||||||
|
return validator.isURL(url)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
module.exports = miscValidators
|
module.exports = miscValidators
|
||||||
|
@ -17,8 +17,8 @@ function checkConfig () {
|
|||||||
const required = [ 'listen.port',
|
const required = [ 'listen.port',
|
||||||
'webserver.https', 'webserver.host', 'webserver.port',
|
'webserver.https', 'webserver.host', 'webserver.port',
|
||||||
'database.host', 'database.port', 'database.suffix',
|
'database.host', 'database.port', 'database.suffix',
|
||||||
'storage.certs', 'storage.uploads', 'storage.logs',
|
'storage.certs', 'storage.uploads', 'storage.logs', 'storage.thumbnails',
|
||||||
'network.friends', 'electron.debug' ]
|
'electron.debug' ]
|
||||||
const miss = []
|
const miss = []
|
||||||
|
|
||||||
for (const key of required) {
|
for (const key of required) {
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const config = require('config')
|
|
||||||
const each = require('async/each')
|
const each = require('async/each')
|
||||||
const eachLimit = require('async/eachLimit')
|
const eachLimit = require('async/eachLimit')
|
||||||
const eachSeries = require('async/eachSeries')
|
const eachSeries = require('async/eachSeries')
|
||||||
@ -44,7 +43,7 @@ function getMyCertificate (callback) {
|
|||||||
fs.readFile(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.pub', 'utf8', callback)
|
fs.readFile(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.pub', 'utf8', callback)
|
||||||
}
|
}
|
||||||
|
|
||||||
function makeFriends (callback) {
|
function makeFriends (urls, callback) {
|
||||||
const podsScore = {}
|
const podsScore = {}
|
||||||
|
|
||||||
logger.info('Make friends!')
|
logger.info('Make friends!')
|
||||||
@ -54,8 +53,6 @@ function makeFriends (callback) {
|
|||||||
return callback(err)
|
return callback(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
const urls = config.get('network.friends')
|
|
||||||
|
|
||||||
eachSeries(urls, function (url, callbackEach) {
|
eachSeries(urls, function (url, callbackEach) {
|
||||||
computeForeignPodsList(url, podsScore, callbackEach)
|
computeForeignPodsList(url, podsScore, callbackEach)
|
||||||
}, function (err) {
|
}, function (err) {
|
||||||
|
@ -10,6 +10,11 @@ const validatorsPod = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function makeFriends (req, res, next) {
|
function makeFriends (req, res, next) {
|
||||||
|
req.checkBody('urls', 'Should have an array of urls').isArray()
|
||||||
|
req.checkBody('urls', 'Should be an url').isEachUrl()
|
||||||
|
|
||||||
|
logger.debug('Checking makeFriends parameters', { parameters: req.body })
|
||||||
|
|
||||||
friends.hasFriends(function (err, hasFriends) {
|
friends.hasFriends(function (err, hasFriends) {
|
||||||
if (err) {
|
if (err) {
|
||||||
logger.error('Cannot know if we have friends.', { error: err })
|
logger.error('Cannot know if we have friends.', { error: err })
|
||||||
|
@ -108,10 +108,40 @@ describe('Test parameters validator', function () {
|
|||||||
})
|
})
|
||||||
|
|
||||||
describe('When making friends', function () {
|
describe('When making friends', function () {
|
||||||
|
const body = {
|
||||||
|
urls: [ 'http://localhost:9002' ]
|
||||||
|
}
|
||||||
|
|
||||||
|
it('Should fail without urls', function (done) {
|
||||||
|
request(server.url)
|
||||||
|
.post(path + '/makefriends')
|
||||||
|
.set('Authorization', 'Bearer faketoken')
|
||||||
|
.set('Accept', 'application/json')
|
||||||
|
.expect(401, done)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('Should fail with urls is not an array', function (done) {
|
||||||
|
request(server.url)
|
||||||
|
.post(path + '/makefriends')
|
||||||
|
.send({ urls: 'http://localhost:9002' })
|
||||||
|
.set('Authorization', 'Bearer faketoken')
|
||||||
|
.set('Accept', 'application/json')
|
||||||
|
.expect(401, done)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('Should fail if the array is not composed by urls', function (done) {
|
||||||
|
request(server.url)
|
||||||
|
.post(path + '/makefriends')
|
||||||
|
.send({ urls: [ 'http://localhost:9002', 'localhost:coucou' ] })
|
||||||
|
.set('Authorization', 'Bearer faketoken')
|
||||||
|
.set('Accept', 'application/json')
|
||||||
|
.expect(401, done)
|
||||||
|
})
|
||||||
|
|
||||||
it('Should fail with a invalid token', function (done) {
|
it('Should fail with a invalid token', function (done) {
|
||||||
request(server.url)
|
request(server.url)
|
||||||
.get(path + '/makefriends')
|
.post(path + '/makefriends')
|
||||||
.query({ start: 'hello' })
|
.send(body)
|
||||||
.set('Authorization', 'Bearer faketoken')
|
.set('Authorization', 'Bearer faketoken')
|
||||||
.set('Accept', 'application/json')
|
.set('Accept', 'application/json')
|
||||||
.expect(401, done)
|
.expect(401, done)
|
||||||
@ -119,8 +149,8 @@ describe('Test parameters validator', function () {
|
|||||||
|
|
||||||
it('Should fail if the user is not an administrator', function (done) {
|
it('Should fail if the user is not an administrator', function (done) {
|
||||||
request(server.url)
|
request(server.url)
|
||||||
.get(path + '/makefriends')
|
.post(path + '/makefriends')
|
||||||
.query({ start: 'hello' })
|
.send(body)
|
||||||
.set('Authorization', 'Bearer ' + userAccessToken)
|
.set('Authorization', 'Bearer ' + userAccessToken)
|
||||||
.set('Accept', 'application/json')
|
.set('Accept', 'application/json')
|
||||||
.expect(403, done)
|
.expect(403, done)
|
||||||
|
@ -27,13 +27,38 @@ function makeFriends (url, accessToken, expectedStatus, end) {
|
|||||||
expectedStatus = 204
|
expectedStatus = 204
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Which pod makes friends with which pod
|
||||||
|
const friendsMatrix = {
|
||||||
|
'http://localhost:9001': [
|
||||||
|
'http://localhost:9002'
|
||||||
|
],
|
||||||
|
'http://localhost:9002': [
|
||||||
|
'http://localhost:9003'
|
||||||
|
],
|
||||||
|
'http://localhost:9003': [
|
||||||
|
'http://localhost:9001'
|
||||||
|
],
|
||||||
|
'http://localhost:9004': [
|
||||||
|
'http://localhost:9002'
|
||||||
|
],
|
||||||
|
'http://localhost:9005': [
|
||||||
|
'http://localhost:9001',
|
||||||
|
'http://localhost:9004'
|
||||||
|
],
|
||||||
|
'http://localhost:9006': [
|
||||||
|
'http://localhost:9001',
|
||||||
|
'http://localhost:9002',
|
||||||
|
'http://localhost:9003'
|
||||||
|
]
|
||||||
|
}
|
||||||
const path = '/api/v1/pods/makefriends'
|
const path = '/api/v1/pods/makefriends'
|
||||||
|
|
||||||
// The first pod make friend with the third
|
// The first pod make friend with the third
|
||||||
request(url)
|
request(url)
|
||||||
.get(path)
|
.post(path)
|
||||||
.set('Accept', 'application/json')
|
.set('Accept', 'application/json')
|
||||||
.set('Authorization', 'Bearer ' + accessToken)
|
.set('Authorization', 'Bearer ' + accessToken)
|
||||||
|
.send({ 'urls': friendsMatrix[url] })
|
||||||
.expect(expectedStatus)
|
.expect(expectedStatus)
|
||||||
.end(function (err, res) {
|
.end(function (err, res) {
|
||||||
if (err) throw err
|
if (err) throw err
|
||||||
|
Loading…
Reference in New Issue
Block a user