Server: rename Pods -> Pod
authorChocobozzz <florian.bigard@gmail.com>
Mon, 19 Dec 2016 20:50:20 +0000 (21:50 +0100)
committerChocobozzz <florian.bigard@gmail.com>
Mon, 19 Dec 2016 20:50:20 +0000 (21:50 +0100)
server/controllers/api/remote.js
server/models/pod.js [new file with mode: 0644]
server/models/pods.js [deleted file]

index d856576a9f2ae880c4f134bc4f9b3750566e0d41..a6753a2b08202dde69cc57315d1ff0de5a47b260 100644 (file)
@@ -56,8 +56,6 @@ function addRemoteVideo (videoToCreateData, fromHost, callback) {
   waterfall([
 
     function findOrCreatePod (callback) {
-      fromHost
-
       const query = {
         where: {
           host: fromHost
diff --git a/server/models/pod.js b/server/models/pod.js
new file mode 100644 (file)
index 0000000..2c1f562
--- /dev/null
@@ -0,0 +1,139 @@
+'use strict'
+
+const map = require('lodash/map')
+
+const constants = require('../initializers/constants')
+
+// ---------------------------------------------------------------------------
+
+module.exports = function (sequelize, DataTypes) {
+  const Pod = sequelize.define('Pod',
+    {
+      host: {
+        type: DataTypes.STRING
+      },
+      publicKey: {
+        type: DataTypes.STRING(5000)
+      },
+      score: {
+        type: DataTypes.INTEGER,
+        defaultValue: constants.FRIEND_SCORE.BASE
+      }
+      // Check createdAt
+    },
+    {
+      classMethods: {
+        associate,
+
+        countAll,
+        incrementScores,
+        list,
+        listAllIds,
+        listBadPods,
+        load,
+        loadByHost,
+        removeAll
+      },
+      instanceMethods: {
+        toFormatedJSON
+      }
+    }
+  )
+
+  return Pod
+}
+
+// TODO: max score -> constants.FRIENDS_SCORE.MAX
+// TODO: validation
+// PodSchema.path('host').validate(validator.isURL)
+// PodSchema.path('publicKey').required(true)
+// PodSchema.path('score').validate(function (value) { return !isNaN(value) })
+
+// ------------------------------ METHODS ------------------------------
+
+function toFormatedJSON () {
+  const json = {
+    id: this.id,
+    host: this.host,
+    score: this.score,
+    createdAt: this.createdAt
+  }
+
+  return json
+}
+
+// ------------------------------ Statics ------------------------------
+
+function associate (models) {
+  this.belongsToMany(models.Request, {
+    foreignKey: 'podId',
+    through: models.RequestToPod,
+    onDelete: 'CASCADE'
+  })
+}
+
+function countAll (callback) {
+  return this.count().asCallback(callback)
+}
+
+function incrementScores (ids, value, callback) {
+  if (!callback) callback = function () {}
+
+  const update = {
+    score: this.sequelize.literal('score +' + value)
+  }
+
+  const query = {
+    where: {
+      id: {
+        $in: ids
+      }
+    }
+  }
+
+  return this.update(update, query).asCallback(callback)
+}
+
+function list (callback) {
+  return this.findAll().asCallback(callback)
+}
+
+function listAllIds (callback) {
+  const query = {
+    attributes: [ 'id' ]
+  }
+
+  return this.findAll(query).asCallback(function (err, pods) {
+    if (err) return callback(err)
+
+    return callback(null, map(pods, 'id'))
+  })
+}
+
+function listBadPods (callback) {
+  const query = {
+    where: {
+      score: { $lte: 0 }
+    }
+  }
+
+  return this.findAll(query).asCallback(callback)
+}
+
+function load (id, callback) {
+  return this.findById(id).asCallback(callback)
+}
+
+function loadByHost (host, callback) {
+  const query = {
+    where: {
+      host: host
+    }
+  }
+
+  return this.findOne(query).asCallback(callback)
+}
+
+function removeAll (callback) {
+  return this.destroy().asCallback(callback)
+}
diff --git a/server/models/pods.js b/server/models/pods.js
deleted file mode 100644 (file)
index 2c1f562..0000000
+++ /dev/null
@@ -1,139 +0,0 @@
-'use strict'
-
-const map = require('lodash/map')
-
-const constants = require('../initializers/constants')
-
-// ---------------------------------------------------------------------------
-
-module.exports = function (sequelize, DataTypes) {
-  const Pod = sequelize.define('Pod',
-    {
-      host: {
-        type: DataTypes.STRING
-      },
-      publicKey: {
-        type: DataTypes.STRING(5000)
-      },
-      score: {
-        type: DataTypes.INTEGER,
-        defaultValue: constants.FRIEND_SCORE.BASE
-      }
-      // Check createdAt
-    },
-    {
-      classMethods: {
-        associate,
-
-        countAll,
-        incrementScores,
-        list,
-        listAllIds,
-        listBadPods,
-        load,
-        loadByHost,
-        removeAll
-      },
-      instanceMethods: {
-        toFormatedJSON
-      }
-    }
-  )
-
-  return Pod
-}
-
-// TODO: max score -> constants.FRIENDS_SCORE.MAX
-// TODO: validation
-// PodSchema.path('host').validate(validator.isURL)
-// PodSchema.path('publicKey').required(true)
-// PodSchema.path('score').validate(function (value) { return !isNaN(value) })
-
-// ------------------------------ METHODS ------------------------------
-
-function toFormatedJSON () {
-  const json = {
-    id: this.id,
-    host: this.host,
-    score: this.score,
-    createdAt: this.createdAt
-  }
-
-  return json
-}
-
-// ------------------------------ Statics ------------------------------
-
-function associate (models) {
-  this.belongsToMany(models.Request, {
-    foreignKey: 'podId',
-    through: models.RequestToPod,
-    onDelete: 'CASCADE'
-  })
-}
-
-function countAll (callback) {
-  return this.count().asCallback(callback)
-}
-
-function incrementScores (ids, value, callback) {
-  if (!callback) callback = function () {}
-
-  const update = {
-    score: this.sequelize.literal('score +' + value)
-  }
-
-  const query = {
-    where: {
-      id: {
-        $in: ids
-      }
-    }
-  }
-
-  return this.update(update, query).asCallback(callback)
-}
-
-function list (callback) {
-  return this.findAll().asCallback(callback)
-}
-
-function listAllIds (callback) {
-  const query = {
-    attributes: [ 'id' ]
-  }
-
-  return this.findAll(query).asCallback(function (err, pods) {
-    if (err) return callback(err)
-
-    return callback(null, map(pods, 'id'))
-  })
-}
-
-function listBadPods (callback) {
-  const query = {
-    where: {
-      score: { $lte: 0 }
-    }
-  }
-
-  return this.findAll(query).asCallback(callback)
-}
-
-function load (id, callback) {
-  return this.findById(id).asCallback(callback)
-}
-
-function loadByHost (host, callback) {
-  const query = {
-    where: {
-      host: host
-    }
-  }
-
-  return this.findOne(query).asCallback(callback)
-}
-
-function removeAll (callback) {
-  return this.destroy().asCallback(callback)
-}