2016-02-07 05:23:23 -05:00
|
|
|
'use strict'
|
|
|
|
|
2016-10-21 05:20:45 -04:00
|
|
|
const each = require('async/each')
|
2016-03-16 17:29:27 -04:00
|
|
|
const mongoose = require('mongoose')
|
2016-06-28 14:10:32 -04:00
|
|
|
const map = require('lodash/map')
|
2016-06-30 16:39:08 -04:00
|
|
|
const validator = require('express-validator').validator
|
2016-02-07 05:23:23 -05:00
|
|
|
|
2016-03-16 17:29:27 -04:00
|
|
|
const constants = require('../initializers/constants')
|
2016-02-07 05:23:23 -05:00
|
|
|
|
2016-10-21 05:20:45 -04:00
|
|
|
const Video = mongoose.model('Video')
|
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2016-06-30 16:39:08 -04:00
|
|
|
const PodSchema = mongoose.Schema({
|
2016-11-14 14:03:04 -05:00
|
|
|
host: String,
|
2016-02-07 05:23:23 -05:00
|
|
|
publicKey: String,
|
2016-08-26 12:55:10 -04:00
|
|
|
score: { type: Number, max: constants.FRIEND_SCORE.MAX },
|
|
|
|
createdDate: {
|
|
|
|
type: Date,
|
|
|
|
default: Date.now
|
|
|
|
}
|
2016-02-07 05:23:23 -05:00
|
|
|
})
|
|
|
|
|
2016-11-14 14:03:04 -05:00
|
|
|
PodSchema.path('host').validate(validator.isURL)
|
2016-06-30 16:39:08 -04:00
|
|
|
PodSchema.path('publicKey').required(true)
|
|
|
|
PodSchema.path('score').validate(function (value) { return !isNaN(value) })
|
2016-02-07 05:23:23 -05:00
|
|
|
|
2016-08-26 12:55:10 -04:00
|
|
|
PodSchema.methods = {
|
2016-10-02 06:19:02 -04:00
|
|
|
toFormatedJSON
|
2016-08-26 12:55:10 -04:00
|
|
|
}
|
|
|
|
|
2016-06-30 16:39:08 -04:00
|
|
|
PodSchema.statics = {
|
2016-10-02 06:19:02 -04:00
|
|
|
countAll,
|
|
|
|
incrementScores,
|
|
|
|
list,
|
|
|
|
listAllIds,
|
|
|
|
listBadPods,
|
|
|
|
load,
|
2016-11-14 14:03:04 -05:00
|
|
|
loadByHost,
|
2016-10-02 06:19:02 -04:00
|
|
|
removeAll
|
2016-02-07 05:23:23 -05:00
|
|
|
}
|
|
|
|
|
2016-06-30 16:39:08 -04:00
|
|
|
PodSchema.pre('save', function (next) {
|
|
|
|
const self = this
|
2015-06-09 11:41:40 -04:00
|
|
|
|
2016-11-14 14:03:04 -05:00
|
|
|
Pod.loadByHost(this.host, function (err, pod) {
|
2016-06-30 16:39:08 -04:00
|
|
|
if (err) return next(err)
|
2016-01-23 12:31:58 -05:00
|
|
|
|
2016-06-30 16:39:08 -04:00
|
|
|
if (pod) return next(new Error('Pod already exists.'))
|
2015-06-09 11:41:40 -04:00
|
|
|
|
2016-06-30 16:39:08 -04:00
|
|
|
self.score = constants.FRIEND_SCORE.BASE
|
|
|
|
return next()
|
|
|
|
})
|
|
|
|
})
|
2016-01-23 12:31:58 -05:00
|
|
|
|
2016-10-21 05:20:45 -04:00
|
|
|
PodSchema.pre('remove', function (next) {
|
|
|
|
// Remove the videos owned by this pod too
|
2016-11-14 14:03:04 -05:00
|
|
|
Video.listByHost(this.host, function (err, videos) {
|
2016-10-21 05:20:45 -04:00
|
|
|
if (err) return next(err)
|
|
|
|
|
|
|
|
each(videos, function (video, callbackEach) {
|
|
|
|
video.remove(callbackEach)
|
|
|
|
}, next)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2016-06-30 16:39:08 -04:00
|
|
|
const Pod = mongoose.model('Pod', PodSchema)
|
2016-06-18 10:13:54 -04:00
|
|
|
|
2016-08-26 12:55:10 -04:00
|
|
|
// ------------------------------ METHODS ------------------------------
|
|
|
|
|
|
|
|
function toFormatedJSON () {
|
|
|
|
const json = {
|
|
|
|
id: this._id,
|
2016-11-14 14:03:04 -05:00
|
|
|
host: this.host,
|
2016-08-26 12:55:10 -04:00
|
|
|
score: this.score,
|
|
|
|
createdDate: this.createdDate
|
|
|
|
}
|
|
|
|
|
|
|
|
return json
|
|
|
|
}
|
|
|
|
|
2016-06-30 16:39:08 -04:00
|
|
|
// ------------------------------ Statics ------------------------------
|
|
|
|
|
|
|
|
function countAll (callback) {
|
|
|
|
return this.count(callback)
|
2016-02-07 05:23:23 -05:00
|
|
|
}
|
2016-01-23 12:31:58 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
function incrementScores (ids, value, callback) {
|
|
|
|
if (!callback) callback = function () {}
|
2016-06-30 16:39:08 -04:00
|
|
|
return this.update({ _id: { $in: ids } }, { $inc: { score: value } }, { multi: true }, callback)
|
2016-02-07 05:23:23 -05:00
|
|
|
}
|
2016-01-23 12:31:58 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
function list (callback) {
|
2016-06-30 16:39:08 -04:00
|
|
|
return this.find(callback)
|
2016-02-07 05:23:23 -05:00
|
|
|
}
|
2015-06-09 11:41:40 -04:00
|
|
|
|
2016-06-18 10:13:54 -04:00
|
|
|
function listAllIds (callback) {
|
2016-06-30 16:39:08 -04:00
|
|
|
return this.find({}, { _id: 1 }, function (err, pods) {
|
2016-06-28 14:10:32 -04:00
|
|
|
if (err) return callback(err)
|
|
|
|
|
|
|
|
return callback(null, map(pods, '_id'))
|
|
|
|
})
|
2016-06-18 10:13:54 -04:00
|
|
|
}
|
|
|
|
|
2016-06-30 16:39:08 -04:00
|
|
|
function listBadPods (callback) {
|
|
|
|
return this.find({ score: 0 }, callback)
|
2016-02-07 05:23:23 -05:00
|
|
|
}
|
2015-06-09 11:41:40 -04:00
|
|
|
|
2016-06-30 16:39:08 -04:00
|
|
|
function load (id, callback) {
|
|
|
|
return this.findById(id, callback)
|
2016-02-07 05:23:23 -05:00
|
|
|
}
|
2016-01-31 05:23:52 -05:00
|
|
|
|
2016-11-14 14:03:04 -05:00
|
|
|
function loadByHost (host, callback) {
|
|
|
|
return this.findOne({ host }, callback)
|
2016-02-07 05:23:23 -05:00
|
|
|
}
|
2016-01-31 05:23:52 -05:00
|
|
|
|
2016-06-30 16:39:08 -04:00
|
|
|
function removeAll (callback) {
|
|
|
|
return this.remove({}, callback)
|
|
|
|
}
|