diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-12-11 21:50:51 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-12-19 21:22:28 +0100 |
commit | feb4bdfd9b46e87aadfa7c0d5338cde887d1f58c (patch) | |
tree | 2abc9fbc9569760e218fd52835850b757344b420 /server/models/pods.js | |
parent | 108626609eda75e4ecc0a83a650a4d53c46220e0 (diff) | |
download | PeerTube-feb4bdfd9b46e87aadfa7c0d5338cde887d1f58c.tar.gz PeerTube-feb4bdfd9b46e87aadfa7c0d5338cde887d1f58c.tar.zst PeerTube-feb4bdfd9b46e87aadfa7c0d5338cde887d1f58c.zip |
First version with PostgreSQL
Diffstat (limited to 'server/models/pods.js')
-rw-r--r-- | server/models/pods.js | 156 |
1 files changed, 88 insertions, 68 deletions
diff --git a/server/models/pods.js b/server/models/pods.js index 49c73472a..2c1f56203 100644 --- a/server/models/pods.js +++ b/server/models/pods.js | |||
@@ -1,79 +1,62 @@ | |||
1 | 'use strict' | 1 | 'use strict' |
2 | 2 | ||
3 | const each = require('async/each') | ||
4 | const mongoose = require('mongoose') | ||
5 | const map = require('lodash/map') | 3 | const map = require('lodash/map') |
6 | const validator = require('express-validator').validator | ||
7 | 4 | ||
8 | const constants = require('../initializers/constants') | 5 | const constants = require('../initializers/constants') |
9 | 6 | ||
10 | const Video = mongoose.model('Video') | ||
11 | |||
12 | // --------------------------------------------------------------------------- | 7 | // --------------------------------------------------------------------------- |
13 | 8 | ||
14 | const PodSchema = mongoose.Schema({ | 9 | module.exports = function (sequelize, DataTypes) { |
15 | host: String, | 10 | const Pod = sequelize.define('Pod', |
16 | publicKey: String, | 11 | { |
17 | score: { type: Number, max: constants.FRIEND_SCORE.MAX }, | 12 | host: { |
18 | createdDate: { | 13 | type: DataTypes.STRING |
19 | type: Date, | 14 | }, |
20 | default: Date.now | 15 | publicKey: { |
21 | } | 16 | type: DataTypes.STRING(5000) |
22 | }) | 17 | }, |
23 | 18 | score: { | |
24 | PodSchema.path('host').validate(validator.isURL) | 19 | type: DataTypes.INTEGER, |
25 | PodSchema.path('publicKey').required(true) | 20 | defaultValue: constants.FRIEND_SCORE.BASE |
26 | PodSchema.path('score').validate(function (value) { return !isNaN(value) }) | 21 | } |
27 | 22 | // Check createdAt | |
28 | PodSchema.methods = { | 23 | }, |
29 | toFormatedJSON | 24 | { |
25 | classMethods: { | ||
26 | associate, | ||
27 | |||
28 | countAll, | ||
29 | incrementScores, | ||
30 | list, | ||
31 | listAllIds, | ||
32 | listBadPods, | ||
33 | load, | ||
34 | loadByHost, | ||
35 | removeAll | ||
36 | }, | ||
37 | instanceMethods: { | ||
38 | toFormatedJSON | ||
39 | } | ||
40 | } | ||
41 | ) | ||
42 | |||
43 | return Pod | ||
30 | } | 44 | } |
31 | 45 | ||
32 | PodSchema.statics = { | 46 | // TODO: max score -> constants.FRIENDS_SCORE.MAX |
33 | countAll, | 47 | // TODO: validation |
34 | incrementScores, | 48 | // PodSchema.path('host').validate(validator.isURL) |
35 | list, | 49 | // PodSchema.path('publicKey').required(true) |
36 | listAllIds, | 50 | // PodSchema.path('score').validate(function (value) { return !isNaN(value) }) |
37 | listBadPods, | ||
38 | load, | ||
39 | loadByHost, | ||
40 | removeAll | ||
41 | } | ||
42 | |||
43 | PodSchema.pre('save', function (next) { | ||
44 | const self = this | ||
45 | |||
46 | Pod.loadByHost(this.host, function (err, pod) { | ||
47 | if (err) return next(err) | ||
48 | |||
49 | if (pod) return next(new Error('Pod already exists.')) | ||
50 | |||
51 | self.score = constants.FRIEND_SCORE.BASE | ||
52 | return next() | ||
53 | }) | ||
54 | }) | ||
55 | |||
56 | PodSchema.pre('remove', function (next) { | ||
57 | // Remove the videos owned by this pod too | ||
58 | Video.listByHost(this.host, function (err, videos) { | ||
59 | if (err) return next(err) | ||
60 | |||
61 | each(videos, function (video, callbackEach) { | ||
62 | video.remove(callbackEach) | ||
63 | }, next) | ||
64 | }) | ||
65 | }) | ||
66 | |||
67 | const Pod = mongoose.model('Pod', PodSchema) | ||
68 | 51 | ||
69 | // ------------------------------ METHODS ------------------------------ | 52 | // ------------------------------ METHODS ------------------------------ |
70 | 53 | ||
71 | function toFormatedJSON () { | 54 | function toFormatedJSON () { |
72 | const json = { | 55 | const json = { |
73 | id: this._id, | 56 | id: this.id, |
74 | host: this.host, | 57 | host: this.host, |
75 | score: this.score, | 58 | score: this.score, |
76 | createdDate: this.createdDate | 59 | createdAt: this.createdAt |
77 | } | 60 | } |
78 | 61 | ||
79 | return json | 62 | return json |
@@ -81,39 +64,76 @@ function toFormatedJSON () { | |||
81 | 64 | ||
82 | // ------------------------------ Statics ------------------------------ | 65 | // ------------------------------ Statics ------------------------------ |
83 | 66 | ||
67 | function associate (models) { | ||
68 | this.belongsToMany(models.Request, { | ||
69 | foreignKey: 'podId', | ||
70 | through: models.RequestToPod, | ||
71 | onDelete: 'CASCADE' | ||
72 | }) | ||
73 | } | ||
74 | |||
84 | function countAll (callback) { | 75 | function countAll (callback) { |
85 | return this.count(callback) | 76 | return this.count().asCallback(callback) |
86 | } | 77 | } |
87 | 78 | ||
88 | function incrementScores (ids, value, callback) { | 79 | function incrementScores (ids, value, callback) { |
89 | if (!callback) callback = function () {} | 80 | if (!callback) callback = function () {} |
90 | return this.update({ _id: { $in: ids } }, { $inc: { score: value } }, { multi: true }, callback) | 81 | |
82 | const update = { | ||
83 | score: this.sequelize.literal('score +' + value) | ||
84 | } | ||
85 | |||
86 | const query = { | ||
87 | where: { | ||
88 | id: { | ||
89 | $in: ids | ||
90 | } | ||
91 | } | ||
92 | } | ||
93 | |||
94 | return this.update(update, query).asCallback(callback) | ||
91 | } | 95 | } |
92 | 96 | ||
93 | function list (callback) { | 97 | function list (callback) { |
94 | return this.find(callback) | 98 | return this.findAll().asCallback(callback) |
95 | } | 99 | } |
96 | 100 | ||
97 | function listAllIds (callback) { | 101 | function listAllIds (callback) { |
98 | return this.find({}, { _id: 1 }, function (err, pods) { | 102 | const query = { |
103 | attributes: [ 'id' ] | ||
104 | } | ||
105 | |||
106 | return this.findAll(query).asCallback(function (err, pods) { | ||
99 | if (err) return callback(err) | 107 | if (err) return callback(err) |
100 | 108 | ||
101 | return callback(null, map(pods, '_id')) | 109 | return callback(null, map(pods, 'id')) |
102 | }) | 110 | }) |
103 | } | 111 | } |
104 | 112 | ||
105 | function listBadPods (callback) { | 113 | function listBadPods (callback) { |
106 | return this.find({ score: 0 }, callback) | 114 | const query = { |
115 | where: { | ||
116 | score: { $lte: 0 } | ||
117 | } | ||
118 | } | ||
119 | |||
120 | return this.findAll(query).asCallback(callback) | ||
107 | } | 121 | } |
108 | 122 | ||
109 | function load (id, callback) { | 123 | function load (id, callback) { |
110 | return this.findById(id, callback) | 124 | return this.findById(id).asCallback(callback) |
111 | } | 125 | } |
112 | 126 | ||
113 | function loadByHost (host, callback) { | 127 | function loadByHost (host, callback) { |
114 | return this.findOne({ host }, callback) | 128 | const query = { |
129 | where: { | ||
130 | host: host | ||
131 | } | ||
132 | } | ||
133 | |||
134 | return this.findOne(query).asCallback(callback) | ||
115 | } | 135 | } |
116 | 136 | ||
117 | function removeAll (callback) { | 137 | function removeAll (callback) { |
118 | return this.remove({}, callback) | 138 | return this.destroy().asCallback(callback) |
119 | } | 139 | } |