2016-02-07 04:23:23 -06:00
|
|
|
'use strict'
|
|
|
|
|
2016-03-16 16:29:27 -05:00
|
|
|
const mongoose = require('mongoose')
|
2016-06-28 13:10:32 -05:00
|
|
|
const map = require('lodash/map')
|
2016-06-30 15:39:08 -05:00
|
|
|
const validator = require('express-validator').validator
|
2016-02-07 04:23:23 -06:00
|
|
|
|
2016-03-16 16:29:27 -05:00
|
|
|
const constants = require('../initializers/constants')
|
2016-02-07 04:23:23 -06:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2016-06-30 15:39:08 -05:00
|
|
|
const PodSchema = mongoose.Schema({
|
2016-02-07 04:23:23 -06:00
|
|
|
url: String,
|
|
|
|
publicKey: String,
|
2016-06-30 15:39:08 -05:00
|
|
|
score: { type: Number, max: constants.FRIEND_SCORE.MAX }
|
2016-02-07 04:23:23 -06:00
|
|
|
})
|
|
|
|
|
2016-06-30 15:39:08 -05:00
|
|
|
// TODO: set options (TLD...)
|
|
|
|
PodSchema.path('url').validate(validator.isURL)
|
|
|
|
PodSchema.path('publicKey').required(true)
|
|
|
|
PodSchema.path('score').validate(function (value) { return !isNaN(value) })
|
2016-02-07 04:23:23 -06:00
|
|
|
|
2016-06-30 15:39:08 -05:00
|
|
|
PodSchema.statics = {
|
|
|
|
countAll: countAll,
|
2016-02-07 04:23:23 -06:00
|
|
|
incrementScores: incrementScores,
|
|
|
|
list: list,
|
2016-06-18 09:13:54 -05:00
|
|
|
listAllIds: listAllIds,
|
2016-06-30 15:39:08 -05:00
|
|
|
listOnlyUrls: listOnlyUrls,
|
|
|
|
listBadPods: listBadPods,
|
|
|
|
load: load,
|
|
|
|
loadByUrl: loadByUrl,
|
|
|
|
removeAll: removeAll
|
2016-02-07 04:23:23 -06:00
|
|
|
}
|
|
|
|
|
2016-06-30 15:39:08 -05:00
|
|
|
PodSchema.pre('save', function (next) {
|
|
|
|
const self = this
|
2015-06-09 10:41:40 -05:00
|
|
|
|
2016-06-30 15:39:08 -05:00
|
|
|
Pod.loadByUrl(this.url, function (err, pod) {
|
|
|
|
if (err) return next(err)
|
2016-01-23 11:31:58 -06:00
|
|
|
|
2016-06-30 15:39:08 -05:00
|
|
|
if (pod) return next(new Error('Pod already exists.'))
|
2015-06-09 10:41:40 -05:00
|
|
|
|
2016-06-30 15:39:08 -05:00
|
|
|
self.score = constants.FRIEND_SCORE.BASE
|
|
|
|
return next()
|
|
|
|
})
|
|
|
|
})
|
2016-01-23 11:31:58 -06:00
|
|
|
|
2016-06-30 15:39:08 -05:00
|
|
|
const Pod = mongoose.model('Pod', PodSchema)
|
2016-06-18 09:13:54 -05:00
|
|
|
|
2016-06-30 15:39:08 -05:00
|
|
|
// ------------------------------ Statics ------------------------------
|
|
|
|
|
|
|
|
function countAll (callback) {
|
|
|
|
return this.count(callback)
|
2016-02-07 04:23:23 -06:00
|
|
|
}
|
2016-01-23 11:31:58 -06:00
|
|
|
|
2016-02-07 04:23:23 -06:00
|
|
|
function incrementScores (ids, value, callback) {
|
|
|
|
if (!callback) callback = function () {}
|
2016-06-30 15:39:08 -05:00
|
|
|
return this.update({ _id: { $in: ids } }, { $inc: { score: value } }, { multi: true }, callback)
|
2016-02-07 04:23:23 -06:00
|
|
|
}
|
2016-01-23 11:31:58 -06:00
|
|
|
|
2016-02-07 04:23:23 -06:00
|
|
|
function list (callback) {
|
2016-06-30 15:39:08 -05:00
|
|
|
return this.find(callback)
|
2016-02-07 04:23:23 -06:00
|
|
|
}
|
2015-06-09 10:41:40 -05:00
|
|
|
|
2016-06-18 09:13:54 -05:00
|
|
|
function listAllIds (callback) {
|
2016-06-30 15:39:08 -05:00
|
|
|
return this.find({}, { _id: 1 }, function (err, pods) {
|
2016-06-28 13:10:32 -05:00
|
|
|
if (err) return callback(err)
|
|
|
|
|
|
|
|
return callback(null, map(pods, '_id'))
|
|
|
|
})
|
2016-06-18 09:13:54 -05:00
|
|
|
}
|
|
|
|
|
2016-06-30 15:39:08 -05:00
|
|
|
function listOnlyUrls (callback) {
|
|
|
|
return this.find({}, { _id: 0, url: 1 }, callback)
|
2016-06-18 09:13:54 -05:00
|
|
|
}
|
|
|
|
|
2016-06-30 15:39:08 -05:00
|
|
|
function listBadPods (callback) {
|
|
|
|
return this.find({ score: 0 }, callback)
|
2016-02-07 04:23:23 -06:00
|
|
|
}
|
2015-06-09 10:41:40 -05:00
|
|
|
|
2016-06-30 15:39:08 -05:00
|
|
|
function load (id, callback) {
|
|
|
|
return this.findById(id, callback)
|
2016-02-07 04:23:23 -06:00
|
|
|
}
|
2016-01-31 04:23:52 -06:00
|
|
|
|
2016-06-30 15:39:08 -05:00
|
|
|
function loadByUrl (url, callback) {
|
|
|
|
return this.findOne({ url: url }, callback)
|
2016-02-07 04:23:23 -06:00
|
|
|
}
|
2016-01-31 04:23:52 -06:00
|
|
|
|
2016-06-30 15:39:08 -05:00
|
|
|
function removeAll (callback) {
|
|
|
|
return this.remove({}, callback)
|
|
|
|
}
|