]>
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({ |
9f10b292 C |
15 | url: String, |
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 | |
a3ee6fa2 C |
24 | // TODO: set options (TLD...) |
25 | PodSchema.path('url').validate(validator.isURL) | |
26 | PodSchema.path('publicKey').required(true) | |
27 | PodSchema.path('score').validate(function (value) { return !isNaN(value) }) | |
9f10b292 | 28 | |
53572423 | 29 | PodSchema.methods = { |
c4403b29 | 30 | toFormatedJSON |
53572423 C |
31 | } |
32 | ||
a3ee6fa2 | 33 | PodSchema.statics = { |
c4403b29 C |
34 | countAll, |
35 | incrementScores, | |
36 | list, | |
37 | listAllIds, | |
38 | listBadPods, | |
39 | load, | |
40 | loadByUrl, | |
41 | removeAll | |
9f10b292 C |
42 | } |
43 | ||
a3ee6fa2 C |
44 | PodSchema.pre('save', function (next) { |
45 | const self = this | |
8c308c2b | 46 | |
a3ee6fa2 C |
47 | Pod.loadByUrl(this.url, function (err, pod) { |
48 | if (err) return next(err) | |
45239549 | 49 | |
a3ee6fa2 | 50 | if (pod) return next(new Error('Pod already exists.')) |
8c308c2b | 51 | |
a3ee6fa2 C |
52 | self.score = constants.FRIEND_SCORE.BASE |
53 | return next() | |
54 | }) | |
55 | }) | |
45239549 | 56 | |
80a6c9e7 C |
57 | PodSchema.pre('remove', function (next) { |
58 | // Remove the videos owned by this pod too | |
59 | Video.listByUrl(this.url, function (err, videos) { | |
60 | if (err) return next(err) | |
61 | ||
62 | each(videos, function (video, callbackEach) { | |
63 | video.remove(callbackEach) | |
64 | }, next) | |
65 | }) | |
66 | }) | |
67 | ||
a3ee6fa2 | 68 | const Pod = mongoose.model('Pod', PodSchema) |
528a9efa | 69 | |
53572423 C |
70 | // ------------------------------ METHODS ------------------------------ |
71 | ||
72 | function toFormatedJSON () { | |
73 | const json = { | |
74 | id: this._id, | |
75 | url: this.url, | |
76 | score: this.score, | |
77 | createdDate: this.createdDate | |
78 | } | |
79 | ||
80 | return json | |
81 | } | |
82 | ||
a3ee6fa2 C |
83 | // ------------------------------ Statics ------------------------------ |
84 | ||
85 | function countAll (callback) { | |
86 | return this.count(callback) | |
9f10b292 | 87 | } |
45239549 | 88 | |
9f10b292 C |
89 | function incrementScores (ids, value, callback) { |
90 | if (!callback) callback = function () {} | |
a3ee6fa2 | 91 | return this.update({ _id: { $in: ids } }, { $inc: { score: value } }, { multi: true }, callback) |
9f10b292 | 92 | } |
45239549 | 93 | |
9f10b292 | 94 | function list (callback) { |
a3ee6fa2 | 95 | return this.find(callback) |
9f10b292 | 96 | } |
8c308c2b | 97 | |
528a9efa | 98 | function listAllIds (callback) { |
a3ee6fa2 | 99 | return this.find({}, { _id: 1 }, function (err, pods) { |
00057e85 C |
100 | if (err) return callback(err) |
101 | ||
102 | return callback(null, map(pods, '_id')) | |
103 | }) | |
528a9efa C |
104 | } |
105 | ||
a3ee6fa2 C |
106 | function listBadPods (callback) { |
107 | return this.find({ score: 0 }, callback) | |
9f10b292 | 108 | } |
8c308c2b | 109 | |
a3ee6fa2 C |
110 | function load (id, callback) { |
111 | return this.findById(id, callback) | |
9f10b292 | 112 | } |
c45f7f84 | 113 | |
a3ee6fa2 C |
114 | function loadByUrl (url, callback) { |
115 | return this.findOne({ url: url }, callback) | |
9f10b292 | 116 | } |
c45f7f84 | 117 | |
a3ee6fa2 C |
118 | function removeAll (callback) { |
119 | return this.remove({}, callback) | |
120 | } |