aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/pod.js
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2016-12-19 21:50:20 +0100
committerChocobozzz <florian.bigard@gmail.com>2016-12-19 21:50:20 +0100
commit3897209f46f4c4581be2b8963bf9acc28ca5032b (patch)
tree13b47a4f86e826713602cdd34aebb1ef152ee204 /server/models/pod.js
parentfeb4bdfd9b46e87aadfa7c0d5338cde887d1f58c (diff)
downloadPeerTube-3897209f46f4c4581be2b8963bf9acc28ca5032b.tar.gz
PeerTube-3897209f46f4c4581be2b8963bf9acc28ca5032b.tar.zst
PeerTube-3897209f46f4c4581be2b8963bf9acc28ca5032b.zip
Server: rename Pods -> Pod
Diffstat (limited to 'server/models/pod.js')
-rw-r--r--server/models/pod.js139
1 files changed, 139 insertions, 0 deletions
diff --git a/server/models/pod.js b/server/models/pod.js
new file mode 100644
index 000000000..2c1f56203
--- /dev/null
+++ b/server/models/pod.js
@@ -0,0 +1,139 @@
1'use strict'
2
3const map = require('lodash/map')
4
5const constants = require('../initializers/constants')
6
7// ---------------------------------------------------------------------------
8
9module.exports = function (sequelize, DataTypes) {
10 const Pod = sequelize.define('Pod',
11 {
12 host: {
13 type: DataTypes.STRING
14 },
15 publicKey: {
16 type: DataTypes.STRING(5000)
17 },
18 score: {
19 type: DataTypes.INTEGER,
20 defaultValue: constants.FRIEND_SCORE.BASE
21 }
22 // Check createdAt
23 },
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
44}
45
46// TODO: max score -> constants.FRIENDS_SCORE.MAX
47// TODO: validation
48// PodSchema.path('host').validate(validator.isURL)
49// PodSchema.path('publicKey').required(true)
50// PodSchema.path('score').validate(function (value) { return !isNaN(value) })
51
52// ------------------------------ METHODS ------------------------------
53
54function toFormatedJSON () {
55 const json = {
56 id: this.id,
57 host: this.host,
58 score: this.score,
59 createdAt: this.createdAt
60 }
61
62 return json
63}
64
65// ------------------------------ Statics ------------------------------
66
67function associate (models) {
68 this.belongsToMany(models.Request, {
69 foreignKey: 'podId',
70 through: models.RequestToPod,
71 onDelete: 'CASCADE'
72 })
73}
74
75function countAll (callback) {
76 return this.count().asCallback(callback)
77}
78
79function incrementScores (ids, value, callback) {
80 if (!callback) callback = function () {}
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)
95}
96
97function list (callback) {
98 return this.findAll().asCallback(callback)
99}
100
101function listAllIds (callback) {
102 const query = {
103 attributes: [ 'id' ]
104 }
105
106 return this.findAll(query).asCallback(function (err, pods) {
107 if (err) return callback(err)
108
109 return callback(null, map(pods, 'id'))
110 })
111}
112
113function listBadPods (callback) {
114 const query = {
115 where: {
116 score: { $lte: 0 }
117 }
118 }
119
120 return this.findAll(query).asCallback(callback)
121}
122
123function load (id, callback) {
124 return this.findById(id).asCallback(callback)
125}
126
127function loadByHost (host, callback) {
128 const query = {
129 where: {
130 host: host
131 }
132 }
133
134 return this.findOne(query).asCallback(callback)
135}
136
137function removeAll (callback) {
138 return this.destroy().asCallback(callback)
139}