]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/pods.js
First version with PostgreSQL
[github/Chocobozzz/PeerTube.git] / server / models / pods.js
CommitLineData
9f10b292
C
1'use strict'
2
00057e85 3const map = require('lodash/map')
9f10b292 4
f0f5567b 5const constants = require('../initializers/constants')
9f10b292
C
6
7// ---------------------------------------------------------------------------
8
feb4bdfd
C
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
53572423
C
44}
45
feb4bdfd
C
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) })
528a9efa 51
53572423
C
52// ------------------------------ METHODS ------------------------------
53
54function toFormatedJSON () {
55 const json = {
feb4bdfd 56 id: this.id,
49abbbbe 57 host: this.host,
53572423 58 score: this.score,
feb4bdfd 59 createdAt: this.createdAt
53572423
C
60 }
61
62 return json
63}
64
a3ee6fa2
C
65// ------------------------------ Statics ------------------------------
66
feb4bdfd
C
67function associate (models) {
68 this.belongsToMany(models.Request, {
69 foreignKey: 'podId',
70 through: models.RequestToPod,
71 onDelete: 'CASCADE'
72 })
73}
74
a3ee6fa2 75function countAll (callback) {
feb4bdfd 76 return this.count().asCallback(callback)
9f10b292 77}
45239549 78
9f10b292
C
79function incrementScores (ids, value, callback) {
80 if (!callback) callback = function () {}
feb4bdfd
C
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)
9f10b292 95}
45239549 96
9f10b292 97function list (callback) {
feb4bdfd 98 return this.findAll().asCallback(callback)
9f10b292 99}
8c308c2b 100
528a9efa 101function listAllIds (callback) {
feb4bdfd
C
102 const query = {
103 attributes: [ 'id' ]
104 }
105
106 return this.findAll(query).asCallback(function (err, pods) {
00057e85
C
107 if (err) return callback(err)
108
feb4bdfd 109 return callback(null, map(pods, 'id'))
00057e85 110 })
528a9efa
C
111}
112
a3ee6fa2 113function listBadPods (callback) {
feb4bdfd
C
114 const query = {
115 where: {
116 score: { $lte: 0 }
117 }
118 }
119
120 return this.findAll(query).asCallback(callback)
9f10b292 121}
8c308c2b 122
a3ee6fa2 123function load (id, callback) {
feb4bdfd 124 return this.findById(id).asCallback(callback)
9f10b292 125}
c45f7f84 126
49abbbbe 127function loadByHost (host, callback) {
feb4bdfd
C
128 const query = {
129 where: {
130 host: host
131 }
132 }
133
134 return this.findOne(query).asCallback(callback)
9f10b292 135}
c45f7f84 136
a3ee6fa2 137function removeAll (callback) {
feb4bdfd 138 return this.destroy().asCallback(callback)
a3ee6fa2 139}