aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--server/controllers/api/v1/pods.js19
-rw-r--r--server/helpers/customValidators.js1
-rw-r--r--server/initializers/constants.js9
-rw-r--r--server/initializers/database.js1
-rw-r--r--server/lib/friends.js20
-rw-r--r--server/middlewares/secure.js6
-rw-r--r--server/models/pods.js102
-rw-r--r--server/models/request.js48
8 files changed, 109 insertions, 97 deletions
diff --git a/server/controllers/api/v1/pods.js b/server/controllers/api/v1/pods.js
index 9dd9197b3..feb6bd958 100644
--- a/server/controllers/api/v1/pods.js
+++ b/server/controllers/api/v1/pods.js
@@ -7,12 +7,12 @@ const mongoose = require('mongoose')
7const logger = require('../../../helpers/logger') 7const logger = require('../../../helpers/logger')
8const friends = require('../../../lib/friends') 8const friends = require('../../../lib/friends')
9const middlewares = require('../../../middlewares') 9const middlewares = require('../../../middlewares')
10const Pods = require('../../../models/pods')
11const oAuth2 = middlewares.oauth2 10const oAuth2 = middlewares.oauth2
12const reqValidator = middlewares.reqValidators.pods 11const reqValidator = middlewares.reqValidators.pods
13const signatureValidator = middlewares.reqValidators.remote.signature 12const signatureValidator = middlewares.reqValidators.remote.signature
14 13
15const router = express.Router() 14const router = express.Router()
15const Pod = mongoose.model('Pod')
16const Video = mongoose.model('Video') 16const Video = mongoose.model('Video')
17 17
18router.get('/', listPodsUrl) 18router.get('/', listPodsUrl)
@@ -33,7 +33,11 @@ function addPods (req, res, next) {
33 33
34 async.waterfall([ 34 async.waterfall([
35 function addPod (callback) { 35 function addPod (callback) {
36 Pods.add(informations, callback) 36 const pod = new Pod(informations)
37 pod.save(function (err, podCreated) {
38 // Be sure about the number of parameters for the callback
39 return callback(err, podCreated)
40 })
37 }, 41 },
38 42
39 function sendMyVideos (podCreated, callback) { 43 function sendMyVideos (podCreated, callback) {
@@ -60,7 +64,7 @@ function addPods (req, res, next) {
60} 64}
61 65
62function listPodsUrl (req, res, next) { 66function listPodsUrl (req, res, next) {
63 Pods.listAllUrls(function (err, podsUrlList) { 67 Pod.listOnlyUrls(function (err, podsUrlList) {
64 if (err) return next(err) 68 if (err) return next(err)
65 69
66 res.json(podsUrlList) 70 res.json(podsUrlList)
@@ -79,8 +83,13 @@ function removePods (req, res, next) {
79 const url = req.body.signature.url 83 const url = req.body.signature.url
80 84
81 async.waterfall([ 85 async.waterfall([
82 function (callback) { 86 function loadPod (callback) {
83 Pods.remove(url, function (err) { 87 Pod.loadByUrl(url, callback)
88 },
89
90 function removePod (pod, callback) {
91 pod.remove(function (err) {
92 // Be sure we only return one argument in the callback
84 return callback(err) 93 return callback(err)
85 }) 94 })
86 }, 95 },
diff --git a/server/helpers/customValidators.js b/server/helpers/customValidators.js
index 4d6139a3d..b666644c0 100644
--- a/server/helpers/customValidators.js
+++ b/server/helpers/customValidators.js
@@ -85,6 +85,7 @@ function isVideoNameValid (value) {
85} 85}
86 86
87function isVideoPodUrlValid (value) { 87function isVideoPodUrlValid (value) {
88 // TODO: set options (TLD...)
88 return validator.isURL(value) 89 return validator.isURL(value)
89} 90}
90 91
diff --git a/server/initializers/constants.js b/server/initializers/constants.js
index 1f9876c4b..55c555d04 100644
--- a/server/initializers/constants.js
+++ b/server/initializers/constants.js
@@ -4,7 +4,10 @@
4const API_VERSION = 'v1' 4const API_VERSION = 'v1'
5 5
6// Score a pod has when we create it as a friend 6// Score a pod has when we create it as a friend
7let FRIEND_BASE_SCORE = 100 7const FRIEND_SCORE = {
8 BASE: 100,
9 MAX: 1000
10}
8 11
9// Time to wait between requests to the friends (10 min) 12// Time to wait between requests to the friends (10 min)
10let INTERVAL = 600000 13let INTERVAL = 600000
@@ -54,7 +57,7 @@ const VIDEOS_CONSTRAINTS_FIELDS = {
54 57
55// Special constants for a test instance 58// Special constants for a test instance
56if (isTestInstance() === true) { 59if (isTestInstance() === true) {
57 FRIEND_BASE_SCORE = 20 60 FRIEND_SCORE.BASE = 20
58 INTERVAL = 10000 61 INTERVAL = 10000
59 VIDEOS_CONSTRAINTS_FIELDS.DURATION.max = 14 62 VIDEOS_CONSTRAINTS_FIELDS.DURATION.max = 14
60} 63}
@@ -63,7 +66,7 @@ if (isTestInstance() === true) {
63 66
64module.exports = { 67module.exports = {
65 API_VERSION: API_VERSION, 68 API_VERSION: API_VERSION,
66 FRIEND_BASE_SCORE: FRIEND_BASE_SCORE, 69 FRIEND_SCORE: FRIEND_SCORE,
67 INTERVAL: INTERVAL, 70 INTERVAL: INTERVAL,
68 PAGINATION_COUNT_DEFAULT: PAGINATION_COUNT_DEFAULT, 71 PAGINATION_COUNT_DEFAULT: PAGINATION_COUNT_DEFAULT,
69 PODS_SCORE: PODS_SCORE, 72 PODS_SCORE: PODS_SCORE,
diff --git a/server/initializers/database.js b/server/initializers/database.js
index d0d969663..e97531781 100644
--- a/server/initializers/database.js
+++ b/server/initializers/database.js
@@ -6,6 +6,7 @@ const mongoose = require('mongoose')
6const logger = require('../helpers/logger') 6const logger = require('../helpers/logger')
7 7
8// Bootstrap models 8// Bootstrap models
9require('../models/pods')
9require('../models/video') 10require('../models/video')
10// Request model needs Video model 11// Request model needs Video model
11require('../models/request') 12require('../models/request')
diff --git a/server/lib/friends.js b/server/lib/friends.js
index 617cc1ab4..a93467c13 100644
--- a/server/lib/friends.js
+++ b/server/lib/friends.js
@@ -9,16 +9,16 @@ const request = require('request')
9const constants = require('../initializers/constants') 9const constants = require('../initializers/constants')
10const logger = require('../helpers/logger') 10const logger = require('../helpers/logger')
11const peertubeCrypto = require('../helpers/peertubeCrypto') 11const peertubeCrypto = require('../helpers/peertubeCrypto')
12const Pods = require('../models/pods')
13const requests = require('../helpers/requests') 12const requests = require('../helpers/requests')
14 13
15const http = config.get('webserver.https') ? 'https' : 'http' 14const http = config.get('webserver.https') ? 'https' : 'http'
16const host = config.get('webserver.host') 15const host = config.get('webserver.host')
17const port = config.get('webserver.port') 16const port = config.get('webserver.port')
17const Pod = mongoose.model('Pod')
18const Request = mongoose.model('Request') 18const Request = mongoose.model('Request')
19const Video = mongoose.model('Video') 19const Video = mongoose.model('Video')
20 20
21const pods = { 21const friends = {
22 addVideoToFriends: addVideoToFriends, 22 addVideoToFriends: addVideoToFriends,
23 hasFriends: hasFriends, 23 hasFriends: hasFriends,
24 getMyCertificate: getMyCertificate, 24 getMyCertificate: getMyCertificate,
@@ -33,7 +33,7 @@ function addVideoToFriends (video) {
33} 33}
34 34
35function hasFriends (callback) { 35function hasFriends (callback) {
36 Pods.count(function (err, count) { 36 Pod.countAll(function (err, count) {
37 if (err) return callback(err) 37 if (err) return callback(err)
38 38
39 const hasFriends = (count !== 0) 39 const hasFriends = (count !== 0)
@@ -79,7 +79,7 @@ function quitFriends (callback) {
79 79
80 async.waterfall([ 80 async.waterfall([
81 function getPodsList (callbackAsync) { 81 function getPodsList (callbackAsync) {
82 return Pods.list(callbackAsync) 82 return Pod.list(callbackAsync)
83 }, 83 },
84 84
85 function announceIQuitMyFriends (pods, callbackAsync) { 85 function announceIQuitMyFriends (pods, callbackAsync) {
@@ -106,7 +106,7 @@ function quitFriends (callback) {
106 }, 106 },
107 107
108 function removePodsFromDB (callbackAsync) { 108 function removePodsFromDB (callbackAsync) {
109 Pods.removeAll(function (err) { 109 Pod.removeAll(function (err) {
110 return callbackAsync(err) 110 return callbackAsync(err)
111 }) 111 })
112 }, 112 },
@@ -160,7 +160,7 @@ function sendOwnedVideosToPod (podId) {
160 160
161// --------------------------------------------------------------------------- 161// ---------------------------------------------------------------------------
162 162
163module.exports = pods 163module.exports = friends
164 164
165// --------------------------------------------------------------------------- 165// ---------------------------------------------------------------------------
166 166
@@ -230,8 +230,12 @@ function makeRequestsToWinningPods (cert, podsList, callback) {
230 } 230 }
231 231
232 if (res.statusCode === 200) { 232 if (res.statusCode === 200) {
233 Pods.add({ url: pod.url, publicKey: body.cert, score: constants.FRIEND_BASE_SCORE }, function (err, podCreated) { 233 const podObj = new Pod({ url: pod.url, publicKey: body.cert })
234 if (err) logger.error('Cannot add friend %s pod.', pod.url) 234 podObj.save(function (err, podCreated) {
235 if (err) {
236 logger.error('Cannot add friend %s pod.', pod.url, { error: err })
237 return callbackEach()
238 }
235 239
236 // Add our videos to the request scheduler 240 // Add our videos to the request scheduler
237 sendOwnedVideosToPod(podCreated._id) 241 sendOwnedVideosToPod(podCreated._id)
diff --git a/server/middlewares/secure.js b/server/middlewares/secure.js
index ad7b0fbf7..fbaf4d0f2 100644
--- a/server/middlewares/secure.js
+++ b/server/middlewares/secure.js
@@ -1,8 +1,10 @@
1'use strict' 1'use strict'
2 2
3const logger = require('../helpers/logger') 3const logger = require('../helpers/logger')
4const mongoose = require('mongoose')
4const peertubeCrypto = require('../helpers/peertubeCrypto') 5const peertubeCrypto = require('../helpers/peertubeCrypto')
5const Pods = require('../models/pods') 6
7const Pod = mongoose.model('Pod')
6 8
7const secureMiddleware = { 9const secureMiddleware = {
8 decryptBody: decryptBody 10 decryptBody: decryptBody
@@ -10,7 +12,7 @@ const secureMiddleware = {
10 12
11function decryptBody (req, res, next) { 13function decryptBody (req, res, next) {
12 const url = req.body.signature.url 14 const url = req.body.signature.url
13 Pods.findByUrl(url, function (err, pod) { 15 Pod.loadByUrl(url, function (err, pod) {
14 if (err) { 16 if (err) {
15 logger.error('Cannot get signed url in decryptBody.', { error: err }) 17 logger.error('Cannot get signed url in decryptBody.', { error: err })
16 return res.sendStatus(500) 18 return res.sendStatus(500)
diff --git a/server/models/pods.js b/server/models/pods.js
index 9502d92e4..bf43d7b25 100644
--- a/server/models/pods.js
+++ b/server/models/pods.js
@@ -2,107 +2,89 @@
2 2
3const mongoose = require('mongoose') 3const mongoose = require('mongoose')
4const map = require('lodash/map') 4const map = require('lodash/map')
5const validator = require('express-validator').validator
5 6
6const constants = require('../initializers/constants') 7const constants = require('../initializers/constants')
7const logger = require('../helpers/logger')
8 8
9// --------------------------------------------------------------------------- 9// ---------------------------------------------------------------------------
10 10
11const podsSchema = mongoose.Schema({ 11const PodSchema = mongoose.Schema({
12 url: String, 12 url: String,
13 publicKey: String, 13 publicKey: String,
14 score: { type: Number, max: constants.FRIEND_BASE_SCORE } 14 score: { type: Number, max: constants.FRIEND_SCORE.MAX }
15}) 15})
16const PodsDB = mongoose.model('pods', podsSchema)
17 16
18// --------------------------------------------------------------------------- 17// TODO: set options (TLD...)
18PodSchema.path('url').validate(validator.isURL)
19PodSchema.path('publicKey').required(true)
20PodSchema.path('score').validate(function (value) { return !isNaN(value) })
19 21
20const Pods = { 22PodSchema.statics = {
21 add: add, 23 countAll: countAll,
22 count: count,
23 findById: findById,
24 findByUrl: findByUrl,
25 findBadPods: findBadPods,
26 incrementScores: incrementScores, 24 incrementScores: incrementScores,
27 list: list, 25 list: list,
28 listAllIds: listAllIds, 26 listAllIds: listAllIds,
29 listAllUrls: listAllUrls, 27 listOnlyUrls: listOnlyUrls,
30 remove: remove, 28 listBadPods: listBadPods,
31 removeAll: removeAll, 29 load: load,
32 removeAllByIds: removeAllByIds 30 loadByUrl: loadByUrl,
31 removeAll: removeAll
33} 32}
34 33
35// TODO: check if the pod is not already a friend 34PodSchema.pre('save', function (next) {
36function add (data, callback) { 35 const self = this
37 if (!callback) callback = function () {}
38 const params = {
39 url: data.url,
40 publicKey: data.publicKey,
41 score: constants.FRIEND_BASE_SCORE
42 }
43 36
44 PodsDB.create(params, callback) 37 Pod.loadByUrl(this.url, function (err, pod) {
45} 38 if (err) return next(err)
46 39
47function count (callback) { 40 if (pod) return next(new Error('Pod already exists.'))
48 return PodsDB.count(callback)
49}
50 41
51function findBadPods (callback) { 42 self.score = constants.FRIEND_SCORE.BASE
52 PodsDB.find({ score: 0 }, callback) 43 return next()
53} 44 })
45})
54 46
55function findById (id, callback) { 47const Pod = mongoose.model('Pod', PodSchema)
56 PodsDB.findById(id, callback)
57}
58 48
59function findByUrl (url, callback) { 49// ------------------------------ Statics ------------------------------
60 PodsDB.findOne({ url: url }, callback) 50
51function countAll (callback) {
52 return this.count(callback)
61} 53}
62 54
63function incrementScores (ids, value, callback) { 55function incrementScores (ids, value, callback) {
64 if (!callback) callback = function () {} 56 if (!callback) callback = function () {}
65 PodsDB.update({ _id: { $in: ids } }, { $inc: { score: value } }, { multi: true }, callback) 57 return this.update({ _id: { $in: ids } }, { $inc: { score: value } }, { multi: true }, callback)
66} 58}
67 59
68function list (callback) { 60function list (callback) {
69 PodsDB.find(function (err, podsList) { 61 return this.find(callback)
70 if (err) {
71 logger.error('Cannot get the list of the pods.')
72 return callback(err)
73 }
74
75 return callback(null, podsList)
76 })
77} 62}
78 63
79function listAllIds (callback) { 64function listAllIds (callback) {
80 return PodsDB.find({}, { _id: 1 }, function (err, pods) { 65 return this.find({}, { _id: 1 }, function (err, pods) {
81 if (err) return callback(err) 66 if (err) return callback(err)
82 67
83 return callback(null, map(pods, '_id')) 68 return callback(null, map(pods, '_id'))
84 }) 69 })
85} 70}
86 71
87function listAllUrls (callback) { 72function listOnlyUrls (callback) {
88 return PodsDB.find({}, { _id: 0, url: 1 }, callback) 73 return this.find({}, { _id: 0, url: 1 }, callback)
89} 74}
90 75
91function remove (url, callback) { 76function listBadPods (callback) {
92 if (!callback) callback = function () {} 77 return this.find({ score: 0 }, callback)
93 PodsDB.remove({ url: url }, callback)
94} 78}
95 79
96function removeAll (callback) { 80function load (id, callback) {
97 if (!callback) callback = function () {} 81 return this.findById(id, callback)
98 PodsDB.remove(callback)
99} 82}
100 83
101function removeAllByIds (ids, callback) { 84function loadByUrl (url, callback) {
102 if (!callback) callback = function () {} 85 return this.findOne({ url: url }, callback)
103 PodsDB.remove({ _id: { $in: ids } }, callback)
104} 86}
105 87
106// --------------------------------------------------------------------------- 88function removeAll (callback) {
107 89 return this.remove({}, callback)
108module.exports = Pods 90}
diff --git a/server/models/request.js b/server/models/request.js
index 2a407388a..db6ad5409 100644
--- a/server/models/request.js
+++ b/server/models/request.js
@@ -6,9 +6,9 @@ const mongoose = require('mongoose')
6 6
7const constants = require('../initializers/constants') 7const constants = require('../initializers/constants')
8const logger = require('../helpers/logger') 8const logger = require('../helpers/logger')
9const Pods = require('../models/pods')
10const requests = require('../helpers/requests') 9const requests = require('../helpers/requests')
11 10
11const Pod = mongoose.model('Pod')
12const Video = mongoose.model('Video') 12const Video = mongoose.model('Video')
13 13
14let timer = null 14let timer = null
@@ -31,7 +31,7 @@ RequestSchema.pre('save', function (next) {
31 const self = this 31 const self = this
32 32
33 if (self.to.length === 0) { 33 if (self.to.length === 0) {
34 Pods.listAllIds(function (err, podIds) { 34 Pod.listAllIds(function (err, podIds) {
35 if (err) return next(err) 35 if (err) return next(err)
36 36
37 // No friends 37 // No friends
@@ -140,7 +140,7 @@ function makeRequests () {
140 const requestToMake = requestsToMake[toPodId] 140 const requestToMake = requestsToMake[toPodId]
141 141
142 // FIXME: mongodb request inside a loop :/ 142 // FIXME: mongodb request inside a loop :/
143 Pods.findById(toPodId, function (err, toPod) { 143 Pod.load(toPodId, function (err, toPod) {
144 if (err) { 144 if (err) {
145 logger.error('Error finding pod by id.', { err: err }) 145 logger.error('Error finding pod by id.', { err: err })
146 return callbackEach() 146 return callbackEach()
@@ -185,7 +185,7 @@ function makeRequests () {
185function removeBadPods () { 185function removeBadPods () {
186 async.waterfall([ 186 async.waterfall([
187 function findBadPods (callback) { 187 function findBadPods (callback) {
188 Pods.findBadPods(function (err, pods) { 188 Pod.listBadPods(function (err, pods) {
189 if (err) { 189 if (err) {
190 logger.error('Cannot find bad pods.', { error: err }) 190 logger.error('Cannot find bad pods.', { error: err })
191 return callback(err) 191 return callback(err)
@@ -199,21 +199,23 @@ function removeBadPods () {
199 if (pods.length === 0) return callback(null) 199 if (pods.length === 0) return callback(null)
200 200
201 const urls = map(pods, 'url') 201 const urls = map(pods, 'url')
202 const ids = map(pods, '_id')
203 202
204 Video.listByUrls(urls, function (err, videosList) { 203 Video.listByUrls(urls, function (err, videosList) {
205 if (err) { 204 if (err) {
206 logger.error('Cannot list videos urls.', { error: err, urls: urls }) 205 logger.error('Cannot list videos urls.', { error: err, urls: urls })
207 return callback(null, ids, []) 206 return callback(null, pods, [])
208 } 207 }
209 208
210 return callback(null, ids, videosList) 209 return callback(null, pods, videosList)
211 }) 210 })
212 }, 211 },
213 212
214 function removeVideosOfTheseBadPods (podIds, videosList, callback) { 213 function removeVideosOfTheseBadPods (pods, videosList, callback) {
215 // We don't have to remove pods, skip 214 // We don't have to remove pods, skip
216 if (typeof podIds === 'function') return podIds(null) 215 if (typeof pods === 'function') {
216 callback = pods
217 return callback(null)
218 }
217 219
218 async.each(videosList, function (video, callbackEach) { 220 async.each(videosList, function (video, callbackEach) {
219 video.remove(callbackEach) 221 video.remove(callbackEach)
@@ -224,22 +226,30 @@ function removeBadPods () {
224 return 226 return
225 } 227 }
226 228
227 return callback(null, podIds) 229 return callback(null, pods)
228 }) 230 })
229 }, 231 },
230 232
231 function removeBadPodsFromDB (podIds, callback) { 233 function removeBadPodsFromDB (pods, callback) {
232 // We don't have to remove pods, skip 234 // We don't have to remove pods, skip
233 if (typeof podIds === 'function') return podIds(null) 235 if (typeof pods === 'function') {
236 callback = pods
237 return callback(null)
238 }
239
240 async.each(pods, function (pod, callbackEach) {
241 pod.remove(callbackEach)
242 }, function (err) {
243 if (err) return callback(err)
234 244
235 Pods.removeAllByIds(podIds, callback) 245 return callback(null, pods.length)
246 })
236 } 247 }
237 ], function (err, removeResult) { 248 ], function (err, numberOfPodsRemoved) {
238 if (err) { 249 if (err) {
239 logger.error('Cannot remove bad pods.', { error: err }) 250 logger.error('Cannot remove bad pods.', { error: err })
240 } else if (removeResult) { 251 } else if (numberOfPodsRemoved) {
241 const podsRemoved = removeResult.result.n 252 logger.info('Removed %d pods.', numberOfPodsRemoved)
242 logger.info('Removed %d pods.', podsRemoved)
243 } else { 253 } else {
244 logger.info('No need to remove bad pods.') 254 logger.info('No need to remove bad pods.')
245 } 255 }
@@ -249,11 +259,11 @@ function removeBadPods () {
249function updatePodsScore (goodPods, badPods) { 259function updatePodsScore (goodPods, badPods) {
250 logger.info('Updating %d good pods and %d bad pods scores.', goodPods.length, badPods.length) 260 logger.info('Updating %d good pods and %d bad pods scores.', goodPods.length, badPods.length)
251 261
252 Pods.incrementScores(goodPods, constants.PODS_SCORE.BONUS, function (err) { 262 Pod.incrementScores(goodPods, constants.PODS_SCORE.BONUS, function (err) {
253 if (err) logger.error('Cannot increment scores of good pods.') 263 if (err) logger.error('Cannot increment scores of good pods.')
254 }) 264 })
255 265
256 Pods.incrementScores(badPods, constants.PODS_SCORE.MALUS, function (err) { 266 Pod.incrementScores(badPods, constants.PODS_SCORE.MALUS, function (err) {
257 if (err) logger.error('Cannot decrement scores of bad pods.') 267 if (err) logger.error('Cannot decrement scores of bad pods.')
258 removeBadPods() 268 removeBadPods()
259 }) 269 })