]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/friends.js
Server: make a basic "quick and dirty update" for videos
[github/Chocobozzz/PeerTube.git] / server / lib / friends.js
index 3d3d0fdeed1da06bb6f0cc37147a5b5f3a32f0a8..424a30801f441f90960b220bbb6b1e656d355536 100644 (file)
@@ -3,30 +3,43 @@
 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 activate () {
+  requestScheduler.activate()
+  requestSchedulerVideoQadu.activate()
+}
+
 function addVideoToFriends (videoData, transaction, callback) {
   const options = {
-    type: 'add',
+    type: ENDPOINT_ACTIONS.ADD,
     endpoint: constants.REQUEST_ENDPOINTS.VIDEOS,
     data: videoData,
     transaction
@@ -36,7 +49,7 @@ function addVideoToFriends (videoData, transaction, callback) {
 
 function updateVideoToFriends (videoData, transaction, callback) {
   const options = {
-    type: 'update',
+    type: ENDPOINT_ACTIONS.UPDATE,
     endpoint: constants.REQUEST_ENDPOINTS.VIDEOS,
     data: videoData,
     transaction
@@ -46,7 +59,7 @@ function updateVideoToFriends (videoData, transaction, callback) {
 
 function removeVideoToFriends (videoParams) {
   const options = {
-    type: 'remove',
+    type: ENDPOINT_ACTIONS.REMOVE,
     endpoint: constants.REQUEST_ENDPOINTS.VIDEOS,
     data: videoParams
   }
@@ -54,7 +67,22 @@ function removeVideoToFriends (videoParams) {
 }
 
 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) {
@@ -66,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)
@@ -96,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) {
@@ -137,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)
 
@@ -161,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)
       })
     })
   })
@@ -189,7 +223,7 @@ function computeForeignPodsList (host, podsScore, callback) {
       else podsScore[foreignPodHost] = 1
     })
 
-    callback()
+    return callback()
   })
 }
 
@@ -198,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) {
@@ -225,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 = {
@@ -235,6 +270,7 @@ function makeRequestsToWinningPods (cert, podsList, callback) {
       method: 'POST',
       json: {
         host: constants.CONFIG.WEBSERVER.HOST,
+        email: constants.CONFIG.ADMIN.EMAIL,
         publicKey: cert
       }
     }
@@ -247,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 })
@@ -267,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()
@@ -278,7 +314,7 @@ function makeRequestsToWinningPods (cert, podsList, callback) {
 // { type, endpoint, data, toIds, transaction }
 function createRequest (options, callback) {
   if (!callback) callback = function () {}
-  if (options.toIds) return _createRequest(options, callback)
+  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(options.transaction, function (err, podIds) {
@@ -288,44 +324,14 @@ function createRequest (options, callback) {
     }
 
     const newOptions = Object.assign(options, { toIds: podIds })
-    return _createRequest(newOptions, callback)
+    return requestScheduler.createRequest(newOptions, callback)
   })
 }
 
-// { type, endpoint, data, toIds, transaction }
-function _createRequest (options, callback) {
-  const type = options.type
-  const endpoint = options.endpoint
-  const data = options.data
-  const toIds = options.toIds
-  const transaction = options.transaction
-
-  const pods = []
-
-  // If there are no destination pods abort
-  if (toIds.length === 0) return callback(null)
-
-  toIds.forEach(function (toPod) {
-    pods.push(db.Pod.build({ id: toPod }))
-  })
-
-  const createQuery = {
-    endpoint,
-    request: {
-      type: type,
-      data: data
-    }
-  }
-
-  const dbRequestOptions = {
-    transaction
-  }
-
-  return db.Request.create(createQuery, dbRequestOptions).asCallback(function (err, request) {
-    if (err) return callback(err)
+function createVideoQaduRequest (options, callback) {
+  if (!callback) callback = function () {}
 
-    return request.setPods(pods, dbRequestOptions).asCallback(callback)
-  })
+  requestSchedulerVideoQadu.createRequest(options, callback)
 }
 
 function isMe (host) {