X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Ffriends.js;h=424a30801f441f90960b220bbb6b1e656d355536;hb=9e167724f7e933f41d9ea2e1c31772bf4c560a28;hp=4afb91b8bf23512d978ce4f3befbfde9d4054796;hpb=55fa55a9be566cca2ba95322f2ae23b434aed62a;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/friends.js b/server/lib/friends.js index 4afb91b8b..424a30801 100644 --- a/server/lib/friends.js +++ b/server/lib/friends.js @@ -3,41 +3,86 @@ const each = require('async/each') const eachLimit = require('async/eachLimit') const eachSeries = require('async/eachSeries') -const fs = require('fs') const request = require('request') const waterfall = require('async/waterfall') const constants = require('../initializers/constants') const db = require('../initializers/database') const logger = require('../helpers/logger') +const peertubeCrypto = require('../helpers/peertube-crypto') const requests = require('../helpers/requests') +const RequestScheduler = require('./request-scheduler') +const RequestVideoQaduScheduler = require('./request-video-qadu-scheduler') + +const ENDPOINT_ACTIONS = constants.REQUEST_ENDPOINT_ACTIONS[constants.REQUEST_ENDPOINTS.VIDEOS] + +const requestScheduler = new RequestScheduler() +const requestSchedulerVideoQadu = new RequestVideoQaduScheduler() const friends = { + activate, addVideoToFriends, updateVideoToFriends, reportAbuseVideoToFriend, + quickAndDirtyUpdateVideoToFriends, hasFriends, - getMyCertificate, makeFriends, quitFriends, removeVideoToFriends, sendOwnedVideosToPod } -function addVideoToFriends (videoData) { - createRequest('add', constants.REQUEST_ENDPOINTS.VIDEOS, videoData) +function activate () { + requestScheduler.activate() + requestSchedulerVideoQadu.activate() } -function updateVideoToFriends (videoData) { - createRequest('update', constants.REQUEST_ENDPOINTS.VIDEOS, videoData) +function addVideoToFriends (videoData, transaction, callback) { + const options = { + type: ENDPOINT_ACTIONS.ADD, + endpoint: constants.REQUEST_ENDPOINTS.VIDEOS, + data: videoData, + transaction + } + createRequest(options, callback) +} + +function updateVideoToFriends (videoData, transaction, callback) { + const options = { + type: ENDPOINT_ACTIONS.UPDATE, + endpoint: constants.REQUEST_ENDPOINTS.VIDEOS, + data: videoData, + transaction + } + createRequest(options, callback) } function removeVideoToFriends (videoParams) { - createRequest('remove', constants.REQUEST_ENDPOINTS.VIDEOS, videoParams) + const options = { + type: ENDPOINT_ACTIONS.REMOVE, + endpoint: constants.REQUEST_ENDPOINTS.VIDEOS, + data: videoParams + } + createRequest(options) } function reportAbuseVideoToFriend (reportData, video) { - createRequest('report-abuse', constants.REQUEST_ENDPOINTS.VIDEOS, reportData, [ video.Author.podId ]) + const options = { + type: ENDPOINT_ACTIONS.REPORT_ABUSE, + endpoint: constants.REQUEST_ENDPOINTS.VIDEOS, + data: reportData, + toIds: [ video.Author.podId ] + } + createRequest(options) +} + +function quickAndDirtyUpdateVideoToFriends (videoId, type, transaction, callback) { + const options = { + videoId, + type, + transaction + } + return createVideoQaduRequest(options, callback) } function hasFriends (callback) { @@ -49,15 +94,11 @@ function hasFriends (callback) { }) } -function getMyCertificate (callback) { - fs.readFile(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.pub', 'utf8', callback) -} - function makeFriends (hosts, callback) { const podsScore = {} logger.info('Make friends!') - getMyCertificate(function (err, cert) { + peertubeCrypto.getMyPublicCert(function (err, cert) { if (err) { logger.error('Cannot read public cert.') return callback(err) @@ -79,11 +120,15 @@ function makeFriends (hosts, callback) { function quitFriends (callback) { // Stop pool requests - db.Request.deactivate() + requestScheduler.deactivate() waterfall([ function flushRequests (callbackAsync) { - db.Request.flush(callbackAsync) + requestScheduler.flush(err => callbackAsync(err)) + }, + + function flushVideoQaduRequests (callbackAsync) { + requestSchedulerVideoQadu.flush(err => callbackAsync(err)) }, function getPodsList (callbackAsync) { @@ -120,7 +165,7 @@ function quitFriends (callback) { } ], function (err) { // Don't forget to re activate the scheduler, even if there was an error - db.Request.activate() + requestScheduler.activate() if (err) return callback(err) @@ -144,7 +189,13 @@ function sendOwnedVideosToPod (podId) { return } - createRequest('add', constants.REQUEST_ENDPOINTS.VIDEOS, remoteVideo, [ podId ]) + const options = { + type: 'add', + endpoint: constants.REQUEST_ENDPOINTS.VIDEOS, + data: remoteVideo, + toIds: [ podId ] + } + createRequest(options) }) }) }) @@ -172,7 +223,7 @@ function computeForeignPodsList (host, podsScore, callback) { else podsScore[foreignPodHost] = 1 }) - callback() + return callback() }) } @@ -181,6 +232,7 @@ function computeWinningPods (hosts, podsScore) { // Only add a pod if it exists in more than a half base pods const podsList = [] const baseScore = hosts.length / 2 + Object.keys(podsScore).forEach(function (podHost) { // If the pod is not me and with a good score we add it if (isMe(podHost) === false && podsScore[podHost] > baseScore) { @@ -208,9 +260,9 @@ function getForeignPodsList (host, callback) { function makeRequestsToWinningPods (cert, podsList, callback) { // Stop pool requests - db.Request.deactivate() + requestScheduler.deactivate() // Flush pool requests - db.Request.forceSend() + requestScheduler.forceSend() eachLimit(podsList, constants.REQUESTS_IN_PARALLEL, function (pod, callbackEach) { const params = { @@ -218,6 +270,7 @@ function makeRequestsToWinningPods (cert, podsList, callback) { method: 'POST', json: { host: constants.CONFIG.WEBSERVER.HOST, + email: constants.CONFIG.ADMIN.EMAIL, publicKey: cert } } @@ -230,7 +283,7 @@ function makeRequestsToWinningPods (cert, podsList, callback) { } if (res.statusCode === 200) { - const podObj = db.Pod.build({ host: pod.host, publicKey: body.cert }) + const podObj = db.Pod.build({ host: pod.host, publicKey: body.cert, email: body.email }) podObj.save().asCallback(function (err, podCreated) { if (err) { logger.error('Cannot add friend %s pod.', pod.host, { error: err }) @@ -250,7 +303,7 @@ function makeRequestsToWinningPods (cert, podsList, callback) { }, function endRequests () { // Final callback, we've ended all the requests // Now we made new friends, we can re activate the pool of requests - db.Request.activate() + requestScheduler.activate() logger.debug('makeRequestsToWinningPods finished.') return callback() @@ -258,50 +311,27 @@ function makeRequestsToWinningPods (cert, podsList, callback) { } // Wrapper that populate "toIds" argument with all our friends if it is not specified -function createRequest (type, endpoint, data, toIds) { - if (toIds) return _createRequest(type, endpoint, data, toIds) +// { type, endpoint, data, toIds, transaction } +function createRequest (options, callback) { + if (!callback) callback = function () {} + if (options.toIds) return requestScheduler.createRequest(options, callback) // If the "toIds" pods is not specified, we send the request to all our friends - db.Pod.listAllIds(function (err, podIds) { + db.Pod.listAllIds(options.transaction, function (err, podIds) { if (err) { logger.error('Cannot get pod ids', { error: err }) return } - return _createRequest(type, endpoint, data, podIds) + const newOptions = Object.assign(options, { toIds: podIds }) + return requestScheduler.createRequest(newOptions, callback) }) } -function _createRequest (type, endpoint, data, toIds) { - const pods = [] - - // If there are no destination pods abort - if (toIds.length === 0) return - - toIds.forEach(function (toPod) { - pods.push(db.Pod.build({ id: toPod })) - }) - - const createQuery = { - endpoint, - request: { - type: type, - data: data - } - } +function createVideoQaduRequest (options, callback) { + if (!callback) callback = function () {} - // We run in transaction to keep coherency between Request and RequestToPod tables - db.sequelize.transaction(function (t) { - const dbRequestOptions = { - transaction: t - } - - return db.Request.create(createQuery, dbRequestOptions).then(function (request) { - return request.setPods(pods, dbRequestOptions) - }) - }).asCallback(function (err) { - if (err) logger.error('Error in createRequest transaction.', { error: err }) - }) + requestSchedulerVideoQadu.createRequest(options, callback) } function isMe (host) {