mirror of
				https://github.com/Chocobozzz/PeerTube.git
				synced 2025-02-25 18:55:32 -06:00 
			
		
		
		
	Server: make friends urls come from the request instead of the
configuration file
This commit is contained in:
		@@ -18,8 +18,5 @@ storage:
 | 
			
		||||
  logs: 'logs/'
 | 
			
		||||
  thumbnails: 'thumbnails/'
 | 
			
		||||
 | 
			
		||||
network:
 | 
			
		||||
  friends: []
 | 
			
		||||
 | 
			
		||||
electron:
 | 
			
		||||
  debug: false
 | 
			
		||||
 
 | 
			
		||||
@@ -14,7 +14,3 @@ storage:
 | 
			
		||||
  uploads: 'test1/uploads/'
 | 
			
		||||
  logs: 'test1/logs/'
 | 
			
		||||
  thumbnails: 'test1/thumbnails/'
 | 
			
		||||
 | 
			
		||||
network:
 | 
			
		||||
  friends:
 | 
			
		||||
    - 'http://localhost:9002'
 | 
			
		||||
 
 | 
			
		||||
@@ -14,7 +14,3 @@ storage:
 | 
			
		||||
  uploads: 'test2/uploads/'
 | 
			
		||||
  logs: 'test2/logs/'
 | 
			
		||||
  thumbnails: 'test2/thumbnails/'
 | 
			
		||||
 | 
			
		||||
network:
 | 
			
		||||
  friends:
 | 
			
		||||
    - 'http://localhost:9003'
 | 
			
		||||
 
 | 
			
		||||
@@ -14,7 +14,3 @@ storage:
 | 
			
		||||
  uploads: 'test3/uploads/'
 | 
			
		||||
  logs: 'test3/logs/'
 | 
			
		||||
  thumbnails: 'test3/thumbnails/'
 | 
			
		||||
 | 
			
		||||
network:
 | 
			
		||||
  friends:
 | 
			
		||||
    - 'http://localhost:9001'
 | 
			
		||||
 
 | 
			
		||||
@@ -14,7 +14,3 @@ storage:
 | 
			
		||||
  uploads: 'test4/uploads/'
 | 
			
		||||
  logs: 'test4/logs/'
 | 
			
		||||
  thumbnails: 'test4/thumbnails/'
 | 
			
		||||
 | 
			
		||||
network:
 | 
			
		||||
  friends:
 | 
			
		||||
    - 'http://localhost:9002'
 | 
			
		||||
 
 | 
			
		||||
@@ -14,8 +14,3 @@ storage:
 | 
			
		||||
  uploads: 'test5/uploads/'
 | 
			
		||||
  logs: 'test5/logs/'
 | 
			
		||||
  thumbnails: 'test5/thumbnails/'
 | 
			
		||||
 | 
			
		||||
network:
 | 
			
		||||
  friends:
 | 
			
		||||
    - 'http://localhost:9001'
 | 
			
		||||
    - 'http://localhost:9004'
 | 
			
		||||
 
 | 
			
		||||
@@ -14,9 +14,3 @@ storage:
 | 
			
		||||
  uploads: 'test6/uploads/'
 | 
			
		||||
  logs: 'test6/logs/'
 | 
			
		||||
  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.post('/', validators.podsAdd, addPods)
 | 
			
		||||
