]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/pods.js
Server: add pod created date and score to the list controller
[github/Chocobozzz/PeerTube.git] / server / models / pods.js
index bf43d7b25c14315672fe3c27031791d6763b4d57..59de2d60cb62dd07b17445f24386b1d29c3aba7c 100644 (file)
@@ -11,7 +11,11 @@ const constants = require('../initializers/constants')
 const PodSchema = mongoose.Schema({
   url: String,
   publicKey: String,
-  score: { type: Number, max: constants.FRIEND_SCORE.MAX }
+  score: { type: Number, max: constants.FRIEND_SCORE.MAX },
+  createdDate: {
+    type: Date,
+    default: Date.now
+  }
 })
 
 // TODO: set options (TLD...)
@@ -19,12 +23,15 @@ PodSchema.path('url').validate(validator.isURL)
 PodSchema.path('publicKey').required(true)
 PodSchema.path('score').validate(function (value) { return !isNaN(value) })
 
+PodSchema.methods = {
+  toFormatedJSON: toFormatedJSON
+}
+
 PodSchema.statics = {
   countAll: countAll,
   incrementScores: incrementScores,
   list: list,
   listAllIds: listAllIds,
-  listOnlyUrls: listOnlyUrls,
   listBadPods: listBadPods,
   load: load,
   loadByUrl: loadByUrl,
@@ -46,6 +53,19 @@ PodSchema.pre('save', function (next) {
 
 const Pod = mongoose.model('Pod', PodSchema)
 
+// ------------------------------ METHODS ------------------------------
+
+function toFormatedJSON () {
+  const json = {
+    id: this._id,
+    url: this.url,
+    score: this.score,
+    createdDate: this.createdDate
+  }
+
+  return json
+}
+
 // ------------------------------ Statics ------------------------------
 
 function countAll (callback) {
@@ -69,10 +89,6 @@ function listAllIds (callback) {
   })
 }
 
-function listOnlyUrls (callback) {
-  return this.find({}, { _id: 0, url: 1 }, callback)
-}
-
 function listBadPods (callback) {
   return this.find({ score: 0 }, callback)
 }