Server: add config endpoint
[oweals/peertube.git] / server / lib / friends.js
index 9b38693c705438f6243dc315f85b7afdafe17be5..23accfa453168ff72dda8efbf884a41b35f3bea3 100644 (file)
@@ -3,6 +3,7 @@
 const each = require('async/each')
 const eachLimit = require('async/eachLimit')
 const eachSeries = require('async/eachSeries')
+const series = require('async/series')
 const request = require('request')
 const waterfall = require('async/waterfall')
 
@@ -11,18 +12,40 @@ const db = require('../initializers/database')
 const logger = require('../helpers/logger')
 const peertubeCrypto = require('../helpers/peertube-crypto')
 const requests = require('../helpers/requests')
+const utils = require('../helpers/utils')
+const RequestScheduler = require('./request-scheduler')
+const RequestVideoQaduScheduler = require('./request-video-qadu-scheduler')
+const RequestVideoEventScheduler = require('./request-video-event-scheduler')
 
 const ENDPOINT_ACTIONS = constants.REQUEST_ENDPOINT_ACTIONS[constants.REQUEST_ENDPOINTS.VIDEOS]
 
+const requestScheduler = new RequestScheduler()
+const requestVideoQaduScheduler = new RequestVideoQaduScheduler()
+const requestVideoEventScheduler = new RequestVideoEventScheduler()
+
 const friends = {
+  activate,
   addVideoToFriends,
   updateVideoToFriends,
   reportAbuseVideoToFriend,
+  quickAndDirtyUpdateVideoToFriends,
+  quickAndDirtyUpdatesVideoToFriends,
+  addEventToRemoteVideo,
+  addEventsToRemoteVideo,
   hasFriends,
   makeFriends,
   quitFriends,
   removeVideoToFriends,
-  sendOwnedVideosToPod
+  sendOwnedVideosToPod,
+  getRequestScheduler,
+  getRequestVideoQaduScheduler,
+  getRequestVideoEventScheduler
+}
+
+function activate () {
+  requestScheduler.activate()
+  requestVideoQaduScheduler.activate()
+  requestVideoEventScheduler.activate()
 }
 
 function addVideoToFriends (videoData, transaction, callback) {
@@ -64,6 +87,52 @@ function reportAbuseVideoToFriend (reportData, video) {
   createRequest(options)
 }
 
+function quickAndDirtyUpdateVideoToFriends (qaduParams, transaction, callback) {
+  const options = {
+    videoId: qaduParams.videoId,
+    type: qaduParams.type,
+    transaction
+  }
+  return createVideoQaduRequest(options, callback)
+}
+
+function quickAndDirtyUpdatesVideoToFriends (qadusParams, transaction, finalCallback) {
+  const tasks = []
+
+  qadusParams.forEach(function (qaduParams) {
+    const fun = function (callback) {
+      quickAndDirtyUpdateVideoToFriends(qaduParams, transaction, callback)
+    }
+
+    tasks.push(fun)
+  })
+
+  series(tasks, finalCallback)
+}
+
+function addEventToRemoteVideo (eventParams, transaction, callback) {
+  const options = {
+    videoId: eventParams.videoId,
+    type: eventParams.type,
+    transaction
+  }
+  createVideoEventRequest(options, callback)
+}
+
+function addEventsToRemoteVideo (eventsParams, transaction, finalCallback) {
+  const tasks = []
+
+  eventsParams.forEach(function (eventParams) {
+    const fun = function (callback) {
+      addEventToRemoteVideo(eventParams, transaction, callback)
+    }
+
+    tasks.push(fun)
+  })
+
+  series(tasks, finalCallback)
+}
+
 function hasFriends (callback) {
   db.Pod.countAll(function (err, count) {
     if (err) return callback(err)
@@ -99,11 +168,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) {
+      requestVideoQaduScheduler.flush(err => callbackAsync(err))
     },
 
     function getPodsList (callbackAsync) {
@@ -140,7 +213,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)
 
@@ -176,6 +249,18 @@ function sendOwnedVideosToPod (podId) {
   })
 }
 
+function getRequestScheduler () {
+  return requestScheduler
+}
+
+function getRequestVideoQaduScheduler () {
+  return requestVideoQaduScheduler
+}
+
+function getRequestVideoEventScheduler () {
+  return requestVideoEventScheduler
+}
+
 // ---------------------------------------------------------------------------
 
 module.exports = friends
@@ -235,9 +320,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 = {
@@ -278,7 +363,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()
@@ -289,7 +374,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) {
@@ -299,44 +384,20 @@ 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
-    }
-  }
+function createVideoQaduRequest (options, callback) {
+  if (!callback) callback = utils.createEmptyCallback()
 
-  const dbRequestOptions = {
-    transaction
-  }
+  requestVideoQaduScheduler.createRequest(options, callback)
+}
 
-  return db.Request.create(createQuery, dbRequestOptions).asCallback(function (err, request) {
-    if (err) return callback(err)
+function createVideoEventRequest (options, callback) {
+  if (!callback) callback = utils.createEmptyCallback()
 
-    return request.setPods(pods, dbRequestOptions).asCallback(callback)
-  })
+  requestVideoEventScheduler.createRequest(options, callback)
 }
 
 function isMe (host) {