diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-12-19 21:50:20 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-12-19 21:50:20 +0100 |
commit | 3897209f46f4c4581be2b8963bf9acc28ca5032b (patch) | |
tree | 13b47a4f86e826713602cdd34aebb1ef152ee204 /server/models/pod.js | |
parent | feb4bdfd9b46e87aadfa7c0d5338cde887d1f58c (diff) | |
download | PeerTube-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.js | 139 |
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 | |||
3 | const map = require('lodash/map') | ||
4 | |||
5 | const constants = require('../initializers/constants') | ||
6 | |||
7 | // --------------------------------------------------------------------------- | ||
8 | |||
9 | module.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 | |||
54 | function 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 | |||
67 | function associate (models) { | ||
68 | this.belongsToMany(models.Request, { | ||
69 | foreignKey: 'podId', | ||
70 | through: models.RequestToPod, | ||
71 | onDelete: 'CASCADE' | ||
72 | }) | ||
73 | } | ||
74 | |||
75 | function countAll (callback) { | ||
76 | return this.count().asCallback(callback) | ||
77 | } | ||
78 | |||
79 | function 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 | |||
97 | function list (callback) { | ||
98 | return this.findAll().asCallback(callback) | ||
99 | } | ||
100 | |||
101 | function 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 | |||
113 | function listBadPods (callback) { | ||
114 | const query = { | ||
115 | where: { | ||
116 | score: { $lte: 0 } | ||
117 | } | ||
118 | } | ||
119 | |||
120 | return this.findAll(query).asCallback(callback) | ||
121 | } | ||
122 | |||
123 | function load (id, callback) { | ||
124 | return this.findById(id).asCallback(callback) | ||
125 | } | ||
126 | |||
127 | function loadByHost (host, callback) { | ||
128 | const query = { | ||
129 | where: { | ||
130 | host: host | ||
131 | } | ||
132 | } | ||
133 | |||
134 | return this.findOne(query).asCallback(callback) | ||
135 | } | ||
136 | |||
137 | function removeAll (callback) { | ||
138 | return this.destroy().asCallback(callback) | ||
139 | } | ||