Server: do not make friends with myself
authorChocobozzz <florian.bigard@gmail.com>
Sun, 23 Oct 2016 17:31:47 +0000 (19:31 +0200)
committerChocobozzz <florian.bigard@gmail.com>
Wed, 26 Oct 2016 18:28:34 +0000 (20:28 +0200)
server/lib/friends.js

index 55cecc53ed9efc6c2048057af2d4613afbb50c61..b2ad0bbb3038a55d2c1b371cf1fa6028f6f21d2a 100644 (file)
@@ -6,6 +6,7 @@ const eachSeries = require('async/eachSeries')
 const fs = require('fs')
 const mongoose = require('mongoose')
 const request = require('request')
+const urlUtil = require('url')
 const waterfall = require('async/waterfall')
 
 const constants = require('../initializers/constants')
@@ -173,8 +174,11 @@ function computeWinningPods (urls, podsScore) {
   // Only add a pod if it exists in more than a half base pods
   const podsList = []
   const baseScore = urls.length / 2
-  Object.keys(podsScore).forEach(function (pod) {
-    if (podsScore[pod] > baseScore) podsList.push({ url: pod })
+  Object.keys(podsScore).forEach(function (podUrl) {
+    // If the pod is not me and with a good score we add it
+    if (isMe(podUrl) === false && podsScore[podUrl] > baseScore) {
+      podsList.push({ url: podUrl })
+    }
   })
 
   return podsList
@@ -262,3 +266,15 @@ function createRequest (type, data, to) {
     if (err) logger.error('Cannot save the request.', { error: err })
   })
 }
+
+function isMe (url) {
+  const parsedUrl = urlUtil.parse(url)
+
+  const hostname = parsedUrl.hostname
+  const port = parseInt(parsedUrl.port)
+
+  const myHostname = constants.CONFIG.WEBSERVER.HOST
+  const myPort = constants.CONFIG.WEBSERVER.PORT
+
+  return hostname === myHostname && port === myPort
+}