]>
Commit | Line | Data |
---|---|---|
9f10b292 C |
1 | 'use strict' |
2 | ||
80a6c9e7 | 3 | const each = require('async/each') |
f0f5567b | 4 | const mongoose = require('mongoose') |
00057e85 | 5 | const map = require('lodash/map') |
a3ee6fa2 | 6 | const validator = require('express-validator').validator |
9f10b292 | 7 | |
f0f5567b | 8 | const constants = require('../initializers/constants') |
9f10b292 | 9 | |
80a6c9e7 C |
10 | const Video = mongoose.model('Video') |
11 | ||
9f10b292 C |
12 | // --------------------------------------------------------------------------- |
13 | ||
a3ee6fa2 | 14 | const PodSchema = mongoose.Schema({ |
49abbbbe | 15 | host: String, |
9f10b292 | 16 | publicKey: String, |
53572423 C |
17 | score: { type: Number, max: constants.FRIEND_SCORE.MAX }, |
18 | createdDate: { | |
19 | type: Date, | |
20 | default: Date.now | |
21 | } | |
9f10b292 | 22 | }) |
9f10b292 | 23 | |
49abbbbe | 24 | PodSchema.path('host').validate(validator.isURL) |
a3ee6fa2 C |
25 | PodSchema.path('publicKey').required(true) |
26 | PodSchema.path('score').validate(function (value) { return !isNaN(value) }) | |
9f10b292 | 27 | |
53572423 | 28 | PodSchema.methods = { |
c4403b29 | 29 | toFormatedJSON |
53572423 C |
30 | } |
31 | ||
a3ee6fa2 | 32 | PodSchema.statics = { |
c4403b29 C |
33 | countAll, |
34 | incrementScores, | |
35 | list, | |
36 | listAllIds, | |
37 | listBadPods, | |
38 | load, | |
49abbbbe | 39 | loadByHost, |
c4403b29 | 40 | removeAll |
9f10b292 C |
41 | } |
42 | ||
a3ee6fa2 C |
43 | PodSchema.pre('save', function (next) { |
44 | const self = this | |
8c308c2b | 45 | |
49abbbbe | 46 | Pod.loadByHost(this.host, function (err, pod) { |
a3ee6fa2 | 47 | if (err) return next(err) |
45239549 | 48 | |
a3ee6fa2 | 49 | if (pod) return next(new Error('Pod already exists.')) |
8c308c2b | 50 | |
a3ee6fa2 C |
51 | self.score = constants.FRIEND_SCORE.BASE |
52 | return next() | |
53 | }) | |
54 | }) | |
45239549 | 55 | |
80a6c9e7 C |
56 | PodSchema.pre('remove', function (next) { |
57 | // Remove the videos owned by this pod too | |
49abbbbe | 58 | Video.listByHost(this.host, function (err, videos) { |
80a6c9e7 C |
59 | if (err) return next(err) |
60 | ||
61 | each(videos, function (video, callbackEach) { | |
62 | video.remove(callbackEach) | |
63 | }, next) | |
64 | }) | |
65 | }) | |
66 | ||
a3ee6fa2 | 67 | const Pod = mongoose.model('Pod', PodSchema) |
528a9efa | 68 | |
53572423 C |
69 | // ------------------------------ METHODS ------------------------------ |
70 | ||
71 | function toFormatedJSON () { | |
72 | const json = { | |
73 | id: this._id, | |
49abbbbe | 74 | host: this.host, |
53572423 C |
75 | score: this.score, |
76 | createdDate: this.createdDate | |
77 | } | |
78 | ||
79 | return json | |
80 | } | |
81 | ||
a3ee6fa2 C |
82 | // ------------------------------ Statics ------------------------------ |
83 | ||
84 | function countAll (callback) { | |
85 | return this.count(callback) | |
9f10b292 | 86 | } |
45239549 | 87 | |
9f10b292 C |
88 | function incrementScores (ids, value, callback) { |
89 | if (!callback) callback = function () {} | |
a3ee6fa2 | 90 | return this.update({ _id: { $in: ids } }, { $inc: { score: value } }, { multi: true }, callback) |
9f10b292 | 91 | } |
45239549 | 92 | |
9f10b292 | 93 | function list (callback) { |
a3ee6fa2 | 94 | return this.find(callback) |
9f10b292 | 95 | } |
8c308c2b | 96 | |
528a9efa | 97 | function listAllIds (callback) { |
a3ee6fa2 | 98 | return this.find({}, { _id: 1 }, function (err, pods) { |
00057e85 C |
99 | if (err) return callback(err) |
100 | ||
101 | return callback(null, map(pods, '_id')) | |
102 | }) | |
528a9efa C |
103 | } |
104 | ||
a3ee6fa2 C |
105 | function listBadPods (callback) { |
106 | return this.find({ score: 0 }, callback) | |
9f10b292 | 107 | } |
8c308c2b | 108 | |
a3ee6fa2 C |
109 | function load (id, callback) { |
110 | return this.findById(id, callback) | |
9f10b292 | 111 | } |
c45f7f84 | 112 | |
49abbbbe C |
113 | function loadByHost (host, callback) { |
114 | return this.findOne({ host }, callback) | |
9f10b292 | 115 | } |
c45f7f84 | 116 | |
a3ee6fa2 C |
117 | function removeAll (callback) { |
118 | return this.remove({}, callback) | |
119 | } |