]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/pod.js
Server: propagate video update to other pods
[github/Chocobozzz/PeerTube.git] / server / models / pod.js
1 'use strict'
2
3 const map = require('lodash/map')
4
5 const constants = require('../initializers/constants')
6 const customPodsValidators = require('../helpers/custom-validators').pods
7
8 // ---------------------------------------------------------------------------
9
10 module.exports = function (sequelize, DataTypes) {
11 const Pod = sequelize.define('Pod',
12 {
13 host: {
14 type: DataTypes.STRING,
15 allowNull: false,
16 validate: {
17 isHost: function (value) {
18 const res = customPodsValidators.isHostValid(value)
19 if (res === false) throw new Error('Host not valid.')
20 }
21 }
22 },
23 publicKey: {
24 type: DataTypes.STRING(5000),
25 allowNull: false
26 },
27 score: {
28 type: DataTypes.INTEGER,
29 defaultValue: constants.FRIEND_SCORE.BASE,
30 allowNull: false,
31 validate: {
32 isInt: true,
33 max: constants.FRIEND_SCORE.MAX
34 }
35 }
36 },
37 {
38 indexes: [
39 {
40 fields: [ 'host' ]
41 },
42 {
43 fields: [ 'score' ]
44 }
45 ],
46 classMethods: {
47 associate,
48
49 countAll,
50 incrementScores,
51 list,
52 listAllIds,
53 listBadPods,
54 load,
55 loadByHost,
56 removeAll
57 },
58 instanceMethods: {
59 toFormatedJSON
60 }
61 }
62 )
63
64 return Pod
65 }
66
67 // ------------------------------ METHODS ------------------------------
68
69 function toFormatedJSON () {
70 const json = {
71 id: this.id,
72 host: this.host,
73 score: this.score,
74 createdAt: this.createdAt
75 }
76
77 return json
78 }
79
80 // ------------------------------ Statics ------------------------------
81
82 function associate (models) {
83 this.belongsToMany(models.Request, {
84 foreignKey: 'podId',
85 through: models.RequestToPod,
86 onDelete: 'cascade'
87 })
88 }
89
90 function countAll (callback) {
91 return this.count().asCallback(callback)
92 }
93
94 function incrementScores (ids, value, callback) {
95 if (!callback) callback = function () {}
96
97 const update = {
98 score: this.sequelize.literal('score +' + value)
99 }
100
101 const options = {
102 where: {
103 id: {
104 $in: ids
105 }
106 },
107 // In this case score is a literal and not an integer so we do not validate it
108 validate: false
109 }
110
111 return this.update(update, options).asCallback(callback)
112 }
113
114 function list (callback) {
115 return this.findAll().asCallback(callback)
116 }
117
118 function listAllIds (callback) {
119 const query = {
120 attributes: [ 'id' ]
121 }
122
123 return this.findAll(query).asCallback(function (err, pods) {
124 if (err) return callback(err)
125
126 return callback(null, map(pods, 'id'))
127 })
128 }
129
130 function listBadPods (callback) {
131 const query = {
132 where: {
133 score: { $lte: 0 }
134 }
135 }
136
137 return this.findAll(query).asCallback(callback)
138 }
139
140 function load (id, callback) {
141 return this.findById(id).asCallback(callback)
142 }
143
144 function loadByHost (host, callback) {
145 const query = {
146 where: {
147 host: host
148 }
149 }
150
151 return this.findOne(query).asCallback(callback)
152 }
153
154 function removeAll (callback) {
155 return this.destroy().asCallback(callback)
156 }