]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/pod.js
Update migrations code
[github/Chocobozzz/PeerTube.git] / server / models / pod.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 }
feb4bdfd
C
22 },
23 {
24 classMethods: {
25 associate,
26
27 countAll,
28 incrementScores,
29 list,
30 listAllIds,
31 listBadPods,
32 load,
33 loadByHost,
34 removeAll
35 },
36 instanceMethods: {
37 toFormatedJSON
38 }
39 }
40 )
41
42 return Pod
53572423
C
43}
44
feb4bdfd
C
45// TODO: max score -> constants.FRIENDS_SCORE.MAX
46// TODO: validation
47// PodSchema.path('host').validate(validator.isURL)
48// PodSchema.path('publicKey').required(true)
49// PodSchema.path('score').validate(function (value) { return !isNaN(value) })
528a9efa 50
53572423
C
51// ------------------------------ METHODS ------------------------------
52
53function toFormatedJSON () {
54 const json = {
feb4bdfd 55 id: this.id,
49abbbbe 56 host: this.host,
53572423 57 score: this.score,
feb4bdfd 58 createdAt: this.createdAt
53572423
C
59 }
60
61 return json
62}
63
a3ee6fa2
C
64// ------------------------------ Statics ------------------------------
65
feb4bdfd
C
66function associate (models) {
67 this.belongsToMany(models.Request, {
68 foreignKey: 'podId',
69 through: models.RequestToPod,
7920c273 70 onDelete: 'cascade'
feb4bdfd
C
71 })
72}
73
a3ee6fa2 74function countAll (callback) {
feb4bdfd 75 return this.count().asCallback(callback)
9f10b292 76}
45239549 77
9f10b292
C
78function incrementScores (ids, value, callback) {
79 if (!callback) callback = function () {}
feb4bdfd
C
80
81 const update = {
82 score: this.sequelize.literal('score +' + value)
83 }
84
85 const query = {
86 where: {
87 id: {
88 $in: ids
89 }
90 }
91 }
92
93 return this.update(update, query).asCallback(callback)
9f10b292 94}
45239549 95
9f10b292 96function list (callback) {
feb4bdfd 97 return this.findAll().asCallback(callback)
9f10b292 98}
8c308c2b 99
528a9efa 100function listAllIds (callback) {
feb4bdfd
C
101 const query = {
102 attributes: [ 'id' ]
103 }
104
105 return this.findAll(query).asCallback(function (err, pods) {
00057e85
C
106 if (err) return callback(err)
107
feb4bdfd 108 return callback(null, map(pods, 'id'))
00057e85 109 })
528a9efa
C
110}
111
a3ee6fa2 112function listBadPods (callback) {
feb4bdfd
C
113 const query = {
114 where: {
115 score: { $lte: 0 }
116 }
117 }
118
119 return this.findAll(query).asCallback(callback)
9f10b292 120}
8c308c2b 121
a3ee6fa2 122function load (id, callback) {
feb4bdfd 123 return this.findById(id).asCallback(callback)
9f10b292 124}
c45f7f84 125
49abbbbe 126function loadByHost (host, callback) {
feb4bdfd
C
127 const query = {
128 where: {
129 host: host
130 }
131 }
132
133 return this.findOne(query).asCallback(callback)
9f10b292 134}
c45f7f84 135
a3ee6fa2 136function removeAll (callback) {
feb4bdfd 137 return this.destroy().asCallback(callback)
a3ee6fa2 138}