router.get('/makefriends',
 | 
			
		||||
router.post('/makefriends',
 | 
			
		||||
  oAuth.authenticate,
 | 
			
		||||
  admin.ensureIsAdmin,
 | 
			
		||||
  validators.makeFriends,
 | 
			
		||||
@@ -83,7 +83,9 @@ function listPodsUrl (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)
 | 
			
		||||
 | 
			
		||||
    res.type('json').status(204).end()
 | 
			
		||||
 
 | 
			
		||||
@@ -1,8 +1,11 @@
 | 
			
		||||
'use strict'
 | 
			
		||||
 | 
			
		||||
const validator = require('express-validator').validator
 | 
			
		||||
 | 
			
		||||
const miscValidators = {
 | 
			
		||||
  exists: exists,
 | 
			
		||||
  isArray: isArray
 | 
			
		||||
  isArray: isArray,
 | 
			
		||||
  isEachUrl: isEachUrl
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function exists (value) {
 | 
			
		||||
@@ -13,6 +16,12 @@ function isArray (value) {
 | 
			
		||||
  return Array.isArray(value)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function isEachUrl (urls) {
 | 
			
		||||
  return urls.every(function (url) {
 | 
			
		||||
    return validator.isURL(url)
 | 
			
		||||
  })
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ---------------------------------------------------------------------------
 | 
			
		||||
 | 
			
		||||
module.exports = miscValidators
 | 
			
		||||
 
 | 
			
		||||
@@ -17,8 +17,8 @@ function checkConfig () {
 | 
			
		||||
  const required = [ 'listen.port',
 | 
			
		||||
    'webserver.https', 'webserver.host', 'webserver.port',
 | 
			
		||||
    'database.host', 'database.port', 'database.suffix',
 | 
			
		||||
    'storage.certs', 'storage.uploads', 'storage.logs',
 | 
			
		||||
    'network.friends', 'electron.debug' ]
 | 
			
		||||
    'storage.certs', 'storage.uploads', 'storage.logs', 'storage.thumbnails',
 | 
			
		||||
    'electron.debug' ]
 | 
			
		||||
  const miss = []
 | 
			
		||||
 | 
			
		||||
  for (const key of required) {
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,5 @@
 | 
			
		||||
'use strict'
 | 
			
		||||
 | 
			
		||||
const config = require('config')
 | 
			
		||||
const each = require('async/each')
 | 
			
		||||
const eachLimit = require('async/eachLimit')
 | 
			
		||||
const eachSeries = require('async/eachSeries')
 | 
			
		||||
@@ -44,7 +43,7 @@ function getMyCertificate (callback) {
 | 
			
		||||
  fs.readFile(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.pub', 'utf8', callback)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function makeFriends (callback) {
 | 
			
		||||
function makeFriends (urls, callback) {
 | 
			
		||||
  const podsScore = {}
 | 
			
		||||
 | 
			
		||||
  logger.info('Make friends!')
 | 
			
		||||
@@ -54,8 +53,6 @@ function makeFriends (callback) {
 | 
			
		||||
      return callback(err)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    const urls = config.get('network.friends')
 | 
			
		||||
 | 
			
		||||
    eachSeries(urls, function (url, callbackEach) {
 | 
			
		||||
      computeForeignPodsList(url, podsScore, callbackEach)
 | 
			
		||||
    }, function (err) {
 | 
			
		||||
 
 | 
			
		||||
@@ -10,6 +10,11 @@ const validatorsPod = {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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) {
 | 
			
		||||
    if (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 () {
 | 
			
		||||
        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) {
 | 
			
		||||
          request(server.url)
 | 
			
		||||
            .get(path + '/makefriends')
 | 
			
		||||
            .query({ start: 'hello' })
 | 
			
		||||
            .post(path + '/makefriends')
 | 
			
		||||
            .send(body)
 | 
			
		||||
            .set('Authorization', 'Bearer faketoken')
 | 
			
		||||
            .set('Accept', 'application/json')
 | 
			
		||||
            .expect(401, done)
 | 
			
		||||
@@ -119,8 +149,8 @@ describe('Test parameters validator', function () {
 | 
			
		||||
 | 
			
		||||
        it('Should fail if the user is not an administrator', function (done) {
 | 
			
		||||
          request(server.url)
 | 
			
		||||
            .get(path + '/makefriends')
 | 
			
		||||
            .query({ start: 'hello' })
 | 
			
		||||
            .post(path + '/makefriends')
 | 
			
		||||
            .send(body)
 | 
			
		||||
            .set('Authorization', 'Bearer ' + userAccessToken)
 | 
			
		||||
            .set('Accept', 'application/json')
 | 
			
		||||
            .expect(403, done)
 | 
			
		||||
 
 | 
			
		||||
@@ -27,13 +27,38 @@ function makeFriends (url, accessToken, expectedStatus, end) {
 | 
			
		||||
    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'
 | 
			
		||||
 | 
			
		||||
  // The first pod make friend with the third
 | 
			
		||||
  request(url)
 | 
			
		||||
    .get(path)
 | 
			
		||||
    .post(path)
 | 
			
		||||
    .set('Accept', 'application/json')
 | 
			
		||||
    .set('Authorization', 'Bearer ' + accessToken)
 | 
			
		||||
    .send({ 'urls': friendsMatrix[url] })
 | 
			
		||||
    .expect(expectedStatus)
 | 
			
		||||
    .end(function (err, res) {
 | 
			
		||||
      if (err) throw err
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user