aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/pods.js
blob: 49c73472abd6a2e8073ee755edaf02b4739a3e61 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
'use strict'

const each = require('async/each')
const mongoose = require('mongoose')
const map = require('lodash/map')
const validator = require('express-validator').validator

const constants = require('../initializers/constants')

const Video = mongoose.model('Video')

// ---------------------------------------------------------------------------

const PodSchema = mongoose.Schema({
  host: String,
  publicKey: String,
  score: { type: Number, max: constants.FRIEND_SCORE.MAX },
  createdDate: {
    type: Date,
    default: Date.now
  }
})

PodSchema.path('host').validate(validator.isURL)
PodSchema.path('publicKey').required(true)
PodSchema.path('score').validate(function (value) { return !isNaN(value) })

PodSchema.methods = {
  toFormatedJSON
}

PodSchema.statics = {
  countAll,
  incrementScores,
  list,
  listAllIds,
  listBadPods,
  load,
  loadByHost,
  removeAll
}

PodSchema.pre('save', function (next) {
  const self = this

  Pod.loadByHost(this.host, function (err, pod) {
    if (err) return next(err)

    if (pod) return next(new Error('Pod already exists.'))

    self.score = constants.FRIEND_SCORE.BASE
    return next()
  })
})

PodSchema.pre('remove', function (next) {
  // Remove the videos owned by this pod too
  Video.listByHost(this.host, function (err, videos) {
    if (err) return next(err)

    each(videos, function (video, callbackEach) {
      video.remove(callbackEach)
    }, next)
  })
})

const Pod = mongoose.model('Pod', PodSchema)

// ------------------------------ METHODS ------------------------------

function toFormatedJSON () {
  const json = {
    id: this._id,
    host: this.host,
    score: this.score,
    createdDate: this.createdDate
  }

  return json
}

// ------------------------------ Statics ------------------------------

function countAll (callback) {
  return this.count(callback)
}

function incrementScores (ids, value, callback) {
  if (!callback) callback = function () {}
  return this.update({ _id: { $in: ids } }, { $inc: { score: value } }, { multi: true }, callback)
}

function list (callback) {
  return this.find(callback)
}

function listAllIds (callback) {
  return this.find({}, { _id: 1 }, function (err, pods) {
    if (err) return callback(err)

    return callback(null, map(pods, '_id'))
  })
}

function listBadPods (callback) {
  return this.find({ score: 0 }, callback)
}

function load (id, callback) {
  return this.findById(id, callback)
}

function loadByHost (host, callback) {
  return this.findOne({ host }, callback)
}

function removeAll (callback) {
  return this.remove({}, callback)
}