]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/friends.js
Do not generate a random password for test env
[github/Chocobozzz/PeerTube.git] / server / lib / friends.js
index d81a603ad5267e4007cd139c6e54923ade9a8e36..617cc1ab4522e227781e29e350357cf014a4e39e 100644 (file)
@@ -3,20 +3,20 @@
 const async = require('async')
 const config = require('config')
 const fs = require('fs')
+const mongoose = require('mongoose')
 const request = require('request')
 
 const constants = require('../initializers/constants')
 const logger = require('../helpers/logger')
 const peertubeCrypto = require('../helpers/peertubeCrypto')
 const Pods = require('../models/pods')
-const requestsScheduler = require('../lib/requestsScheduler')
 const requests = require('../helpers/requests')
-const videos = require('../lib/videos')
-const Videos = require('../models/videos')
 
 const http = config.get('webserver.https') ? 'https' : 'http'
 const host = config.get('webserver.host')
 const port = config.get('webserver.port')
+const Request = mongoose.model('Request')
+const Video = mongoose.model('Video')
 
 const pods = {
   addVideoToFriends: addVideoToFriends,
@@ -29,10 +29,7 @@ const pods = {
 }
 
 function addVideoToFriends (video) {
-  // ensure namePath is null
-  video.namePath = null
-
-  requestsScheduler.addRequest('add', video)
+  createRequest('add', video)
 }
 
 function hasFriends (callback) {
@@ -76,9 +73,9 @@ function makeFriends (callback) {
 
 function quitFriends (callback) {
   // Stop pool requests
-  requestsScheduler.deactivate()
+  Request.deactivate()
   // Flush pool requests
-  requestsScheduler.flush()
+  Request.flush()
 
   async.waterfall([
     function getPodsList (callbackAsync) {
@@ -117,22 +114,17 @@ function quitFriends (callback) {
     function listRemoteVideos (callbackAsync) {
       logger.info('Broke friends, so sad :(')
 
-      Videos.listFromRemotes(callbackAsync)
+      Video.listRemotes(callbackAsync)
     },
 
     function removeTheRemoteVideos (videosList, callbackAsync) {
-      videos.removeRemoteVideos(videosList, function (err) {
-        if (err) {
-          logger.error('Cannot remove remote videos.', { error: err })
-          return callbackAsync(err)
-        }
-
-        return callbackAsync(null)
-      })
+      async.each(videosList, function (video, callbackEach) {
+        video.remove(callbackEach)
+      }, callbackAsync)
     }
   ], function (err) {
     // Don't forget to re activate the scheduler, even if there was an error
-    requestsScheduler.activate()
+    Request.activate()
 
     if (err) return callback(err)
 
@@ -141,26 +133,26 @@ function quitFriends (callback) {
   })
 }
 
-function removeVideoToFriends (video) {
-  requestsScheduler.addRequest('remove', video)
+function removeVideoToFriends (videoParams) {
+  createRequest('remove', videoParams)
 }
 
 function sendOwnedVideosToPod (podId) {
-  Videos.listOwned(function (err, videosList) {
+  Video.listOwned(function (err, videosList) {
     if (err) {
       logger.error('Cannot get the list of videos we own.')
       return
     }
 
     videosList.forEach(function (video) {
-      videos.convertVideoToRemote(video, function (err, remoteVideo) {
+      video.toRemoteJSON(function (err, remoteVideo) {
         if (err) {
           logger.error('Cannot convert video to remote.', { error: err })
           // Don't break the process
           return
         }
 
-        requestsScheduler.addRequestTo([ podId ], 'add', remoteVideo)
+        createRequest('add', remoteVideo, [ podId ])
       })
     })
   })
@@ -216,9 +208,9 @@ function getForeignPodsList (url, callback) {
 
 function makeRequestsToWinningPods (cert, podsList, callback) {
   // Stop pool requests
-  requestsScheduler.deactivate()
+  Request.deactivate()
   // Flush pool requests
-  requestsScheduler.forceSend()
+  Request.forceSend()
 
   async.eachLimit(podsList, constants.REQUESTS_IN_PARALLEL, function (pod, callbackEach) {
     const params = {
@@ -254,9 +246,26 @@ 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
-    requestsScheduler.activate()
+    Request.activate()
 
     logger.debug('makeRequestsToWinningPods finished.')
     return callback()
   })
 }
+
+function createRequest (type, data, to) {
+  const req = new Request({
+    request: {
+      type: type,
+      data: data
+    }
+  })
+
+  if (to) {
+    req.to = to
+  }
+
+  req.save(function (err) {
+    if (err) logger.error('Cannot save the request.', { error: err })
+  })
+}